//////////////////////////////////////////////////////////////////////////////
//
// Asura_Platform_NetLinkManager.cpp
//
// Copyright (c) 2007 Rebellion. All rights reserved.
//
// Description:
//
//     PC implementation of network link class functions.
//
//////////////////////////////////////////////////////////////////////////////

// I N C L U D E S ///////////////////////////////////////////////////////////

#include "Core/Asura.h"

#include "Network/Asura_NetLinkManager.h"

#ifdef ASURA_PC_SUPPORT_STEAMWORKS
#include "Network/Steamworks/Asura_PC_NetSocketSteamworks.h"
#include "Network/Steamworks/Asura_PC_Steamworks.h"
#endif

#ifdef ASURA_SUPPORT_LOBBY
#include "Network/Online/Asura_Lobby.h"
#endif //#ifdef ASURA_SUPPORT_LOBBY

#ifdef ASURA_SUPPORT_EXTERNAL_NETWORK

#include <winsock.h>

enum ASURA_PC_NETLINK_PHASE
{
	ASURA_PC_NETLINK_PHASE_IDLE,
	ASURA_PC_NETLINK_PHASE_STARTING,

#ifdef ASURA_SUPPORT_LOBBY
	ASURA_PC_NETLINK_PHASE_GAMECOMPLETION_START,
	ASURA_PC_NETLINK_PHASE_GAMECOMPLETION_UPDATE,
#endif //ASURA_SUPPORT_LOBBY
};


static ASURA_PC_NETLINK_PHASE g_eNetlinkPhase = ASURA_PC_NETLINK_PHASE_IDLE;


// Platform_UpdateXXX() functions should call FinishedPlatformXXX() when finished.
void Asura_NetLinkManager::Platform_UpdateConnecting()
{
	switch (g_eNetlinkPhase)
	{
	case ASURA_PC_NETLINK_PHASE_STARTING:
		{
			// Check steam works
			#ifdef ASURA_PC_SUPPORT_STEAMWORKS
			if(		Asura_PC_Steamworks::IsSteamworksEnabled() 
				&&	!Asura_PC_NetSocketSteamworks::Initialise()	)
			{
				g_eNetlinkPhase = ASURA_PC_NETLINK_PHASE_IDLE;
				FinishedPlatformConnecting(false);
				return;
			}
			#endif // ASURA_PC_SUPPORT_STEAMWORKS
			
			#ifdef ASURA_SUPPORT_LOBBY
			g_eNetlinkPhase = ASURA_PC_NETLINK_PHASE_GAMECOMPLETION_START;
			#else
			g_eNetlinkPhase = ASURA_PC_NETLINK_PHASE_IDLE;
			FinishedPlatformConnecting(true);
			#endif //#ifdef ASURA_SUPPORT_LOBBY
			break;
		}

#ifdef ASURA_SUPPORT_LOBBY
	case ASURA_PC_NETLINK_PHASE_GAMECOMPLETION_START:
		{
			Asura_Lobby_System::GetGameCompletionHistory().Platform_GetData_Start();
			g_eNetlinkPhase = ASURA_PC_NETLINK_PHASE_GAMECOMPLETION_UPDATE;
			break;
		}

	case ASURA_PC_NETLINK_PHASE_GAMECOMPLETION_UPDATE:
		{
			if (Asura_Lobby_System::GetGameCompletionHistory().Platform_GetData_Update())
			{
				g_eNetlinkPhase = ASURA_PC_NETLINK_PHASE_IDLE;
				FinishedPlatformConnecting(true);
			}
			break;
		}
#endif //#ifdef ASURA_SUPPORT_LOBBY

	default:
		{
			// Unexpected state, abort.
			Asura_Assert(false, "unexpected connection phase");
			g_eNetlinkPhase = ASURA_PC_NETLINK_PHASE_IDLE;
			FinishedPlatformConnecting(false);
			break;
		}
	}
}

void Asura_NetLinkManager::Platform_UpdateConnecting_PostRender()
{
}

void Asura_NetLinkManager::Platform_UpdateDisconnecting()
{
#ifdef ASURA_PC_SUPPORT_STEAMWORKS
	if( !Asura_PC_Steamworks::IsSteamworksEnabled() )
#endif
	{
		WSACleanup();
	}
	FinishedPlatformDisconnecting();
}

void Asura_NetLinkManager::Platform_UpdateConnected()
{
}

// Platform_InitiateConnection() function should return true iff requested operation was started.
bool Asura_NetLinkManager::Platform_InitiateConnection()
{
	g_eNetlinkPhase = ASURA_PC_NETLINK_PHASE_STARTING;
#ifdef ASURA_PC_SUPPORT_STEAMWORKS
	if( !Asura_PC_Steamworks::IsSteamworksEnabled() )
#endif
	{
		WSADATA data;
		WSAStartup(0x0101, &data);
	}
	return true;
}

void Asura_NetLinkManager::Platform_InitiateDisconnection()
{
}


#endif // #ifdef ASURA_SUPPORT_EXTERNAL_NETWORK

