#include "NetCli_ChannelChild.h"
#include "../Interface/WorldFrames/ChatFrame.h"

map<string, int>			NetCli_ChannelChild::s_channel;
map<string, int>			NetCli_ChannelChild::s_zoneChannel;

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

	This = new NetCli_ChannelChild;
	return NetCli_Channel::_Init();

	s_channel.clear();
	s_zoneChannel.clear();
}

//-----------------------------------------------------------------------------
bool NetCli_ChannelChild::_Release()
{
	NetCli_Channel::_Release();
	if (This)
	{
		delete This;
		This = NULL;
	}

	s_channel.clear();
	s_zoneChannel.clear();
	return true;
}
//-----------------------------------------------------------------------------
// arg1 頻道名稱
// return 傳回頻道代碼,　若失敗則傳回 -1
int	NetCli_ChannelChild::GetChannelID(const char* name)
{
	map<string, int>::iterator iter = s_channel.find(name);
	if ( iter != s_channel.end() )
		return iter->second;
	return -1;
}
//-----------------------------------------------------------------------------
int NetCli_ChannelChild::GetZoneChannelID(const char* name)
{
	map<string, int>::iterator iter = s_zoneChannel.find(name);
	if ( iter != s_zoneChannel.end() )
		return iter->second;
	return -1;
}
//-----------------------------------------------------------------------------
void NetCli_ChannelChild::R_CliConnectToChat( int ChatSockID )
{
	SetSrvID_Chat( ChatSockID );
}
//-----------------------------------------------------------------------------
void NetCli_ChannelChild::R_ClitoCli_Data( const char* Data , int Size )
{
	//測試
	char Buf[1024];
	sprintf( Buf , "[Channel轉送測試] : %s" , Data );
	g_pChatFrame->AddMessage( 5, Buf );
}
//-----------------------------------------------------------------------------
void NetCli_ChannelChild::R_CreateChannelOK( int ChannelID , const char* Name )
{
	char Buf[1024];
	sprintf( Buf , "建立頻道 %s(%d)" , Name , ChannelID );
	g_pChatFrame->AddMessage( 5, Buf );
}
//-----------------------------------------------------------------------------
void NetCli_ChannelChild::R_CreateChannelFailed( const char* Name )
{
	char Buf[1024];
	sprintf( Buf , "建立頻道 %s 失敗" , Name );
	g_pChatFrame->AddMessage( 5, Buf );

	g_pChatFrame->LeaveChannelByName(Name);
}
//-----------------------------------------------------------------------------
void NetCli_ChannelChild::R_JoinChannelOK( int ChannelID , const char* Name )
{
	char Buf[1024];
	sprintf( Buf , "加入頻道 %s(%d)" , Name , ChannelID );
	g_pChatFrame->AddMessage( 5, Buf );

	s_channel.insert( make_pair(Name, ChannelID) );
}
//-----------------------------------------------------------------------------
void NetCli_ChannelChild::R_JoinChannelFailed( const char* Name )
{
	char Buf[1024];
	sprintf( Buf , "加入頻道 %s 失敗" , Name  );
	g_pChatFrame->AddMessage( 5, Buf );

	g_pChatFrame->LeaveChannelByName(Name);
}
//-----------------------------------------------------------------------------
void NetCli_ChannelChild::R_LeaveChannelOK( int ChannelID )
{
	char buf[1024];
	for ( map<string, int>::iterator iter = s_channel.begin(); iter != s_channel.end(); iter++ )
	{
		if ( iter->second == ChannelID )
		{
			sprintf( buf , "離開頻道 %d.%s", iter->second, iter->first.c_str() );
			g_pChatFrame->AddMessage( 5, buf );
			s_channel.erase(iter);
			return;
		}
	}

	sprintf( buf , "[ERROR!!]離開頻道 %d,該頻道不存在", ChannelID );
	g_pChatFrame->AddMessage( 5, buf );
}
//-----------------------------------------------------------------------------
void NetCli_ChannelChild::R_LeaveChannelFailed( int ChannelID )
{
	char Buf[1024];
	sprintf( Buf , "離開頻道 %d 失敗" , ChannelID  );
	g_pChatFrame->AddMessage( 5, Buf );
}
//-----------------------------------------------------------------------------
void NetCli_ChannelChild::R_ChannelPlayerListResult	( GroupObjectTypeEnum Type , int ChannelID , vector<string> &Name )
{
	for( unsigned int i = 0 ; i < Name.size() ;i ++ )
		g_pChatFrame->AddMessage( 5, Name[i].c_str() );
}
//-----------------------------------------------------------------------------
void NetCli_ChannelChild::R_JoinPlayerDisabledResult ( int ChannelID , GroupObjectTypeEnum Type , bool JoinEnabled )
{
	char Buf[1024];
	if( JoinEnabled )
		sprintf( Buf , "開放其他玩家加入 頻道 %d" , ChannelID  );
	else
		sprintf( Buf , "禁止其他玩家加入 頻道 %d" , ChannelID  );
	g_pChatFrame->AddMessage( 5, Buf );
}
//-----------------------------------------------------------------------------
//系統頻道列表
void NetCli_ChannelChild::R_SysChannel( GroupObjectTypeEnum Type , int ChannelID , char* Name )
{
	char Buf[1024];
	switch( Type )
	{
	case EM_GroupObjectType_SysChannel:
		sprintf( Buf , "系統頻道 ID = %d Name = %s" , ChannelID , Name );
		break;
	case EM_GroupObjectType_SysZoneChannel:
		sprintf( Buf , "區域頻道 ID = %d Name = %s" , ChannelID , Name );
		s_zoneChannel.insert( make_pair(Name, ChannelID) );
		break;
	}
	g_pChatFrame->AddMessage( 5, Buf );
}
//-----------------------------------------------------------------------------