#include "../../netwaker_member/NetWakerClientInc.h"
#include "../mainproc/GameMain.h"
#include "../WorldFrames/UnitFrame.h"
#include "../WorldFrames/ItemClipboard.h"
#include "../WorldFrames/ActionFrame.h"
#include "../MessageBox/MessageBox.h"
#include "../interface/Debug/DebugMessage.h"
#include "GameControl.h"

#define CLICK_INVALID_TIME					0.2f
#define MAX_CAMERA_THETA					0.95f
#define MIN_CAMERA_THETA					-0.95f
#define MAX_CAMERA_DISTANCE					500.0f
#define MIN_CAMERA_DISTANCE					10.0f

CGameControl*			g_pGameControl = NULL;

int	LuaFunc_GetFramerate(lua_State* L);
int	LuaFunc_GetCursorPos(lua_State* L);
int	LuaFunc_GetPlayerPosition(lua_State* L);
int	LuaFunc_GetZoneID(lua_State* L);
int LuaFunc_IsShiftKeyDown(lua_State* L);
int	LuaFunc_GetMouseMoveOffset(lua_State* L);
int LuaFunc_GetScreenWidth(lua_State* L);
int LuaFunc_GetScreenHeight(lua_State* L);

int	LuaFunc_SpellIsTargeting(lua_State* L);
int	LuaFunc_SpellTargetUnit(lua_State* L);
int	LuaFunc_DropItemOnUnit(lua_State* L);
int	LuaFunc_TargetUnit(lua_State* L);

// ----------------------------------------------------------------------------
CGameControl::CGameControl(CInterface* object)
	: CInterfaceBase(object)
{
	g_pGameControl = this;

	m_pUiWorldFrame			= NULL;	
	m_isLockCursor			= false;
	m_isLockForward			= false;
	m_isJump				= false;
	m_forwardDelta			= 0.0f;
	m_rightDelta			= 0.0f;
}

// ----------------------------------------------------------------------------
CGameControl::~CGameControl()
{
	g_pGameControl = NULL;

	m_pUiWorldFrame			= NULL;
	m_isLockForward			= false;
	m_isLockCursor			= false;
}

// ----------------------------------------------------------------------------
void CGameControl::RegisterFunc()
{
	lua_State* L = GetLuaStateWorld();
	if (L)
	{
		lua_register(L, "GetFramerate",			LuaFunc_GetFramerate);
		lua_register(L, "GetCursorPos",			LuaFunc_GetCursorPos);
		lua_register(L, "GetPlayerPosition",	LuaFunc_GetPlayerPosition);
		lua_register(L, "IsShiftKeyDown",		LuaFunc_IsShiftKeyDown);
		lua_register(L, "GetMouseMoveOffset",	LuaFunc_GetMouseMoveOffset);
		lua_register(L, "GetZoneID",			LuaFunc_GetZoneID);
		lua_register(L, "GetScreenWidth",		LuaFunc_GetScreenWidth);
		lua_register(L, "GetScreenHeight",		LuaFunc_GetScreenHeight);
		
		lua_register(L, "SpellIsTargeting",		LuaFunc_SpellIsTargeting);
		lua_register(L, "SpellTargetUnit",		LuaFunc_SpellTargetUnit);
		lua_register(L, "DropItemOnUnit",		LuaFunc_DropItemOnUnit);
		lua_register(L, "TargetUnit",			LuaFunc_TargetUnit);
	}
}

// ----------------------------------------------------------------------------
void CGameControl::Update(float elapsedTime)
{
	m_isJump				= false;
	m_forwardDelta			= 0.0f;
	m_rightDelta			= 0.0f;	

	// 是否擁有鍵盤焦點
	HandleKeyboard(elapsedTime);

	// 滑鼠訊息
	HandleMouse(elapsedTime);

	// 處理角色移動
	HandleMove(elapsedTime);	

	if (m_isLockCursor)
	{
		CUiInput* input = m_interface->GetUiInput();

		// 旋轉鏡頭
		if (input->GetXDelta() != 0.0f || input->GetYDelta() != 0.0f)
		{
			if (input->GetButtonState(MOUSE_LBUTTON) & CUiInput::eKey_Press || input->GetButtonState(MOUSE_RBUTTON) & CUiInput::eKey_Press )
			{
				RotateCamera(input->GetXDelta() * -0.005f, input->GetYDelta() * -0.005f, input->GetButtonState(MOUSE_RBUTTON) & CUiInput::eKey_Press);
			}
		}
	}		
}

// ----------------------------------------------------------------------------
bool CGameControl::MsgProc(UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	// Open ChatFrmae_EditBox
	switch(iMsg)
	{
	case WM_CHAR:
		switch( wParam )
		{
		// Enter
		case L'\r':
			{
				CUiFrame* frame = (CUiFrame*)g_pInterface->GetUiWorld()->FindObject("ChatFrameEditBox");
				if (frame)
				{
					frame->SetVisible(true);
					frame->SetFocus();
				}
				else
				{
					CUiFrame* frame = (CUiFrame*)g_pInterface->GetUiWorld()->FindObject("ChatFrameInput");
					if ( frame )
						frame->SetVisible(true);
				}
			}
			break;
		}		
		break;
	}	
	
	return true;
}

/*
// ----------------------------------------------------------------------------
void CGameControl::SetScripts()
{
	CUi* ui = m_interface->GetUiWorld();
	if (ui)
	{
		m_pUiWorldFrame = (CUiFrame*)ui->FindObject("WorldFrame");
		if (m_pUiWorldFrame)
		{			
			m_pUiWorldFrame->SetMouseEnable(true);
			m_pUiWorldFrame->RegisterForClicks(CUiFrame::eMouse_Flag_LButton | CUiFrame::eMouse_Flag_RButton);

			m_pUiWorldFrame->SetScripts(CUiFrame::eEventType_MouseDown,		NULL, UiWorld_OnMouseDown);
			m_pUiWorldFrame->SetScripts(CUiFrame::eEventType_MouseUp,		NULL, UiWorld_OnMouseUp);
			m_pUiWorldFrame->SetScripts(CUiFrame::eEventType_Click,			NULL, UiWorld_OnMouseClick);
			m_pUiWorldFrame->SetScripts(CUiFrame::eEventType_MouseWheel,	NULL, UiWorld_OnMouseWheel);			
			m_pUiWorldFrame->SetScripts(CUiFrame::eEventType_ReceiveDrag,	NULL, UiWorld_OnReceiveDrag);
			m_pUiWorldFrame->SetScripts(CUiFrame::eEventType_EnterPressed,	NULL, UiWorld_OnEnterPressed);
		}
	}
}
*/

// ----------------------------------------------------------------------------
void CGameControl::HandleKeyboard(float elapsedTime)
{	
	CUi* ui = m_interface->GetUiWorld();	
	CRoleSprite* player = g_pGameMain->GetPlayer();		

	if (ui == NULL || player == NULL || ui->GetKeyboardFocus() )
		return;

	lua_State* L = ui->GetLuaState();
	CUiInput* input = ui->GetUiInput();		

	// Changed walk <-> run
	if (input->GetKeyState(KEY_DIVIDE) & CUiInput::eKey_Down)
		player->SetWalkState(!player->GetWalkState());

	// Changed Lock forward
	if (input->GetKeyState(KEY_NUMLOCK) & CUiInput::eKey_Down)
		m_isLockForward = !m_isLockForward;	

	// Move Up
	if (input->GetKeyState(KEY_W) & CUiInput::eKey_Press)
		m_forwardDelta += elapsedTime;

	// Move Down
	if ( input->GetKeyState(KEY_S) & CUiInput::eKey_Press )
		m_forwardDelta -= elapsedTime;

	// Move Right
	if ( input->GetKeyState(KEY_D) & CUiInput::eKey_Press )
		m_rightDelta += elapsedTime;

	// Move Left
	if ( input->GetKeyState(KEY_A) & CUiInput::eKey_Press )
		m_rightDelta -= elapsedTime;

	// 跳
	if ( input->GetKeyState(KEY_SPACE) & CUiInput::eKey_Down )
		m_isJump = true;		

	// Rotate Left
	if ( input->GetKeyState(KEY_Q) & CUiInput::eKey_Press )
		RotateCamera( elapsedTime*1.5f, 0.0f, true );

	// Rotate Right
	if ( input->GetKeyState(KEY_E) & CUiInput::eKey_Press )
		RotateCamera( -elapsedTime*1.5f, 0.0f, true );	

	// hot key 1, 2, ..., -, =
	if (input->GetKeyState(KEY_1) & CUiInput::eKey_Down)
		g_pActionFrame->Use(0);
	if (input->GetKeyState(KEY_2) & CUiInput::eKey_Down)
		g_pActionFrame->Use(1);
	if (input->GetKeyState(KEY_3) & CUiInput::eKey_Down)
		g_pActionFrame->Use(2);
	if (input->GetKeyState(KEY_4) & CUiInput::eKey_Down)
		g_pActionFrame->Use(3);
	if (input->GetKeyState(KEY_5) & CUiInput::eKey_Down)
		g_pActionFrame->Use(4);
	if (input->GetKeyState(KEY_6) & CUiInput::eKey_Down)
		g_pActionFrame->Use(5);
	if (input->GetKeyState(KEY_7) & CUiInput::eKey_Down)
		g_pActionFrame->Use(6);
	if (input->GetKeyState(KEY_8) & CUiInput::eKey_Down)
		g_pActionFrame->Use(7);
	if (input->GetKeyState(KEY_9) & CUiInput::eKey_Down)
		g_pActionFrame->Use(8);
	if (input->GetKeyState(KEY_0) & CUiInput::eKey_Down)
		g_pActionFrame->Use(9);
	if (input->GetKeyState(KEY_MINUS) & CUiInput::eKey_Down)
		g_pActionFrame->Use(10);
	if (input->GetKeyState(KEY_EQUALS) & CUiInput::eKey_Down)
		g_pActionFrame->Use(11);

	// Reply Tell
	if (input->GetKeyState(KEY_R) & CUiInput::eKey_Down)
	{
		lua_getglobal(L, "ChatEdit_ReplyTell");
		lua_call(L, 0, 0);
	}

	// Holster
	if (input->GetKeyState(KEY_Z) & CUiInput::eKey_Down)
	{
		/*
		if ( !player->GetCombatState() )
		{
			if ( player->IsUnholster() )
				g_pGameMain->SetPlayerAction(eAction_Holster);
			else
				g_pGameMain->SetPlayerAction(eAction_Unholster);
		}
		*/
	}

	// Sit
	if (input->GetKeyState(KEY_X) & CUiInput::eKey_Down)
	{
		/*
		if (!player->IsDead() && !player->IsSleep())
		{
			if (player->IsSit())
				g_pGameMain->SetPlayerAction(eAction_StandUp);
			else
				g_pGameMain->SetPlayerAction(eAction_Sit);
		}
		*/
	}

	// BackPackage Interface
	if (input->GetKeyState(KEY_B) & CUiInput::eKey_Down)
	{
		CUiFrame* frame = (CUiFrame*)ui->FindObject("BagFrame");
		if (frame)
			frame->SetVisible(!frame->IsVisible());
	}

	// Character Status Interface
	if (input->GetKeyState(KEY_C) & CUiInput::eKey_Down)
	{
		CUiFrame* frame = (CUiFrame*)ui->FindObject("CharacterFrame");
		if (frame)
			frame->SetVisible(!frame->IsVisible());
	}

	// QuestBook Interface
		if (input->GetKeyState(KEY_L) & CUiInput::eKey_Down)
		{
			CUiFrame* frame = (CUiFrame*)ui->FindObject("UI_QuestBook");
			if (frame)
				frame->SetVisible(!frame->IsVisible());
		}

	// SkillBook Interface 
		if (input->GetKeyState(KEY_K) & CUiInput::eKey_Down)
		{
			CUiFrame* frame = (CUiFrame*)ui->FindObject("UI_SkillBook");
			if (frame)
				frame->SetVisible(!frame->IsVisible());
		}

	// WorldMap Interface 
		if (input->GetKeyState(KEY_M) & CUiInput::eKey_Down)
		{
			CUiFrame* frame = (CUiFrame*)ui->FindObject("UI_WorldMapFrame");
			if (frame)
				frame->SetVisible(!frame->IsVisible());
		}

	// Social Interface
		if (input->GetKeyState(KEY_O) & CUiInput::eKey_Down)
		{
			CUiFrame* frame = (CUiFrame*)ui->FindObject("SocialFrame");
			if (frame)
				frame->SetVisible(!frame->IsVisible());
		}

	// Test
		if (input->GetKeyState(KEY_T) & CUiInput::eKey_Down)
		{
			MessageBoxInfo msg;
			
			msg.iType			= 1;
			msg.szString		= "測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試";
			msg.iNumButton		= 1;
			msg.szButtonName[0]	= "確認";
			msg.iInputMode		= 0;


			g_pMessageBoxFrame->MessageBox( msg, NULL );
		}

}

// ----------------------------------------------------------------------------
void CGameControl::HandleMouse(float elapsedTime)
{
	CUi* ui = m_interface->GetUiWorld();
	CRoleSprite* player = g_pGameMain->GetPlayer();	

	if (ui == NULL || player == NULL || (ui->GetMouseFocus() && ui->GetMouseFocus() != ui->GetRoot()))
		return;

	CUiInput* input = ui->GetUiInput();

	// 滾輪滾動
	if ( input->GetZDelta() != 0 )
		OnMouseWheel(input->GetZDelta());

	// 按鈕
	for (int key = 0; key < MAX_MOUSE_BUTTON_NUM; key++)
	{
		if (input->GetButtonDown(key))
			OnMouseDown(key);

		if (input->GetButtonUp(key))
			OnMouseUp(key);
	}
}

// ----------------------------------------------------------------------------
void CGameControl::HandleMove(float elapsedTime)
{
	CRoleSprite* player = g_pGameMain->GetPlayer();
	if (player)
	{
		// Lock forward
		if ( m_isLockForward )
			m_forwardDelta = elapsedTime;

		// 挖制角色移動
		player->SetMoveVector( player->ComputeMoveVector(m_forwardDelta, m_rightDelta) );

		// 跳
		if ( m_isJump )
			g_pGameMain->Jump();
	}
}

// ----------------------------------------------------------------------------
void CGameControl::OnMouseDown(int key)
{
	switch(key)
	{
	case MOUSE_LBUTTON:
		LockCursor();
		break;

	case MOUSE_RBUTTON:
		LockCursor();
		break;

	case MOUSE_MBUTTON:
		break;

	case MOUSE_XBUTTON:
		break;
	}
}

// ----------------------------------------------------------------------------
void CGameControl::OnMouseUp(int key)
{
	CUiInput* input = m_interface->GetUiInput();
	switch(key)
	{
	case MOUSE_LBUTTON:
	case MOUSE_RBUTTON:
		UnlockCursor();
		if ( input->GetLastButtonPushedTime(key) < CLICK_INVALID_TIME )
			OnClick(key);
		break;

	case MOUSE_MBUTTON:
		break;

	case MOUSE_XBUTTON:
		break;
	}	
}

// ----------------------------------------------------------------------------
void CGameControl::OnClick(int key)
{
	CRoleSprite* current = g_pGameMain->GetMouseInPlayer();
	CRoleSprite* target = g_pGameMain->GetTargetPlayer();
	CRoleSprite* player = g_pGameMain->GetPlayer();

	switch(key)
	{
	case MOUSE_LBUTTON:
		if ( g_pItemClipboard->GetType() == CItemClipboard::eItem_Type_None )
		{
			if ( current )
			{
				g_pGameMain->SetTargetPlayer( current );

				switch ( current->GetCheckedStatus() )
				{
				case eChecked_TouchPlot:
				case eChecked_QuestNPC:
				case eChecked_QuestObj:
					// run quest
					if ( player )
					{
						float dis = (current->GetPos() - player->GetPos()).Magnitude();
						if ( dis > 25.0f )
							g_pDebugMessage->DebugOutput(2, "距離過遠,無法觸發事件");
						else
						{
							switch ( current->GetCheckedStatus() )
							{
							case eChecked_QuestNPC:
								CNetCli_Script::OnEvent_TouchQuestNPC( current->GetIdentify() );
								break;

							case eChecked_TouchPlot:
								CNetCli_Script::OnEvent_TouchPlot( current->GetIdentify() );
								break;

							case eChecked_QuestObj:
								CNetCli_Script::OnEvent_TouchQuestObj( current->GetIdentify() );
								break;
							}
						}						
					}
					break;

				case eChecked_Fight:		// 攻擊目標
					if ( target == current && !current->GetDeadState() )
					{
						g_pGameMain->Fight();
					}
				}
			}
		}
		else
			OnReceiveDrag();
		break;

	case MOUSE_RBUTTON:
		g_pGameMain->CancelAttack();
		g_pItemClipboard->Clear();
		break;

	case MOUSE_MBUTTON:
		break;
	case MOUSE_XBUTTON:
		break;
	}
}

// ----------------------------------------------------------------------------
void CGameControl::OnMouseWheel(int delta)
{
	CRuCamera* camera = g_pGameMain->GetCamera();

	if (camera)
	{
		float zDelta = delta * 0.02f;
		float dis = (camera->GetTargetPosition() - camera->GetCameraPosition()).Magnitude();

		if ( dis - zDelta > MAX_CAMERA_DISTANCE )
			zDelta -= MAX_CAMERA_DISTANCE - (dis - zDelta);
		if ( dis - zDelta < MIN_CAMERA_DISTANCE )
			zDelta -= MIN_CAMERA_DISTANCE - (dis - zDelta);

		camera->MoveCamera( CRuVector3(0.0f, 0.0f, zDelta), true );
		g_pGameMain->GetCameraController()->UpdatePresetCameraConfiguration();
	}	
}

// ----------------------------------------------------------------------------
void CGameControl::LockCursor()
{
	m_isLockCursor = true;
	m_interface->SetCapture();
	g_pGameMain->LockCursorPos();
	g_pGameMain->SetCursorVisible( false );
}

// ----------------------------------------------------------------------------
void CGameControl::UnlockCursor()
{
	CUiInput* input = m_interface->GetUiInput();
	if (input)
	{		
		if (input->GetButtonState(MOUSE_LBUTTON) & CUiInput::eKey_Press || input->GetButtonState(MOUSE_RBUTTON) & CUiInput::eKey_Press )
			return;

		m_isLockCursor = false;
		m_interface->ReleaseCapture();
		g_pGameMain->UnlockCursorPos();
		g_pGameMain->SetCursorVisible( true );
	}
}

// ----------------------------------------------------------------------------
void CGameControl::OnReceiveDrag()
{
	CRoleSprite* player = g_pGameMain->GetMouseInPlayer();

	switch (g_pItemClipboard->GetType())
	{
	case CItemClipboard::eItem_Type_Bag:
		{
			RoleDataEx* role = CNetGlobal::RoleData();
			ClipboardBagItem backpackItem = g_pItemClipboard->GetBagItem();
			if (player && player->GetObjectType() == eSpriteObject_Player)		// open trade 
			{
			}
			else
			{				
				ItemFieldStruct* pItemField = role->GetBodyItem( backpackItem.pos );
				if ( pItemField )
					NetCli_Item::_DeleteItem_Body( *pItemField, backpackItem.pos );
				g_pItemClipboard->Clear();
			}
		}
		break;
			
	case CItemClipboard::eItem_Type_Bank:
		{
			RoleDataEx* role = CNetGlobal::RoleData();
			ClipboardBankItem bankItem = g_pItemClipboard->GetBankItem();
			if (player && player->GetObjectType() == eSpriteObject_Player)		// open trade 
			{
			}
			else
			{				
				ItemFieldStruct* pItemField = role->GetBankItem( bankItem.pos );
				if ( pItemField )
					NetCli_Item::_DeleteItem_Bank( *pItemField, bankItem.pos );
				g_pItemClipboard->Clear();
			}
		}		
		break;

	case CItemClipboard::eItem_Type_Equipment:
		{ 
			RoleDataEx* role = CNetGlobal::RoleData();
			ClipboardEquipmentItem equipmentItem = g_pItemClipboard->GetEquipmentItem();
			if (player && player->GetObjectType() == eSpriteObject_Player)		// open trade 
			{
			}
			else
			{				
				ItemFieldStruct* pItemField = role->GetEqItem( equipmentItem.pos );
				if (pItemField)
					NetCli_Item::_DeleteItem_EQ( *pItemField, equipmentItem.pos );
				g_pItemClipboard->Clear();
			}
		}		
		break;

	case CItemClipboard::eItem_Type_Shop:
		break;

	case CItemClipboard::eItem_Type_Trade:
		break;

	case CItemClipboard::eItem_Type_Skill:
		g_pItemClipboard->Clear();
		break;

	case CItemClipboard::eItem_Type_Action:
		g_pItemClipboard->Clear();
		break;
	}
}

// ----------------------------------------------------------------------------
void CGameControl::RotateCamera(float xDelta, float yDelta, bool isModifyPlayer)
{
	CRuCamera* camera = g_pGameMain->GetCamera();
	if ( camera == NULL || (xDelta == 0.0f && yDelta ==0.0f) )
		return;

	CRuVector3 dir = camera->GetTargetPosition() - camera->GetCameraPosition();
	//dir.m_x = abs( dir.m_x );
	//dir.m_z = abs( dir.m_z );
	dir.Normalize();
	float theta = dir.Dot( CRuVector3(0.0f, 1.0f, 0.0f) );	

	if ( theta + yDelta > MAX_CAMERA_THETA )
		yDelta = MAX_CAMERA_THETA - theta;			
	if ( theta + yDelta < MIN_CAMERA_THETA )
		yDelta = MIN_CAMERA_THETA - theta;

	camera->Cycle( xDelta, CRuVector3(0.0f, 1.0f, 0.0f) );
	camera->Cycle( yDelta, camera->GetCameraRightVector() );

	if ( isModifyPlayer )
	{
		CRoleSprite* player = g_pGameMain->GetPlayer();
		if ( player )
			player->SetDirection(camera->GetTargetPosition() - camera->GetCameraPosition());
	}
}

// ----------------------------------------------------------------------------
int LuaFunc_GetFramerate(lua_State* L)
{
	lua_pushnumber(L, g_pGameMain->GetFramerate());
	return 1;
}

// ----------------------------------------------------------------------------
int	LuaFunc_GetCursorPos(lua_State* L)
{
	CUiInput* input = g_pInterface->GetUiInput();
	if ( input )
	{
		lua_pushnumber(L, input->GetXPos());
		lua_pushnumber(L, input->GetYPos());
		return 2;
	}
	return 0;
}

// ----------------------------------------------------------------------------
int	LuaFunc_GetZoneID(lua_State* L)
{
	lua_pushnumber(L, g_pGameMain->GetZoneID());
	return 1;
}

// ----------------------------------------------------------------------------
int LuaFunc_GetScreenWidth(lua_State* L)
{
	lua_pushnumber(L, g_pGameMain->GetScreenWidth());
	return 1;
}

// ----------------------------------------------------------------------------
int LuaFunc_GetScreenHeight(lua_State* L)
{
	lua_pushnumber(L, g_pGameMain->GetScreenHeight());
	return 1;
}

// ----------------------------------------------------------------------------
int	LuaFunc_GetPlayerPosition(lua_State* L)
{
	CRoleSprite* player = g_pGameMain->GetPlayer();
	if (player)
	{
		CRuVector3 pos = player->GetPos();
		lua_pushnumber(L, pos.m_x);
		lua_pushnumber(L, pos.m_y);
		lua_pushnumber(L, pos.m_z);
		
	}
	else
	{
		lua_pushnumber(L, 0);
		lua_pushnumber(L, 0);
		lua_pushnumber(L, 0);
	}
	return 3;
}

// ----------------------------------------------------------------------------
int LuaFunc_IsShiftKeyDown(lua_State* L)
{
	CUiInput* input = g_pInterface->GetUiInput();
	if (input)
	{
		if (input->GetKeyState(KEY_SHIFT) & CUiInput::eKey_Press)
			lua_pushboolean(L, true);
		else
			lua_pushboolean(L, false);
		return 1;
	}
	return 0;
}

// ----------------------------------------------------------------------------
int	LuaFunc_GetMouseMoveOffset(lua_State* L)
{
	CUiInput* input = g_pInterface->GetUiInput();
	if (input)
	{		
		lua_pushnumber(L, input->GetXDelta());
		lua_pushnumber(L, input->GetYDelta());
		return 2;
	}
	return 0;
}

// ----------------------------------------------------------------------------
int	LuaFunc_SpellIsTargeting(lua_State* L)
{
	lua_pushboolean(L, false);
	return 1;
}

// ----------------------------------------------------------------------------
int	LuaFunc_SpellTargetUnit(lua_State* L)
{
	if ( lua_isstring( L, 1) )
	{
		int index;
		switch ( CUnitFrame::GetUnitType( lua_tostring(L, 1), index ) )
		{
		case CUnitFrame::eUnit_Player:
			{
				CRoleSprite* player = g_pGameMain->GetPlayer();
				if ( player )
				{
				}
			}
			break;

		case CUnitFrame::eUnit_Target:
			{
				CRoleSprite* player = g_pGameMain->GetTargetPlayer();
				if ( player )
				{
				}
			}
			break;

		case CUnitFrame::eUnit_Party:
			{
				PartyMemberInfo* member = NetCli_PartyChild::GetPartyMember(index - 1);
				if ( member )
				{
				}
			}
			break;

		case CUnitFrame::eUnit_Raid:
			break;

		case CUnitFrame::eUnit_Pet:
			break;
		}
	}
	return 0;
}

// ----------------------------------------------------------------------------
int	LuaFunc_DropItemOnUnit(lua_State* L)
{
	if ( lua_isstring( L, 1) )
	{
		int index;
		switch ( CUnitFrame::GetUnitType( lua_tostring(L, 1), index ) )
		{
		case CUnitFrame::eUnit_Player:
			{
				CRoleSprite* player = g_pGameMain->GetPlayer();
				if ( player )
				{
				}
			}
			break;

		case CUnitFrame::eUnit_Target:
			{
				CRoleSprite* player = g_pGameMain->GetTargetPlayer();
				if ( player )
				{
				}
			}
			break;

		case CUnitFrame::eUnit_Party:
			{
				PartyMemberInfo* member = NetCli_PartyChild::GetPartyMember(index - 1);
				if ( member )
				{
				}
			}
			break;

		case CUnitFrame::eUnit_Raid:
			break;

		case CUnitFrame::eUnit_Pet:
			break;
		}
	}
	return 0;
}

// ----------------------------------------------------------------------------
int	LuaFunc_TargetUnit(lua_State* L)
{
	if ( lua_isstring( L, 1) )
	{
		int index;
		switch ( CUnitFrame::GetUnitType( lua_tostring(L, 1), index ) )
		{
		case CUnitFrame::eUnit_Player:
			g_pGameMain->SetTargetPlayer( g_pGameMain->GetPlayer() );
			break;

		case CUnitFrame::eUnit_Target:
			break;

		case CUnitFrame::eUnit_Party:			
			{
				PartyMemberInfo* member = NetCli_PartyChild::GetPartyMember(index - 1);
				if ( member )
				{
					CRoleSprite* player = g_pGameMain->FindRoleSprite( member->GItemID );
					if ( player )
						g_pGameMain->SetTargetPlayer( player );
				}
			}
			break;

		case CUnitFrame::eUnit_Raid:
			break;

		case CUnitFrame::eUnit_Pet:
			break;
		}
	}
	return 0;
}