#include "../../Interface/WorldFrames/MailFrame.h"
#include "NetCli_MailChild.h"

vector<NetInboxInfo>			NetCli_MailChild::m_inbox;
int								NetCli_MailChild::m_indoxNumItems;
int								NetCli_MailChild::m_inboxMaxNumItems;

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

	This = new NetCli_MailChild;
	ClearInboxList();
	return NetCli_Mail::_Init();
}

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

//-----------------------------------------------------------------------------
void NetCli_MailChild::ClearInboxList()
{
	m_inbox.clear();
	m_indoxNumItems = 0;
	m_inboxMaxNumItems = 0;
}

//-----------------------------------------------------------------------------
NetInboxInfo*	NetCli_MailChild::GetInboxInfo(int index)
{
	if ( (unsigned int)index < m_inbox.size() )
		return &m_inbox[index];
	return NULL;
}

//-----------------------------------------------------------------------------
bool NetCli_MailChild::InboxItemCanDelete(int index)
{
	NetInboxInfo* inboxItem = GetInboxInfo(index);
	if ( inboxItem )
	{
		if ( inboxItem->wasReturned || inboxItem->isSystem )
			return true;

		if ( inboxItem->money == 0 && inboxItem->itemData.IsEmpty() )
			return true;
	}	
	return false;
}

//-----------------------------------------------------------------------------
void NetCli_MailChild::ReadInboxItem(int index)
{	
	NetInboxInfo* inboxItem = GetInboxInfo(index);
	if ( inboxItem && !inboxItem->wasRead )
	{
		inboxItem->wasRead = true;
		if ( inboxItem->leftTime > 3 * 24 * 60 )
			inboxItem->leftTime = 3 * 24 * 60;

		SL_ReadMail(inboxItem->DBID);
	}
}

//-----------------------------------------------------------------------------
void NetCli_MailChild::DeleteInboxItem(int index)
{
	NetInboxInfo* inboxItem = GetInboxInfo(index);
	if ( inboxItem && InboxItemCanDelete(index) )
	{
		SL_DelMail(inboxItem->DBID);
		m_inbox.erase( m_inbox.begin() + index );
		m_indoxNumItems--;
		m_inboxMaxNumItems--;
	}
}

//-----------------------------------------------------------------------------
void NetCli_MailChild::ReturnInboxItem(int index)
{
	NetInboxInfo* inboxItem = GetInboxInfo(index);
	if ( inboxItem )
	{
		SL_SendBack(inboxItem->DBID);
		m_inbox.erase( m_inbox.begin() + index );
		m_indoxNumItems--;
		m_inboxMaxNumItems--;
	}
}

//-----------------------------------------------------------------------------
void NetCli_MailChild::TakeInboxItem(int index)
{
	NetInboxInfo* inboxItem = GetInboxInfo(index);
	if ( inboxItem )
	{
		int money = 0;
		if ( inboxItem->CODAmount > 0 )
		{
			if ( inboxItem->CODAmount > CNetGlobal::RoleData()->BodyMoney() )
				return;					// 金錢不足

			money = -inboxItem->CODAmount;
		}

		if ( inboxItem->money > 0 )
			money = inboxItem->money;
		else if ( inboxItem->itemData.IsEmpty() )
			return;						// 沒有物品及金錢

		SL_GetItem( inboxItem->DBID, money );
	}
}

//-----------------------------------------------------------------------------
void NetCli_MailChild::RL_SendMailResult	( SendMailResultEnum Result )
{
	g_pMailFrame->SendMailUnlocked();

	if ( Result == EM_SendMailResult_OK )
	{
		g_pMailFrame->Clear();
		g_pMailFrame->SendWorldEvent("MAIL_SEND_SUCCESS");
	}
	else
	{
		switch ( Result )
		{
		case EM_SendMailResult_Unknow:		
		case EM_SendMailResult_MoneyError:
		case EM_SendMailResult_ItemError:
		case EM_SendMailResult_TargetNotFind:
		case EM_SendMailResult_TargetMailFull:			
			break;
		}
		g_pMailFrame->SendWorldEvent("MAIL_FAILED");
	}
}
void NetCli_MailChild::RL_GetItemResult		( int MailDBID , bool Result )
{
	for ( vector<NetInboxInfo>::iterator iter = m_inbox.begin(); iter != m_inbox.end(); iter++ )
	{
		NetInboxInfo& info = *iter;
		if ( info.DBID == MailDBID )
		{
			if ( Result )
			{
				info.CODAmount = 0;
				info.money = 0;
				info.itemData.Init();
				g_pMailFrame->InboxUpdate();
			}
			break;
		}
	}
}
void NetCli_MailChild::RD_MailListResult	( int MailDBID , int Index , MailBaseInfoStruct&  MailBaseInfo , char* Content )
{
	NetInboxInfo inboxItem;
	inboxItem.DBID			= MailDBID;
	inboxItem.sender		= MailBaseInfo.OrgSendName.Begin();
	inboxItem.subject		= MailBaseInfo.Title.Begin();
	inboxItem.leftTime		= MailBaseInfo.LiveTime;
	inboxItem.wasRead		= MailBaseInfo.IsRead;
	inboxItem.wasReturned	= MailBaseInfo.IsSendBack;
	inboxItem.itemData		= MailBaseInfo.Item;
	inboxItem.content		= ( Content ) ? Content : "";
	inboxItem.isSystem		= MailBaseInfo.IsSystem;

	if ( MailBaseInfo.IsSendMoney )
	{
		inboxItem.money		= MailBaseInfo.Money;		
		inboxItem.CODAmount	= 0;
	}
	else
	{
		inboxItem.money		= 0;		
		inboxItem.CODAmount	= MailBaseInfo.Money;
	}
	m_inbox.push_back(inboxItem);

	// 檢查是否最後一個
	if ( m_inbox.size() == (unsigned int)m_indoxNumItems )
	{
		g_pMailFrame->InboxUpdate();
	}
}
/*
void NetCli_MailChild::RD_ReadMailResult	( int MailDBID , MailBaseInfoStruct&  MailBaseInfo , char* Content , bool Result )
{
	if ( !Result )
		return;

	for ( vector<NetInboxInfo>::iterator iter = m_inbox.begin(); iter != m_inbox.end(); iter++ )
	{
		NetInboxInfo& inboxItem = *iter;
		if ( inboxItem.DBID == MailDBID )
		{
			inboxItem.content		= Content;
			inboxItem.wasRead		= MailBaseInfo.IsRead;
			return;
		}
	}
}
*/
void NetCli_MailChild::RD_MailListCountResult	( int MaxCount , int Count )
{
	m_indoxNumItems = Count;
	m_inboxMaxNumItems = MaxCount;
}
//-----------------------------------------------------------------------------