#include "Game.h"
#include "cGameWin.h"
#include "resource.h"

cGameWin::cGameWin(){

}

cGameWin::~cGameWin(){

}

BOOL cGameWin::RegisterClass(){
	return cWindow::RegisterClass(
		"GameWin", 
		CS_HREDRAW | CS_VREDRAW, 
		LoadIcon(*GetGameApp(), MAKEINTRESOURCE(IDR_GAMEAPP)),
		LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)),
		(HBRUSH)GetStockObject(BLACK_BRUSH));
}

//////////////////////////////////////////////////////////////////////
// Window Prodedure
//////////////////////////////////////////////////////////////////////

LRESULT cGameWin::Proc(UINT uMsg, WPARAM wParam, LPARAM lParam){
	switch(uMsg){
	case WM_CREATE:
		if(!OnCreate(wParam, lParam))
			return -1;
		return 0;
	case WM_DESTROY:
		OnDestroy(wParam, lParam);
		return 0;
	case WM_PAINT:
		OnPaint(wParam, lParam);
		return 0;
	case WM_KEYDOWN:
		OnKeyDown(wParam, lParam);
		return 0;
	case WM_TIMER:
		OnTimer(wParam, lParam);
		return 0;
	}
	return DefProc(uMsg, wParam, lParam);
}


BOOL cGameWin::OnCreate(WPARAM wParam, LPARAM lParam){
	if(!Game.Open())
		return FALSE;
	SetTimer(0, 32); // 1초에 32프레임을 재생
	return TRUE;
}

void cGameWin::OnDestroy(WPARAM wParam, LPARAM lParam){
	KillTimer(0);
	Game.Close();
}

void cGameWin::OnPaint(WPARAM wParam, LPARAM lParam){
	PAINTSTRUCT ps;
	RECT rc;

	BeginPaint(&ps);
	EndPaint(&ps);

	GetClientRect(&rc);
	ClientToScreen(&rc);

	Game.Draw(rc.left, rc.top);
}

void cGameWin::OnKeyDown(WPARAM wParam, LPARAM lParam){
	switch(wParam){
	case VK_ESCAPE:
		((cGameAppWin *)GetParent())
			->ChangePage(GP_TITLE);
		break;
	}
}

void cGameWin::OnTimer(WPARAM wParam, LPARAM lParam){
	Invalidate(FALSE);
	switch(Game.Run()){
	case GRR_USEREXIT:
		((cGameAppWin *)GetParent())
			->ChangePage(GP_TITLE);
		break;
	case GRR_GAMECLEAR:
		((cGameAppWin *)GetParent())
			->ChangePage(GP_GAMECLEAR);
		break;
	case GRR_GAMEOVER:
		((cGameAppWin *)GetParent())
			->ChangePage(GP_GAMEOVER);
		break;
	}
}

