#include "NetCli_ACChild.h"
#include "../../mainproc/GameMain.h"
#include "../Interface/WorldFrames/AuctionFrame.h"
#include "../Interface/WorldFrames/ChatFrame.h"

vector<AuctionBuyItemInfo>		NetCli_ACChild::s_buyList;
vector<AuctionSellItemInfo>		NetCli_ACChild::s_sellList;
vector<AuctionHistoryItemInfo>	NetCli_ACChild::s_history;
map<int, AuctionBrowseItemInfo>	NetCli_ACChild::s_browseList;

bool							NetCli_ACChild::s_isUpdateBuyList;
bool							NetCli_ACChild::s_isUpdateSellList;
int								NetCli_ACChild::s_browseMaxCount;
int								NetCli_ACChild::s_borwseNextCount;
int								NetCli_ACChild::s_browseLastIndex;
int								NetCli_ACChild::s_searchItemSize;
int								NetCli_ACChild::s_searchItemCount;

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

	This = new NetCli_ACChild;

	s_buyList.clear();
	s_sellList.clear();
	s_history.clear();
	s_browseList.clear();

	s_isUpdateBuyList = false;
	s_isUpdateSellList = false;

	s_browseMaxCount = 0;
	s_borwseNextCount = 0;
	s_browseLastIndex = 0;

	return NetCli_AC::_Init();
}

//-----------------------------------------------------------------------------
bool NetCli_ACChild::_Release()
{
	NetCli_AC::_Release();
	if (This)
	{
		delete This;
		This = NULL;
	}
	return true;
}
//-----------------------------------------------------------------------------
void NetCli_ACChild::ClearBrowseList()
{
	s_browseList.clear();
	s_browseMaxCount = 0;
	s_borwseNextCount = 0;
	s_browseLastIndex = 0;
}
//-----------------------------------------------------------------------------
void NetCli_ACChild::ClearBuyList()
{
	s_buyList.clear();
	s_isUpdateBuyList = true;
}

//-----------------------------------------------------------------------------
void NetCli_ACChild::ClearSellList()
{
	s_sellList.clear();
	s_isUpdateSellList = true;
}

//-----------------------------------------------------------------------------
void NetCli_ACChild::AddBuyItem(ACItemInfoStruct& Data , char* SellerName , int BuyerDBID)
{
	if ( SellerName == NULL )
		SellerName = "";

	s_isUpdateBuyList = true;
	for ( vector<AuctionBuyItemInfo>::iterator iter = s_buyList.begin(); iter != s_buyList.end(); iter++ )
	{
		if ( (*iter).data.DBID == Data.DBID )
		{
			(*iter).isHigher	= (BuyerDBID == g_pGameMain->GetDBID());
			(*iter).data		= Data;
			(*iter).sellerName	= SellerName;
			return;
		}
	}

	AuctionBuyItemInfo info;
	info.isHigher	= (BuyerDBID == g_pGameMain->GetDBID());
	info.data		= Data;
	info.sellerName	= SellerName;
	s_buyList.push_back(info);
}
//-----------------------------------------------------------------------------
void NetCli_ACChild::DeleteBuyItem(int ACItemDBID)
{	
	for ( vector<AuctionBuyItemInfo>::iterator iter = s_buyList.begin(); iter != s_buyList.end(); iter++ )
	{
		if ( (*iter).data.DBID == ACItemDBID )
		{
			s_buyList.erase(iter);
			s_isUpdateBuyList = true;
			return;
		}
	}
}
//-----------------------------------------------------------------------------
void NetCli_ACChild::UpdateBuyItem(int ACItemDBID , ACItemStateENUM state , int newPrices, int buyerDBID)
{
	for ( vector<AuctionBuyItemInfo>::iterator iter = s_buyList.begin(); iter != s_buyList.end(); iter++ )
	{
		if ( (*iter).data.DBID == ACItemDBID )
		{
			switch ( state )
			{
			case EM_ACItemState_Normal:
				s_isUpdateBuyList		= true;
				(*iter).data.NowPrices	= newPrices;
				(*iter).isHigher		= (g_pGameMain->GetDBID() == buyerDBID);
				break;

			case EM_ACItemState_BuyOut:
			case EM_ACItemState_Cancel:
				DeleteBuyItem(ACItemDBID);
				break;
			}
			break;
		}
	}
}
//-----------------------------------------------------------------------------
void NetCli_ACChild::AddSellItem(ACItemInfoStruct& Data , char* BuyerName)
{
	if ( BuyerName == NULL )
		BuyerName = "";

	s_isUpdateSellList = true;
	for ( vector<AuctionSellItemInfo>::iterator iter = s_sellList.begin(); iter != s_sellList.end(); iter++ )
	{
		if ( (*iter).data.DBID == Data.DBID )
		{
			(*iter).data = Data;
			(*iter).buyerName = BuyerName;
			return;
		}
	}

	AuctionSellItemInfo info;
	info.buyerName	= BuyerName;
	info.data		= Data;
	s_sellList.push_back(info);
}
//-----------------------------------------------------------------------------
void NetCli_ACChild::DeleteSellItem(int ACItemDBID)
{
	for ( vector<AuctionSellItemInfo>::iterator iter = s_sellList.begin(); iter != s_sellList.end(); iter++ )
	{
		if ( (*iter).data.DBID == ACItemDBID )
		{
			s_sellList.erase(iter);
			s_isUpdateSellList = true;
			return;
		}
	}
}
//-----------------------------------------------------------------------------
void NetCli_ACChild::UpdateSellItem(int ACItemDBID, ACItemStateENUM state, int prices, char* buyer)
{
	for ( vector<AuctionSellItemInfo>::iterator iter = s_sellList.begin(); iter != s_sellList.end(); iter++ )
	{
		if ( (*iter).data.DBID == ACItemDBID )
		{
			switch ( state )
			{
			case EM_ACItemState_Normal:
				s_isUpdateSellList		= true;
				(*iter).buyerName		= buyer;
				(*iter).data.NowPrices	= prices;
				break;

			case EM_ACItemState_BuyOut:
			case EM_ACItemState_Cancel:
				DeleteSellItem(ACItemDBID);
				break;
			}
			break;
		}
	}
}
//-----------------------------------------------------------------------------
int NetCli_ACChild::GetBrowseMaxPages()
{
	return (s_browseLastIndex + s_borwseNextCount) / 50 + 1;
}
//-----------------------------------------------------------------------------
AuctionBuyItemInfo*	NetCli_ACChild::GetBuyItem(int index)
{
	if ( (unsigned int)index < s_buyList.size() )
		return &s_buyList[index];
	return NULL;
}
//-----------------------------------------------------------------------------
AuctionSellItemInfo* NetCli_ACChild::GetSellItem(int index)
{
	if ( (unsigned int)index < s_sellList.size() )
		return &s_sellList[index];
	return NULL;
}
//-----------------------------------------------------------------------------
AuctionBrowseItemInfo* NetCli_ACChild::GetBrowseItem(int index)
{
	map<int, AuctionBrowseItemInfo>::iterator iter = s_browseList.find(index);
	if ( iter != s_browseList.end() )
		return &(iter->second);
	return NULL;
}
//-----------------------------------------------------------------------------
AuctionHistoryItemInfo*	NetCli_ACChild::GetHistoryItem(int index)
{
	if ( (unsigned int)index < s_history.size() )
		return &s_history[index];
	return NULL;
}
//-----------------------------------------------------------------------------
void NetCli_ACChild::RD_SearchItemCountResult	( int MaxCount , int Count , int BeginIndex )
{
	char	Buf[1024];
	sprintf( Buf , "[AC搜尋] : MaxCount=%d Count=%d  BeginIndex=%d " , MaxCount , Count , BeginIndex  );	

	g_pChatFrame->AddMessage(5, Buf);

	if ( BeginIndex < 50 )
	{
		ClearBrowseList();

		s_browseMaxCount = MaxCount;	// 記錄查尋總筆數
		s_borwseNextCount = s_browseMaxCount;
	}

	s_borwseNextCount -= Count;
	s_searchItemSize = Count;			// 取得單次查尋個數
	s_searchItemCount = 0;				// 計數器
}
void NetCli_ACChild::RD_SearchItemResult( int Index , ACItemInfoStruct& Data , char* SellerName , int BuyerDBID )
{
	ItemFieldStruct& Item = Data.Item;
	GameObjDbStruct* ItemDB = GetObj( Item.OrgObjID );
	if( ItemDB == NULL )
		return;
	char	Buf[1024];
	sprintf( Buf , "[AC搜尋] : Index=%d ItemDBID=%d  Name=%s " , Index , Data.DBID , ItemDB->Name  );	
	g_pChatFrame->AddMessage(5 , Buf );

	AuctionBrowseItemInfo info;
	info.data		= Data;
	info.index		= Index;
	info.isBuyer	= (g_pGameMain->GetDBID() == BuyerDBID);
	info.sellerName	= SellerName;
	s_browseList.insert(make_pair(Index, info));

	s_browseLastIndex = Index;
	s_searchItemCount++;
	if ( s_searchItemCount >= s_searchItemSize )
	{
		// 新頁次更新,主動送出更新頁面事件
		g_pAuctionFrame->SendWorldEvent("AUCTION_BROWSE_UPDATE");
	}
}
//-----------------------------------------------------------------------------
void NetCli_ACChild::RD_FixSearchItemResult		( int ACItemDBID , ACItemStateENUM	State , int NewPrices , int BuyerDBID )
{
	char	Buf[1024];
	sprintf(Buf , "AC物品修正 ACItemDBID=%d State=%d NewPrices=%d " , ACItemDBID , State , NewPrices);
	g_pChatFrame->AddMessage(5, Buf );

	map<int, AuctionBrowseItemInfo>::iterator iter = s_browseList.begin();
	for ( ; iter != s_browseList.end(); iter++ )
	{
		if ( (iter->second).data.DBID == ACItemDBID )
			break;
	}

	switch ( State )
	{
	case EM_ACItemState_Normal:
		if ( iter != s_browseList.end() )
		{
			(iter->second).data.NowPrices = NewPrices;
			(iter->second).isBuyer = (g_pGameMain->GetDBID() == BuyerDBID);

			// 通知UI做單筆資料修正
			if ( BuyerDBID == g_pGameMain->GetDBID() )
			{
				lua_State* L = g_pAuctionFrame->GetLuaStateWorld();

				lua_pushnumber(L, iter->first);
				lua_setglobal(L, "arg1");
				lua_pushnumber(L, NewPrices);
				lua_setglobal(L, "arg2");
				g_pAuctionFrame->SendWorldEvent("AUCTION_BROWSE_BID_SUCCESS");
			}
		}
		break;

	case EM_ACItemState_BuyOut:
	case EM_ACItemState_Cancel:
		s_browseMaxCount--;
		if ( iter != s_browseList.end() )
		{
			map<int, AuctionBrowseItemInfo>::iterator perv_iter = iter;
			if ( perv_iter != s_browseList.begin() )
			{
				perv_iter--;
				s_browseLastIndex = perv_iter->first;
			}
			else
				s_browseLastIndex = 0;


			// 刪除該筆資料,資料不做修正
			s_browseList.erase(iter);

			// 通知UI更新頁面
			if ( BuyerDBID == g_pGameMain->GetDBID() )
				g_pAuctionFrame->SendWorldEvent("AUCTION_BROWSE_UPDATE");
		}
		else
		{
			s_borwseNextCount--;
		}
		break;
	}	
}
//-----------------------------------------------------------------------------
void NetCli_ACChild::RD_SelfCompetitive_BuyItem	( ACItemInfoStruct& Data , char* SellerName , int BuyerDBID  )
{
	AddBuyItem(Data, SellerName, BuyerDBID);
}
//-----------------------------------------------------------------------------
void NetCli_ACChild::RD_SelfCompetitive_SellItem( ACItemInfoStruct& Data , char* BuyerName )
{
	AddSellItem(Data, BuyerName);
}
//-----------------------------------------------------------------------------
void NetCli_ACChild::RD_CancelSellItemResult	( int ACItemDBID )
{
	DeleteSellItem(ACItemDBID);
}
//-----------------------------------------------------------------------------
void NetCli_ACChild::RD_CloseAC					( )
{
	g_pChatFrame->AddMessage(5, "要求關閉AC" );
	ClearBrowseList();
	ClearBuyList();
	ClearSellList();
	g_pAuctionFrame->SendWorldEvent("AUCTION_CLOSE");
}
//-----------------------------------------------------------------------------
void NetCli_ACChild::RD_OpenAC					( bool Result )
{
	g_pChatFrame->AddMessage(5, "要求開啟AC" );
	ClearBrowseList();
	ClearBuyList();
	ClearSellList();
	g_pAuctionFrame->OpenAC();
}
//-----------------------------------------------------------------------------
// 物品購買回傳
void NetCli_ACChild::RL_BuyItemResult			( bool Result , int ACItemDBID , int Prices , ACItemStateENUM State )
{
	if ( Result )
	{
		// 當購買成功時,需以物品編號(DBID)找出原始資料,並加入購買清單內
		if ( State == EM_ACItemState_Normal )
		{
			ACItemInfoStruct data;

			map<int, AuctionBrowseItemInfo>::iterator browse_iter = s_browseList.begin();
			for ( ; browse_iter != s_browseList.end(); browse_iter++ )
			{
				if ( (browse_iter->second).data.DBID == ACItemDBID )
				{
					data = (browse_iter->second).data;
					data.NowPrices = Prices;
					AddBuyItem(data, (char*)(browse_iter->second).sellerName.c_str(), g_pGameMain->GetDBID());
					g_pChatFrame->AddMessage(5, "競標成功");
					return;
				}
			}

			vector<AuctionBuyItemInfo>::iterator buy_iter = s_buyList.begin();
			for ( ; buy_iter != s_buyList.end(); buy_iter++ )
			{
				if ( (*buy_iter).data.DBID == ACItemDBID )
				{
					data = (*buy_iter).data;
					data.NowPrices = Prices;
					AddBuyItem(data, (char*)(*buy_iter).sellerName.c_str(), g_pGameMain->GetDBID());
					g_pChatFrame->AddMessage(5, "競標成功");
					return;
				}
			}
		}
	}
	else
	{
		g_pChatFrame->AddMessage(5, "競標失敗");
	}
}
//-----------------------------------------------------------------------------
void NetCli_ACChild::RL_SellItemResult			( bool Result , ACItemInfoStruct& Item )
{
	if ( Result )
	{
		g_pChatFrame->AddMessage(5, "拍賣成功");

		AddSellItem(Item, NULL);
	}
	else
	{
		// 拍賣失敗
		g_pChatFrame->AddMessage(5, "拍賣失敗");
	}

	// Update Auction Auction Item
	g_pAuctionFrame->SetAuctionItemIndex(-1);
}
//-----------------------------------------------------------------------------
void NetCli_ACChild::RD_LostCompetitiveItem( int ACItemDBID , ItemFieldStruct& Item )
{
	/*
	char temp[MAX_PATH];
	GameObjDbStruct* itemDB = CNetGlobal::GetObj(Item.OrgObjID);
	RoleDataEx::GetItemFieldName(&Item, temp, MAX_PATH);
	g_pChatFrame->AddMessage(5, "[%s]出價被超過了", temp);
	*/
}
//-----------------------------------------------------------------------------
void NetCli_ACChild::RD_FixBuyOrderItem( int ACItemDBID , ACItemStateENUM State , int NewPrices , int BuyerDBID )
{
	UpdateBuyItem(ACItemDBID, State, NewPrices, BuyerDBID);
}
//-----------------------------------------------------------------------------
void NetCli_ACChild::RD_FixSellOrderItem( int ACItemDBID , ACItemStateENUM State , int NewPrices , char* BuyerName )
{
	UpdateSellItem(ACItemDBID, State, NewPrices, BuyerName);
}
//-----------------------------------------------------------------------------
void NetCli_ACChild::RD_ItemHistoryListResult	( vector< DB_ACItemHistoryStruct >& ItemList )
{
	AuctionHistoryItemInfo info;

	s_history.clear();
	for ( vector<DB_ACItemHistoryStruct>::iterator iter = ItemList.begin(); iter != ItemList.end(); iter++ )
	{
		info.price = (*iter).Prices;
		info.data = (*iter).Date;
		s_history.push_back(info);
	}

	// 更新歷史資料
	g_pAuctionFrame->SendWorldEvent("AUCTION_HISTORY_SHOW");
}
//-----------------------------------------------------------------------------
void NetCli_ACChild::RD_SelfCompetitive_ItemDataEnd(  )
{
	int a = 0;
}