#include "CSystemInfo.h"
#define APPCONFIG L"config.ini"
#define THREADPROCESS "ThreadProcess"
#define PID "pid"
#define THREADID "TheardId"

CSystemInfo::CSystemInfo()
{

}


bool CSystemInfo::IsHanvedCurrentWindow()
{
	std::wstring ss1 = PathCore::smPath_Temp + L"\\" + APPCONFIG;
	wchar_t* ptr = _wcsdup(ss1.c_str());
	std::string ss = CharConvert::GetMultiByteW(ptr);
	HANDLE h = ::CreateMutex(NULL, TRUE, ExeCore::smName_ExeProcess.c_str());

	if (h != NULL)
	{
		if (GetLastError() == ERROR_ALREADY_EXISTS)
		{
			
			DWORD pidIni = LogIni::ReadIntFromini(THREADPROCESS, PID, 0, ss.c_str());
		    DWORD TheardIdIni = LogIni::ReadIntFromini(THREADPROCESS, THREADID, 0, ss.c_str());

			HWND hWnd = ::GetTopWindow(0);
			while (hWnd)
			{
				DWORD pid = 0;
				DWORD dwTheardId = ::GetWindowThreadProcessId(hWnd, &pid);

				if (dwTheardId != 0)
				{
					if (pid == pidIni && TheardIdIni == dwTheardId)
					{
						bool isWindowVisible = IsWindowVisible(hWnd);

						if (isWindowVisible == true)
						{
							ShowWindow(hWnd, SW_SHOWNOACTIVATE);
							break;
						}
					}

				}

				hWnd = ::GetNextWindow(hWnd, GW_HWNDNEXT);

			}

			CloseHandle(h);
			h = NULL;
			return true;
		}
	}

	return false;
}


 bool CSystemInfo::isWriteAppProcessInfoToIni(HWND hwnd)
{
	std::wstring ss1 = PathCore::smPath_Temp + L"\\" + APPCONFIG;
	wchar_t* ptr = _wcsdup(ss1.c_str());
	std::string ss = CharConvert::GetMultiByteW(ptr);
	DWORD pid = 0;
	DWORD dwTheardId = ::GetWindowThreadProcessId(hwnd, &pid);
	BOOL readpid = LogIni::WriteIntToini(THREADPROCESS, PID, pid, ss.c_str());
	BOOL readTheardId = LogIni::WriteIntToini(THREADPROCESS, THREADID, dwTheardId, ss.c_str());

	return (readpid > 0 && readTheardId > 0);
}
