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

CBankFrame*			g_pBankFrame = NULL;

int	LuaFunc_GetBankItemInfo			(lua_State* L);
int	LuaFunc_GetBankCount			(lua_State* L);
int	LuaFunc_PickupBankItem			(lua_State* L);
int LuaFunc_DrawBankItem			(lua_State* L);
int	LuaFunc_CloseBank				(lua_State* L);

// ----------------------------------------------------------------------------
CBankFrame::CBankFrame(CInterface* object)
	: CInterfaceBase(object)
{
	m_bankManage	= 0;

	g_pBankFrame	= this;
}

// ----------------------------------------------------------------------------
CBankFrame::~CBankFrame()
{
	g_pBankFrame	= NULL;
}

// ----------------------------------------------------------------------------
void CBankFrame::RegisterFunc()
{
	lua_State* L = GetLuaStateWorld();
	if (L)
	{
		lua_register(L, "GetBankItemInfo",		LuaFunc_GetBankItemInfo);
		lua_register(L, "GetBankCount",			LuaFunc_GetBankCount);
		lua_register(L, "PickupBankItem",		LuaFunc_PickupBankItem);
		lua_register(L, "DrawBankItem",			LuaFunc_DrawBankItem);
		lua_register(L, "CloseBank",			LuaFunc_CloseBank);
	}
}

// ----------------------------------------------------------------------------
void CBankFrame::Update(float elapsedTime)
{
	if ( m_bankManage != 0 )
	{		
		CRoleSprite* sprite = g_pGameMain->FindRoleSprite(m_bankManage);
		if ( sprite == NULL || g_pGameMain->GetDistanceWithPlayer(sprite) > _MAX_ROLE_TOUCH_MAXRANGE_ )		
			Close();
	}
}

// ----------------------------------------------------------------------------
void CBankFrame::UnlockedItem()
{
	ClipboardBankItem bankItem = g_pItemClipboard->GetBankItem();
	ItemFieldStruct* item = CNetGlobal::RoleData()->GetBankItem(bankItem.pos);
	if ( item && item->Pos == EM_ItemState_CliDisabled )
		item->Pos = EM_ItemState_NONE;
}

// ----------------------------------------------------------------------------
void CBankFrame::Open(int bankManage)
{
	Close();	

	m_bankManage = bankManage;

	if ( m_bankManage != 0 )
	{
		// 開啟界面
		CUiFrame* frame = (CUiFrame*)m_interface->GetUiWorld()->FindObject("BankFrame");
		if ( frame )
		{
			frame->SetVisible( true );
		}
	}
}

// ----------------------------------------------------------------------------
void CBankFrame::Close()
{
	if ( m_bankManage != 0 )
	{
		m_bankManage = 0;

		// 關閉界面
		CUiFrame* frame = (CUiFrame*)m_interface->GetUiWorld()->FindObject("BankFrame");
		if ( frame )
		{
			frame->SetVisible( false );
		}

		// 通知銀行關閉
		NetCli_Item::_CloseBank();
	}
}

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

// ----------------------------------------------------------------------------
void CBankFrame::Paste(int index)
{
	switch(g_pItemClipboard->GetType())
	{	
	case CItemClipboard::eItem_Type_None:			// 拿取物品
		Cut(index);
		return;

	case CItemClipboard::eItem_Type_Bag:			// 背包 -> 銀行
		{
			ClipboardBagItem bagItem = g_pItemClipboard->GetBagItem();
			Deposit( index, eBank_Type_Bag, bagItem.pos );
		}
		break;

	case CItemClipboard::eItem_Type_Bank:			// 銀行 -> 銀行
		{
			ClipboardBankItem bankItem = g_pItemClipboard->GetBankItem();
			Deposit( index, eBank_Type_Bank, bankItem.pos );
		}
		break;

	case CItemClipboard::eItem_Type_Equipment:		// 裝備 -> 銀行
		{
			ClipboardEquipmentItem equipmentItem = g_pItemClipboard->GetEquipmentItem();
			Deposit( index, eBank_Type_Equipment, equipmentItem.pos );
		}
		break;

	case CItemClipboard::eItem_Type_Shop:			// 商店 -> 銀行
	case CItemClipboard::eItem_Type_Trade:			// 交易 -> 銀行
	case CItemClipboard::eItem_Type_Skill:			// 技能 -> 銀行
	case CItemClipboard::eItem_Type_Action:			// 快捷 -> 銀行
		break;
	}

	// clear item clipboard data
	g_pItemClipboard->Clear();
}

// ----------------------------------------------------------------------------
// 存入物品
// index 銀行索引值
// srcType 來源物品類型
// pos 索引值
void CBankFrame::Deposit(int index, int srcType, int pos)
{
	ItemFieldStruct* bankItem = CNetGlobal::RoleData()->GetBankItem(index);
	if ( bankItem && bankItem->Pos == EM_ItemState_NONE )
	{
		ItemFieldStruct* item = NULL;

		switch ( srcType )
		{
		case eBank_Type_Bag:
			item = CNetGlobal::RoleData()->GetBodyItem(pos);
			if ( item )
			{
				bankItem->Pos = item->Pos = EM_ItemState_CliDisabled;
				NetCli_Item::_ExchangeField_BodyBank(*item, pos, *bankItem , index);
			}
			break;

		case eBank_Type_Bank:
			item = CNetGlobal::RoleData()->GetBankItem(pos);
			if ( item )
			{
				bankItem->Pos = item->Pos = EM_ItemState_CliDisabled;
				NetCli_Item::_ExchangeField_Bank(*item, pos, *bankItem, index);
			}
			break;

		case eBank_Type_Equipment:
			item = CNetGlobal::RoleData()->GetEqItem(pos);
			if ( item )
			{
				bankItem->Pos = item->Pos = EM_ItemState_CliDisabled;
				NetCli_EQ::S_SetEQ_Bank(*bankItem, index, pos);
			}
			break;
		}
	}
}

// ----------------------------------------------------------------------------
// 取出物品
// 銀行索引值
void CBankFrame::Draw(int index)
{
	int pos = CNetGlobal::RoleData()->SearchBodyEmptyPos();
	if ( pos != -1 )
		Deposit(index, eBank_Type_Bag, pos);
}

// ----------------------------------------------------------------------------
int	LuaFunc_GetBankItemInfo(lua_State* L)
{
	ItemFieldStruct* item = CNetGlobal::RoleData()->GetBankItem(luaL_checkint(L, 1) - 1);
	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_GetBankCount(lua_State* L)
{
	lua_pushnumber(L, CNetGlobal::RoleData()->BaseData.Bank.Count);
	return 1;
}

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

// ----------------------------------------------------------------------------
int LuaFunc_DrawBankItem(lua_State* L)
{
	g_pBankFrame->Draw( luaL_checkint(L, 1) - 1 );
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_CloseBank(lua_State* L)
{
	g_pBankFrame->Close();
	return 0;
}