#include "NetCli_login.h"
//-------------------------------------------------------------------
CNetCli_Login*		CNetCli_Login::m_pThis		= NULL;
DWORD				CNetCli_Login::m_dwStep		= 0;

#ifdef _AUTOLOGIN
	bool		CNetCli_Login::m_bAutoLogin;
	char		CNetCli_Login::m_szAutoLogingID[32];
	char		CNetCli_Login::m_szAutoLogingPW[32];
	int			CNetCli_Login::m_iAutoLoginRoleID;
#endif

//-------------------------------------------------------------------
bool CNetCli_Login::_Init()
{
	_Net.RegOnLoginFunction				( OnEvent_Login				);
	_Net.RegOnLogoutFunction			( OnEvent_Logout			);
	_Net.RegOnLoginFailFunction			( OnEvent_LogFailed			); 

	_Net.RegOnSrvConnectFunction		( OnEvent_ServerConnect		);
	_Net.RegOnSrvDisConnectFunction		( OnEvent_ServerDisConnect	);


	_Net.RegOnSrvPacketFunction			( EM_PG_Login_M2C_IdentifyMaster,			OnPacket_IdentifyMaster			);
	_Net.RegOnSrvPacketFunction			( EM_PG_Login_M2C_SampleRoleData,			OnPacket_SampleRoleData			);
	_Net.RegOnSrvPacketFunction			( EM_PG_Login_M2C_SampleRoleDataEnd,		OnPacket_SampleRoleDataEnd		);

	_Net.RegOnSrvPacketFunction			( EM_PG_Login_M2C_CreateRoleResult,			OnPacket_CreateRoleResult		);
	_Net.RegOnSrvPacketFunction			( EM_PG_Login_M2C_DeleteRoleResult,			OnPacket_DeleteRoleResult		);

	_Net.RegOnSrvPacketFunction			( EM_PG_Login_M2C_SelectRoleResult,			OnPacket_SelectRoleResult		);

	_Net.RegOnSrvPacketFunction			( EM_PG_Login_M2C_EnterWorld,				OnPacket_EnterWorld				);
	_Net.RegOnSrvPacketFunction			( EM_PG_Login_M2C_LeaveWorld,				OnPacket_LeaveWorld				);

	return true;
}
//-------------------------------------------------------------------
void CNetCli_Login::_ClearRoleData()
{
	// 刪除所有作業用的暫時資料
	map< int , SampleRoleData* >::iterator it;
	SampleRoleData* pSampleRole = NULL;

	for( it = m_pThis->m_mapSampleRole.begin(); it != m_pThis->m_mapSampleRole.end(); it++ )
	{
		pSampleRole = it->second;
		delete pSampleRole;
	}

	m_pThis->m_mapSampleRole.clear();
	m_pThis->m_vecDBID.clear();
}
//-------------------------------------------------------------------
void CNetCli_Login::OnEvent_Login		( string sInfo )
{
	m_pThis->On_Login( sInfo.c_str() );
}
//-------------------------------------------------------------------
void CNetCli_Login::OnEvent_Logout		()
{
	m_pThis->On_Logout();
}
//-------------------------------------------------------------------
void CNetCli_Login::OnEvent_LogFailed	( SYSLogFailedENUM emLogFailed )
{
	m_pThis->On_LoginFailed( emLogFailed );
}
//-------------------------------------------------------------------
void CNetCli_Login::OnEvent_ServerConnect	( int iServerID , string sServerName )
{
	if( m_dwStep == 0 )
	{
		m_dwStep++; // 連線到 Switch 成功
	}

	m_pThis->On_ServerConnect( iServerID, sServerName.c_str() );
}
//-------------------------------------------------------------------
void CNetCli_Login::OnEvent_ServerDisConnect	( int iServerID )
{
	if( m_dwStep == 1 )
	{
		m_dwStep++; // 脫離 Switch
	}



	m_pThis->On_ServerDisConnect( iServerID );
}
//-------------------------------------------------------------------
bool CNetCli_Login::_Release()				{ return true;				}
//-------------------------------------------------------------------
void CNetCli_Login::OnPacket_IdentifyMaster			( int iServerID , int iSize , PVOID pData )
{
	PG_Login_M2C_IdentifyMaster* ptIdentifyMaster = (PG_Login_M2C_IdentifyMaster*)pData;
	CNetGlobal::SetSrvID_Master( iServerID );
}
//-------------------------------------------------------------------
void CNetCli_Login::OnPacket_SampleRoleData			( int iServerID , int iSize , PVOID pData )
{
	M_PACKET( PG_Login_M2C_SampleRoleData );
	
	SampleRoleData* pSampleRole = new SampleRoleData;
	*pSampleRole				= pPacket->SampleRole;
	
	m_pThis->m_mapSampleRole[ pPacket->iDBID ] = pSampleRole;
	
	int index = m_pThis->m_vecDBID.size();
	m_pThis->m_vecDBID.push_back( pPacket->iDBID );

	m_pThis->On_SamepleRoleData( index, pSampleRole );
}
//-------------------------------------------------------------------
void CNetCli_Login::OnPacket_SampleRoleDataEnd		( int iServerID , int iSize , PVOID pData )
{
	PG_Login_M2C_SampleRoleDataEnd* ptSampleRoleDatEnd = (PG_Login_M2C_SampleRoleDataEnd*)pData;
	m_pThis->On_SamepleRoleDataEnd();
}
//-------------------------------------------------------------------
void CNetCli_Login::OnPacket_SelectRoleResult		( int iServerID , int iSize , PVOID pData )
{
	M_PACKET( PG_Login_M2C_SelectRoleResult );

	m_pThis->On_SelectRoleResult( pPacket->emResult, pPacket->iDBID );

	if(  pPacket->emResult == EM_SelectRole_OK )
	{
		map< int, SampleRoleData* >::iterator it = m_pThis->m_mapSampleRole.find( pPacket->iDBID );

		if( it != m_pThis->m_mapSampleRole.end() )
		{
			SampleRoleData* pSampleRole = it->second;
			m_pThis->On_EnterWorld( pPacket->iDBID, pSampleRole );
		}	

		_ClearRoleData();
	}

	

}
//-------------------------------------------------------------------
void CNetCli_Login::OnPacket_CreateRoleResult		( int iServerID , int iSize , PVOID pData )
{
	M_PACKET( PG_Login_M2C_CreateRoleResult );

	if( pPacket->emResult == EM_CreateRole_OK )
	{
		SampleRoleData* pSampleRole = new SampleRoleData;
		*pSampleRole				= pPacket->SampleRole;

		m_pThis->m_mapSampleRole[ pPacket->iDBID ] = pSampleRole;

		int index = m_pThis->m_vecDBID.size();
		m_pThis->m_vecDBID.push_back( pPacket->iDBID );
	}

	m_pThis->On_CreateRoleResult( pPacket->emResult );
}
//-------------------------------------------------------------------
void CNetCli_Login::OnPacket_DeleteRoleResult		( int iServerID , int iSize , PVOID pData )
{
	M_PACKET( PG_Login_M2C_DeleteRoleResult );

	if( pPacket->emResult == EM_DeleteRole_OK )
	{
		for( vector< int >::iterator it = m_pThis->m_vecDBID.begin(); it != m_pThis->m_vecDBID.end(); it++ )
		{
			if( *it == pPacket->iDBID )
			{
				m_pThis->m_vecDBID.erase( it );
				break;
			}
		}

		map< int, SampleRoleData*>::iterator itRole = m_pThis->m_mapSampleRole.find( pPacket->iDBID );
		if( itRole != m_pThis->m_mapSampleRole.end() )
		{
			SampleRoleData* pSampleRole = itRole->second;
			m_pThis->m_mapSampleRole.erase( itRole );

			delete pSampleRole;
		}
	}

	m_pThis->On_DeleteRoleResult( pPacket->emResult );
}
//-------------------------------------------------------------------
void CNetCli_Login::OnPacket_EnterWorld				( int iServerID , int iSize , PVOID pData )
{
	M_PACKET( PG_Login_M2C_EnterWorld );
	//m_pThis->On_EnterWorld();
}
//-------------------------------------------------------------------
void CNetCli_Login::OnPacket_LeaveWorld				( int iServerID , int iSize , PVOID pData )
{
	M_PACKET( PG_Login_M2C_LeaveWorld );
	m_pThis->On_LeaveWorld( pPacket->iDBID );
}
//-------------------------------------------------------------------
void CNetCli_Login::CreateRole		( CreateRoleData* pCreateRoleData )
{
	PG_Login_C2M_CreateRole			Packet;

	Packet.iFieldIndex		= m_pThis->m_mapSampleRole.size();
	Packet.CreateRole		= *pCreateRoleData;

	SendToMaster( sizeof( PG_Login_C2M_CreateRole ), &Packet );
}
//-------------------------------------------------------------------
void CNetCli_Login::SelectRole		( int iFieldIndex )
{
	PG_Login_C2M_SelectRole ptSelectRole;

	if( m_pThis->m_vecDBID.size() > (size_t)iFieldIndex )
	{
		int iDBID = m_pThis->m_vecDBID[ iFieldIndex ];    
		ptSelectRole.iDBID	= iDBID;
	
		SendToMaster( sizeof( PG_Login_C2M_SelectRole ), &ptSelectRole );
	}
}
//-------------------------------------------------------------------
void CNetCli_Login::DeleteRole		( int iFieldIndex )
{
	PG_Login_C2M_DeleteRole Packet;

	if( m_pThis->m_vecDBID.size() > (size_t)iFieldIndex )
	{
		int iDBID = m_pThis->m_vecDBID[ iFieldIndex ];    
		Packet.iDBID	= iDBID;

		SendToMaster( sizeof( PG_Login_C2M_DeleteRole ), &Packet );
	}
}
//-------------------------------------------------------------------
bool CNetCli_Login::Login	( const char* szIP, int iPort, char* szAccount, char* szPassword )
{
	char szTempIP[255];
	strcpy( szTempIP, szIP );
	return CNetGlobal::NetConnect( szTempIP, iPort, szAccount, szPassword );
}

//-------------------------------------------------------------------
bool CNetCli_Login::Logout	()
{
	_ClearRoleData();
	return CNetGlobal::Logout();
}
//-------------------------------------------------------------------
void CNetCli_Login::RquestRoleData()
{
	// 依續把人物資料重新發送
	map< int , SampleRoleData* >::iterator it;
	SampleRoleData* pRole = NULL;

	int index = 0;

	for( it = m_pThis->m_mapSampleRole.begin(); it != m_pThis->m_mapSampleRole.end(); it++ )
	{
		pRole = it->second;
		m_pThis->On_SamepleRoleData( index, pRole );
		index++;
	}
	m_pThis->On_SamepleRoleDataEnd();
}


