#include "../../mainproc/GameMain.h"
#include "../../netwaker_member/NetWakerClientInc.h"
#include "ItemClipboard.h"
#include "StoreFrame.h"
#include "BankFrame.h"
#include "BagFrame.h"

CBagFrame*					g_pBagFrame		= NULL;

int	LuaFunc_GetBagItemInfo(lua_State* L);
int	LuaFunc_GetBagCount(lua_State* L);
int	LuaFunc_PickupBagItem(lua_State* L);
int	LuaFunc_UseBagItem(lua_State* L);

// ----------------------------------------------------------------------------
CBagFrame::CBagFrame(CInterface* object)
: CInterfaceBase(object)
{
	g_pBagFrame = this;

	m_time		= 0.0f;
}

// ----------------------------------------------------------------------------
CBagFrame::~CBagFrame()
{
	g_pBagFrame = NULL;
}

// ----------------------------------------------------------------------------
void CBagFrame::RegisterFunc()
{
	lua_State* L = GetLuaStateWorld();
	if (L)
	{
		lua_register(L, "GetBagItemInfo",		LuaFunc_GetBagItemInfo);
		lua_register(L, "GetBagCount",			LuaFunc_GetBagCount);
		lua_register(L, "PickupBagItem",		LuaFunc_PickupBagItem);
		lua_register(L, "UseBagItem",			LuaFunc_UseBagItem);
	}
}

// ----------------------------------------------------------------------------
void CBagFrame::Update(float elapsedTime)
{
	m_time += elapsedTime;

	if (m_time >= 0.2f)
	{
		m_time -= 0.2f;
		NetCli_ItemChild::_Update();
	}
}

// ----------------------------------------------------------------------------
void CBagFrame::UnlockedItem()
{
	RoleDataEx* role = CNetGlobal::RoleData();
	ClipboardBagItem bagItem = g_pItemClipboard->GetBagItem();

	ItemFieldStruct* item = role->GetBodyItem(bagItem.pos);
	if ( item && item->Pos == EM_ItemState_CliDisabled )
		item->Pos = EM_ItemState_NONE;
}	

// ----------------------------------------------------------------------------
bool CBagFrame::Use(int index)
{	
	ItemFieldStruct* item = CNetGlobal::RoleData()->GetBodyItem(index);
	if ( item && !item->IsEmpty() && item->Pos == EM_ItemState_NONE )
	{
		// 若商店開啟中
		if ( g_pStoreFrame->IsOpen() )
		{
			g_pStoreFrame->Sell(index);
		}
		// 若銀行開啟中
		else if ( g_pBankFrame->IsOpen() )
		{
			g_pBankFrame->Deposit( CNetGlobal::RoleData()->SearchBankEmptyPos(), CBankFrame::eBank_Type_Bag, index );				
		}
		else
		{
			GameObjDbStruct* itemDB = CNetGlobal::GetObj(item->OrgObjID);
			if ( itemDB && itemDB->Classification == EM_ObjectClass_Item )
			{
				switch ( itemDB->Item.Class )
				{
				case EM_Item_Normal:
					// 檢查物品是否有法術效果
					if ( itemDB->Item.IncMagic_Onuse != 0 )
					{
						CRoleSprite* targetPlayer = g_pGameMain->GetTargetPlayer();
						item->Pos = EM_USE_ITEM;
						if ( targetPlayer )
							NetCli_Item::_UseItem_Body(*item, index, targetPlayer->GetIdentify() );
						else
							NetCli_Item::_UseItem_Body(*item, index, -1);
					}
					break;

				case EM_Item_Weapon:
					// 武器裝主手
					NetCli_EQ::S_SetEQ_Body(*item, index, EM_EQWearPos_MainHand);
					break;

				case EM_Item_Armor:
					switch (itemDB->Item.ArmorPos)
					{
					case EM_ArmorPos_Head:
						NetCli_EQ::S_SetEQ_Body(*item, index, EM_EQWearPos_Head);
						break;
					case EM_ArmorPos_Gloves:
						NetCli_EQ::S_SetEQ_Body(*item, index, EM_EQWearPos_Gloves);
						break;
					case EM_ArmorPos_Shoes:
						NetCli_EQ::S_SetEQ_Body(*item, index, EM_EQWearPos_Shoes);
						break;
					case EM_ArmorPos_Clothes:
						NetCli_EQ::S_SetEQ_Body(*item, index, EM_EQWearPos_Clothes);
						break;
					case EM_ArmorPos_Pants:
						NetCli_EQ::S_SetEQ_Body(*item, index, EM_EQWearPos_Pants);
						break;
					case EM_ArmorPos_Back:
						NetCli_EQ::S_SetEQ_Body(*item, index, EM_EQWearPos_Back);
						break;
					case EM_ArmorPos_Belt:
						NetCli_EQ::S_SetEQ_Body(*item, index, EM_EQWearPos_Belt);
						break;
					case EM_ArmorPos_Shoulder:
						NetCli_EQ::S_SetEQ_Body(*item, index, EM_EQWearPos_Shoulder);
						break;
					case EM_ArmorPos_Necklace:
						NetCli_EQ::S_SetEQ_Body(*item, index, EM_EQWearPos_Necklace);
						break;
					case EM_ArmorPos_Ring:
						NetCli_EQ::S_SetEQ_Body(*item, index, EM_EQWearPos_Ring1);
						break;
					case EM_ArmorPos_Earring:
						NetCli_EQ::S_SetEQ_Body(*item, index, EM_EQWearPos_Earring1);
						break;
					case EM_ArmorPos_SecondHand:
						NetCli_EQ::S_SetEQ_Body(*item, index, EM_EQWearPos_SecondHand);
						break;
					}				
					break;

				case EM_Item_Rune:
					{
					}
					break;
				}
			}
		}
		return true;
	}
	return false;
}

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

// ----------------------------------------------------------------------------
void CBagFrame::Paste(int index)
{
	RoleDataEx* role = CNetGlobal::RoleData();
	ItemFieldStruct* destItem = role->GetBodyItem(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_Item::_ExchangeField_Body(*srcItem, bagItem.pos, *destItem , 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_Item::_ExchangeField_BankBody(*srcItem, bankItem.pos, *destItem, 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_Body(*destItem, index, equipmentItem.pos);				
				}
			}
			break;

		case CItemClipboard::eItem_Type_Shop:			// 商店 -> 背包
			{
				ClipboardShopItem shopItem = g_pItemClipboard->GetShopItem();
				g_pStoreFrame->Buy( shopItem.type, shopItem.pos );
			}
			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();
}

// ----------------------------------------------------------------------------
int	CBagFrame::GetIndex(int objectID)
{
	RoleDataEx* role = CNetGlobal::RoleData();
	for ( int i = 0; i < role->BaseData.Body.Count; i++ )
	{
		ItemFieldStruct* item = role->GetBodyItem(i);
		if ( item && item->OrgObjID == objectID )
			return i;
	}
	return -1;
}

// ----------------------------------------------------------------------------
int	CBagFrame::GetIndex(const char* name)
{
	return -1;
}

// ----------------------------------------------------------------------------
void CBagFrame::Locked(int index)
{
	ItemFieldStruct* item = CNetGlobal::RoleData()->GetBodyItem(index);
	if ( item )
		item->Pos = EM_ItemState_CliDisabled;
}

// ----------------------------------------------------------------------------
void CBagFrame::Unlocked(int index)
{
	ItemFieldStruct* item = CNetGlobal::RoleData()->GetBodyItem(index);
	if ( item )
	{
		switch( item->Pos )
		{
		case EM_ItemState_CliDisabled:
			item->Pos = EM_ItemState_NONE;
			break;
		}
	}
}

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

	if (role)
	{
		ItemFieldStruct* item = role->GetBodyItem(index);
		if (item)
		{
			GameObjDbStruct* itemDB = CNetGlobal::GetObj(item->OrgObjID);
			if (itemDB)
			{
				lua_pushstring(L, itemDB->ImageObj->ACTField);			// push item texture file
				lua_pushstring(L, itemDB->Name);						// push Item name
				lua_pushnumber(L, item->Count);							// push item count
				lua_pushboolean(L, item->Pos != EM_ItemState_NONE);		// push item is locked
				return 4;
			}
		}
		lua_pushstring(L, "Interface\\Buttons\\emptySlot");				// push item texture file
		return 1;
	}

	return 0;
}

// ----------------------------------------------------------------------------
int	LuaFunc_GetBagCount(lua_State* L)
{
	lua_pushnumber(L, 30);
	return 1;
}

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

// ----------------------------------------------------------------------------
int	LuaFunc_UseBagItem(lua_State* L)
{	
	g_pBagFrame->Use( luaL_checkint(L, 1) - 1 );
	return 0;
}