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

CStoreFrame*				g_pStoreFrame = NULL;

int	LuaFunc_GetStoreItemInfo			(lua_State* L);
int	LuaFunc_GetStoreItemCount			(lua_State* L);
int LuaFunc_PickupStoreItem				(lua_State* L);
int LuaFunc_BuyStoreItem				(lua_State* L);
int	LuaFunc_GetPawnshopItemInfo			(lua_State* L);
int	LuaFunc_GetPawnshopItemCount		(lua_State* L);
int	LuaFunc_PickupPawnshopItem			(lua_State* L);
int	LuaFunc_RetrievePawnshopItem		(lua_State* L);
int	LuaFunc_CloseStore					(lua_State* L);

// ----------------------------------------------------------------------------
CStoreFrame::CStoreFrame(CInterface* object)
	: CInterfaceBase(object)
{
	Clear();

	g_pStoreFrame = this;
}

// ----------------------------------------------------------------------------
CStoreFrame::~CStoreFrame()
{
	g_pStoreFrame = NULL;
}

// ----------------------------------------------------------------------------
void CStoreFrame::RegisterFunc()
{
	lua_State* L = GetLuaStateWorld();
	if (L)
	{
		lua_register( L, "GetStoreItemInfo",		LuaFunc_GetStoreItemInfo );
		lua_register( L, "GetStoreItemCount",		LuaFunc_GetStoreItemCount );
		lua_register( L, "PickupStoreItem",			LuaFunc_PickupStoreItem );
		lua_register( L, "BuyStoreItem",			LuaFunc_BuyStoreItem );
		lua_register( L, "GetPawnshopItemInfo",		LuaFunc_GetPawnshopItemInfo );
		lua_register( L, "GetPawnshopItemCount",	LuaFunc_GetPawnshopItemCount );
		lua_register( L, "PickupPawnshopItem",		LuaFunc_PickupPawnshopItem );
		lua_register( L, "RetrievePawnshopItem",	LuaFunc_RetrievePawnshopItem );
		lua_register( L, "CloseStore",				LuaFunc_CloseStore );
	}
}

// ----------------------------------------------------------------------------
void CStoreFrame::Update(float elapsedTime)
{
	if ( IsOpen() )
	{
		CRoleSprite* sprite = g_pGameMain->FindRoleSprite(m_npcId);
		if ( sprite == NULL || g_pGameMain->GetDistanceWithPlayer(sprite) > _MAX_ROLE_TOUCH_MAXRANGE_ )
			Close();
	}
}

// ----------------------------------------------------------------------------
void CStoreFrame::Open(int npcId, int storeId, int* itemCount)
{
	Close();

	GameObjDbStruct* itemDB = CNetGlobal::GetObj( storeId );
	if ( itemDB && itemDB->Classification == EM_ObjectClass_Shop )
	{
		m_count		= 0;						// 記錄總販賣種類數量
		m_npcId		= npcId;					// 
		m_shopId	= storeId;					// 商店物品資料

		memset( m_itemCount, 0, sizeof(m_itemCount) );		// 物品販賣數量
		memset( m_index, 0, sizeof(m_index) );				// 物品在資料庫索引值

		for ( int i = 0; i < MAX_STORE_ITEM; i++ )
		{
			if ( itemDB->Shop.SellItemID[i] != 0 )
			{
				m_index[m_count] = i;
				m_itemCount[m_count] = itemCount[i];
				m_count++;
			}
		}

		// 開啟商店界面
		CUiFrame* frame = (CUiFrame*)m_interface->GetUiWorld()->FindObject( "StoreFrame" );
		if ( frame )
		{
			frame->SetVisible( true );
		}
	}
}

// ----------------------------------------------------------------------------
void CStoreFrame::Close()
{
	if ( m_shopId != 0 )
	{
		Clear();

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

		// 通知關閉商店
		NetCli_Shop::S_ShopCloseNotify();
	}
}

// ----------------------------------------------------------------------------
void CStoreFrame::UpdateBuy(int index, int count)
{
	if ( !IsOpen() )
		return;

	for ( int i = 0; i < m_count; i++ )
	{
		if ( m_index[i] == index )
		{
			m_itemCount[i] = count;			
			SendWorldEvent( UiEvent_StoreUpdateBuyGoods );		// 更新物品數量
			break;
		}
	}
}

// ----------------------------------------------------------------------------
void CStoreFrame::UpdateSell()
{
	// 更新物品數量
	SendWorldEvent( UiEvent_StoreUpdateSellGoods );	
}

// ----------------------------------------------------------------------------
int CStoreFrame::GetItemId(int index)
{
	if ( IsOpen() && index >= 0 && index < m_count )
	{
		GameObjDbStruct* itemDB = CNetGlobal::GetObj( m_shopId );
		return itemDB->Shop.SellItemID[m_index[index]];
	}
	return 0;
}

// ----------------------------------------------------------------------------
int CStoreFrame::GetItemCount(int index)
{
	if ( IsOpen() && index >= 0 && index < m_count )
		return m_itemCount[index];
	return 0;
}

// ----------------------------------------------------------------------------
void CStoreFrame::Clear()
{
	m_shopId = 0;
	m_npcId = 0;
	m_count = 0;
	memset( m_itemCount, 0 , sizeof(m_itemCount) );
	memset( m_index, 0, sizeof(m_index) );
}

// ----------------------------------------------------------------------------
void CStoreFrame::Cut(int state, int index)
{
	if ( !IsOpen() )
		return;

	switch ( state )
	{
	case eStore_Type_Buy:
		{
			GameObjDbStruct* itemDB = CNetGlobal::GetObj( GetItemId(index) );
			if ( itemDB )
			{
				g_pItemClipboard->SetFile( itemDB->ImageObj->ACTField );
				g_pItemClipboard->PickupShop( this, eStore_Type_Buy, index );
			}
		}
		break;

	case eStore_Type_Sell:
		break;
	}
}

// ----------------------------------------------------------------------------
void CStoreFrame::Paste(int state, int index)
{
	if ( !IsOpen() )
		return;

	switch ( state )
	{
	case eStore_Type_Buy:
		switch ( g_pItemClipboard->GetType() )
		{
		case CItemClipboard::eItem_Type_None:			// 拿取物
			Cut( eStore_Type_Buy, index );
			return;

		case CItemClipboard::eItem_Type_Bag:			// 背包
			{
				ClipboardBagItem bagItem = g_pItemClipboard->GetBagItem();
				Sell(bagItem.pos);
				g_pItemClipboard->Clear();
			}
			break;
		}
		break;

	case eStore_Type_Sell:
		switch ( g_pItemClipboard->GetType() )
		{
		case CItemClipboard::eItem_Type_None:			// 拿取物
			return;

		case CItemClipboard::eItem_Type_Bag:			// 背包
			{
				ClipboardBagItem bagItem = g_pItemClipboard->GetBagItem();
				Sell(bagItem.pos);
				g_pItemClipboard->Clear();
			}
		}
		break;
	}

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

// ----------------------------------------------------------------------------
void CStoreFrame::Buy(int type, int index)
{
	if ( !IsOpen() && index < 0 || index >= m_count )
		return;

	switch ( type )
	{
	case eStore_Type_Buy:
		{
			GameObjDbStruct* shop = CNetGlobal::GetObj( m_shopId );
			NetCli_Shop::S_BuyItemRequest( GetItemId(index), 1 );
		}
		break;

	case eStore_Type_Sell:
		{
			SellItemFieldStruct* sellItem = CNetGlobal::RoleData()->GetSellItem(index);
			if ( sellItem )
				NetCli_Shop::S_BuySellItemRecord( *sellItem );
		}
		break;
	}	
}

// ----------------------------------------------------------------------------
// 目前只提供有背包賣出
// pos 背包序列索引值
void CStoreFrame::Sell(int pos)
{
	ItemFieldStruct* item = CNetGlobal::RoleData()->GetBodyItem(pos);
	if ( item )
	{
		item->Pos = EM_ItemState_CliDisabled;
		NetCli_Shop::S_SelectSellItemRequest( pos, item );
	}
}

// ----------------------------------------------------------------------------
int	LuaFunc_GetStoreItemInfo(lua_State* L)
{
	int index = luaL_checkint(L, 1) - 1;
	if ( !g_pStoreFrame->IsOpen() )
		return 0;

	if ( index >= 0 && index < g_pStoreFrame->GetCount() )
	{
		GameObjDbStruct* shop = CNetGlobal::GetObj( g_pStoreFrame->GetShopId() );
		GameObjDbStruct* goods = CNetGlobal::GetObj( g_pStoreFrame->GetItemId(index) );
		if ( goods )
		{
			int stock = g_pStoreFrame->GetItemCount(index);
			lua_pushstring( L, goods->ImageObj->ACTField );
			lua_pushstring( L, goods->Name );
			lua_pushnumber( L, (int)((goods->Cost * shop->Shop.BuyRate) / 100) );
			lua_pushnumber( L, 0 );
			lua_pushnumber( L, stock );
			return 5;
		}
	}

	return 0;
}

// ----------------------------------------------------------------------------
int	LuaFunc_GetStoreItemCount(lua_State* L)
{
	lua_pushnumber( L, g_pStoreFrame->GetCount() );
	return 1;
}

// ----------------------------------------------------------------------------
int LuaFunc_PickupStoreItem(lua_State* L)
{
	g_pStoreFrame->Paste( CStoreFrame::eStore_Type_Buy, luaL_checkint(L, 1) - 1 );
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_BuyStoreItem(lua_State* L)
{
	g_pStoreFrame->Buy( CStoreFrame::eStore_Type_Buy, luaL_checkint(L, 1) - 1 );
	return 0;
}

// ----------------------------------------------------------------------------
int	LuaFunc_GetPawnshopItemInfo(lua_State* L)
{
	int index = luaL_checkint(L, 1) - 1;
	if ( !g_pStoreFrame->IsOpen() )
		return 0;

	SellItemFieldStruct* item = CNetGlobal::RoleData()->GetSellItem(index);
	if ( item )
	{
		GameObjDbStruct* itemDB = CNetGlobal::GetObj( item->Item.OrgObjID );
		if ( itemDB )
		{
			lua_pushstring( L, itemDB->ImageObj->ACTField );
			lua_pushstring( L, itemDB->Name );
			lua_pushnumber( L, item->Money );
			lua_pushnumber( L, item->Item.Count );
			lua_pushnumber( L, 0 );
			return 5;
		}
	}
	
	return 0;
}

// ----------------------------------------------------------------------------
int	LuaFunc_GetPawnshopItemCount(lua_State* L)
{
	lua_pushnumber( L, CNetGlobal::RoleData()->GetSellItemCount() );
	return 1;
}

// ----------------------------------------------------------------------------
int	LuaFunc_PickupPawnshopItem(lua_State* L)
{
	g_pStoreFrame->Paste( CStoreFrame::eStore_Type_Sell, luaL_checkint(L, 1) - 1 );
	return 0;
}

// ----------------------------------------------------------------------------
int	LuaFunc_RetrievePawnshopItem(lua_State* L)
{
	g_pStoreFrame->Buy( CStoreFrame::eStore_Type_Sell, luaL_checkint(L, 1) - 1 );
	return 0;
}

// ----------------------------------------------------------------------------
int	LuaFunc_CloseStore(lua_State* L)
{
	g_pStoreFrame->Close();
	return 0;
}