#include "Game.h"
#include "cGameAppWin.h"
#include "resource.h"
#include "cTitleWin.h"
#include "cGameWin.h"
#include "cGameClearWin.h"
#include "cGameOverWin.h"
#include "cHighScoreWin.h"

cGameAppWin::cGameAppWin(){
}

cGameAppWin::~cGameAppWin(){

}

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

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

LRESULT cGameAppWin::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_CLOSE:
		PostQuitMessage(0);
		return 0;
	case WM_SETFOCUS:
		if(pChildWin) pChildWin->SetFocus();
		return 0;
	case WM_PAINT:
		OnPaint(wParam, lParam);
		return 0;
	}
	return DefProc(uMsg, wParam, lParam);
}


BOOL cGameAppWin::OnCreate(WPARAM wParam, LPARAM lParam){
	// 다이렉트 드로우를 초기화 한다.
	cDisplay *pDisp = GetDisplay();

	if(!pDisp->Create(hWnd)) return FALSE;
	if(!pDisp->SetCooperativeLevel(DDSCL_NORMAL)) return FALSE;
	if(!pDisp->CreateDoubleBuffer()) return FALSE;

	pChildWin = 0;
	ChangePage(GP_TITLE);
	return TRUE;
}

void cGameAppWin::OnDestroy(WPARAM wParam, LPARAM lParam){
	// 다이렉트 드로우를 제거한다.
	cDisplay *pDisp = GetDisplay();

	ChangePage(GP_NONE);

	pDisp->DestroyDoubleBuffer();
	pDisp->Destroy();
}

void cGameAppWin::OnPaint(WPARAM wParam, LPARAM lParam){
	PAINTSTRUCT ps;
	HDC hdc;
	hdc = BeginPaint(&ps);
	TextOut(hdc, 0, 0, "GameAppWin");
	EndPaint(&ps);
}

void cGameAppWin::ChangePage(eGamePage NewPage){
	if(pChildWin){
		pChildWin->Destroy();
		delete pChildWin;
		pChildWin = 0;
	}

	switch(NewPage){
	case GP_TITLE:
		pChildWin = new cTitleWin;
		break;
	case GP_BODY:
		pChildWin = new cGameWin;
		break;
	case GP_GAMECLEAR:
		pChildWin = new cGameClearWin;
		break;
	case GP_GAMEOVER:
		pChildWin = new cGameOverWin;
		break;
	case GP_HIGHSCORE:
		pChildWin = new cHighScoreWin;
		break;
	case GP_NONE:
		PostMessage(WM_CLOSE, 0, 0);
		break;
	};

	if(pChildWin == 0) return;
	pChildWin->Create(
		WS_EX_LEFT,
		NULL,
		WS_CHILD | WS_VISIBLE,
		0, 0, 640, 480,
		this);
	if(pChildWin) pChildWin->SetFocus();
}
