// cGameSelectWindow.cpp: implementation of the cGameSelectWindow class.
//
//////////////////////////////////////////////////////////////////////

#include "cGameSelectWindow.h"
#include "TOOLMemory.h"
#include "cASAppWindow.h"
#include "cGameCreateDlg.h"
#include "cLoginDlg.h"

#include "resource.h"

BOOL cGameSelectWindow::ClassRegistered = false;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

cGameSelectWindow::cGameSelectWindow(){
}

cGameSelectWindow::~cGameSelectWindow(){
	Destroy();
}

//////////////////////////////////////////////////////////////////////
// Window Class Register / Create Window Extended
//////////////////////////////////////////////////////////////////////
BOOL cGameSelectWindow::RegisterClass(){
	ClassRegistered = cWindow::RegisterClass(
		CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW,
		NULL, LoadCursor(hInstance, MAKEINTRESOURCE(IDC_AS)),
		HBRUSH(COLOR_BTNFACE + 1));
	return ClassRegistered;
}

BOOL cGameSelectWindow::Create(cWindow *_pParent, cASEssence *_pEssence){
	RECT rc;

	pEssence = _pEssence;

	GetParent()->GetClientRect(&rc);

	return cWindow::Create(
		WS_EX_LEFT, // Extended Window Style
		NULL, // Caption
		WS_CHILD | WS_VISIBLE, // Window Style
		rc.left, rc.top, // x, y
		rc.right, rc.bottom, // Width, Height
		_pParent, // Parent Window
		NULL); // Menu
}

//////////////////////////////////////////////////////////////////////
// Window Procedure
//////////////////////////////////////////////////////////////////////

LRESULT cGameSelectWindow::WindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam){
	switch(uMsg){
	case WM_CREATE:
		OnCreate(lParam);
		break;
	case WM_DESTROY:
		OnDestroy();
		break;
	case WM_COMMAND:
		OnCommand(wParam, lParam);
		return 0;
	}
	return cWindow::Proc(uMsg, wParam, lParam);
}

//////////////////////////////////////////////////////////////////////
// Message Handlers
//////////////////////////////////////////////////////////////////////
void cGameSelectWindow::OnCreate(LPARAM lParam){
	UserInfo.SetName("GameSel");

	// 차일드 컨트롤 들을 등록 한다.
	CreateWindowEx(WS_EX_LEFT | WS_EX_CLIENTEDGE, "listbox", "게임 목록", 
		WS_TABSTOP | WS_CHILD | WS_VISIBLE | WS_VSCROLL | LBS_SORT | LBS_NOTIFY,
		32, 64, 560, 160, hWnd, (HMENU)IDC_GAME_LIST, hInstance, NULL); 
	CreateWindowEx(WS_EX_LEFT | WS_EX_CLIENTEDGE, "button", "새 게임", 
		WS_TABSTOP | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
		32, 240, 100, 32, hWnd, (HMENU)IDC_NEW_BTN, hInstance, NULL);
	CreateWindowEx(WS_EX_LEFT | WS_EX_CLIENTEDGE, "button", "지우기", 
		WS_TABSTOP | WS_DISABLED | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 
		140, 240, 100, 32, hWnd, (HMENU)IDC_DELETE_BTN, hInstance, NULL);
	CreateWindowEx(WS_EX_LEFT | WS_EX_CLIENTEDGE, "button", "<< 취소", 
		WS_TABSTOP | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 
		32, 420, 100, 32, hWnd, (HMENU)IDC_CANCEL_BTN, hInstance, NULL);
	CreateWindowEx(WS_EX_LEFT | WS_EX_CLIENTEDGE, "button", "다음 >>", 
		WS_TABSTOP | WS_DISABLED | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_DEFPUSHBUTTON, 
		500, 420, 100, 32, hWnd, (HMENU)IDC_NEXT_BTN, hInstance, NULL);

	// 객체 안식처에서 게임 목록의 포인터를 얻어온다.
	cGameInfoList *pGameInfoList = *pEssence;
	if(!pGameInfoList){
		MessageBox("게임 목록을 읽을 수 없습니다.", "오류", MB_OK);
		PostMessage(WM_CLOSE, 0, 0);
		return;
	}
	// 게임 리스트 콘트롤에 항목을 삽입한다.
	cGameInfo gi;
	for(pGameInfoList->FindFirst(); pGameInfoList->IsExist(); pGameInfoList->FindNext()){
		pGameInfoList->GetItem(gi);
		::SendMessage(GetItem(IDC_GAME_LIST)->hWnd, LB_ADDSTRING, 0, (LPARAM)(gi.GetName()));
	}
	if(SendMessage(GetItem(IDC_GAME_LIST)->hWnd, LB_SETCURSEL, 0, 0) != LB_ERR){
		EnableWindow(GetItem(IDC_DELETE_BTN)->hWnd, TRUE);
		EnableWindow(GetItem(IDC_NEXT_BTN)->hWnd, TRUE);
	}
}

void cGameSelectWindow::OnDestroy(){
}


void cGameSelectWindow::OnCommand(WPARAM wParam, LPARAM lParam){
	switch(LOWORD(wParam)){ // Control ID
	case IDC_GAME_LIST: 
		switch(HIWORD(wParam)){ // Notification Code
		case LBN_SELCANCEL:
			EnableWindow(GetItem(IDC_DELETE_BTN)->hWnd, FALSE);
			break;
		case LBN_SELCHANGE:
			EnableWindow(GetItem(IDC_DELETE_BTN)->hWnd, TRUE);
			EnableWindow(GetItem(IDC_NEXT_BTN)->hWnd, TRUE);
			break;
		case LBN_DBLCLK:
			OnNextBtnClicked();
			break;
		}
		break;
	case IDC_NEW_BTN:
		OnNewBtnClicked();
		break;
	case IDC_DELETE_BTN:
		OnDeleteBtnClicked();
		break;
	case IDC_CANCEL_BTN:
		PostMessage(GetParent(), WM_COMMAND, (WPARAM)GS_CANCEL, (LPARAM)hWnd);
		break;
	case IDC_NEXT_BTN:
		OnNextBtnClicked();
		break;

	}
}

void cGameSelectWindow::OnNewBtnClicked(){
// 새로운 게임을 추가할 수 있도록 한다.
	cGameInfo GameInfo;
	cGameCreateDlg dlg;
	cGameInfoList *pGameInfoList = *pEssence;

	if(dlg.DoModal(this, &GameInfo) == IDOK){
		if(!pGameInfoList->Add(GameInfo)){
			MessageBox(hWnd, "게임의 이름이 적당하지 않거나\n동일한 게임 이름이 존재 합나다.\n다른 이름을 입력하세요!", "오류", MB_OK);
			return;
		}
		SendMessage(GetItem(IDC_GAME_LIST), LB_ADDSTRING, 0, (LPARAM)GameInfo.GetName());
		EnableWindow(GetItem(IDC_NEXT_BTN), TRUE);
	}
}

void cGameSelectWindow::OnDeleteBtnClicked(){
// 선택된 게임을 지운다.
	cGameInfo gi;
	int Index;
	cGameInfoList *pGameInfoList = *pEssence;

	Index = SendMessage(GetItem(IDC_GAME_LIST), LB_GETCURSEL, 0, 0);
	if(Index == LB_ERR) return;

	SendMessage(GetItem(IDC_GAME_LIST), LB_GETTEXT, Index, LPARAM(gi.GetName()));
	SendMessage(GetItem(IDC_GAME_LIST), LB_DELETESTRING, Index, 0);
	pGameInfoList->Erase(gi);
	if(SendMessage(GetItem(IDC_GAME_LIST), LB_SETCURSEL, 0, 0) != LB_ERR){
		EnableWindow(GetItem(IDC_DELETE_BTN), TRUE);
		EnableWindow(GetItem(IDC_NEXT_BTN), TRUE);
	}else{
		EnableWindow(GetItem(IDC_DELETE_BTN), FALSE);
		EnableWindow(GetItem(IDC_NEXT_BTN), FALSE);
	}
}

void cGameSelectWindow::OnNextBtnClicked(){
	// 게임자의 ID를 통해 로그인을 한다.
	cLoginDlg LoginDlg;
//	cNewPlayerDlg NewDlg;

	cGameInfo giSelected, gi;
	int Index;

	Index = SendMessage(GetItem(IDC_GAME_LIST), LB_GETCURSEL, 0, 0);
	if(Index == LB_ERR) return;

	SendMessage(GetItem(IDC_GAME_LIST), LB_GETTEXT, Index, LPARAM(giSelected.GetName()));

	// 선택된 게임을 찾는다.
	cGameInfoList *pGameInfoList = *pEssence;
	for(pGameInfoList->FindFirst(); pGameInfoList->IsExist(); pGameInfoList->FindNext()){
		pGameInfoList->GetItem(gi);
		if(gi == giSelected){
			if(LoginDlg.DoModal(this, &UserInfo) == IDOK){
				PostMessage(GetParent(), WM_COMMAND, GS_OK, (LPARAM)hWnd);
			}
			break;
		}
	}
}

void cGameSelectWindow::GetGameData(cGameInfo *pGameInfo, cUserInfo *pUserInfo){
	*pGameInfo = GameInfo;
	*pUserInfo = UserInfo;
}
