#include "stdafx.h"
#include "GMTool.h"
#include "resource.h"

#include "Monster_Common.h"
#include "MonsterScript.h"
#include "Protocol_Cheat.h"
#include "Cheat_Common.h"

#include "ObjectManager.h"
#include "Hero.h"

void cGMTool::InitDlgMonster()
{
	HWND hWnd = mSubDlg[GMTOOL_SUBDLG_MONSTER];
	
	/// 에디트박스 기본 값 설정
	SetDlgItemInt( hWnd, IDC_EDIT_MONSTERCOUNT, 1, false );

	/// 위치세팅
	RECT rc0;
	::GetWindowRect( mMainDlg, &rc0 );
	int x = rc0.right;
	int y = rc0.top + GetSystemMetrics(SM_CYCAPTION);
	::SetWindowPos( hWnd, 0, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );

	mSubDlgPos[GMTOOL_SUBDLG_MONSTER].x = 0;
	mSubDlgPos[GMTOOL_SUBDLG_MONSTER].y = GetSystemMetrics(SM_CYCAPTION);
}

/// 몬스터 관련 이벤트 
void cGMTool::OnCommandMonster( int id, int msg )
{
	switch( id )
	{
	case IDC_BUTTON_MONCANCEL:
		::ShowWindow( mSubDlg[GMTOOL_SUBDLG_MONSTER], SW_HIDE );
		::SetActiveWindow( mParentWnd );
		break;
	case IDC_BUTTON_MONCREATE:	/// 몬스터 생성
		if( NETWORK->IsConnected() == true )
		{
			/// 몬스터 인덱스 받기
			HWND hComDlg = GetDlgItem(mSubDlg[GMTOOL_SUBDLG_MONSTER], IDC_COMBO_MONSTERLIST);
			int i = SendMessage( hComDlg, CB_GETCURSEL, 0, 0 );
			long monIdx = SendMessage( hComDlg, CB_GETITEMDATA, i, 0 );

			/// 수량 받기
			BOOL retcode;
			int count = GetDlgItemInt( mSubDlg[GMTOOL_SUBDLG_MONSTER], IDC_EDIT_MONSTERCOUNT, &retcode, false );
			if( retcode == false )
				return;

			if( count == 0 )
				return;

			if( count > 128 )
			{
				SetDlgItemInt( mSubDlg[GMTOOL_SUBDLG_MONSTER], IDC_EDIT_MONSTERCOUNT, 128, false );
				count = 128;
			}

			/// 생성
			MSG_REQ_CHEAT_SUMMON msg;
			::memset( &msg, 0, sizeof( msg ) );

			msg.Category = NM_CHEAT;
			msg.Protocol = NM_CHEAT_SUMMON_REQ;
			msg.mMonsterNum = monIdx;
			msg.mValue = count;
			NETWORK->SendNetworkMsg( (char*)&msg, sizeof(msg) );

			/// 초기화
			//SetDlgItemInt( mSubDlg[GMTOOL_SUBDLG_MONSTER], IDC_EDIT_MONSTERCOUNT, 1, false );
		}
		break;
	case IDC_BUTTON_MONDELETE:	/// 몬스터 제거
		if( NETWORK->IsConnected() == true )
		{
			cBaseObject* pTarget = HERO->GetTargetObject();
			if( pTarget == NULL )
				return;

			if( pTarget->GetObjectType() != eOBJECTTYPE_MONSTER )
				return;

			MSG_REQ_CHEAT_REMOVE msg;
			::memset( &msg, 0, sizeof( msg ) );

			msg.Category = NM_CHEAT;
			msg.Protocol = NM_CHEAT_REMOVE_REQ;
			msg.mObjIndex = pTarget->GetObjectID();
			NETWORK->SendNetworkMsg( (char*)&msg, sizeof(msg) );
		}
		break;
	case IDC_BUTTON_MONKILL:	/// 몬스터 사냥 ( 드랍 & 경헙치 있음 )
		if( NETWORK->IsConnected() == true )
		{
			cBaseObject* pTarget = HERO->GetTargetObject();
			if( pTarget == NULL )
				return;

			if( pTarget->GetObjectType() != eOBJECTTYPE_MONSTER )
				return;

			MSG_REQ_CHEAT_KILL msg;
			::memset( &msg, 0, sizeof( msg ) );

			msg.Category = NM_CHEAT;
			msg.Protocol = NM_CHEAT_KILL_REQ;
			msg.mObjIndex = pTarget->GetObjectID();
			NETWORK->SendNetworkMsg( (char*)&msg, sizeof(msg) );
		}
		break;
	case IDC_BUTTON_MONDELETE_RANGE:	/// 몬스터 일정 반경 제거
		if( NETWORK->IsConnected() == true )
		{
			MSG_REQ_CHEAT_REMOVE_MONSTER msg;
			::memset( &msg, 0, sizeof( msg ) );

			msg.Category = NM_CHEAT;
			msg.Protocol = NM_CHEAT_REMOVE_MONSTER_REQ;
			NETWORK->SendNetworkMsg( (char*)&msg, sizeof(msg) );
		}
		break;
	}
}

bool cGMTool::LoadMonsterData()
{
	/// 몬스터 리스트 콤포 받기
	HWND nameCombo = GetDlgItem( mSubDlg[GMTOOL_SUBDLG_MONSTER], IDC_COMBO_MONSTERLIST );
	if( ::IsWindow( nameCombo ) == false )
	{
		assert(0);
		return false;
	}

	/// 몬스터 세팅 
	typedef const tPointerHashMap<unsigned long, void*> cMonsterHashMap;
	cMonsterHashMap* monsterMap = MONSTERSCRIPT->GetMonsterMap();
	if( !monsterMap )
		return false;

	cMonsterHashMap::cConstIterator i = monsterMap->Begin();
	cMonsterHashMap::cConstIterator end = monsterMap->End();
	for( ; i != end; ++i )
	{
		sMonsterScript* p = (sMonsterScript*)(i->mSecond);
		if( p )
		{
			int i = SendMessage( nameCombo, CB_ADDSTRING, 0, (LPARAM)(p->mName));
			SendMessage( nameCombo, CB_SETITEMDATA, i, (LPARAM)p->mMonsterClassIdx );
		}
	}

	/// 콤보 박스 기본 선택 셋팅
	SendMessage( GetDlgItem(mSubDlg[GMTOOL_SUBDLG_MONSTER], IDC_COMBO_MONSTERLIST), CB_SETCURSEL, 0, 0 );
	return true;
}