#pragma once

// -----------------------------------------------------------------------------
// radar.h
// -----------------------------------------------------------------------------
// RadarFeature: reveals every enemy on the minimap by writing the spotted
// flag (m_bSpotted) of their entity spotted state. Walks the 64 player
// controller slots and resolves each pawn through m_hPlayerPawn, like the
// original cheat. The bomb entities are spotted too: the carried/dropped
// C4 (class C_C4) and the planted bomb (C_PlantedC4) get the same flag,
// so the bomb shows on the minimap regardless of team. Their entity
// indices come from a slow class-name scan (1 Hz) and are re-validated
// per write through their identity slots.
// -----------------------------------------------------------------------------

#include "../core/memory.h"
#include "../core/offsets.h"
#include "../sdk/entitylist.h"
#include "../sdk/localplayer.h"

namespace features {

// Enemy reveal on the minimap running on its own thread.
class Radar
{
public:
	// Binds the memory reader, offsets, entity list and local player.
	void Initialize(const core::ProcessMemory* memory, const core::GameOffsets* offsets,
		const sdk::EntityList* entities, const sdk::LocalPlayer* player);

	// Starts the radar thread.
	void Start();

	// Thread entry point.
	static unsigned long __stdcall ThreadProc(void* param);

private:
	// Clears the spotted flags of every previously marked enemy (called
	// exactly once when the radar is toggled off).
	void ClearSpotted();

	// Re-locates the bomb entities (carried/dropped C4 + planted C4) with
	// a class-name scan over the raw entity list. Runs at most once per
	// second; the hot loop re-validates the cached entries per write.
	void ScanBombs();

	// Writes the spotted flag on one bomb entity (no-op when the cache is
	// empty or the entity slot no longer points at the cached entity).
	void SpotBomb(uintptr_t entity, uintptr_t identity, uintptr_t spottedStateOffset, bool value);

	// Refreshes the bomb cache once per second and spots both bomb
	// entities (carried/dropped C4 + planted C4).
	void SpotBombs();

	const core::ProcessMemory* m_memory = nullptr;
	const core::GameOffsets* m_offsets = nullptr;
	const sdk::EntityList* m_entities = nullptr;
	const sdk::LocalPlayer* m_player = nullptr;

	// Previous toggle state; used to clear the spotted flags exactly once
	// when the feature is switched off (afterwards the thread rests
	// without touching memory at all).
	bool m_lastEnabled = false;

	// Cached bomb entities (entity pointer + identity slot address for
	// cheap per-write re-validation), refreshed by ScanBombs.
	uintptr_t m_c4Entity = 0;         // carried/dropped bomb (C_C4)
	uintptr_t m_c4Identity = 0;
	uintptr_t m_plantedC4Entity = 0;  // planted bomb (C_PlantedC4)
	uintptr_t m_plantedC4Identity = 0;
	unsigned long m_lastBombScan = 0; // GetTickCount of the last ScanBombs
};

} // namespace features