#include "NetGlobal.h"
#include "../NetWaker_Member/NetWakerClientInc.h"
#include "../Interface/interface.h"
#include "../Interface/WorldFrames/TradeFrame.h"

bool						CNetGlobal::_IsInitial				= false;
CliNetWaker					CNetGlobal::_Net;
ServerID					CNetGlobal::_ServerNetID;
FunctionSchedularClass*     CNetGlobal::_Schedular = NULL;
RoleDataEx					CNetGlobal::_RoleData;                     //¨¤¦βΈκ®Ζ

void CNetGlobal::Initial(const char* db_filename)
{    
	_IsInitial = true;
    _Schedular = FunctionSchedularClass::New();

	ObjectDataClass::Init(db_filename);

	Net_GSrvConnectChild::_Init();
	CNetCli_Login_Child::_Init();
	NetCli_AttackChild::_Init();	
	NetCli_EQChild::_Init();
	NetCli_ItemChild::_Init();
	NetCli_MagicChild::_Init();
	NetCli_MoveChild::_Init();
	NetCli_RoleValueChild::_Init();
	NetCli_ShopChild::_Init();
	NetCli_TalkChild::_Init();
	NetCli_TradeChild::_Init();
	CNetCli_Script_Child::Init();
	NetCli_PartyChild::_Init();
	NetCli_ChannelChild::_Init();
	NetCli_FriendListChild::_Init();
	NetCli_MailChild::_Init();
	NetCli_ACChild::_Init();
	NetCli_RecipeChild::_Init();
	NetCli_GuildChild::_Init();

    //Net_MoveChild::Init();
    //NetCli_TalkChild::Init();
    //;

	RoleDataEx::St_GetObjDB	    = GetObj;
	RoleDataEx::St_UpDateEvent  = St_UpDateEvent;
	

	// CNetScript::Init();
}

void CNetGlobal::Release()
{
	if ( _IsInitial )
	{
		ObjectDataClass::Release();

		Net_GSrvConnectChild::_Release();
		CNetCli_Login_Child::_Release();
		NetCli_EQChild::_Release();
		NetCli_ItemChild::_Release();
		NetCli_MagicChild::_Release();
		NetCli_MoveChild::_Release();
		NetCli_RoleValueChild::_Release();
		NetCli_ShopChild::_Release();
		NetCli_TalkChild::_Release();
		NetCli_TradeChild::_Release();
		CNetCli_Script_Child::Release();
		NetCli_PartyChild::_Release();
		NetCli_ChannelChild::_Release();
		NetCli_FriendListChild::_Release();
		NetCli_MailChild::_Release();
		NetCli_ACChild::_Release();
		NetCli_RecipeChild::_Release();
		NetCli_GuildChild::_Release();
		_Schedular->Del();
	}
}

void CNetGlobal::Process( FLOAT frameTime, FLOAT elapsedTime )
{
    _Net.Process();
    _Schedular->Process( );
	_RoleData.OnTimeProc();

	NetCli_MagicChild::_Update(elapsedTime);
}
//--------------------------------------------------------------------------------------
FunctionSchedularClass* CNetGlobal::Schedular( )
{
    return _Schedular;
}
//--------------------------------------------------------------------------------------
void CNetGlobal::St_UpDateEvent(RoleDataEx *Role , RoleUpdateEvent Event)
{	
	if ( Event.Money )
	{
		g_pInterface->GetUiWorld()->SendEvent( UiEvent_Role_Money_Change );
	}
	if( Event.Bank)
	{
		//g_pInterface->GetUi()->SendEvent()
		//CFrame::SendEventString( _Def_UIEvent_OnBankUpDate_, 0, 0 );
	}
	if( Event.Body )
	{

		g_pInterface->GetUiWorld()->SendEvent( UiEvent_BagListUpdate );
		g_pTradeFrame->Refresh();
		/*
		CFrame::SendEventString( _Def_UIEvent_OnItemUpDate_, 0, 0 );
		if( CUI_Trade::IsVisible()  ) 
		{
			CUI_Trade::UpDateMyTardeItemInfo( );
		}
		*/
	}

	if( Event.Eq )
	{
		//CFrame::SendEventString( _Def_UIEvent_OnEQUpDate_, 0, 0 );
	}

	if( Event.Trade || Event.Body )
	{
		/*
		if( CUI_Trade::IsVisible() )
		{
			CFrame::SendEventString( _Def_UIEvent_OnTradeUpDate_, 0, 0 );
		} 
		*/
	}

	if( Event.HPMPSP )
	{
		g_pInterface->GetUiWorld()->SendEvent( UiEvent_RoleStatusChange );
	}


	if( Event.ExpChange )
	{
		g_pInterface->GetUiWorld()->SendEvent( UiEvent_Role_Exp_Change );
	}


	if( Event.Recalculate )
	{
		g_pInterface->GetUiWorld()->SendEvent( UiEvent_RoleAttributeChange );
	}

	if( Event.ReSetTalbe )
	{
		g_pInterface->GetUiWorld()->SendEvent( UiEvent_RoleTableChange );
	}

	if( Event.LvChange )
	{
		g_pInterface->GetUiWorld()->SendEvent( UiEvent_RoleLVChange );
	}

}
//----------------------------------------------------------------------------	----------
bool   CNetGlobal::SendToLocal( int Size , void* Data )
{
    if( _ServerNetID.m_iLocal == -1 )
        return false;

    _Net.SendToSrv( _ServerNetID.m_iLocal , Size , Data );
    return true;
}
//--------------------------------------------------------------------------------------
bool   CNetGlobal::SendToMaster( int iSize , void* pData )
{
    if( _ServerNetID.m_iMaster == -1 ) 
        return false;

    return _Net.SendToSrv( _ServerNetID.m_iMaster , iSize, pData );
}
//--------------------------------------------------------------------------------------
bool   CNetGlobal::SendToChat( int iSize , void* pData )
{
	if( _ServerNetID.m_iChat == -1 ) 
		return false;

	return _Net.SendToSrv( _ServerNetID.m_iChat , iSize, pData );
}
//--------------------------------------------------------------------------------------
bool     CNetGlobal::NetConnect( char* IP , int Port , char* Account , char* Password )
{
    _Net.Connect( IP , Port , Account , Password );

    return true;
}

void     CNetGlobal::Print( int iLV, char* format,... )
{
	static char buf[1024];
	va_list args;
	va_start( args, format );
	vsprintf( buf, format, args );
	va_end( args);
	OutputDebugString( buf);
	printf("%s", buf);
}
//--------------------------------------------------------------------------------------
bool     CNetGlobal::Logout( )
{
    _Net.Logout();
    return true;
}
//--------------------------------------------------------------------------------------