#include "../Interface/WorldFrames/ChatFrame.h"
#include "../Interface/WorldFrames/BankFrame.h"
#include "NetCli_ItemChild.h"

bool NetCli_ItemChild::m_isLockQueue = false;

//-----------------------------------------------------------------------------
bool NetCli_ItemChild::_Init()
{
	if (This)
		return false;

	This = new NetCli_ItemChild;
	return NetCli_Item::_Init();
}

//-----------------------------------------------------------------------------
bool NetCli_ItemChild::_Release()
{
	NetCli_Item::_Release();
	if (This)
	{
		delete This;
		This = NULL;
	}
	return true;
}

//-----------------------------------------------------------------------------
void NetCli_ItemChild::_Update()
{
	if ( _RoleData.TempData.ItemTemp.IsEmpty() || m_isLockQueue )
		return;

	ItemFieldStruct* item = _RoleData.TempData.ItemTemp.Begin();
	GameObjDbStruct* itemDB = GetObj( item->OrgObjID );
	if ( itemDB == NULL )
		return;

	int pos = _RoleData.SearchBodyFirstFit( item );
	if ( pos != - 1 )
	{
		ItemFieldStruct* bagItem = _RoleData.GetBodyItem(pos);
		if ( bagItem == NULL || bagItem->Pos != EM_ItemState_NONE )
			return;

		m_isLockQueue = true;
		bagItem->Pos = EM_ItemState_CliDisabled;
		//NetCli_Item::_GetBufferItem( *item, pos, item->Count );
		int Count = __min( item->Count , itemDB->MaxHeap - bagItem->Count );
		NetCli_Item::_GetBufferItem( *item, pos, Count );
	}
	else
	{
		g_pChatFrame->AddMessage(9, "警告! 背包物品已滿");
		if ( _RoleData.TempData.ItemTemp.Size() >= 7 )
		{
			char tmp[MAX_PATH];
			sprintf( tmp, "丟棄暫存物品 %s(%d)", itemDB->Name, item->OrgObjID );
			g_pChatFrame->AddMessage(9, tmp);
			NetCli_Item::_DelBufferItem( *item );
			_RoleData.TempData.ItemTemp.Pop_Front();
		}
	}
}

//-----------------------------------------------------------------------------
void NetCli_ItemChild::R_GetItemOK()
{
	int a = 0;
}

//-----------------------------------------------------------------------------
void NetCli_ItemChild::R_GetGroundItemFailed()
{
	int a = 0;
	//_RequestBufferInfo();
}

//-----------------------------------------------------------------------------
void NetCli_ItemChild::R_GetBufferItemFailed()
{
	int a = 0;
}

//-----------------------------------------------------------------------------
void NetCli_ItemChild::R_ClsItemInBuffer()
{
	_RoleData.TempData.ItemTemp.Clear();
}

//-----------------------------------------------------------------------------
void NetCli_ItemChild::R_NewItemInBuffer(ItemFieldStruct& Item)
{	
	_RoleData.AddItemtoBuf( Item );

	char itemName[MAX_PATH];
	if ( RoleDataEx::GetItemFieldName(&Item, itemName, MAX_PATH) )
	{
		char tmp[MAX_PATH];
		sprintf( tmp, "你獲得[%s]", itemName );
		g_pChatFrame->AddMessage( 0, tmp );
	}
}

//-----------------------------------------------------------------------------
void NetCli_ItemChild::R_DeleteItemOK_Body()
{
	int a = 0;
}

//-----------------------------------------------------------------------------
void NetCli_ItemChild::R_DeleteItemOK_Bank()
{
	int a = 0;
}

//-----------------------------------------------------------------------------
void NetCli_ItemChild::R_DeleteItemOK_EQ()
{
	int a = 0;
}

//-----------------------------------------------------------------------------
void NetCli_ItemChild::R_DeleteItemFailed()
{
	int a = 0;
}

//-----------------------------------------------------------------------------
void NetCli_ItemChild::R_ExchangeFieldOK_Body()
{
	int a = 0;
}

//-----------------------------------------------------------------------------
void NetCli_ItemChild::R_ExchangeFieldOK_Bank()
{
	int a = 0;
}

//-----------------------------------------------------------------------------
void NetCli_ItemChild::R_ExchangeFieldOK_BodyBank()
{
	int a = 0;
}

//-----------------------------------------------------------------------------
void NetCli_ItemChild::R_ExchangeFieldFailed_Body()
{
	int a = 0;
}

//-----------------------------------------------------------------------------
void NetCli_ItemChild::R_ExchangeFieldFailed_Bank()
{
	int a = 0;
}

//-----------------------------------------------------------------------------
void NetCli_ItemChild::R_ExchangeFieldFailed_BodyBank()
{
	int a = 0;
}

//-----------------------------------------------------------------------------
void NetCli_ItemChild::R_BodyBankMoneyExchangeOK()
{
	int a = 0;
}

//-----------------------------------------------------------------------------
void NetCli_ItemChild::R_BodyBankMoneyExchangeFalse()
{
	int a = 0;
}

//-----------------------------------------------------------------------------
void NetCli_ItemChild::R_FixItemInfo_Body(ItemFieldStruct& Item , int Pos)
{
	_RoleData.SetItemInfo_Body( Item , Pos );
}

//-----------------------------------------------------------------------------
void NetCli_ItemChild::R_FixItemInfo_Bank(ItemFieldStruct& Item , int Pos)
{
	_RoleData.SetItemInfo_Bank( Item , Pos );
}

//-----------------------------------------------------------------------------
void NetCli_ItemChild::R_FixItemInfo_EQ(ItemFieldStruct& Item , int Pos)
{
	_RoleData.SetItemInfo_EQ( Item , Pos );
}

//-----------------------------------------------------------------------------
void NetCli_ItemChild::R_FixAllItemInfo(ItemStruct& Body , ItemStruct& Bank , EQStruct& EQ)
{
	_RoleData.BaseData.Body = Body;
	_RoleData.BaseData.Bank = Bank;
	_RoleData.BaseData.EQ   = EQ;
	_RoleData.TempData.UpdateInfo.Bank = true;
	_RoleData.TempData.UpdateInfo.Body = true;
	_RoleData.TempData.UpdateInfo.Eq  = true;
}

//-----------------------------------------------------------------------------
void NetCli_ItemChild::R_FixMoney(int BodyMoney , int BankMoney)
{
	char Buf[256];
	int D_BodyMoney = BodyMoney - _RoleData.BaseData.Body.Money;
	_RoleData.BaseData.Body.Money = BodyMoney;
	_RoleData.BaseData.Bank.Money = BankMoney;
	_RoleData.TempData.UpdateInfo.Money = true;

	sprintf( Buf , "獲得金錢 (%d)" , D_BodyMoney );
	g_pChatFrame->AddMessage( 5 , Buf );
}

//-----------------------------------------------------------------------------
void NetCli_ItemChild::R_MaxItemCount(int MaxBodyItem , int MaxBankItem)
{
	_RoleData.BaseData.Body.Count = MaxBodyItem;
	_RoleData.BaseData.Bank.Count = MaxBankItem;
}

//-----------------------------------------------------------------------------
void NetCli_ItemChild::R_OpenBank( int GItemID )
{
	g_pBankFrame->Open( GItemID );
}
//-----------------------------------------------------------------------------
void NetCli_ItemChild::R_CloseBank( )
{
	g_pBankFrame->Close();
}
//-----------------------------------------------------------------------------
void NetCli_ItemChild::R_UseItemOK(int Pos)
{
	int a = 0;
}

//-----------------------------------------------------------------------------
void NetCli_ItemChild::R_UseItemFailed(int Pos)
{
	ItemFieldStruct* Item = _RoleData.GetBodyItem( Pos );
	if( Item == NULL )
		return;

	if(     Item->Pos == EM_USE_ITEM 
		||  Item->Pos == EM_ItemState_CliDisabled    )
	{
		Item->Pos = EM_ItemState_NONE;
		_RoleData.TempData.UpdateInfo.Body = true;
	}
	//g_pError->AddMessage( 0, 0, "物品使用失敗");
}

//-----------------------------------------------------------------------------
void NetCli_ItemChild::R_GetBufferItemOK( int Count )
{
	ItemFieldStruct* item = _RoleData.TempData.ItemTemp.Begin();
	item->Count -= Count;
	m_isLockQueue = false;
	if ( item->Count <= 0 )
		_RoleData.TempData.ItemTemp.Pop_Front();
}
void NetCli_ItemChild::R_SetKeyItemFlag	( int KeyItemID , bool Value )
{
	int ID = KeyItemID - Def_ObjectClass_KeyItem;

	if( Value == true )
		_RoleData.BaseData.KeyItem.SetFlagOn( ID );
	else
		_RoleData.BaseData.KeyItem.SetFlagOff( ID );
}