// C++ Headers
#include <ctime>

// Windows Headers
#define Rectangle WinRectangle
#define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers
#include <windows.h>
#undef Rectangle 

// Project Headers
#include "Application.h"
#include "Window.h"
#include "Utils.h"


BEGIN_NAMESPACE


struct ApplicationData
{
	int screenW, screenH;
	clock_t lastTick;
	Window* mainWindow;
};


extern LRESULT CALLBACK MainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);


Application::Application()
{
	m_data = new ApplicationData;
	m_data->screenW = GetSystemMetrics(SM_CXSCREEN);
	m_data->screenH= GetSystemMetrics(SM_CYSCREEN);
	WNDCLASSEXW wcx = { sizeof(WNDCLASSEXA), CS_OWNDC | CS_HREDRAW | CS_VREDRAW, MainWndProc,
			0, 0, GetModuleHandle(0), NULL, NULL, NULL, NULL, L"MainWClass", NULL };
	RegisterClassExW(&wcx);
	//m_data->mainWindow = new Window("Test", a_fullscreen);
	//ShowCursor(FALSE);
}


Application::~Application()
{
	//delete m_data->mainWindow;
	delete m_data;
}


void Application::exec()
{
	normalLoop();
}


void Application::normalLoop()
{
	MSG msg;
	while (GetMessageW(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg); 
		DispatchMessageW(&msg); 
	}
}


void Application::gameLoop()
{
	MSG msg;
	while (true)
	{
		// Remove all messages off the queue ... makes it look like we are responding
		while ( GetQueueStatus(QS_ALLEVENTS) )
			PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);

		clock_t tick = clock();
		m_data->lastTick = tick;

		SHORT escape = GetAsyncKeyState(VK_ESCAPE);
		if ( escape )
			break;

		m_data->mainWindow->repaint();
	}
}


END_NAMESPACE
