#include "../../mainproc/GameMain.h"
#include "../../netwaker_member/NetWakerClientInc.h"
#include "../Debug/DebugMessage.h"
#include "ItemClipboard.h"
#include "Equipment.h"

CEquipment*			g_pEquipment		= NULL;

int	LuaFunc_GetEquipmentItemInfo(lua_State* L);
int	LuaFunc_PickupEquipmentItem(lua_State* L);
int	LuaFunc_UseEquipmentItem(lua_State* L);
int	LuaFunc_GetRoleAttribute(lua_State* L);
int	LuaFunc_GetRoleClass(lua_State* L);
int	LuaFunc_GetRoleLevel(lua_State* L);
int	LuaFunc_GetRoleMoney(lua_State* L);
int	LuaFunc_GetRoleExp(lua_State* L);
int	LuaFunc_GetRoleLVExp(lua_State* L);
int	LuaFunc_AddRoleAttribute(lua_State* L);

int LuaFunc_GetRoleTitle(lua_State* L);
int LuaFunc_GetRoleGuild(lua_State* L);
int LuaFunc_GetRoleIntroduce(lua_State* L);

int LuaFunc_SetRoleIntroduce(lua_State* L);


// ----------------------------------------------------------------------------
CEquipment::CEquipment(CInterface* object)
	: CInterfaceBase(object)
{
	g_pEquipment = this;
}

// ----------------------------------------------------------------------------
CEquipment::~CEquipment()
{
	g_pEquipment = NULL;
}

// ----------------------------------------------------------------------------
void CEquipment::RegisterFunc()
{
	lua_State* L = GetLuaStateWorld();
	if (L)
	{
		lua_register(L, "GetEquipmentItemInfo",		LuaFunc_GetEquipmentItemInfo	);
		lua_register(L, "PickupEquipmentItem",		LuaFunc_PickupEquipmentItem		);
		lua_register(L, "UseEquipmentItem",			LuaFunc_UseEquipmentItem		);
		lua_register(L, "GetRoleAttribute",			LuaFunc_GetRoleAttribute		);
		lua_register(L, "GetRoleClass",				LuaFunc_GetRoleClass			);
		lua_register(L, "GetRoleLevel",				LuaFunc_GetRoleLevel			);
		lua_register(L, "GetRoleMoney",				LuaFunc_GetRoleMoney			);
		lua_register(L, "GetRoleExp",				LuaFunc_GetRoleExp				);
		lua_register(L, "GetRoleLVExp",				LuaFunc_GetRoleLVExp			);
		lua_register(L, "AddRoleAttribute",			LuaFunc_AddRoleAttribute		);

		lua_register(L, "GetRoleTitle",				LuaFunc_GetRoleTitle			);
		lua_register(L, "GetRoleGuild",				LuaFunc_GetRoleGuild			);
		lua_register(L, "GetRoleIntroduce",			LuaFunc_GetRoleIntroduce		);
		lua_register(L, "SetRoleIntroduce",			LuaFunc_SetRoleIntroduce		);
	}
}

// ----------------------------------------------------------------------------
void CEquipment::UnlockedItem()
{	
	ClipboardEquipmentItem equipmenyItem = g_pItemClipboard->GetEquipmentItem();
	ItemFieldStruct* item = CNetGlobal::RoleData()->GetEqItem(equipmenyItem.pos);
	if ( item && item->Pos == EM_ItemState_CliDisabled )
		item->Pos = EM_ItemState_NONE;
}	

// ----------------------------------------------------------------------------
bool CEquipment::Cut(int index)
{
	ItemFieldStruct* item = CNetGlobal::RoleData()->GetEqItem( index );
	if ( item )
	{
		GameObjDbStruct* itemDB = CNetGlobal::GetObj( item->OrgObjID );
		if ( itemDB )
		{
			item->Pos = EM_ItemState_CliDisabled;
			g_pItemClipboard->SetFile( itemDB->ImageObj->ACTField );
			g_pItemClipboard->PickupEquipment(this, item->OrgObjID, index);
			return true;
		}
	}
	return false;
}

// ----------------------------------------------------------------------------
void CEquipment::Paste(int index)
{
	RoleDataEx* role = CNetGlobal::RoleData();
	ItemFieldStruct* destItem = role->GetEqItem( index );

	if ( destItem && destItem->Pos == EM_ItemState_NONE )
	{
		switch(g_pItemClipboard->GetType())
		{
		case CItemClipboard::eItem_Type_None:			// 拿取物品
			Cut(index);
			return;

		case CItemClipboard::eItem_Type_Bag:			// 裝備 -> 背包
			{
				ClipboardBagItem bagItem = g_pItemClipboard->GetBagItem();
				ItemFieldStruct* srcItem = role->GetBodyItem(bagItem.pos);
				if ( destItem && srcItem )
				{
					destItem->Pos = srcItem->Pos = EM_ItemState_CliDisabled;
					NetCli_EQ::S_SetEQ_Body(*srcItem, bagItem.pos, index);
				}
			}
			break;

		case CItemClipboard::eItem_Type_Bank:			// 銀行 -> 背包
			{
				ClipboardBankItem bankItem = g_pItemClipboard->GetBankItem();
				ItemFieldStruct* srcItem = role->GetBankItem(bankItem.pos);
				if ( destItem && srcItem )
				{
					destItem->Pos = srcItem->Pos = EM_ItemState_CliDisabled;
					NetCli_EQ::S_SetEQ_Bank(*srcItem, bankItem.pos, index);
				}
			}
			break;

		case CItemClipboard::eItem_Type_Equipment:		// 裝備 -> 背包
			{
				ClipboardEquipmentItem equipmentItem = g_pItemClipboard->GetEquipmentItem();
				ItemFieldStruct* srcItem = role->GetEqItem(equipmentItem.pos);
				if ( destItem && srcItem )
				{
					destItem->Pos = srcItem->Pos = EM_ItemState_CliDisabled;
					NetCli_EQ::S_SetEQ_EQ(*srcItem, equipmentItem.pos, index);
				}
			}
			break;

		case CItemClipboard::eItem_Type_Shop:			// 商店 -> 背包
			break;

		case CItemClipboard::eItem_Type_Trade:			// 交易 -> 背包
			break;

		case CItemClipboard::eItem_Type_Skill:			// 技能 -> 背包
			break;

		case CItemClipboard::eItem_Type_Action:			// 快捷 -> 背包
			break;
		}

		// clear item clipboard data
		g_pItemClipboard->Clear();
	}
}
//----------------------------------------------------------------------------
bool CEquipment::Use(int index)
{
	RoleDataEx* role = CNetGlobal::RoleData();
	ItemFieldStruct* item = role->GetEqItem(index);

	if ( item && !item->IsEmpty() && item->Pos == EM_ItemState_NONE )
	{
		GameObjDbStruct* itemDB = CNetGlobal::GetObj(item->OrgObjID);
		if ( itemDB && itemDB->Item.IncMagic_Onuse != 0 )
		{
			CRoleSprite* targetPlayer = g_pGameMain->GetTargetPlayer();
			item->Pos = EM_USE_ITEM;
			if ( targetPlayer )
				NetCli_Item::_UseItem_EQ(*item, index, targetPlayer->GetIdentify());
			else
				NetCli_Item::_UseItem_EQ(*item, index, -1);

			return true;
		}
	}
	return false;
}

// ----------------------------------------------------------------------------
int	LuaFunc_GetEquipmentItemInfo(lua_State* L)
{
	RoleDataEx* role = CNetGlobal::RoleData();
	int index = luaL_checkint(L, 1) - 1;	

	ItemFieldStruct* item = role->GetEqItem(index);
	if ( item )
	{
		if ( item->IsEmpty() )
		{
			switch (index)
			{
			case EM_EQWearPos_Head:			// 0
				lua_pushstring(L, "interface\\EquipmentFrame\\Equipment-Slot-Helmet");
				break;

			case EM_EQWearPos_Gloves:		// 1
				lua_pushstring(L, "interface\\EquipmentFrame\\Equipment-Slot-Hands");
				break;

			case EM_EQWearPos_Shoes:		// 2
				lua_pushstring(L, "interface\\EquipmentFrame\\Equipment-Slot-Feet");
				break;

			case EM_EQWearPos_Clothes:		// 3
				lua_pushstring(L, "interface\\EquipmentFrame\\Equipment-Slot-Chest");
				break;

			case EM_EQWearPos_Pants:		// 4
				lua_pushstring(L, "interface\\EquipmentFrame\\Equipment-Slot-Legs");
				break;

			case EM_EQWearPos_Back:			// 5
				lua_pushstring(L, "interface\\EquipmentFrame\\Equipment-Slot-Back");
				break;

			case EM_EQWearPos_Belt:			// 6
				lua_pushstring(L, "interface\\EquipmentFrame\\Equipment-Slot-Waist");
				break;

			case EM_EQWearPos_Shoulder:		// 7
				lua_pushstring(L, "interface\\EquipmentFrame\\Equipment-Slot-Shoulder");
				break;

			case EM_EQWearPos_Necklace:		// 8
				lua_pushstring(L, "interface\\EquipmentFrame\\Equipment-Slot-Necklace");
				break;

			case EM_EQWearPos_Ammo:			// 9
				lua_pushstring(L, "interface\\EquipmentFrame\\Equipment-Slot-Ranged");
				break;

			case EM_EQWearPos_Ring1:		// 10
			case EM_EQWearPos_Ring2:		// 11
				lua_pushstring(L, "interface\\EquipmentFrame\\Equipment-Slot-Rings");
				break;
			
			case EM_EQWearPos_Earring1:		// 12
			case EM_EQWearPos_Earring2:		// 13
				lua_pushstring(L, "interface\\EquipmentFrame\\Equipment-Slot-Earrings");
				break;

			case EM_EQWearPos_MainHand:		// 14
				lua_pushstring(L, "interface\\EquipmentFrame\\Equipment-Slot-MainHand");
				break;

			case EM_EQWearPos_SecondHand:	// 15
				lua_pushstring(L, "interface\\EquipmentFrame\\Equipment-Slot-Secondary");
				break;

			case EM_EQWearPos_Manufactory:	// 16
				lua_pushstring(L, "interface\\EquipmentFrame\\Equipment-Slot-Pick");
				break;
			}
			lua_pushnumber(L, 0);
			lua_pushboolean(L, false);			
		}
		else
		{
			GameObjDbStruct* itemDB = CNetGlobal::GetObj(item->OrgObjID);
			if (itemDB)
			{
				lua_pushstring(L, itemDB->ImageObj->ACTField);			// push item texture file
				lua_pushnumber(L, item->Count);							// push item count
				lua_pushboolean(L, item->Pos != EM_ItemState_NONE);		// push item is locked
			}
		}
		return 3;
	}
	return 0;
}

// ----------------------------------------------------------------------------
int	LuaFunc_PickupEquipmentItem(lua_State* L)
{	
	g_pEquipment->Paste(luaL_checkint(L, 1) - 1);
	return 0;
}

// ----------------------------------------------------------------------------
int	LuaFunc_UseEquipmentItem(lua_State* L)
{	
	g_pEquipment->Use(luaL_checkint(L, 1) - 1);
	return 0;
}
// ----------------------------------------------------------------------------
int	LuaFunc_GetRoleLevel(lua_State* L)
{
	if( g_pEquipment )
	{
		int			iType	= luaL_checkint( L, 1 );
		RoleDataEx* pRole	= CNetGlobal::RoleData();
		int			iVal	= 0;

		if( iType == 1 )
			iVal	= pRole->TempData.Attr.Level;		// 主職
		else
			iVal	= pRole->TempData.Attr.Level_Sub;	// 副職

		lua_pushnumber( L, iVal );
		return 1;
	}
	return 0;
}
// ----------------------------------------------------------------------------
int	LuaFunc_GetRoleClass(lua_State* L)
{
	if( g_pEquipment )
	{
		int			iType	= luaL_checkint( L, 1 );
		RoleDataEx* pRole	= CNetGlobal::RoleData();
		int			iVal	= 0;

		if( iType == 1 )
			iVal	= pRole->TempData.Attr.Voc;		// 主職
		else
			iVal	= pRole->TempData.Attr.Voc_Sub;	// 副職

		lua_pushnumber( L, iVal );
		return 1;
	}
	return 0;
}
// ----------------------------------------------------------------------------
int	LuaFunc_GetRoleMoney(lua_State* L)
{
	int			iType	= luaL_checkint( L, 1 );
	RoleDataEx* pRole	= CNetGlobal::RoleData();

	int			iMoney	= pRole->BaseData.Body.Money;
	
	lua_pushnumber( L, iMoney );

	return 1;
}
// ----------------------------------------------------------------------------
int	LuaFunc_GetRoleExp(lua_State* L)
{
	RoleDataEx* pRole	= CNetGlobal::RoleData();

	int			iExp	= pRole->GetExp();

	lua_pushnumber( L, iExp );

	return 1;
}
// ----------------------------------------------------------------------------
int	LuaFunc_GetRoleLVExp(lua_State* L)
{
	RoleDataEx* pRole	= CNetGlobal::RoleData();

	int			iLVExp	= pRole->GetLVExp();

	lua_pushnumber( L, iLVExp );

	return 1;
}
// ----------------------------------------------------------------------------
int	LuaFunc_GetRoleAttribute(lua_State* L)
{
	if( g_pEquipment )
	{
		int			iType	= luaL_checkint( L, 1 );
		RoleDataEx* pRole	= CNetGlobal::RoleData();
		float		iVal	= 0.0f;

		switch( iType )
		{
		case 1:	// 力量
			iVal	= pRole->TempData.Attr.Fin.STR; break;

		case 2:	// 耐力
			iVal	= pRole->TempData.Attr.Fin.STA; break;

		case 3: // AGI 敏捷
			iVal	= pRole->TempData.Attr.Fin.AGI; break;

		case 4: // Int 精神
			iVal	= pRole->TempData.Attr.Fin.MND; break;			

		case 5: // Mnd 智慧
			iVal	= pRole->TempData.Attr.Fin.INT; break;			

		case 6:	// Earth resist
			iVal	= pRole->TempData.Attr.Fin.Resist.Earth; break;

		case 7:	// Water resist
			iVal	= pRole->TempData.Attr.Fin.Resist.Water; break;

		case 8:	// Fire resist
			iVal	= pRole->TempData.Attr.Fin.Resist.Fire; break;

		case 9:	 // Wind resist
			iVal	= pRole->TempData.Attr.Fin.Resist.Wind; break;

		case 10: // Light resist
			iVal	= pRole->TempData.Attr.Fin.Resist.Light; break;

		case 11: // Dark resist
			iVal	= pRole->TempData.Attr.Fin.Resist.Darkness; break;

		case 12: // 物理攻擊
			iVal	= pRole->TempData.Attr.Fin.ATK; break;

		case 13: // 物理防禦
			iVal	= pRole->TempData.Attr.Fin.DEF; break;

		case 14: // 魔法攻擊
			iVal	= pRole->TempData.Attr.Fin.MATK; break;

		case 15: // 魔法防禦
			iVal	= pRole->TempData.Attr.Fin.MDEF; break;
		case 16: // 剩餘升級點數
			iVal	= pRole->TempData.Attr.Ability->Point; break;
		}
	
		lua_pushnumber( L, (int)iVal );
		return 1;
	}
	return 0;
}
// ----------------------------------------------------------------------------
int	LuaFunc_AddRoleAttribute(lua_State* L)
{
	if( g_pEquipment )
	{
		int			iType	= luaL_checkint( L, 1 );
		RoleDataEx* pRole	= CNetGlobal::RoleData();

		if( pRole->TempData.Attr.Ability->Point == 0 )
			return 0;

		switch( iType )
		{
		case 1:	// 力量
			NetCli_RoleValue::S_RequestSetPoint( EM_RoleValue_STR ); break;

		case 2:	// 耐力
			NetCli_RoleValue::S_RequestSetPoint( EM_RoleValue_STA ); break;

		case 3: // AGI 敏捷
			NetCli_RoleValue::S_RequestSetPoint( EM_RoleValue_AGI ); break;

		case 4: // Int 精神
			NetCli_RoleValue::S_RequestSetPoint( EM_RoleValue_INT ); break;

		case 5: // Mnd 智慧
			NetCli_RoleValue::S_RequestSetPoint( EM_RoleValue_MND ); break;
		}
	}

	return 0;
}
// ----------------------------------------------------------------------------
int LuaFunc_GetRoleTitle(lua_State* L)
{
	RoleDataEx* pRole	= CNetGlobal::RoleData();
	return 0;
}
// ----------------------------------------------------------------------------
int LuaFunc_GetRoleGuild(lua_State* L)
{

	return 0;
}
// ----------------------------------------------------------------------------
int LuaFunc_GetRoleIntroduce(lua_State* L)
{
	RoleDataEx* pRole	= CNetGlobal::RoleData();

	if( pRole )
	{		
		lua_pushstring( L, pRole->SelfData.FriendList.Note.Begin() );
	}

	return 1;
}
// ----------------------------------------------------------------------------
int LuaFunc_SetRoleIntroduce(lua_State* L)
{
	const char* cszString  = luaL_checkstring( L, 1 );
	
	char	szBuff[ 1024 ];
	strcpy( szBuff, cszString );

	NetCli_FriendList::SL_ModifyMyInfo( szBuff );


	return 1;
}