#include "stdafx.h"
#include "ServerStage.h"
#include "ServerUIManager.h"

#include "StageManager.h"
#include "GameResourceManager.h"
#include "IntroStage.h"
#include "GameFile.h"

#include "ServerWindow.h"
#include "UIMsgboxEvent.h"
#include "Application.h"

cServerStage::cServerStage()
: mpServerUIManager(0)
{

}

cServerStage::~cServerStage()
{

}

void cServerStage::Init( cStageManager* pStageManager )
{	
	mpStageManager = pStageManager;
}

void cServerStage::InitStage()
{
	if( mInit )	return;

	if( GAMERESOURCEMAN->InitServer() == false )
	{
		assert(0);
	}

	/// UI
	mpServerUIManager = new cServerUIManager;
	mpServerUIManager->Init();
	mInit = true;
}

void cServerStage::Exit()
{
	GAMERESOURCEMAN->LoadEnd();

	SAFE_DELETE( mpServerUIManager );
	mpStageManager = 0;
}

void cServerStage::Open()
{
	/// ui
	mpServerUIManager->Open();

	mLoadComplete = false;
	mCheckServerComplete = false;

	/// 070404 추가 - 곽철중 [서버리스트 요청]
	if ( NETWORK->IsConnected( ) == true )
		ReqServerList();

#ifdef NOCONNECT_SERVER
	mpStageManager->ChangeStage( eStage_Intro );
#endif

	cIntroStage* stage = (cIntroStage*)mpStageManager->GetStage( eStage_Intro );
	if( stage )
		stage->ClearBackImage();

	if( GAMERESOURCEMAN->LoadInitData3() == false )
	{
		assert(0);
	}
}

void cServerStage::Close()
{
	/// UI
	mpServerUIManager->Close();
}

void cServerStage::Process( unsigned long /*deltaTime*/, unsigned long accumTime )
{
	unsigned int flag = GAMERESOURCEMAN->CheckInitData();
	if( flag == eData_Error )
	{
		assert(0);
		NiMessageBox( "Failed load gameData. Please reinstall client or patch.", "GameData Error" );
		::SendMessage( THEAPP->GetHWND(), WM_CLOSE, 0, 0 );
		::PostQuitMessage(0);
		return;
	}
	else if( flag == eData_End )
	{
		mLoadComplete = true;
		GAMERESOURCEMAN->LoadEnd();
	}

	if( mLoadComplete == true && mCheckServerComplete == true )
	{
		/// 모달 처리
		UIMAN->ModalEventFromGameSystem( true );

		/// 스테이지 변경
		STAGEMAN->ChangeStage( eStage_Select );
	}

	/// UI
	mpServerUIManager->Process( accumTime );
}

void cServerStage::ReqServerList()
{
	MSGROOT msg;
	::memset( &msg, 0, sizeof(msg) );

	msg.Category = NM_USER;
	msg.Protocol = NM_USER_SERVER_LIST_REQ;
	NETWORK->SendNetworkMsg( (char*)&msg, sizeof(msg) );

	/// Msgbox 출력
	UIMAN->CreateModal( eStage_Server,
						eMBEVENT_NONE, 
						GAMERESOURCEMAN->GetGameText( 9 ), // 인증 처리 진행
						GAMERESOURCEMAN->GetGameText( 2 ), // 알림
						eSKIN_NONE
						);
}


void cServerStage::NetworkMsgParser( unsigned long , char* pMsg, unsigned long /*length*/ )
{	
	/// 070404 추가 - 곽철중 [서버리스트 & 채널리스트 & 게임접속 응답]
	MSGROOT* pMsgRoot = (MSGROOT*)pMsg;

	switch( pMsgRoot->Category )
	{
	case NM_USER:
		{
			switch( pMsgRoot->Protocol )
			{
			case NM_USER_SERVER_LIST_RES:
				{
					/// 서버리스트 응답 - 성공시 서버리스트 / 실패시 응답 없음
					MSG_RES_SERVERLIST*  pMsgResServerlist = (MSG_RES_SERVERLIST*)pMsg;
					unsigned long        rowServerNo       = 0;

					cServerWindow* pSrvWindow  = (cServerWindow*)UIMAN->GetContainer( eUIID_SERVER_WINDOW );

					/// 서버리스트 삭제.
					if( pSrvWindow )
						pSrvWindow->Clear();

					unsigned long color = eCOLOR_GRAY;
					for ( unsigned long i = 0; i < pMsgResServerlist->RowCount; i++ )
					{
						unsigned int textId = 0;
						switch ( pMsgResServerlist->Servers[ i ].Status )
						{
							/// 초과
						case 4: textId = 700; color = eCOLOR_RED;
							break;
							/// 혼잡
						case 3: textId = 701; color = eCOLOR_ORANGE;
							break;
							/// 양호
						case 2: textId = 702; color = eCOLOR_GREEN;
							break;
							/// 준비중
						case 1: 
						default:
							textId = 703; color = eCOLOR_GRAY;
							break;
						}

						if( pSrvWindow && textId > 0 )
						{
							pSrvWindow->AddServer( pMsgResServerlist->Servers[ i ].NumOfServer,
												   pMsgResServerlist->Servers[ i ].Status,
												   pMsgResServerlist->Servers[ i ].Name,
												   UIMAN->GetUIText( textId ), color );
						}

						if ( pMsgResServerlist->Servers[ i ].NumOfServer == (long)pMsgResServerlist->ServerNo )
							rowServerNo = i;
					}

					if( pSrvWindow )
					{
						pSrvWindow->UpdateButton();
						pSrvWindow->SelectServer( rowServerNo );
					}

					/// 모달 정보 갱신
					UIMAN->ModalEventFromGameSystem( true );

					/// 윈도우 활성화
					mpServerUIManager->Visible();

					/// btn 비활성화
					if( pMsgResServerlist->RowCount == 0 )
					{
						mpServerUIManager->EnableConnectBtn( false );
					}
					else
					{
						mpServerUIManager->EnableConnectBtn( true );
					}
				}
				break;
/*
			case NM_USER_SERVER_SELECT_RES:
				{
					/// 0 아니면 비활성화
					MSG_ERROR* msg = (MSG_ERROR*)pMsg;

					if( msg->ErrorCode == 0 )
						mpServerUIManager->EnableConnectBtn( true );

					UIMAN->ModalEventFromGameSystem( true );
				}
				break;
*/
			case NM_USER_COME_IN_GAME_RES:
				{
					/// 게임접속승인 응답 - 0성공시 / 1실패시 
					MSG_RES_COMEIN_GAME* msg = (MSG_RES_COMEIN_GAME*)pMsg;
					if ( msg->ErrorCode == ERROR_COMEIN_GAME_SUCCESS )
					{
						///
					}
					else
					{
						/// 실패시 처리
						UIMAN->ModalEventFromGameSystem( false );

						/// 서버 목록 재요청
						ReqServerList();
					}
				}
				break;
			case NM_USER_GAMESRV_RES:
				{
					/// 게임서버인증 응답 - 0성공시 / 1실패시 
					MSG_RES_GAMESRV* msg = (MSG_RES_GAMESRV*)pMsg;
					if ( msg->ErrorCode == 0 )
					{
						mCheckServerComplete = true;
						if( mLoadComplete == true )
						{
							/// 모달 처리
							UIMAN->ModalEventFromGameSystem( true );

							/// 스테이지 변경
							STAGEMAN->ChangeStage( eStage_Select );
						}
					}
					else
					{
						UIMAN->ModalEventFromGameSystem( false );
						msg->ErrorCode;
					}
				}
				break;
			}
		}
		break;
	}
}
