// Asura_PC_SteamworksClient.h
// Thomas Young - 04.02.09
// Copyright (C) 2009 Rebellion, All Rights Reserved.

// F I L E   W R A P P E R ///////////////////////////////////////////////////

#ifdef ASURA_USE_PRAGMA_ONCE
#pragma once
#endif

#ifndef ___ASURA_STEAMWORKSCLIENT_H___
#define ___ASURA_STEAMWORKSCLIENT_H___ 1

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

#include "Core/Asura.h"

#ifdef ASURA_PC_SUPPORT_STEAMWORKS

#include "steam/steam_api.h"

// F O R W A R D   D E C L A R A T I O N S ///////////////////////////////////

enum ASURA_PC_STEAMWORKS_AUTH_LEVEL;

class Asura_NetPort;
class Asura_MemHeap;
class Asura_PC_NetSocketSteamworks;
class Asura_PC_Steamworks_AuthTicket;
class Asura_SC_PeerVoice;
class Asura_SC_VoicePeerSocket;

struct Asura_NetAddress;

template< class Asura_SC_VoicePeerSocket >			class Asura_List;
template< class Asura_PC_Steamworks_AuthTicket >	class Asura_List;
template< class Asura_SC_PeerVoice >				class Asura_List;
template< class LobbyEnter_t >						class Asura_PC_Steam_Callback;

// T Y P E D E F S ///////////////////////////////////////////////////////////

typedef void( *Asura_PC_SteamClient_Callback_ClientAccept ) ( const CSteamID xSteamID, void* pUserData );
typedef void( *Asura_PC_SteamClient_Callback_ClientReject ) ( const CSteamID xSteamID, const u_int uReason, void* pUserData );

// D E F I N E S /////////////////////////////////////////////////////////////

#define ASURA_PC_STEAM_MAX_STEAMID_LENGTH		128
#define ASURA_PC_STEAM_MAX_AUTH_BLOB_SIZE		2048
#define ASURA_PC_STEAM_MAX_AUTH_TICKET_SIZE		1024
#define ASURA_PC_STEAMWORKS_CLIENT_MAX_PEERS	64

// C L A S S   D E C L A R A T I O N S ///////////////////////////////////////

class Asura_PC_SteamworksClient
{
	friend class Asura_PC_Steamworks;
	friend class Asura_PC_SteamworksLobbyConnection;

	public:

		static CSteamID 				GetSteamID						( );
		static uint64					GetSteamID_Int64				( );
		static const Asura_UnicodeChar*	GetSteamIDText					( );

		static bool						IsFriend						( const CSteamID xSteamID );

		// Server interops
		
		static bool						IsConnectedToServer				( )		{ return s_bConnectedToServer;	}

		static bool						InitiateServerConnection		( const CSteamID xServerSteamID, const ASURA_PC_STEAMWORKS_AUTH_LEVEL eAuthLevel, const Asura_NetAddress xAddress, const unsigned short usPort );
		static void						TerminateServerConnection		( );

		static Asura_NetAddress			GetServerAddress				( );
		static unsigned short			GetServerPort					( );

		static bool						AcquireAuthBlob					( void* &pBlob, u_int &uBlobSize );
		static void						ReleaseAuthBlob					( );
		
		// Peer 2 Peer

		static bool						InitiateP2PConnection			( const CSteamID xSteamID );
		static void						TerminateP2PConnection			( const CSteamID xSteamID );

		static bool						HandleUserConnection			( const void* pAuthData, const u_int uAuthDataSize, CSteamID xSteamID );
		static void						HandleUserDisconnection			( const CSteamID xSteamID );

		static bool						GetAuthTicketForClient			( const CSteamID xSteamID, char* &pcAuthData, u_int &uAuthDataSize );
		static void						ClearAuthTicketForClient		( const CSteamID xSteamID );
		
		static void						RegisterCallback_ClientAccept	( Asura_PC_SteamClient_Callback_ClientAccept pfnCallback, void* pUserData );
		static void						RegisterCallback_ClientReject	( Asura_PC_SteamClient_Callback_ClientReject pfnCallback, void* pUserData );

		// Voice

		static void						StartVoiceRecording				( );
		static void						StopVoiceRecording				( );
		static bool						IsVoiceRecordingActive			( )		{ return s_bVoiceRecActive; }
		
		static void						SetVoicePlaybackEnabled			( const bool bAllowVoicePlayback )	{ s_bAllowVoicePlayback = bAllowVoicePlayback; }
		static bool						IsVoicePlaybackEnabled			( )									{ return s_bAllowVoicePlayback; }
		static bool						IsPeerVoicePlaying				( const CSteamID xSteamID );

		static void						SetVoiceSendList				( const CSteamID* pxPeerList, const u_int uListSize );
		
		static bool						IsMicrophoneAttached();

		// Remote storage

		static bool						WriteToRemoteStorage			( const void* pData, const u_int uDataSize, const char* szFilename );
		static bool						ReadFromRemoteStorage			( void* pData, const u_int uDataSize, const char* szFilename );
		static bool						RemoteStorageFileExists			( const char* szFilename );


	protected:

		static void						Initialise						( );	// called from Asura_PC_Steamworks
		static void 					Update							( );
		static void 					Shutdown						( );

		static void						ClearClientList					( );

	private:

		Asura_PC_SteamworksClient										( );
		~Asura_PC_SteamworksClient										( );

		// Voice

		static void						AudioVoiceBufferCallback		( void* pContext );
		static void						HandleMessage_VoiceData			( const void* pData, const u_int uDataSize, const CSteamID xSender );

		// Peer 2 Peer
		
		static void 					Callback_TicketResponse			( const ValidateAuthTicketResponse_t* const pxParam );
		
		static void 					RunCallback_AcceptClient		( const CSteamID xSteamID );
		static void 					RunCallback_RejectClient		( const CSteamID xSteamID, const u_int uReason );

		static 
		Asura_PC_Steamworks_AuthTicket*	FindAuthTicket					( const CSteamID xSteamID );

		

		static bool											s_bIsInitialised;

		static bool											s_bConnectedToServer;
		static Asura_NetAddress								s_xServerAddress;
		static unsigned short								s_usServerPort;

		static Asura_UnicodeChar							s_awcSteamIDText[ASURA_PC_STEAM_MAX_STEAMID_LENGTH];
		static char*										s_pcAuthBlob;
		static u_int										s_uAuthBlobSize;

		static bool											s_bVoiceRecActive;
		static bool											s_bMicrophoneAttached;
		static char*										s_pcVoiceRecData;
		static u_int										s_uVoiceRecDataSize;
		static float										s_fLastVoiceRecUpdate;

		static Asura_List< Asura_SC_VoicePeerSocket >		s_xVoicePeerSocketList;
		static SNetListenSocket_t							s_xVoiceListenSocket;
		static bool											s_bAllowVoicePlayback;
		static Asura_List< Asura_SC_PeerVoice >				s_xPeerVoices;
		static Asura_MemHeap*								s_pxVoicePlaybackMemHeap;
		static char*										s_pcVoicePlaybackMemory;

		static Asura_List<Asura_PC_Steamworks_AuthTicket>	s_xPeerAuthTicketList;		// A list of auth tickets we have aquired for peer connections

		static void*										s_pCallbackUserData_Accept;
		static void*										s_pCallbackUserData_Reject;

		static Asura_PC_SteamClient_Callback_ClientAccept	s_pfnAsuraCallback_ClientAccept;
		static Asura_PC_SteamClient_Callback_ClientReject	s_pfnAsuraCallback_ClientReject;

		static Asura_PC_Steam_Callback<	ValidateAuthTicketResponse_t >	s_xCallback_TicketResponse;

};

#endif	// ASURA_PC_SUPPORT_STEAMWORKS

#endif	//___ASURA_STEAMWORKSCLIENT_H___

//eof
