///////////////////////////////////////////////////////////////////////////////
//
// File      : Asura_Platform_Sound.cpp
// Author    : Philip Hall (reworked)
// Created   : 03/10/2007
//
// Copyright (C) 2007 Rebellion. All Rights Reserved
//
///////////////////////////////////////////////////////////////////////////////

#include "Core/Asura.h"

#include "Sound/Asura_StreamingBackgroundSound_System.h"

#ifndef ASURA_USE_AUDIO_SYSTEM

// Asura headers
#include "Sound/Asura_Sound.h"
#include "Debugging/Asura_Assert.h"
#include "Debugging/Asura_LogFile.h"
#include "FileAccess/Asura_DebugFileStream.h"
#include "FileAccess/Asura_FileAccess.h"


// Asura PS3 headers
#include "Sound/Asura_PS3_Sound_System.h"
#include "Sound/Asura_PS3_ReplaceableSounds.h"



int Asura_Sound_System::PlatformInitialise(void)
{
	s_bUseHardware3dPan = false;	// Simulated

	if ( !Asura_PS3_Sound_System::GetAPI().Initialise() )
	{
		// Asura_PS3_Sound_System::GetAPI().Project_HandleInitialisationFailure();
	}

	return Asura_PS3_Sound_System::GetAPI().GetMaxConcurrentSounds();
}


void Asura_Sound_System::PlatformShutdown(void)
{
	Asura_PS3_ReplaceableSound_System::GetAPI().Shutdown();
	Asura_PS3_Sound_System::GetAPI().Shutdown();
}


void Asura_Sound_System::PlatformReset(void)
{
	Asura_PS3_Sound_System::GetAPI().Reset();
	Asura_PS3_Sound_System::GetStreamingAPI().Reset();
}


void Asura_Sound_System::PlatformSetResetPoint(void)
{
	Asura_PS3_Sound_System::GetAPI().SetResetPoint();
}


void Asura_Sound_System::PlatformUpdate()
{
	Asura_PS3_Sound_System::GetAPI().SetListener( s_axSoundCentre[0], s_axSoundOrientation[0] );
	Asura_PS3_Sound_System::GetAPI().Update();
    Asura_PS3_Sound_System::GetStreamingAPI().Update();
}


bool Asura_Sound_System::PlatformLoadSound( const char* szFilename, const int iLoadedSoundID)
{
	return Asura_PS3_Sound_System::GetAPI().LoadSound( szFilename, iLoadedSoundID );
}


bool Asura_Sound_System::PlatformLoadSoundFromResource(Asura_Handle iFileHandle, int iFileSize, int iLoadedSoundID)
{
	return Asura_PS3_Sound_System::GetAPI().LoadSoundFromResource(iFileHandle, iFileSize, iLoadedSoundID);
}


bool Asura_Sound_System::PlatformUnloadSound(const int iLoadedSoundID)
{
	return Asura_PS3_Sound_System::GetAPI().UnloadSound( iLoadedSoundID );
}


bool Asura_Sound_System::PlatformCreateSound(int iLoadedSoundID, Asura_Handle iPlayingSoundHandle, bool bUseEQ, bool bUseHardware3dPan)
{
	return Asura_PS3_Sound_System::GetAPI().CreateSound( iLoadedSoundID, iPlayingSoundHandle, bUseEQ, bUseHardware3dPan );
}


bool Asura_Sound_System::PlatformPlaySound(Asura_Handle iPlayingSoundHandle, bool bLooping)
{
	Asura_Assert( Asura_Sound_System::IsValidPlayingSoundHandle(iPlayingSoundHandle), "Invalid playing sound handle" );
	return Asura_PS3_Sound_System::GetAPI().PlaySound( iPlayingSoundHandle, bLooping );
}


bool Asura_Sound_System::PlatformStopSound( Asura_Handle iPlayingSoundHandle )
{
	Asura_Assert( Asura_Sound_System::IsValidPlayingSoundHandle(iPlayingSoundHandle), "Invalid playing sound handle" );
	return Asura_PS3_Sound_System::GetAPI().StopSound( iPlayingSoundHandle );
}


bool Asura_Sound_System::PlatformDestroySound(Asura_Handle iPlayingSoundHandle)
{
	Asura_Assert( Asura_Sound_System::IsValidPlayingSoundHandle(iPlayingSoundHandle), "Invalid playing sound handle" );
	return Asura_PS3_Sound_System::GetAPI().DestroySound( iPlayingSoundHandle );
}

bool Asura_Sound_System::PlatformSoundIsPlaying(Asura_Handle iPlayingSoundHandle)
{
	Asura_Assert( Asura_Sound_System::IsValidPlayingSoundHandle(iPlayingSoundHandle), "Invalid playing sound handle" );
	return Asura_PS3_Sound_System::GetAPI().IsSoundPlaying( iPlayingSoundHandle );
}

bool Asura_Sound_System::PlatformSoundIsPreparing(Asura_Handle iPlayingSoundHandle)
{
	Asura_Assert( Asura_Sound_System::IsValidPlayingSoundHandle(iPlayingSoundHandle), "Invalid playing sound handle" );
	return Asura_PS3_Sound_System::GetAPI().IsSoundPreparing( iPlayingSoundHandle );
}


bool Asura_Sound_System::PlatformPauseSound(Asura_Handle iPlayingSoundHandle)
{
	Asura_Assert( Asura_Sound_System::IsValidPlayingSoundHandle(iPlayingSoundHandle), "Invalid playing sound handle" );
	return Asura_PS3_Sound_System::GetAPI().PauseSound( iPlayingSoundHandle );
}


bool Asura_Sound_System::PlatformUnpauseSound(Asura_Handle iPlayingSoundHandle)
{
	Asura_Assert( Asura_Sound_System::IsValidPlayingSoundHandle(iPlayingSoundHandle), "Invalid playing sound handle" );
	return Asura_PS3_Sound_System::GetAPI().UnpauseSound( iPlayingSoundHandle );
}


bool Asura_Sound_System::PlatformSetSoundVolumePanAndFade(Asura_Handle iPlayingSoundHandle, float fVolume, float fPan, float fFade)
{
	Asura_Assert( Asura_Sound_System::IsValidPlayingSoundHandle(iPlayingSoundHandle), "Invalid playing sound handle" );
	return Asura_PS3_Sound_System::GetAPI().SetVolumePanAndFade( iPlayingSoundHandle, fVolume, fPan, fFade );
}

bool Asura_Sound_System::Platform_SetSoundWetDryVolume(Asura_Handle iPlayingSoundHandle, float fWetVolume, float fDryVolume)
{
	Asura_Assert( Asura_Sound_System::IsValidPlayingSoundHandle(iPlayingSoundHandle), "Invalid playing sound handle" );
	return Asura_PS3_Sound_System::GetAPI().SetWetDryVolume(iPlayingSoundHandle, fWetVolume, fDryVolume);
}

bool Asura_Sound_System::Platform_SetOcclusionLPF(Asura_Handle iPlayingSoundHandle, float fOcclusion)
{
	Asura_Assert( Asura_Sound_System::IsValidPlayingSoundHandle(iPlayingSoundHandle), "Invalid playing sound handle" );
	Asura_PS3_Sound_System::GetAPI().SetOcclusionLPF(iPlayingSoundHandle, fOcclusion);
	return true;
}

bool Asura_Sound_System::PlatformSetSoundPitch(Asura_Handle iPlayingSoundHandle, float fPitch)
{
	return Asura_PS3_Sound_System::GetAPI().SetPitch( iPlayingSoundHandle, fPitch );		
}


bool Asura_Sound_System::Platform_SetSoundUsesReverb( Asura_Handle iPlayingSoundHandle, bool bUseReverb )
{
	Asura_Assert( Asura_Sound_System::IsValidPlayingSoundHandle(iPlayingSoundHandle), "Invalid playing sound handle" );
	return Asura_PS3_Sound_System::GetAPI().SetUseReverb( iPlayingSoundHandle, bUseReverb );
}


bool Asura_Sound_System::Platform_SetSoundUsesDistanceFilter( Asura_Handle iPlayingSoundHandle, bool bUseFilter )
{
	Asura_Assert( Asura_Sound_System::IsValidPlayingSoundHandle(iPlayingSoundHandle), "Invalid playing sound handle" );
	return Asura_PS3_Sound_System::GetAPI().SetUseDistanceFilter( iPlayingSoundHandle, bUseFilter );
}


////PGH////////////////////////////////////////////////////////////////////////
// Function    : Asura_Sound_System::Platform_SetReverb
// Description : Set global reverb type
//
// Parameters  : 
//   enum ReverbType eType - Global reverb type (applies to all sounds)
//
// Returns     : nothing
///////////////////////////////////////////////////////////////////////////////
void Asura_Sound_System::Platform_SetReverb(enum ReverbType eType, float fReverbAmount)
{
	Asura_PS3_Sound_System::GetAPI().SetReverb( eType, 1.0f, fReverbAmount );
}

bool Asura_Sound_System::PlatformLoadReplaceableSound(const int iLoadedSoundID)
{
	return Asura_PS3_ReplaceableSound_System::GetAPI().LoadReplaceableSound( iLoadedSoundID );
}


bool Asura_Sound_System::PlatformReplaceSound(const int iExistingSoundLoadedID, const int iNewSoundLoadedID)
{
	return Asura_PS3_ReplaceableSound_System::GetAPI().ReplaceSound( iExistingSoundLoadedID, iNewSoundLoadedID );
}


int Asura_Sound_System::Platform_GetMaxActiveReplaceableSounds()
{
	return Asura_PS3_ReplaceableSound_System::GetAPI().GetMaxActiveReplaceableSounds();
}


int Asura_Sound_System::Platform_GetReplaceableSoundSize(const int iLoadedSoundID)
{
    return Asura_PS3_ReplaceableSound_System::GetAPI().GetReplaceableSoundSize(iLoadedSoundID);
}


////PGH////////////////////////////////////////////////////////////////////////
// Function    : Asura_Sound_System::Platform_CreateStreamingSound
// Description : Create a streaming sound
//
// Parameters  : 
//   Asura_Handle iHandle     - Sound handle
//   const char* pszFilename  - Source filename
//
// Returns     : bool - 
///////////////////////////////////////////////////////////////////////////////
bool Asura_Sound_System::Platform_CreateStreamingSound(Asura_Handle iHandle, const char* pszFilename)
{
    return Asura_PS3_Sound_System::GetStreamingAPI().Create( iHandle, pszFilename);
}

void Asura_Sound_System::Platform_PlayStreamingSound(Asura_Handle iHandle, bool bLooping, bool bPreload)
{
	Asura_PS3_Sound_System::GetStreamingAPI().Play( iHandle, bLooping, bPreload );
}


void Asura_Sound_System::Platform_StopStreamingSound(Asura_Handle iHandle)
{
    Asura_PS3_Sound_System::GetStreamingAPI().Stop( iHandle );
}


void Asura_Sound_System::Platform_PauseStreamingSound(Asura_Handle iHandle)
{
	Asura_PS3_Sound_System::GetStreamingAPI().Pause( iHandle );
}


void Asura_Sound_System::Platform_ResumeStreamingSound(Asura_Handle iHandle)
{
	Asura_PS3_Sound_System::GetStreamingAPI().Resume( iHandle );
}


void Asura_Sound_System::Platform_ReleaseStreamingSound(Asura_Handle iHandle)
{
    Asura_PS3_Sound_System::GetStreamingAPI().Release( iHandle );
}


bool Asura_Sound_System::Platform_StreamingSoundIsReady(Asura_Handle iHandle)
{	
    return Asura_PS3_Sound_System::GetStreamingAPI().IsSoundReady( iHandle );
}


void Asura_Sound_System::Platform_StartPreloadedStreamingSound(Asura_Handle iHandle)
{
	
}


void Asura_Sound_System::Platform_SetStreamingSoundOutput(Asura_Handle iHandle, SoundOutputType eOutputType)
{
}


bool Asura_Sound_System::Platform_StreamingSoundIsFinished(Asura_Handle iHandle)
{ 
	return Asura_PS3_Sound_System::GetStreamingAPI().IsFinished( iHandle );
}


float Asura_Sound_System::Platform_GetStreamingSoundPosition(Asura_Handle iHandle)
{ 
	return Asura_PS3_Sound_System::GetStreamingAPI().GetPosition( iHandle );
}


float Asura_Sound_System::Platform_GetStreamingSoundLength(Asura_Handle iHandle)
{ 
	return Asura_PS3_Sound_System::GetStreamingAPI().GetLength( iHandle );
}


int Asura_Sound_System::Platform_GetMaxStreamingSounds()
{ 
	return Asura_PS3_Sound_System::GetStreamingAPI().GetMaxStreamingSounds();
}


void Asura_Sound_System::Platform_EnsureAllStreamingSoundsAreStopped()
{
}


void Asura_Sound_System::Platform_SetStreamingSoundVolume(Asura_Handle iHandle, float fVolume)
{
	Asura_PS3_Sound_System::GetStreamingAPI().SetVolume(iHandle, fVolume);
}


int Asura_Sound_System::Platform_GetMaxSoundsWithEQ(void)
{
	return Asura_PS3_Sound_System::GetAPI().GetMaxConcurrentSoundsWithEQ();
}


void Asura_Sound_System::Platform_SetEQType(Asura_Handle iPlayingSoundHandle, EQType eType, float fLevel)
{
	return Asura_PS3_Sound_System::GetAPI().SetEQType(iPlayingSoundHandle, eType, fLevel);
}


bool Asura_Sound_System::Platform_HardwarePanSetSoundVolume(Asura_Handle iPlayingSoundHandle, float fVolume)
{
	return Asura_PS3_Sound_System::GetAPI().SetVolume(iPlayingSoundHandle, fVolume);
}


bool Asura_Sound_System::Platform_HardwarePanSetSoundPosition(Asura_Handle iPlayingSoundHandle, const Asura_Vector_3& xPosition)
{
	return Asura_PS3_Sound_System::GetAPI().SetPosition(iPlayingSoundHandle, xPosition);
}


bool Asura_Sound_System::Platform_HardwarePanSetSoundSpatial(Asura_Handle iPlayingSoundHandle, bool bIsSpatial)
{
	return Asura_PS3_Sound_System::GetAPI().SetSpatial( iPlayingSoundHandle, bIsSpatial );
}


bool Asura_Sound_System::Platform_HardwarePanSetSoundRadii(Asura_Handle iPlayingSoundHandle, float fInner, float fOuter)
{
	return Asura_PS3_Sound_System::GetAPI().SetRadii( iPlayingSoundHandle, fInner, fOuter );
}


bool Asura_Sound_System::Platform_HardwarePanSetSoundForcedPanAndFade(Asura_Handle iPlayingSoundHandle, float fPan, float fFade)
{
	return Asura_PS3_Sound_System::GetAPI().SetPanAndFade( iPlayingSoundHandle, fPan, fFade );	
}

bool Asura_Sound_System::Platform_SetSoundDistanceFilterGain(Asura_Handle iPlayingSoundHandle, float fNormalisedGain)
{
	Asura_PS3_Sound_System::GetAPI().UpdateDistanceFilter(iPlayingSoundHandle, fNormalisedGain);
	return true;
}

#endif // #ifndef ASURA_USE_AUDIO_SYSTEM
