#include "../../../cs2/entity/C_CSPlayerPawn/C_CSPlayerPawn.h"
#include "../../../templeware/interfaces/CGameEntitySystem/CGameEntitySystem.h"
#include "../../../templeware/interfaces/interfaces.h"
#include "../../../templeware/hooks/hooks.h"
#include "../../../templeware/config/config.h"
#include "../../../templeware/features/aim/aim.h"
#include <Windows.h>
#include <cmath>

// Симуляция нажатия ЛКМ
void SimulateLMBClick() {
    INPUT input = { 0 };
    input.type = INPUT_MOUSE;
    input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
    SendInput(1, &input, sizeof(INPUT));
    ZeroMemory(&input, sizeof(INPUT));
    input.type = INPUT_MOUSE;
    input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
    SendInput(1, &input, sizeof(INPUT));
}

void SimulateLMBDown() {
    INPUT input = { 0 };
    input.type = INPUT_MOUSE;
    input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
    SendInput(1, &input, sizeof(INPUT));
}
void SimulateLMBUp() {
    INPUT input = { 0 };
    input.type = INPUT_MOUSE;
    input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
    SendInput(1, &input, sizeof(INPUT));
}

// Управление удержанием ЛКМ
void SetLMBHeld(bool held) {
    static bool lmbHeld = false;
    if (held && !lmbHeld) {
        SimulateLMBDown();
        lmbHeld = true;
    } else if (!held && lmbHeld) {
        SimulateLMBUp();
        lmbHeld = false;
    }
}

static DWORD lastShotTime = 0;
void Triggerbot() {
    static DWORD lastShotTime = 0;
    static bool firstShot = true;
    // --- Альтернативная кнопка: триггербот по наведению на врага с задержкой ---
    if (Config::triggerbot_alt_key && (GetAsyncKeyState(Config::triggerbot_alt_key) & 0x8000)) {
        C_CSPlayerPawn* lp = H::oGetLocalPlayer(0);
        if (!lp || lp->getHealth() <= 0) { SetLMBHeld(false); return; }
        int crosshairIdx = *(int*)((uintptr_t)lp + 0x1458); // m_iIDEntIndex
        if (crosshairIdx <= 0) { SetLMBHeld(false); return; }
        auto target = I::GameEntity->Instance->Get<C_CSPlayerPawn>(crosshairIdx);
        if (!target) { SetLMBHeld(false); return; }
        if (target->getHealth() <= 0) { SetLMBHeld(false); return; }
        if (Config::team_check && target->getTeam() == lp->getTeam()) { SetLMBHeld(false); return; }
        DWORD64 now = GetTickCount64();
        static DWORD64 lastAltShotTime = 0;
        if (now - lastAltShotTime < static_cast<DWORD64>(Config::triggerbot_delay)) {
            SetLMBHeld(false);
            return;
        }
        SimulateLMBClick();
        lastAltShotTime = now;
        return;
    }
    if (!Config::triggerbot)
        return;
    if (!Config::always_on_triggerbot && Config::triggerbot_key && !(GetAsyncKeyState(Config::triggerbot_key) & 0x8000)) {
        SetLMBHeld(false);
        firstShot = true;
        return;
    }
    if (GetAsyncKeyState(VK_LBUTTON) & 0x8000) {
        SetLMBHeld(false);
        firstShot = true;
        return;
    }
    C_CSPlayerPawn* lp = H::oGetLocalPlayer(0);
    if (!lp || lp->getHealth() <= 0) { firstShot = true; SetLMBHeld(false); return; }
    int crosshairIdx = *(int*)((uintptr_t)lp + 0x1458); // m_iIDEntIndex
    int shotsFired = lp ? lp->getShotsFired() : 0;
    DWORD64 now = GetTickCount64();
    if (Config::shooterAfterAim) {
        if (shotsFired > 0) {
            // После первого выстрела просто удерживаем ЛКМ, игнорируя все условия
            SetLMBHeld(true);
            return;
        } else {
            // До первого выстрела — обычная логика триггербота
            if (crosshairIdx <= 0) {
                SetLMBHeld(false);
                firstShot = true;
                return;
            }
            auto target = I::GameEntity->Instance->Get<C_CSPlayerPawn>(crosshairIdx);
            if (!target) {
                SetLMBHeld(false);
                firstShot = true;
                return;
            }
            if (target->getHealth() <= 0) {
                SetLMBHeld(false);
                firstShot = true;
                return;
            }
            if (Config::team_check && target->getTeam() == lp->getTeam()) {
                SetLMBHeld(false);
                firstShot = true;
                return;
            }
            if (!Config::triggerbot_hold_lmb) {
                if (now - lastShotTime < static_cast<DWORD64>(Config::triggerbot_delay)) {
                    SetLMBHeld(false);
                    firstShot = true;
                    return;
                }
            }
            if (Config::triggerbot_hold_lmb) {
                if (firstShot) {
                    SimulateLMBClick();
                    firstShot = false;
                    lastShotTime = now;
                    return;
                } else {
                    SetLMBHeld(true);
                }
                lastShotTime = now;
                return;
            }
            SimulateLMBClick();
            lastShotTime = now;
            firstShot = true;
            return;
        }
    }
    // --- Обычная логика триггербота, если shooterAfterAim выключен ---
    if (crosshairIdx <= 0) {
        SetLMBHeld(false);
        firstShot = true;
        return;
    }
    auto target = I::GameEntity->Instance->Get<C_CSPlayerPawn>(crosshairIdx);
    if (!target) {
        SetLMBHeld(false);
        firstShot = true;
        return;
    }
    if (target->getHealth() <= 0) {
        SetLMBHeld(false);
        firstShot = true;
        return;
    }
    if (Config::team_check && target->getTeam() == lp->getTeam()) {
        SetLMBHeld(false);
        firstShot = true;
        return;
    }
    if (!Config::triggerbot_hold_lmb) {
        if (now - lastShotTime < static_cast<DWORD64>(Config::triggerbot_delay)) {
            SetLMBHeld(false);
            firstShot = true;
            return;
        }
    }
    if (Config::triggerbot_hold_lmb) {
        if (firstShot) {
            SimulateLMBClick();
            firstShot = false;
            lastShotTime = now;
            return;
        } else {
            SetLMBHeld(true);
        }
        lastShotTime = now;
        return;
    }
    SimulateLMBClick();
    lastShotTime = now;
    firstShot = true;
} 