#include "Game.h"
#include "cTitleWin.h"
#include "cGameAppWin.h"
#include "resource.h"

const int xOrg = 270, yOrg = 240;
const int ItemCount = 3;

cTitleWin::cTitleWin(){

}

cTitleWin::~cTitleWin(){

}

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

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

LRESULT cTitleWin::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;
	}
	return DefProc(uMsg, wParam, lParam);
}


BOOL cTitleWin::OnCreate(WPARAM wParam, LPARAM lParam){
	if(!BackPic.Create("Title.bmp")) return FALSE;
	if(!ButtonBase.Create("StartupMenu.bmp")) return FALSE;
	Buttons[0].CreateReference(ButtonBase, 0, 0, 100, 30);
	Buttons[1].CreateReference(ButtonBase, 0, 40, 100, 70);
	Buttons[2].CreateReference(ButtonBase, 0, 80, 100, 110);
	Buttons[3].CreateReference(ButtonBase, 100, 0, 200, 30);
	Buttons[4].CreateReference(ButtonBase, 100, 40, 200, 70);
	Buttons[5].CreateReference(ButtonBase, 100, 80, 200, 110);

	Select = 0;

	return TRUE;
}

void cTitleWin::OnDestroy(WPARAM wParam, LPARAM lParam){
	Buttons[0].Destroy();
	Buttons[1].Destroy();
	Buttons[2].Destroy();
	Buttons[3].Destroy();
	Buttons[4].Destroy();
	Buttons[5].Destroy();
	ButtonBase.Destroy();
	BackPic.Destroy();
}

void cTitleWin::OnPaint(WPARAM wParam, LPARAM lParam){
	PAINTSTRUCT ps;
	HDC hdc;
	RECT rc;
	cPicture *pBackPic;
	int i;

	hdc = BeginPaint(&ps);
	EndPaint(&ps);

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

	pBackPic = GetBackPic();
	pBackPic->Blt(rc.left, rc.top, &BackPic);

	for(i = 0; i < ItemCount; i ++){
		if(i == Select){
			pBackPic->Blt(
				xOrg + rc.left,
				yOrg + rc.top + i * 60,
				&Buttons[i + ItemCount],
				DDBLT_WAIT | DDBLT_KEYSRC);
		}else{
			pBackPic->Blt(
				xOrg + rc.left,
				yOrg + rc.top + i * 60,
				&Buttons[i],
				DDBLT_WAIT | DDBLT_KEYSRC);
		}
	}
	GetFrontPic()->Blt(NULL, pBackPic, NULL);
}

void cTitleWin::OnKeyDown(WPARAM wParam, LPARAM lParam){
	switch(wParam){
	case VK_LEFT: 
	case VK_UP:
		Select += ItemCount - 1;
		Select %= ItemCount;
		Invalidate(FALSE);
		break;
	case VK_RIGHT:
	case VK_DOWN:
		Select ++;
		Select %= ItemCount;
		Invalidate(FALSE);
		break;
	case VK_RETURN:
		switch(Select){
		case 0:
			((cGameAppWin *)GetParent())
				->ChangePage(GP_BODY);
			break;
		case 1:
			((cGameAppWin *)GetParent())
				->ChangePage(GP_HIGHSCORE);
			break;
		case 2:
			((cGameAppWin *)GetParent())
				->ChangePage(GP_NONE);
			break;
		}
		break;
	}
}
