using Swed64;
using System.Runtime.InteropServices;
using TriggerBotCS2;

Renderer renderer = new Renderer();
Thread renderThread = new Thread(renderer.Start().Wait);
renderThread.Start();

Swed swed = new Swed("cs2"); // detecting cs2

IntPtr client = swed.GetModuleBase("client.dll"); // getting client

//int dwForceAttack = 0x1730020;
int dwEntityList = 0x19BEED0;
int dwLocalPlayerPawn = 0x1824A18;
int dwWindowHeight = 0x5F0424;
int dwWindowWidth = 0x5F0420;

bool ENABLED = false;

const int MOUSEEVENTF_LEFTDOWN = 0x02;
const int MOUSEEVENTF_LEFTUP = 0x04;


// https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
int HOTKEY = 0x70; // F1


int m_iIDEntIndex = 0x13A8; // CEntityIndex
int m_iTeamNum = 0x3C3;


while (true)
{
    IntPtr entityList = swed.ReadPointer(client, dwEntityList);
    IntPtr localPlayerPawn = swed.ReadPointer(client, dwLocalPlayerPawn);

    ENABLED = Renderer._aimBotEnabled;

    int team = swed.ReadInt(localPlayerPawn, m_iTeamNum);
    int entIndex = swed.ReadInt(localPlayerPawn,m_iIDEntIndex);

    Console.WriteLine($"Crosshair/Entity ID: {entIndex}");
    //Console.WriteLine($"Enabled: {ENABLED}");

    if (GetAsyncKeyState(HOTKEY) < 0)
    {
        // Toggle ENABLED state when HOTKEY is pressed
        ENABLED = !ENABLED;

        // Sleep to prevent rapid toggling on a single key press
        Thread.Sleep(300);
    }

    if (entIndex != -1)
    {
        IntPtr listEntry = swed.ReadPointer(entityList, 0x8 * ((entIndex & 0x7FFF) >> 9) + 0x10);
        IntPtr currentPawn = swed.ReadPointer(listEntry, 0x78 * (entIndex & 0x1FF));

        int entityTeam = swed.ReadInt(currentPawn, m_iTeamNum);

        if (team != entityTeam)
        {
            //if (GetAsyncKeyState(HOTKEY) < 0)
            //{
                //ENABLED = true;

                // Simulate left mouse button press
                //swed.WriteInt(client, dwForceAttack, 65537); // +attack
                //int windowWidth = swed.ReadInt(client, dwWindowWidth);
                //int windowHeight = swed.ReadInt(client, dwWindowHeight);

                // Calculate the center of the screen
                //uint X = (uint)(windowWidth / 2);
                //uint Y = (uint)(windowHeight / 2);
                //mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
                //Thread.Sleep(10);
                //swed.WriteInt(client, dwForceAttack, 256); // -attack
                //Thread.Sleep(10);
            //}

            if (ENABLED)
            {
                int windowWidth = swed.ReadInt(client, dwWindowWidth);
                int windowHeight = swed.ReadInt(client, dwWindowHeight);

                // Calculate the center of the screen
                uint X = (uint)(windowWidth / 2);
                uint Y = (uint)(windowHeight / 2);
                mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
            }
        }
    }

    Thread.Sleep(2);

}

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);



[DllImport("user32.dll")]
static extern short GetAsyncKeyState(int vKey);         change enabled only when value changed