#include <time.h>
#include "../../netwaker_member/NetWakerClientInc.h"
#include "../Login/CharacterSelect.h"
#include "ChatFrame.h"
#include "ItemClipboard.h"
#include "BagFrame.h"
#include "AuctionFrame.h"

CAuctionFrame*			g_pAuctionFrame = NULL;

int LuaFunc_ClickAuctionItemButton		(lua_State* L);
int	LuaFunc_GetAuctionItem				(lua_State* L);
int LuaFunc_GetAuctionNumBrowseItems	(lua_State* L);
int LuaFunc_GetAuctionBrowseFilterList	(lua_State* L);
int LuaFunc_GetAuctionBrowseFilterMaxItems	(lua_State* L);
int LuaFunc_GetAuctionNumSellItems		(lua_State* L);
int LuaFunc_GetAuctionBrowseItemInfo	(lua_State* L);
int LuaFunc_GetAuctionSellItemInfo		(lua_State* L);
int	LuaFunc_GetAuctionNumBidItems		(lua_State* L);
int LuaFunc_GetAuctionBidItemInfo		(lua_State* L);
int	LuaFunc_CreateAuctionItem			(lua_State* L);
int LuaFunc_AuctionCancelSell			(lua_State* L);
int LuaFunc_AuctionClose				(lua_State* L);
int	LuaFunc_AuctionBrowseSearchItem		(lua_State* L);
int LuaFunc_AuctionBrowseBuyItem		(lua_State* L);
int LuaFunc_AuctionBidBuyItem			(lua_State* L);
int	LuaFunc_GetAuctionBrowseMaxItems	(lua_State* L);
int	LuaFunc_GetAuctionBrowseMaxPages	(lua_State* L);

int LuaFunc_AuctionBrowsePervPage		(lua_State* L);
int LuaFunc_AuctionBrowseNextPage		(lua_State* L);
int LuaFunc_GetAuctionHistoryItemNums	(lua_State* L);
int LuaFunc_GetAuctionHistoryItemInfo	(lua_State* L);
int LuaFunc_AuctionBrowseHistoryRequest	(lua_State* L);
int LuaFunc_AuctionBidHistoryRequest	(lua_State* L);

// ----------------------------------------------------------------------------
CAuctionFrame::CAuctionFrame(CInterface* object)
	: CInterfaceBase(object)
{
	m_auctionItemIndex	= -1;
	m_lastTime			= 0.0f;
	g_pAuctionFrame		= this;
}

// ----------------------------------------------------------------------------
CAuctionFrame::~CAuctionFrame()
{
	g_pAuctionFrame		= NULL;
}

// ----------------------------------------------------------------------------
void CAuctionFrame::RegisterFunc()
{
	lua_State* L = GetLuaStateWorld();
	if (L)
	{
		// general
		lua_register(L, "AuctionClose",					LuaFunc_AuctionClose);

		// browse tab
		lua_register(L, "AuctionBrowseSearchItem",		LuaFunc_AuctionBrowseSearchItem);
		lua_register(L, "GetAuctionBrowseFilterList",	LuaFunc_GetAuctionBrowseFilterList);
		lua_register(L, "GetAuctionBrowseFilterMaxItems",	LuaFunc_GetAuctionBrowseFilterMaxItems);
		lua_register(L, "GetAuctionNumBrowseItems",		LuaFunc_GetAuctionNumBrowseItems);
		lua_register(L, "GetAuctionBrowseItemInfo",		LuaFunc_GetAuctionBrowseItemInfo);
		lua_register(L, "AuctionBrowseBuyItem",			LuaFunc_AuctionBrowseBuyItem);
		lua_register(L, "GetAuctionBrowseMaxItems",		LuaFunc_GetAuctionBrowseMaxItems);
		lua_register(L, "GetAuctionBrowseMaxPages",		LuaFunc_GetAuctionBrowseMaxPages);
		lua_register(L, "AuctionBrowsePervPage",		LuaFunc_AuctionBrowsePervPage);
		lua_register(L, "AuctionBrowseNextPage",		LuaFunc_AuctionBrowseNextPage);
		lua_register(L, "AuctionBrowseHistoryRequest",	LuaFunc_AuctionBrowseHistoryRequest);
		lua_register(L, "GetAuctionHistoryItemNums",	LuaFunc_GetAuctionHistoryItemNums);
		lua_register(L, "GetAuctionHistoryItemInfo",	LuaFunc_GetAuctionHistoryItemInfo);

		// bid tab
		lua_register(L, "GetAuctionNumBidItems",		LuaFunc_GetAuctionNumBidItems);
		lua_register(L, "GetAuctionBidItemInfo",		LuaFunc_GetAuctionBidItemInfo);
		lua_register(L, "AuctionBidBuyItem",			LuaFunc_AuctionBidBuyItem);
		lua_register(L, "AuctionBidHistoryRequest",		LuaFunc_AuctionBidHistoryRequest);

		// sell tab
		lua_register(L, "ClickAuctionItemButton",		LuaFunc_ClickAuctionItemButton);
		lua_register(L, "GetAuctionNumSellItems",		LuaFunc_GetAuctionNumSellItems);
		lua_register(L, "GetAuctionSellItemInfo",		LuaFunc_GetAuctionSellItemInfo);
		lua_register(L, "GetAuctionItem",				LuaFunc_GetAuctionItem);
		lua_register(L, "CreateAuctionItem",			LuaFunc_CreateAuctionItem);
		lua_register(L, "AuctionCancelSell",			LuaFunc_AuctionCancelSell);
	}
}

// ----------------------------------------------------------------------------
void CAuctionFrame::Update(float elapsedTime)
{
	m_lastTime += elapsedTime;
	if ( m_lastTime > 1.0f )
	{
		m_lastTime = 0.0f;
		if ( NetCli_ACChild::s_isUpdateBuyList )
		{
			NetCli_ACChild::s_isUpdateBuyList = false;
			SendWorldEvent("AUCTION_BUY_UPDATE");
		}
		if ( NetCli_ACChild::s_isUpdateSellList )
		{
			NetCli_ACChild::s_isUpdateSellList = false;
			SendWorldEvent("AUCTION_SELL_UPDATE");
		}
	}
}

// ----------------------------------------------------------------------------
void CAuctionFrame::Pickup()
{
	if ( m_auctionItemIndex != -1 )
	{
		g_pBagFrame->Cut(m_auctionItemIndex);
		SetAuctionItemIndex(-1);
	}
}

// ----------------------------------------------------------------------------
void CAuctionFrame::PutDown(int index)
{
	Pickup();

	ItemFieldStruct* itemData = CNetGlobal::RoleData()->GetBodyItem(index);
	if ( itemData )
		itemData->Pos = EM_ItemState_CliDisabled;
	else
		index = -1;

	SetAuctionItemIndex(index);
}

// ----------------------------------------------------------------------------
void CAuctionFrame::OpenAC()
{
	// 開啟界面
	CUiFrame* frame = (CUiFrame*)m_interface->GetUiWorld()->FindObject("AuctionFrame");
	if ( frame )
	{
		frame->SetVisible( true );
	}
}

// ----------------------------------------------------------------------------
void CAuctionFrame::CloseAC()
{
	SetAuctionItemIndex(-1);
	NetCli_ACChild::SL_CloseAC();
}

// ----------------------------------------------------------------------------
void CAuctionFrame::SetAuctionItemIndex(int index)
{
	if ( m_auctionItemIndex != index )
	{
		m_auctionItemIndex = index;
		SendWorldEvent("AUCTION_AUCTION_INFO_UPDATE");
	}
}

// ----------------------------------------------------------------------------
int LuaFunc_ClickAuctionItemButton(lua_State* L)
{
	switch ( g_pItemClipboard->GetType() )
	{
		// pickup item
	case CItemClipboard::eItem_Type_None:
		g_pAuctionFrame->Pickup();
		break;
		//
	case CItemClipboard::eItem_Type_Bag:		
		{
			ClipboardBagItem bagItem = g_pItemClipboard->GetBagItem();
			g_pItemClipboard->Clear();
			g_pAuctionFrame->PutDown(bagItem.pos);
		}
		break;

	default:
		break;
	}
	return 0;
}

// ----------------------------------------------------------------------------
int	LuaFunc_GetAuctionItem(lua_State* L)
{
	const char* name = "";
	const char* texture = "";
	int stack = 0;
	int sellPrice = 0;

	int index = g_pAuctionFrame->GetAuctionItemIndex();
	ItemFieldStruct* itemData = CNetGlobal::RoleData()->GetBodyItem(index);
	if ( itemData )
	{
		char temp[MAX_PATH];
		GameObjDbStruct* itemDB = CNetGlobal::GetObj(itemData->OrgObjID);
		RoleDataEx::GetItemFieldName( itemData, temp, MAX_PATH );
		if (itemDB)
		{
			name		= temp;
			texture		= itemDB->ImageObj->ACTField;
			stack		= itemData->Count;
			sellPrice	= 100;
			lua_pushstring(L, name);
			lua_pushstring(L, texture);
			lua_pushnumber(L, stack);
			lua_pushnumber(L, sellPrice);
			return 4;
		}
	}	
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_GetAuctionNumBrowseItems(lua_State* L)
{
	lua_pushnumber( L, NetCli_ACChild::GetBrowseNumItems());
	return 1;
}

// ----------------------------------------------------------------------------
int LuaFunc_GetAuctionBrowseFilterList(lua_State* L)
{	
	char tmp[MAX_PATH];	
	const char* str;
	int arg1, arg2;
	int count = 0;

	switch ( lua_gettop(L) )
	{
	case 0:
		while ( true )
		{
			sprintf( tmp, "AC_ITEMTYPENAME_%d", count );
			str = g_ObjectData->GetString(tmp);
			if ( strlen(str) > 0 )
			{
				lua_pushstring(L, str);
				count++;
			}
			else
				break;
		}
		break;

	case 1:
		arg1 = luaL_checkint(L, 1) - 1;
		while ( true )
		{
			sprintf( tmp, "AC_ITEMTYPENAME_%d_%d", arg1, count );
			str = g_ObjectData->GetString(tmp);
			if ( strlen(str) > 0 )
			{
				lua_pushstring(L, str);
				count++;
			}
			else
				break;
		}
		break;

	case 2:
		arg1 = luaL_checkint(L, 1) - 1;
		arg2 = luaL_checkint(L, 2) - 1;
		while ( true )
		{
			sprintf( tmp, "AC_ITEMTYPENAME_%d_%d_%d", arg1, arg2, count );
			str = g_ObjectData->GetString(tmp);
			if ( strlen(str) > 0 )
			{
				lua_pushstring(L, str);
				count++;
			}
			else
				break;
		}
		break;
	}
	return count;
}

// ----------------------------------------------------------------------------
int LuaFunc_GetAuctionBrowseFilterMaxItems(lua_State* L)
{
	char tmp[MAX_PATH];
	const char* str;	
	int arg[2];
	int count = 0;

	for ( int i = 0; true; i++ )
	{
		sprintf( tmp, "AC_ITEMTYPENAME_%d", i );
		str = g_ObjectData->GetString(tmp);
		if ( strlen(str) > 0 )
			count++;
		else
			break;
	}

	if ( lua_isnumber(L, 1) )
	{
		arg[0] = (int)lua_tonumber(L, 1) - 1;

		for ( int i = 0; true; i++ )
		{
			sprintf( tmp, "AC_ITEMTYPENAME_%d_%d", arg[0], i );
			str = g_ObjectData->GetString(tmp);
			if ( strlen(str) > 0 )
				count++;
			else
				break;
		}
		if ( lua_isnumber(L, 2) )
		{
			arg[1] = (int)lua_tonumber(L, 2) - 1;

			for ( int i = 0; true; i++ )
			{
				sprintf( tmp, "AC_ITEMTYPENAME_%d_%d_%d", arg[0], arg[1], i );
				str = g_ObjectData->GetString(tmp);
				if ( strlen(str) > 0 )
					count++;
				else
					break;
			}
		}
	}

	lua_pushnumber(L, count);
	return 1;
}

// ----------------------------------------------------------------------------
int LuaFunc_GetAuctionNumSellItems(lua_State* L)
{
	lua_pushnumber(L, NetCli_ACChild::GetSellNumItems());
	return 1;
}

// ----------------------------------------------------------------------------
int LuaFunc_AuctionCancelSell(lua_State* L)
{
	AuctionSellItemInfo* info = NetCli_ACChild::GetSellItem(luaL_checkint(L, 1) - 1);
	if ( info )
		NetCli_ACChild::SL_CancelSellItem(info->data.DBID);
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_GetAuctionBrowseItemInfo(lua_State* L)
{
	int id = (luaL_checkint(L, 1) - 1) * 50 + (luaL_checkint(L, 2) - 1);

	AuctionBrowseItemInfo* info = NetCli_ACChild::GetBrowseItem(id);
	if ( info )
	{
		char temp[MAX_PATH];
		ItemFieldStruct* itemData = &info->data.Item;
		GameObjDbStruct* itemDB = CNetGlobal::GetObj(itemData->OrgObjID);
		RoleDataEx::GetItemFieldName(itemData, temp, MAX_PATH);
		if ( itemDB )
		{
			lua_pushnumber(L, id);
			lua_pushstring(L, temp);
			lua_pushnumber(L, itemDB->Rare);
			lua_pushstring(L, itemDB->ImageObj->ACTField);
			lua_pushnumber(L, itemDB->Limit.Level);
			lua_pushnumber(L, info->data.LiveTime);
			lua_pushstring(L, info->sellerName.c_str());
			lua_pushnumber(L, info->data.NowPrices);
			lua_pushboolean(L, info->isBuyer);
			if ( info->data.BuyOutPrices == _MAX_MONEY_COUNT_ )
				lua_pushnumber(L, 0);
			else
				lua_pushnumber(L, info->data.BuyOutPrices);
			return 10;
		}
	}
	return 0;
}

// ----------------------------------------------------------------------------
int	LuaFunc_GetAuctionNumBidItems(lua_State* L)
{
	lua_pushnumber(L, NetCli_ACChild::GetBuyNumItems());
	return 1;
}

// ----------------------------------------------------------------------------
int LuaFunc_GetAuctionBidItemInfo(lua_State* L)
{
	AuctionBuyItemInfo* info = NetCli_ACChild::GetBuyItem(luaL_checkint(L, 1) - 1);
	if ( info )
	{
		char temp[MAX_PATH];
		ItemFieldStruct* itemData = &info->data.Item;
		GameObjDbStruct* itemDB = CNetGlobal::GetObj(itemData->OrgObjID);
		RoleDataEx::GetItemFieldName(itemData, temp, MAX_PATH);
		if ( itemDB )
		{
			lua_pushstring(L, temp);
			lua_pushstring(L, itemDB->ImageObj->ACTField);
			lua_pushnumber(L, itemDB->Rare);
			lua_pushnumber(L, itemDB->Limit.Level);
			lua_pushnumber(L, info->data.LiveTime);
			lua_pushstring(L, info->sellerName.c_str());
			lua_pushboolean(L, info->isHigher);
			lua_pushnumber(L, info->data.NowPrices);
			if ( info->data.BuyOutPrices == _MAX_MONEY_COUNT_ )
				lua_pushnumber(L, 0);
			else
				lua_pushnumber(L, info->data.BuyOutPrices);
			return 9;
		}
	}
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_GetAuctionSellItemInfo(lua_State* L)
{
	AuctionSellItemInfo* info = NetCli_ACChild::GetSellItem(luaL_checkint(L, 1) - 1);
	if ( info )
	{
		char temp[MAX_PATH];
		ItemFieldStruct* itemData = &info->data.Item;
		GameObjDbStruct* itemDB = CNetGlobal::GetObj(itemData->OrgObjID);
		RoleDataEx::GetItemFieldName(itemData, temp, MAX_PATH);
		if ( itemDB )
		{
			lua_pushstring(L, temp);
			lua_pushstring(L, itemDB->ImageObj->ACTField);
			lua_pushnumber(L, itemDB->Rare);
			lua_pushnumber(L, itemDB->Limit.Level);
			lua_pushnumber(L, info->data.LiveTime);
			lua_pushstring(L, info->buyerName.c_str());
			lua_pushnumber(L, info->data.NowPrices);
			if ( info->data.BuyOutPrices == _MAX_MONEY_COUNT_ )
				lua_pushnumber(L, 0);
			else
				lua_pushnumber(L, info->data.BuyOutPrices);
			return 8;
		}
	}
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_CreateAuctionItem(lua_State* L)
{
	int index = g_pAuctionFrame->GetAuctionItemIndex();
	if ( index != -1 )
	{
		ItemFieldStruct* itemData;
		int time = luaL_checkint(L, 1);
		int price = luaL_checkint(L, 2);
		int buyout = 0;

		if ( lua_isnumber(L, 3) )
			buyout = (int)lua_tonumber(L, 3);

		if ( buyout < price )
			buyout = _MAX_MONEY_COUNT_;

		switch ( time )
		{
		case 1:
			time = 2 * 60;
			break;

		case 2:
			time = 8 * 60;
			break;

		case 3:
			time = 24 * 60;
			break;
		}

		itemData = CNetGlobal::RoleData()->GetBodyItem(index);
		NetCli_ACChild::SL_SellItem(index, *itemData, price, buyout, time*5);
	}

	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_AuctionClose(lua_State* L)
{
	g_pAuctionFrame->CloseAC();
	return 0;
}

// ----------------------------------------------------------------------------
int	LuaFunc_AuctionBrowseSearchItem(lua_State* L)
{
	ACSearchConditionStruct data;
	RoleDataEx* role = CNetGlobal::RoleData();
	int low = -1;
	int high = -1;

	if ( lua_isstring(L, 1) )
		data.SearchName = lua_tostring(L, 1);
	if ( lua_isnumber(L, 2) )
		low = (int)lua_tonumber(L, 2);
	if ( lua_isnumber(L, 3) )
		high = (int)lua_tonumber(L, 3);

	data.LevelLow = min(low, high);
	data.LevelHi = max(low, high);

	if ( data.LevelLow == 0 )
		data.LevelLow = -1;
	if ( data.LevelHi == 0 || data.LevelHi > 100 )
		data.LevelHi = -1;

	data.Rare = -1;
	if ( lua_isnumber(L, 4) )
	{		
		data.Rare = (int)lua_tonumber(L, 4);
		if ( data.Rare <= 0 )
			data.Rare = -1;
	}

	data.RuneVolume = -1;
	if ( lua_isnumber(L, 5) )
	{
		data.RuneVolume = (int)lua_tonumber(L, 5);
		if ( data.RuneVolume <= 0 )
			data.RuneVolume = -1;
	}

	if ( lua_toboolean(L, 6) )
	{
		switch ( role->BaseData.Sex )
		{
		case EM_Sex_Boy:
			data.Sex.Boy = true;
			break;

		case EM_Sex_Girl:
			data.Sex.Girl = true;
			break;

		default:
			data.Sex._Sex = -1;
			break;
		}

		switch ( role->BaseData.Race )
		{
		case EM_Race1:
			data.Race.Race0 = true;
			break;

		case EM_Race2:
			data.Race.Race1 = true;
			break;

		case EM_Race3:
			data.Race.Race2 = true;
			break;

		case EM_Race4:
			data.Race.Race3 = true;
			break;

		default:
			data.Race._Race = -1;
			break;
		}
		
		switch( role->BaseData.Voc )
		{
		case EM_Vocation_GameMaster:
			data.JobType.Voc0 = true;
			break;

		case EM_Vocation_Warrior:						// 戰士
			data.JobType.Voc1 = true;
			break;

		case EM_Vocation_Ranger:						// 弓箭手
			data.JobType.Voc2 = true;
			break;

		case EM_Vocation_Rogue:						// 盜賊
			data.JobType.Voc3 = true;
			break;

		case EM_Vocation_Wizard:						// 法師
			data.JobType.Voc4 = true;
			break;

		case EM_Vocation_Priest:						// 僧侶
			data.JobType.Voc5 = true;
			break;

		default:
			data.JobType._Voc = -1;
			break;
		}
		data.SearchType._Skill = role->TempData.Attr.Fin.WearSkill._Skill;
	}
	else
	{
		data.Sex._Sex = -1;
		data.Race._Race = -1;
		data.JobType._Voc = -1;
		data.SearchType._Skill = -1;
	}	

	memset( data.Type, -1, sizeof(data.Type) );
	for ( int i = 0; i < 3; i++ )
	{
		if ( lua_isnumber(L, i + 7) )
			data.Type[i] = (int)lua_tonumber(L, i + 7) - 1;
		else
			break;
	}
	NetCli_ACChild::SL_SearchItem(data);
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_AuctionBrowseBuyItem(lua_State* L)
{
	AuctionBrowseItemInfo* info = NetCli_ACChild::GetBrowseItem(luaL_checkint(L, 1));
	if ( info )
	{
		SampleRoleData* roleData;
		int i = 0;
		int price;

		// 不能競標自己的物品
		while ( roleData = g_pCharacterSelect->GetCharInfo(i) )
		{
			if ( strcmp(roleData->RoleName.Begin(), info->sellerName.c_str()) == 0 )
			{
				g_pChatFrame->AddMessage(5, "不能競標自己的物品");
				return 0;
			}
			i++;
		}

		price = info->data.BuyOutPrices;
		if ( lua_isnumber(L, 2) )
		{
			price = (int)lua_tonumber(L, 2);
			if ( price > info->data.BuyOutPrices )
				price = info->data.BuyOutPrices;
		}

		// 競標
		if ( price != info->data.BuyOutPrices )
		{
			// 若最高標者是自己
			if ( info->isBuyer )
			{
				g_pChatFrame->AddMessage(5, "最高標不能重複競標");
				return 0;
			}

			// 競標金額需高於底標
			if ( price <= info->data.NowPrices )
			{
				g_pChatFrame->AddMessage(5, "競標金額需高於底標");
				return 0;
			}
		}		

		// 檢查玩家金錢
		if ( CNetGlobal::RoleData()->BodyMoney() < price )
		{
			g_pChatFrame->AddMessage(5, "金錢不足");
			return 0;
		}
		NetCli_ACChild::SL_BuyItem(info->data.DBID, price);
	}
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_AuctionBidBuyItem(lua_State* L)
{
	AuctionBuyItemInfo* info = NetCli_ACChild::GetBuyItem(luaL_checkint(L, 1) - 1);
	if ( info )
	{
		int i = 0;
		int price;

		price = info->data.BuyOutPrices;
		if ( lua_isnumber(L, 2) )
		{
			price = (int)lua_tonumber(L, 2);
			if ( price > info->data.BuyOutPrices )
				price = info->data.BuyOutPrices;
		}

		// 最高標不能重複競標
		if ( price != info->data.BuyOutPrices )
		{
			if ( info->isHigher )
			{
				g_pChatFrame->AddMessage(5, "最高標不能重複競標");
				return 0;
			}

			// 競標金額需高於底標
			if ( price <= info->data.NowPrices )
			{
				g_pChatFrame->AddMessage(5, "競標金額需高於底標");
				return 0;
			}
		}

		// 檢查玩家金錢
		if ( CNetGlobal::RoleData()->BodyMoney() < price )
		{
			g_pChatFrame->AddMessage(5, "金錢不足");
			return 0;
		}
		NetCli_ACChild::SL_BuyItem(info->data.DBID, price);
	}
	return 0;
}

// ----------------------------------------------------------------------------
int	LuaFunc_GetAuctionBrowseMaxItems(lua_State* L)
{
	lua_pushnumber(L, NetCli_ACChild::GetBrowseMaxNums());
	return 1;
}

// ----------------------------------------------------------------------------
int	LuaFunc_GetAuctionBrowseMaxPages(lua_State* L)
{
	lua_pushnumber(L, NetCli_ACChild::GetBrowseMaxPages());
	return 1;
}

// ----------------------------------------------------------------------------
int LuaFunc_AuctionBrowsePervPage(lua_State* L)
{
	g_pAuctionFrame->SendWorldEvent("AUCTION_BROWSE_UPDATE");
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_AuctionBrowseNextPage(lua_State* L)
{
	if ( NetCli_ACChild::GetBrowseNextCount() > 0 )
		NetCli_ACChild::SL_GetNextSearchItem();
	else
		g_pAuctionFrame->SendWorldEvent("AUCTION_BROWSE_UPDATE");
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_GetAuctionHistoryItemNums(lua_State* L)
{
	lua_pushnumber(L, NetCli_ACChild::GetHistoryNumItems());
	return 1;
}

// ----------------------------------------------------------------------------
int LuaFunc_GetAuctionHistoryItemInfo(lua_State* L)
{
	AuctionHistoryItemInfo* info = NetCli_ACChild::GetHistoryItem(luaL_checkint(L, 1) - 1);
	if ( info )
	{
		struct tm* tmnow;
		time_t time = info->data;

		tmnow = localtime(&time);
		lua_pushnumber(L, info->price);
		lua_pushnumber(L, tmnow->tm_mon+1);
		lua_pushnumber(L, tmnow->tm_mday);
		lua_pushnumber(L, tmnow->tm_hour);
		lua_pushnumber(L, tmnow->tm_min);
		lua_pushnumber(L, tmnow->tm_sec);
		return 6;
	}
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_AuctionBrowseHistoryRequest(lua_State* L)
{
	AuctionBrowseItemInfo* info = NetCli_ACChild::GetBrowseItem(luaL_checkint(L, 1));
	if ( info )
		NetCli_ACChild::SL_ItemHistoryRequest(info->data.Item.OrgObjID, info->data.Item.Inherent);
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_AuctionBidHistoryRequest(lua_State* L)
{
	AuctionBuyItemInfo* info = NetCli_ACChild::GetBuyItem(luaL_checkint(L, 1) - 1);
	if ( info )
		NetCli_ACChild::SL_ItemHistoryRequest(info->data.Item.OrgObjID, info->data.Item.Inherent);
	return 0;
}