#include "driver_interface_v3.h"
#include "hk_driver_interface.h"
#include <TlHelp32.h>

DriverInterfaceV3::DriverInterfaceV3() {
}

DriverInterfaceV3::~DriverInterfaceV3() {
    Cleanup();
}

bool DriverInterfaceV3::Initialize() {
    return 安装驱动();
}

void DriverInterfaceV3::Cleanup() {
    卸载驱动();
}

DWORD DriverInterfaceV3::GetProcessId(const wchar_t* processName) {
    DWORD pid = 0;
    HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (snapshot != INVALID_HANDLE_VALUE) {
        PROCESSENTRY32W entry = { sizeof(entry) };
        if (Process32FirstW(snapshot, &entry)) {
            do {
                if (_wcsicmp(entry.szExeFile, processName) == 0) {
                    pid = entry.th32ProcessID;
                    break;
                }
            } while (Process32NextW(snapshot, &entry));
        }
        CloseHandle(snapshot);
    }
    return pid;
}

uintptr_t DriverInterfaceV3::GetModuleBase(DWORD pid, const wchar_t* moduleName) {
    uintptr_t base = 0;
    取模块地址(pid, moduleName, &base);
    return base;
}

bool DriverInterfaceV3::ReadMemory(DWORD pid, uintptr_t address, void* buffer, size_t size) {
    if (!pid || !address || !buffer || !size) return false;
    无附加_强读内存(pid, (PVOID)address, buffer, (DWORD)size);
    return true;
}

bool DriverInterfaceV3::WriteMemory(DWORD pid, uintptr_t address, void* buffer, size_t size) {
    if (!pid || !address || !buffer || !size) return false;
    无附加_强写内存(pid, (PVOID)address, buffer, (DWORD)size);
    return true;
}

void DriverInterfaceV3::SetCurrentPid(DWORD pid) {
    this->currentPid = pid;
}

uintptr_t DriverInterfaceV3::GetProcessCr3(DWORD pid) {
    // 这里应该是驱动获取 CR3 的逻辑
    // 如果驱动没有导出直接获取 CR3 的函数，我们可能需要通过读取 EPROCESS 获取
    // 但在 HK 驱动中，通常 ReadMemory 会自动处理。
    // 如果存在“限速”，通常是因为驱动在每次读取时都在重新寻找 CR3。
    return this->currentCr3;
}

void DriverInterfaceV3::SetCr3(uintptr_t cr3) {
    this->currentCr3 = cr3;
    // 如果驱动支持传入 CR3 来加速读取，这里应该调用驱动接口
}

void DriverInterfaceV3::UpdateCr3() {
    if (currentPid == 0) return;
    // 定期或在读取失败时更新 CR3
    this->currentCr3 = GetProcessCr3(currentPid);
}
void DriverInterfaceV3::TestRandomMouseMoveLoop(std::atomic<bool>& running) {
    while (running.load(std::memory_order_relaxed)) {
        float x = (float)(rand() % 21 - 10); // -10.0 to 10.0
        float y = (float)(rand() % 21 - 10); // -10.0 to 10.0
        InjectMouseMove(x, y);
        Sleep(20);
    }
}

bool DriverInterfaceV3::InjectMouseClick(int key) {
    INPUT inputs[2] = { 0 };
    inputs[0].type = INPUT_MOUSE;
    inputs[1].type = INPUT_MOUSE;
    
    if (key == VK_LBUTTON) {
        inputs[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
        inputs[1].mi.dwFlags = MOUSEEVENTF_LEFTUP;
    } else if (key == VK_RBUTTON) {
        inputs[0].mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;
        inputs[1].mi.dwFlags = MOUSEEVENTF_RIGHTUP;
    } else {
        return false;
    }
    
    SendInput(2, inputs, sizeof(INPUT));
    return true;
}

void DriverInterfaceV3::InjectMouseMove(float x, float y) {
    mouseXRemainder += x;
    mouseYRemainder += y;

    int moveX = static_cast<int>(mouseXRemainder);
    int moveY = static_cast<int>(mouseYRemainder);

    mouseXRemainder -= (float)moveX;
    mouseYRemainder -= (float)moveY;

    if (moveX != 0 || moveY != 0) {
        INPUT input = { 0 };
        input.type = INPUT_MOUSE;
        input.mi.dwFlags = MOUSEEVENTF_MOVE;
        input.mi.dx = (LONG)moveX;
        input.mi.dy = (LONG)moveY;
        SendInput(1, &input, sizeof(INPUT));
    }
}

void DriverInterfaceV3::SetMouseSignature(int x, int y) {}
bool DriverInterfaceV3::MoveMouse(float x, float y) { 
    InjectMouseMove(x, y);
    return true; 
}

void DriverInterfaceV3::ProtectProcess(DWORD pid) {
    保护进程(pid);
}

void DriverInterfaceV3::KillProcess(DWORD pid) {
    杀死进程(pid);
}
