#include "../../mainproc/GameMain.h"
#include "../../netwaker_member/NetWakerClientInc.h"
#include "PartyFrame.h"
#include "UnitFrame.h"

CUnitFrame*				g_pUnitFrame = NULL;

int LuaFunc_UnitName (lua_State* L);
int LuaFunc_UnitHealth (lua_State* L);
int LuaFunc_UnitMaxHealth (lua_State* L);
int LuaFunc_UnitMana (lua_State* L);
int LuaFunc_UnitMaxMana (lua_State* L);
int LuaFunc_UnitSkill (lua_State* L);
int LuaFunc_UnitMaxSkill (lua_State* L);
int LuaFunc_UnitIsUnit (lua_State* L);
int LuaFunc_UnitIsPlayer (lua_State* L);
int LuaFunc_UnitInParty (lua_State* L);
int	LuaFunc_UnitIsDeadOrGhost(lua_State* L);

// ----------------------------------------------------------------------------
// 索引編號(index)由1開始,0代表無
int CUnitFrame::GetUnitType( const char* unit, int& index )
{
	index = 0;
	if ( unit )
	{
		// 玩家
		if ( strcmp(unit, "player") == 0 )
		{
			return eUnit_Player;
		}
		// 目標
		else if ( strcmp(unit, "target") == 0 )
		{
			return eUnit_Target;
		}
		// 隊伍
		else if ( strncmp(unit, "party", 5) == 0 )
		{
			index = unit[5] - '0';
			return eUnit_Party;
		}
		// 團隊
		else if ( strncmp(unit, "raid", 4) == 0 )
		{
			index = unit[4] - '0';
			return eUnit_Raid;
		}
		// 寵物
		else if ( strcmp(unit, "pet") == 0 )
		{
			return eUnit_Pet;
		}
	}
	return eUnit_None;
}

// ----------------------------------------------------------------------------
const char* CUnitFrame::GetUnitName( const char* unit )
{
	const char* name = NULL;
	int index;

	switch ( GetUnitType(unit, index) )
	{
	case CUnitFrame::eUnit_Player:
		{
			CRoleSprite* player = g_pGameMain->GetPlayer();
			if ( player )
				name = player->GetName();
		}
		break;

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

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

	case CUnitFrame::eUnit_Raid:
		break;

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

// ----------------------------------------------------------------------------
bool CUnitFrame::UnitIsPlayer( const char* unit )
{
	int index;
	switch ( GetUnitType(unit, index) )
	{
	case eUnit_Player:
		return true;

	case eUnit_Target:
		{
			CRoleSprite* player = g_pGameMain->GetTargetPlayer();
			if ( player && player->GetObjectType() == eSpriteObject_Player )
				return true;
		}
		break;

	case eUnit_Party:
		break;

	case eUnit_Raid:
		break;

	case eUnit_Pet:
		break;
	}
	return false;
}

// ----------------------------------------------------------------------------
bool CUnitFrame::UnitInParty( const char* unit )
{
	int index;
	switch ( GetUnitType(unit, index) )
	{
	case eUnit_Player:
		switch ( NetCli_PartyChild::GetPartyState() )
		{
		case ePartyState_Leader:
		case ePartyState_Member:
			return true;
		}
		break;

	case eUnit_Target:
		{
			CRoleSprite* player = g_pGameMain->GetTargetPlayer();
			if ( player )
				return NetCli_PartyChild::InParty( player->GetName() );
		}
		break;

	case eUnit_Party:
		break;

	case eUnit_Raid:
		break;

	case eUnit_Pet:
		break;
	}
	return false;
}

// ----------------------------------------------------------------------------
bool CUnitFrame::UnitIsDeadOrGhost( const char* unit )
{
	int index;
	switch ( GetUnitType(unit, index) )
	{
	case eUnit_Player:
		{
			CRoleSprite* player = g_pGameMain->GetPlayer();
			if ( player )
				return player->GetDeadState();
		}
		break;

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

	case eUnit_Party:
		break;

	case eUnit_Raid:
		break;

	case eUnit_Pet:
		break;
	}
	return false;
}

// ----------------------------------------------------------------------------
bool CUnitFrame::UnitIsUnit( const char* src, const char* dest )
{		
	int src_index;
	switch ( GetUnitType(src, src_index) )
	{
	case eUnit_Player:
		{
			CRoleSprite* player = g_pGameMain->GetPlayer();
			if ( player )
			{
				int dest_index;
				switch ( GetUnitType(dest, dest_index) )
				{
				case eUnit_Player:
					return true;

				case eUnit_Target:
					if ( player == g_pGameMain->GetTargetPlayer() )
						return true;
					break;

				case eUnit_Party:
					break;

				case eUnit_Raid:
					break;

				case eUnit_Pet:
					return false;
				}
			}
		}
		break;

	case eUnit_Target:
		{
			CRoleSprite* player = g_pGameMain->GetTargetPlayer();
			if ( player )
			{
				int dest_index;
				switch ( GetUnitType(dest, dest_index) )
				{
				case eUnit_Player:
					if ( player == g_pGameMain->GetPlayer() )
						return true;
					break;

				case eUnit_Target:
					return true;

				case eUnit_Party:
					break;

				case eUnit_Raid:
					break;

				case eUnit_Pet:
					break;
				}
			}
		}
		break;

	case eUnit_Party:
		break;

	case eUnit_Raid:
		break;

	case eUnit_Pet:
		break;
	}

	return false;
}

// ----------------------------------------------------------------------------
// 索引編號(index)由1開始,0代表無
void CUnitFrame::UnitEvent( const char* eventStr, int type, int index )
{
	lua_State* L = g_pUnitFrame->GetLuaStateWorld();

	switch ( type )
	{
	case eUnit_Player:
		lua_pushstring(L, "player");		
		break;

	case eUnit_Target:
		lua_pushstring(L, "target");
		break;

	case eUnit_Party:
		{
			char unit[256];
			sprintf( unit, "party%d", index );
			lua_pushstring(L, unit);
		}
		break;

	case eUnit_Raid:
		{
			char unit[256];
			sprintf( unit, "raid%d", index );
			lua_pushstring(L, unit);
		}
		break;

	case eUnit_Pet:
		lua_pushstring(L, "pet");
		break;

	default:
		return;
	}

	lua_setglobal(L, "arg1");
	g_pUnitFrame->SendWorldEvent( eventStr );
}

// ----------------------------------------------------------------------------
CUnitFrame::CUnitFrame(CInterface* object)
	: CInterfaceBase(object)
{
	g_pUnitFrame = this;
}

// ----------------------------------------------------------------------------
CUnitFrame::~CUnitFrame()
{
	g_pUnitFrame = NULL;
}

// ----------------------------------------------------------------------------
void CUnitFrame::RegisterFunc()
{
	lua_State* L = GetLuaStateWorld();
	if (L)
	{
		lua_register( L, "UnitName",				LuaFunc_UnitName );
		lua_register( L, "UnitHealth",				LuaFunc_UnitHealth );
		lua_register( L, "UnitMaxHealth",			LuaFunc_UnitMaxHealth );
		lua_register( L, "UnitMana",				LuaFunc_UnitMana );
		lua_register( L, "UnitMaxMana",				LuaFunc_UnitMaxMana );
		lua_register( L, "UnitSkill",				LuaFunc_UnitSkill );
		lua_register( L, "UnitMaxSkill",			LuaFunc_UnitMaxSkill );
		lua_register( L, "UnitIsUnit",				LuaFunc_UnitIsUnit );
		lua_register( L, "UnitIsPlayer",			LuaFunc_UnitIsPlayer );
		lua_register( L, "UnitInParty",				LuaFunc_UnitInParty );
		lua_register( L, "UnitIsDeadOrGhost",		LuaFunc_UnitIsDeadOrGhost );
	}
}

// ----------------------------------------------------------------------------
int LuaFunc_UnitName (lua_State* L)
{
	const char* name = NULL;

	if ( lua_isstring(L, 1) )
		name = CUnitFrame::GetUnitName( lua_tostring(L, 1) );
	
	lua_pushstring( L, name );
	return 1;
}

// ----------------------------------------------------------------------------
int LuaFunc_UnitHealth (lua_State* L)
{
	int health = 0;

	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 )
					health = player->GetHP();
			}
			break;

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

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

		case CUnitFrame::eUnit_Raid:
			break;

		case CUnitFrame::eUnit_Pet:
			break;
		}
	}

	lua_pushnumber( L, health );
	return 1;
}

// ----------------------------------------------------------------------------
int LuaFunc_UnitMaxHealth (lua_State* L)
{
	int healthMax = 0;
	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 )
					healthMax = player->GetMaxHP();
			}
			break;

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

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

		case CUnitFrame::eUnit_Raid:
			break;

		case CUnitFrame::eUnit_Pet:
			break;
		}
	}
	lua_pushnumber( L, healthMax );
	return 1;
}

// ----------------------------------------------------------------------------
int LuaFunc_UnitMana (lua_State* L)
{
	int mana = 0;
	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 )
					mana = player->GetMP();
			}
			break;

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

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

		case CUnitFrame::eUnit_Raid:
			break;

		case CUnitFrame::eUnit_Pet:
			break;
		}
	}
	lua_pushnumber( L, mana );
	return 1;
}

// ----------------------------------------------------------------------------
int LuaFunc_UnitMaxMana (lua_State* L)
{
	int manaMax = 0;
	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 )
					manaMax = player->GetMaxMP();
			}
			break;

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

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

		case CUnitFrame::eUnit_Raid:
			break;

		case CUnitFrame::eUnit_Pet:
			break;
		}
	}	
	lua_pushnumber( L, manaMax );
	return 1;
}

// ----------------------------------------------------------------------------
int LuaFunc_UnitSkill (lua_State* L)
{
	int skill = 0;
	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 )
					skill = player->GetSP();
			}
			break;

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

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

		case CUnitFrame::eUnit_Raid:
			break;

		case CUnitFrame::eUnit_Pet:
			break;
		}
	}
	lua_pushnumber( L, skill );
	return 1;
}

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

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

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

		case CUnitFrame::eUnit_Raid:
			break;

		case CUnitFrame::eUnit_Pet:
			break;
		}
	}
	lua_pushnumber( L, skillMax );
	return 1;
}

// ----------------------------------------------------------------------------
int LuaFunc_UnitIsUnit (lua_State* L)
{
	lua_pushboolean( L, CUnitFrame::UnitIsUnit(luaL_checkstring(L, 1), luaL_checkstring(L, 2)) );
	return 1;
}

// ----------------------------------------------------------------------------
int LuaFunc_UnitIsPlayer (lua_State* L)
{
	lua_pushboolean( L, CUnitFrame::UnitIsPlayer(luaL_checkstring(L, 1)) );
	return 1;
}

// ----------------------------------------------------------------------------
int LuaFunc_UnitInParty (lua_State* L)
{
	lua_pushboolean( L, CUnitFrame::UnitInParty(luaL_checkstring(L, 1)) );
	return 1;
}

// ----------------------------------------------------------------------------
int	LuaFunc_UnitIsDeadOrGhost(lua_State* L)
{
	lua_pushboolean( L, CUnitFrame::UnitIsDeadOrGhost(luaL_checkstring(L, 1)) );
	return 1;
}