#include "../../mainproc/GameMain.h"
#include "../../Interface/WorldFrames/ChatFrame.h"
#include "NetCli_FriendListChild.h"
#include "../../Interface/Debug/DebugMessage.h"
#include "../../interface/WorldFrames/ChatFrame.h"

map< string, int >			NetCli_FriendListChild::m_mapFolderID;
map< string, FriendInfo >		NetCli_FriendListChild::m_mapFriendInfo;
//-----------------------------------------------------------------------------
bool NetCli_FriendListChild::_Init()
{
	if (This)
		return false;

	This = new NetCli_FriendListChild;
	return NetCli_FriendList::_Init();
}

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

//-----------------------------------------------------------------------------
NetCli_FriendListChild::NetCli_FriendListChild()
{

}
//-----------------------------------------------------------------------------
void NetCli_FriendListChild::RChat_BefriendRequest	( char* FriendName )
{
	char Buf[1024];
	sprintf( Buf , "[好友要請] : 好友 = %s" , FriendName );
	g_pDebugMessage->DebugOutput( 5, Buf );

}
//-----------------------------------------------------------------------------
void NetCli_FriendListChild::RL_AddFriend			( char* FriendName )
{
	char Buf[1024];
	sprintf( Buf , "[好友加入] : 好友 = %s" , FriendName );
	g_pDebugMessage->DebugOutput( 5, Buf );

	_RoleData.SelfData.FriendList.AddFriend( FriendName , "" , "" );
	
	int iFriendID = NetCli_FriendListChild::GetFriendID( FriendName );

	lua_State* L = g_pGameMain->GetInterface()->GetUi()->GetLuaState();

	// Friend ID
		lua_pushnumber( L, iFriendID );
		lua_setglobal( L, "arg1" );

	// Folder ID

		lua_pushnumber( L, 1 );
		lua_setglobal( L, "arg2" );
	
	g_pGameMain->GetInterface()->GetUi()->SendEvent( UiEvent_AddFriend );
}
//-----------------------------------------------------------------------------
void NetCli_FriendListChild::RL_DelFriend			( char* FriendName )
{
	char Buf[1024];
	sprintf( Buf , "[好友移除] : 好友 = %s" , FriendName );
	g_pDebugMessage->DebugOutput( 5, Buf );

	_RoleData.SelfData.FriendList.DelFriend( FriendName );
	
	m_mapFriendInfo.erase( FriendName );

	NetCli_FriendListChild::ResetInfo();
	
	g_pGameMain->GetInterface()->GetUi()->SendEvent( UiEvent_ResetFriend );
}
//-----------------------------------------------------------------------------
void NetCli_FriendListChild::RL_FriendList			( FriendListStruct& FriendList )
{
	_RoleData.SelfData.FriendList = FriendList;
	
	NetCli_FriendListChild::ResetInfo();

}
//-----------------------------------------------------------------------------
void NetCli_FriendListChild::RChat_PlayerOnline		( char* RoleName , char* Note , int ZoneID , LookStruct& Look , Voc_ENUM Voc , Voc_ENUM Voc_Sub , int LV , int LV_Sub , Sex_ENUM Sex )
{
	char Buf[1024];
	sprintf( Buf , "[好友上線] : 好友 = %s ZoneID = %d Note = %s" , RoleName  , ZoneID , Note );
	g_pDebugMessage->DebugOutput( 5, Buf );

	FriendInfo	info		= m_mapFriendInfo[ RoleName ];

	info.bOnline			= true;
	info.ZoneID				= ZoneID;
	info.Voc				= Voc;
	info.Voc_Sub			= Voc_Sub;
	info.Sex				= Sex;

	m_mapFriendInfo[ RoleName ] = info;
		
	int iFriendID = GetFriendID( RoleName );

	if( iFriendID != -1 )
	{
		BaseFriendStruct* pFriend = &( _RoleData.SelfData.FriendList.FriendList[ ( iFriendID - 1 ) ] );

		pFriend->Name			= RoleName;
		pFriend->Note			= Note;
	}
}
//-----------------------------------------------------------------------------
void NetCli_FriendListChild::RChat_PlayerOffline	( char* RoleName )
{
	char Buf[1024];
	sprintf( Buf , "[好友下線] : 好友 = %s" , RoleName );
	g_pDebugMessage->DebugOutput( 5, Buf );

	FriendInfo	info		= m_mapFriendInfo[ RoleName ];

	info.bOnline			= false;

	m_mapFriendInfo[ RoleName ] = info;
}
//-----------------------------------------------------------------------------
void NetCli_FriendListChild::RChat_ModifyInfo		( char* RoleName , char* Note , int ZoneID , Voc_ENUM Voc , Voc_ENUM Voc_Sub , int LV , int LV_Sub , Sex_ENUM Sex )
{
	char Buf[1024];
	sprintf( Buf , "[好友資料更新] : 好友 = %s ZoneID = %d Note = %s" , RoleName , ZoneID , Note );
	g_pDebugMessage->DebugOutput( 5, Buf );
	
	FriendInfo	info		= m_mapFriendInfo[ RoleName ];

	info.ZoneID				= ZoneID;
	info.Voc				= Voc;
	info.Voc_Sub			= Voc_Sub;
	info.Sex				= Sex;

	m_mapFriendInfo[ RoleName ] = info;

	int iFriendID = GetFriendID( RoleName );

	if( iFriendID != -1 )
	{

		BaseFriendStruct* pFriend = &( _RoleData.SelfData.FriendList.FriendList[ ( iFriendID - 1 ) ] );

		pFriend->Name			= RoleName;
		pFriend->Note			= Note;
	}
	
	// 更新資料
}
//-----------------------------------------------------------------------------
void NetCli_FriendListChild::OnServerDisConnect		( int ServerID )
{
	//Chat 斷線
	if( ServerID != _ServerNetID.m_iChat )
		return;

	for( int i = 0 ; i < _MAX_FRIENDLIST_COUNT_ ; i++ )
	{
		if( _RoleData.SelfData.FriendList.FriendList[i].IsEmpty() )
			continue;
		This->RChat_PlayerOffline( (char*)_RoleData.SelfData.FriendList.FriendList[i].Name.Begin() );
	}

}
//-----------------------------------------------------------------------------
int NetCli_FriendListChild::GetFriendID			( char* RoleName )
{
	for( int i = 0 ; i < _MAX_FRIENDLIST_COUNT_ ; i++ )
	{
		if( strcmp( _RoleData.SelfData.FriendList.FriendList[i].Name.Begin(), RoleName ) == 0 )
		{
			return i+1;
		}
	}
	return -1;
}
//-----------------------------------------------------------------------------
void	NetCli_FriendListChild::ResetInfo			()
{
	m_mapFolderID.clear();
	m_mapFriendInfo.clear();

	// 分析目錄及好友, 通知 Friend Frame 更新資料

	// 分析目錄結構
	int iFolderID = 2;
	for( int i = 0 ; i < _MAX_FRIENDLIST_COUNT_ ; i++ )
	{
		if( _RoleData.SelfData.FriendList.FriendList[i].IsEmpty() == false )
		{
			if( strcmp( _RoleData.SelfData.FriendList.FriendList[i].Group.Begin(), "" ) != 0 )
			{				
				NetCli_FriendListChild::m_mapFolderID[ _RoleData.SelfData.FriendList.FriendList[i].Group.Begin() ] = iFolderID;
				iFolderID++;
			}
		}
	}

	for( int i = 0 ; i < _MAX_FRIENDLIST_COUNT_ ; i++ )
	{
		BaseFriendStruct* pFriend = &( _RoleData.SelfData.FriendList.FriendList[i] );

		if( pFriend->IsEmpty() == false )
		{	
			lua_State* L = g_pGameMain->GetInterface()->GetUi()->GetLuaState();
			
			// Friend ID
				lua_pushnumber( L, ( i+1 ) );
				lua_setglobal( L, "arg1" );

			// Folder ID
				int iFolderID = -1;
				map< string, int >::iterator it = NetCli_FriendListChild::m_mapFolderID.find( pFriend->Group.Begin() );
				if( it != NetCli_FriendListChild::m_mapFolderID.end() )
				{
					iFolderID = it->second;
				}
				else
				{
					iFolderID = 1;			
				}

				lua_pushnumber( L, iFolderID );
				lua_setglobal( L, "arg2" );
			
			g_pGameMain->GetInterface()->GetUi()->SendEvent( UiEvent_AddFriend );
		}
	}

}
//-----------------------------------------------------------------------------