#include "../mainproc/GameMain.h"
#include "../../netwaker_member/net_mail/NetCli_MailChild.h"
#include "ItemClipboard.h"
#include "BagFrame.h"
#include "MailFrame.h"

CMailFrame*					g_pMailFrame = NULL;

int LuaFunc_CloseMail(lua_State* L);
int LuaFunc_ClickSendMailItemButton(lua_State* L);

// Send
int LuaFunc_SendMail(lua_State* L);
int	LuaFunc_SetSendMailMoney(lua_State* L);
int	LuaFunc_SetSendMailCOD(lua_State* L);
int	LuaFunc_GetSendMailItem(lua_State* L);

// Inbox
int LuaFunc_GetInboxNumItems(lua_State* L);
int LuaFunc_GetInboxHeaderInfo(lua_State* L);
int LuaFunc_InboxItemCanDelete(lua_State* L);
int LuaFunc_DeleteInboxItem(lua_State* L);
int LuaFunc_ReturnInboxItem(lua_State* L);
int	LuaFunc_GetInboxText(lua_State* L);
int LuaFunc_GetInboxItem(lua_State* L);
int	LuaFunc_TakeInboxItem(lua_State* L);
int LuaFunc_TakeInboxTextItem(lua_State* L);


// ----------------------------------------------------------------------------
CMailFrame::CMailFrame(CInterface* object)
	: CInterfaceBase(object)
{
	g_pMailFrame = this;

	m_sendMailLocked	= false;
	m_inboxOpenLocked	= false;
	Clear();
}

// ----------------------------------------------------------------------------
CMailFrame::~CMailFrame()
{
	g_pMailFrame = NULL;
}

// ----------------------------------------------------------------------------
void CMailFrame::RegisterFunc()
{
	lua_State* L = GetLuaStateWorld();
	if (L)
	{
		lua_register(L, "CloseMail",				LuaFunc_CloseMail);
		lua_register(L, "ClickSendMailItemButton",	LuaFunc_ClickSendMailItemButton);

		// Send Mail
		lua_register(L, "SendMail",					LuaFunc_SendMail);
		lua_register(L, "SetSendMailMoney",			LuaFunc_SetSendMailMoney);
		lua_register(L, "SetSendMailCOD",			LuaFunc_SetSendMailCOD);
		lua_register(L, "GetSendMailItem",			LuaFunc_GetSendMailItem);

		// Inbox		
		lua_register(L, "GetInboxNumItems",			LuaFunc_GetInboxNumItems);
		lua_register(L, "GetInboxHeaderInfo",		LuaFunc_GetInboxHeaderInfo);
		lua_register(L, "InboxItemCanDelete",		LuaFunc_InboxItemCanDelete);
		lua_register(L, "DeleteInboxItem",			LuaFunc_DeleteInboxItem);
		lua_register(L, "ReturnInboxItem",			LuaFunc_ReturnInboxItem);
		lua_register(L, "GetInboxText",				LuaFunc_GetInboxText);
		lua_register(L, "GetInboxItem",				LuaFunc_GetInboxItem);
		lua_register(L, "TakeInboxItem",			LuaFunc_TakeInboxItem);
	}
}

// ----------------------------------------------------------------------------
void CMailFrame::OpenMail()
{
	if ( !m_inboxOpenLocked )
	{
		m_inboxOpenLocked = true;
		NetCli_Mail::SL_MailList();
		SendWorldEvent("MAIL_SHOW");
	}
}

// ----------------------------------------------------------------------------
void CMailFrame::CloseMail()
{
	m_inboxOpenLocked = false;	
	NetCli_MailChild::ClearInboxList();
}

// ----------------------------------------------------------------------------
void CMailFrame::InboxUpdate()
{
	SendWorldEvent("MAIL_INBOX_UPDATE");
}

// ----------------------------------------------------------------------------
void CMailFrame::Clear()
{	
	m_isCOD				= 0;
	m_money				= 0;
	m_sendMailItemIndex	= -1;
}

// ----------------------------------------------------------------------------
void CMailFrame::SetMoney(bool isCOD, int money)
{
	m_isCOD = isCOD;
	m_money = money;
}

// ----------------------------------------------------------------------------
// 拿起要寄的物品
void CMailFrame::Pickup()
{
	if ( m_sendMailItemIndex != -1 )
	{
		g_pBagFrame->Cut(m_sendMailItemIndex);
		SetSendMailItemIndex(-1);
	}
}

// ----------------------------------------------------------------------------
// 擺放要寄出物品
// bagIndex 背包索引編號
void CMailFrame::PutDown(int bagIndex)
{
	Pickup();

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

	SetSendMailItemIndex(bagIndex);
}

// ----------------------------------------------------------------------------
void CMailFrame::SendMail(const char* target, const char* subject, const char* body)
{
	if ( !m_sendMailLocked )
	{
		MailBaseInfoStruct mailInfo;	
		
		mailInfo.IsSendMoney	= !m_isCOD;
		mailInfo.Money			= m_money;
		mailInfo.OrgTargetName	= target;
		mailInfo.Title			= subject;
		mailInfo.OrgSendName	= g_pGameMain->GetPlayerName();
		if ( m_sendMailItemIndex != -1 )
		{
			ItemFieldStruct* itemData = CNetGlobal::RoleData()->GetBodyItem(m_sendMailItemIndex);
			if ( itemData )
			{
				mailInfo.Item	= *itemData;
			}
		}
		m_sendMailLocked = true;
		NetCli_Mail::SL_SendMail(mailInfo, m_sendMailItemIndex, (char*)body);
	}
}

// ----------------------------------------------------------------------------
void CMailFrame::SetSendMailItemIndex(int index)
{
	if ( m_sendMailItemIndex != index )
	{
		m_sendMailItemIndex = index;
		SendWorldEvent("MAIL_SEND_INFO_UPDATE");
	}
}

// ----------------------------------------------------------------------------
int LuaFunc_CloseMail(lua_State* L)
{
	g_pMailFrame->CloseMail();
	return 0;
}

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

	default:
		break;
	}
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_SendMail(lua_State* L)
{
	const char* target = luaL_checkstring(L, 1);
	const char* subject = luaL_checkstring(L, 2);
	const char* body = lua_isstring(L,3) ? lua_tostring(L, 3) : "";
	g_pMailFrame->SendMail(target, subject, body);
	return 0;
}

// ----------------------------------------------------------------------------
int	LuaFunc_SetSendMailMoney(lua_State* L)
{
	g_pMailFrame->SetMoney(false, luaL_checkint(L, 1));
	return 0;
}

// ----------------------------------------------------------------------------
int	LuaFunc_SetSendMailCOD(lua_State* L)
{
	g_pMailFrame->SetMoney(true, luaL_checkint(L, 1));
	return 0;
}

// ----------------------------------------------------------------------------
// return	1.item name
//			2.item texture file name
//			3.item stack count
int	LuaFunc_GetSendMailItem(lua_State* L)
{	
	const char* name = NULL;
	const char* texture = "";
	int stack = 0;
	char temp[MAX_PATH];

	int itemIndex = g_pMailFrame->GetSendMailItemIndex();
	ItemFieldStruct* itemData = CNetGlobal::RoleData()->GetBodyItem(itemIndex);
	if ( itemData )
	{
		GameObjDbStruct* itemDB = CNetGlobal::GetObj(itemData->OrgObjID);
		RoleDataEx::GetItemFieldName( itemData, temp, MAX_PATH );
		if (itemDB)
		{
			name		= temp;
			texture		= itemDB->ImageObj->ACTField;
			stack		= itemData->Count;
		}
	}

	lua_pushstring(L, name);
	lua_pushstring(L, texture);
	lua_pushnumber(L, stack);
	return 3;
}

// ----------------------------------------------------------------------------
int LuaFunc_GetInboxNumItems(lua_State* L)
{
	lua_pushnumber(L, NetCli_MailChild::GetInboxNumItems());
	return 1;
}

// ----------------------------------------------------------------------------
// return : packageIcon, sender, subject, money, CODAmount, daysLeft, hasItem, wasRead
int LuaFunc_GetInboxHeaderInfo(lua_State* L)
{		
	NetInboxInfo* header = NetCli_MailChild::GetInboxInfo(luaL_checkint(L, 1) - 1);
	if ( header )
	{
		const char* icon = NULL;
		if ( !header->itemData.IsEmpty() )
		{
			GameObjDbStruct* itemDB = CNetGlobal::GetObj(header->itemData.OrgObjID);
			if ( itemDB )
				icon = itemDB->ImageObj->ACTField;
		}

		lua_pushstring(L, icon);								// icon
		lua_pushstring(L, header->sender.c_str());				// sender
		lua_pushstring(L, header->subject.c_str());				// subject
		lua_pushnumber(L, header->money);						// money
		lua_pushnumber(L, header->CODAmount);					// CODAmount
		lua_pushnumber(L, header->leftTime / (60.0 * 24.0));	// daysLeft
		lua_pushboolean(L, !header->itemData.IsEmpty());		// hasItem
		lua_pushboolean(L, header->wasRead);					// wasRead
		lua_pushboolean(L, header->wasReturned);				// wasRetunred
		lua_pushboolean(L, !header->isSystem);					// canReply
		return 10;
	}	
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_InboxItemCanDelete(lua_State* L)
{
	lua_pushboolean(L, NetCli_MailChild::InboxItemCanDelete(luaL_checkint(L, 1) - 1));
	return 1;
}

// ----------------------------------------------------------------------------
int LuaFunc_DeleteInboxItem(lua_State* L)
{
	NetCli_MailChild::DeleteInboxItem(luaL_checkint(L, 1) - 1);
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_ReturnInboxItem(lua_State* L)
{
	NetCli_MailChild::ReturnInboxItem(luaL_checkint(L, 1) - 1);
	return 0;
}

// ----------------------------------------------------------------------------
int	LuaFunc_GetInboxText(lua_State* L)
{
	int index = luaL_checkint(L, 1) - 1;
	NetInboxInfo* header = NetCli_MailChild::GetInboxInfo(index);
	NetCli_MailChild::ReadInboxItem(index);	

	if ( header )
	{
		lua_pushstring(L, header->content.c_str());
		return 1;
	}
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_GetInboxItem(lua_State* L)
{
	int index = luaL_checkint(L, 1) - 1;
	NetInboxInfo* header = NetCli_MailChild::GetInboxInfo(index);
	NetCli_MailChild::ReadInboxItem(index);

	if ( header && !header->itemData.IsEmpty() )
	{
		char name[MAX_PATH];
		const char* texture = NULL;
		GameObjDbStruct* itemDB;		

		RoleDataEx::GetItemFieldName( &header->itemData, name, MAX_PATH );
		if ( (itemDB = CNetGlobal::GetObj(header->itemData.OrgObjID)) != NULL )
			texture = itemDB->ImageObj->ACTField;

		lua_pushstring(L, name);
		lua_pushstring(L, texture);
		lua_pushnumber(L, header->itemData.Count);
		return 3;
	}
	return 0;
}

// ----------------------------------------------------------------------------
int	LuaFunc_TakeInboxItem(lua_State* L)
{
	NetCli_MailChild::TakeInboxItem( luaL_checkint(L, 1) - 1 );
	return 0;
}