#pragma once

#include "event.h"

#define OBJPREFIX LR"(Local\)"
constexpr size_t guidSize = std::size(L"{00000000-0000-0000-0000-000000000000}"); // including the null terminator
constexpr size_t prefixSize = std::size(OBJPREFIX) - 1;
constexpr size_t objNameSize = prefixSize + guidSize;

struct ProcessInfo
{
	DWORD processId;
	DWORD threadId;
	HWND hWndConsole;
};

enum struct State
{
	Stop,
	Running,
	WaitStop
};

class CProcessManager : public SSingleton<CProcessManager>
{
public:
	
	CProcessManager();
	~CProcessManager();


	void _UpdateClashCmd();

	void SetInsTanCe(HINSTANCE m);

	void SetArgs(std::filesystem::path exePath, std::filesystem::path homeDir, std::filesystem::path cconfigFile);

	void SetConfigFile(std::filesystem::path configFile);


	State IsRunning();
	const PROCESS_INFORMATION& GetSubProcessInfo();
	const PROCESS_INFORMATION& GetClashProcessInfo();
	HWND GetConsoleWindow();

	void StartTest();


	bool Start();

	void Stop();

	void SendStopSignal();


	int SubProcess(std::wstring_view guid);

private:
	State _state = State::Stop;
	std::filesystem::path _exePath;
	std::filesystem::path _homeDir;
	std::filesystem::path _configFile;
	std::wstring _ctlAddr, _ctlSecret, _clashCmd;
	wil::unique_handle _hJob;
	wil::unique_process_information _subProcInfo, _clashProcInfo;
	HWND _hWndConsole = nullptr;
	wil::unique_handle _hEvent;
	HINSTANCE mInstance;

};

