// game/GameLauncher.h
#pragma once

#ifdef CURRENT_MODULE
#undef CURRENT_MODULE
#endif
#define CURRENT_MODULE "GameLauncher"

#include "game/BattlEyeBypass.h"
#include "system/DllInjector.h"
#include "core/Logger.h"
#include <windows.h>



namespace GameLauncher
{
    /// @brief Suspend the game and inject iOperator.dll then resume
    /// @return true = Game started with DLL injected
    inline bool LaunchAndInject(const BattlEyeBypass::GameLaunchInfo& info)
    {
        LOG_I("Launching game in suspended mode...");

        STARTUPINFOW si{ sizeof(si) };
        PROCESS_INFORMATION pi{};

        wchar_t* cmd = const_cast<wchar_t*>(info.CleanCmdLine.c_str());

        BOOL created = CreateProcessW(
            nullptr,                    // lpApplicationName
            cmd,                        // lpCommandLine (mutable!)
            nullptr, nullptr,           // security
            FALSE,                      // inherit handles
            CREATE_SUSPENDED,           // suspend the game
            nullptr,                    // environment
			info.WorkingDir.c_str(),    // ensure that the game's running at its own dir
            &si, &pi
        );

        if (!created)
        {
            LOG_E("CreateProcessW failed: " + std::to_string(GetLastError()));
            return false;
        }

        LOG_I("Game started (suspended) - PID " + std::to_string(pi.dwProcessId));

        ResumeThread(pi.hThread);

        if (!DllInjector::InjectLocalDll(pi.dwProcessId, "iOperator.dll"))
        {
            LOG_E("DLL injection failed - game will run without cheat");
            Sleep(20000);
        }
        else
        {
            LOG_I("iOperator.dll injected successfully!");
            LOG_I("Bootstrap complete. Enjoy your raids!");
        }

        CloseHandle(pi.hThread);
        CloseHandle(pi.hProcess);

        return true;
    }
}