//
// Asura_Platform_Sound.cpp
//
// Created by KOR - 10 October 2006
//
// Copyright (C) 2006 Rebellion, All Rights Reserved.
//

#include "Core/Asura.h"

// This file's header.
#include "Sound/Asura_Sound.h"

// Asura Wii
#include "Sound/Asura_Wii_StreamSound.h"
#include "Sound/Asura_Wii_Sound.h"

int Asura_Sound_System::PlatformInitialise(void)
{
	return Asura_Wii_Sound::Initialise();
}

void Asura_Sound_System::PlatformShutdown(void)
{
	Asura_Wii_Sound::Shutdown();
}

void Asura_Sound_System::PlatformReset(void)
{
	Asura_Wii_Sound::Reset();
}

void Asura_Sound_System::PlatformSetResetPoint(void)
{
	Asura_Wii_Sound::SetResetPoint();
}

bool Asura_Sound_System::PlatformLoadSound( const char* szFilename, int iIndex)
{
	return Asura_Wii_Sound::LoadSound(szFilename, iIndex);
}

bool Asura_Sound_System::PlatformLoadSoundFromResource(Asura_Handle iFileHandle, int iFileSize, int iLoadedSoundIndex)
{
	return Asura_Wii_Sound::LoadSoundFromResource(iFileHandle, iFileSize, iLoadedSoundIndex);
}

bool Asura_Sound_System::PlatformUnloadSound(const int iLoadedSoundIndex)
{
	return Asura_Wii_Sound::UnloadSound(iLoadedSoundIndex);
}

// Replaceable sounds are always available on Wii, as they are streamed.
bool Asura_Sound_System::PlatformLoadReplaceableSound(const int iLoadedSoundIndex)
{
	return true;
}

bool Asura_Sound_System::PlatformReplaceSound(const int iExistingSoundLoadedID, const int iNewSoundLoadedID)
{
	return true;
}

int Asura_Sound_System::Platform_GetMaxActiveReplaceableSounds()
{
	return 2;
}

int Asura_Sound_System::Platform_GetReplaceableSoundSize(int iSoundIndex)
{
	return 0;
}

bool Asura_Sound_System::PlatformCreateSound(int iLoadedSoundIndex, Asura_Handle iSoundHandle, bool bUseEQ, bool bUseHardware3dPan)
{
	return Asura_Wii_Sound::CreateSound(iLoadedSoundIndex, iSoundHandle, bUseEQ, bUseHardware3dPan);
}

bool Asura_Sound_System::PlatformPlaySound(Asura_Handle iSoundHandle, bool bLooping)
{
	return Asura_Wii_Sound::PlaySound(iSoundHandle, bLooping);
}

bool Asura_Sound_System::PlatformStopSound(Asura_Handle iSoundHandle)
{
	return Asura_Wii_Sound::StopSound(iSoundHandle);
}

bool Asura_Sound_System::PlatformDestroySound(Asura_Handle iSoundHandle)
{
	return Asura_Wii_Sound::DestroySound(iSoundHandle);
}

void Asura_Sound_System::PlatformUpdate()
{
	Asura_Wii_Sound::Update();
}

bool Asura_Sound_System::PlatformSoundIsPlaying(Asura_Handle iSoundHandle)
{
	return Asura_Wii_Sound::IsSoundPlaying(iSoundHandle);
}

bool Asura_Sound_System::PlatformSoundIsPreparing( Asura_Handle iPlayingSoundHandle )
{
	return false;
}

bool Asura_Sound_System::PlatformSetSoundVolumePanAndFade(Asura_Handle iSoundHandle, float fVolume, float fPan, float fFade)
{
	return Asura_Wii_Sound::SetSoundVolumePanAndFade(iSoundHandle, fVolume, fPan, fFade);
}

bool Asura_Sound_System::Platform_SetSoundWetDryVolume(Asura_Handle iPlayingSoundHandle, float fWetVolume, float fDryVolume)
{
	//TODO: Write me please
	return true;
}

bool Asura_Sound_System::Platform_SetOcclusionLPF(const Asura_Handle iPlayingSoundHandle, const float fOcclusion)
{
	//TODO: Write me please
	return true;
}

bool Asura_Sound_System::PlatformSetSoundPitch(Asura_Handle iSoundHandle, float fPitch)
{
	return Asura_Wii_Sound::SetSoundPitch(iSoundHandle, fPitch);
}

bool Asura_Sound_System::PlatformPauseSound(Asura_Handle iSoundHandle)
{
	return Asura_Wii_Sound::PauseSound(iSoundHandle);
}

bool Asura_Sound_System::PlatformUnpauseSound(Asura_Handle iSoundHandle)
{
	return Asura_Wii_Sound::UnpauseSound(iSoundHandle);
}

int Asura_Sound_System::Platform_GetMaxSoundsWithEQ(void)
{
	// return number of voices which may have EQ applied. Generally this 
	// will be the same return value as PlatformInitialise() but on PS2 only
	// half the voices can have EQ.
	return 0;
}

void Asura_Sound_System::Platform_SetEQType(Asura_Handle iHandle, EQType eType, float fLevel)
{
}

void Asura_Sound_System::Platform_SetReverb(enum ReverbType eType, float)
{
	// todo
}

// Hardware panning is currently PC-only (it's the only way under DirectSound to access surround speakers)
// All other platforms allow us to specify speaker volumes directly (or if using Dolby PL2 have some concept of front and back as well as pan).
bool Asura_Sound_System::Platform_HardwarePanSetSoundVolume(Asura_Handle iSoundHandle, float fVolume)
{
	Asura_Assert(false, "this platform did not expect to use hardware 3d pan");
	return true;
}

bool Asura_Sound_System::Platform_HardwarePanSetSoundPosition(Asura_Handle iSoundHandle, const Asura_Vector_3& xPosition)
{
	Asura_Assert(false, "this platform did not expect to use hardware 3d pan");
	return true;
}

bool Asura_Sound_System::Platform_HardwarePanSetSoundSpatial(Asura_Handle iSoundHandle, bool bIsSpatial)
{
	Asura_Assert(false, "this platform did not expect to use hardware 3d pan");
	return true;
}

bool Asura_Sound_System::Platform_HardwarePanSetSoundRadii(Asura_Handle iSoundHandle, float fInner, float fOuter)
{
	Asura_Assert(false, "this platform did not expect to use hardware 3d pan");
	return true;
}

bool Asura_Sound_System::Platform_HardwarePanSetSoundForcedPanAndFade(Asura_Handle iSoundHandle, float fPan, float fFade)
{
	Asura_Assert(false, "this platform did not expect to use hardware 3d pan");
	return true;
}

// S T R E A M I N G   S O U N D S ///////////////////////////////////////////

bool Asura_Sound_System::Platform_CreateStreamingSound(Asura_Handle iHandle, const char* pszFilename)
{
    Asura_Assert(iHandle >= 0 && iHandle < ASURA_WII_MAX_CONCURRENT_SOUNDSTREAMS, "invalid handle");
    if (iHandle < 0 || iHandle >= ASURA_WII_MAX_CONCURRENT_SOUNDSTREAMS) return false;
	
    return Asura_Wii_Streaming_Sound_System::Create(iHandle, pszFilename);
}


void Asura_Sound_System::Platform_PlayStreamingSound(Asura_Handle iHandle, bool bLooping, bool bPreload)
{
    Asura_Assert(iHandle >= 0 && iHandle < ASURA_WII_MAX_CONCURRENT_SOUNDSTREAMS, "invalid handle");
    if (iHandle < 0 || iHandle >= ASURA_WII_MAX_CONCURRENT_SOUNDSTREAMS) return;
	
    Asura_Wii_Streaming_Sound_System::Play(iHandle, bLooping);
}


void Asura_Sound_System::Platform_StopStreamingSound(Asura_Handle iHandle)
{
    Asura_Assert(iHandle >= 0 && iHandle < ASURA_WII_MAX_CONCURRENT_SOUNDSTREAMS, "invalid handle");
    if (iHandle < 0 || iHandle >= ASURA_WII_MAX_CONCURRENT_SOUNDSTREAMS) return;
	
    Asura_Wii_Streaming_Sound_System::Stop(iHandle);
}


void Asura_Sound_System::Platform_PauseStreamingSound(Asura_Handle iHandle)
{
    Asura_Assert(iHandle >= 0 && iHandle < ASURA_WII_MAX_CONCURRENT_SOUNDSTREAMS, "invalid handle");
    if (iHandle < 0 || iHandle >= ASURA_WII_MAX_CONCURRENT_SOUNDSTREAMS) return;
    
    Asura_Wii_Streaming_Sound_System::Pause(iHandle);
}


void Asura_Sound_System::Platform_ResumeStreamingSound(Asura_Handle iHandle)
{
    Asura_Assert(iHandle >= 0 && iHandle < ASURA_WII_MAX_CONCURRENT_SOUNDSTREAMS, "invalid handle");
    if (iHandle < 0 || iHandle >= ASURA_WII_MAX_CONCURRENT_SOUNDSTREAMS) return;
    
    Asura_Wii_Streaming_Sound_System::Resume(iHandle);
}


void Asura_Sound_System::Platform_ReleaseStreamingSound(Asura_Handle iHandle)
{
    Asura_Assert(iHandle >= 0 && iHandle < ASURA_WII_MAX_CONCURRENT_SOUNDSTREAMS, "invalid handle");
    if (iHandle < 0 || iHandle >= ASURA_WII_MAX_CONCURRENT_SOUNDSTREAMS) return;
	
    Asura_Wii_Streaming_Sound_System::Release(iHandle);
}


bool Asura_Sound_System::Platform_StreamingSoundIsReady(Asura_Handle iHandle)
{
    Asura_Assert(iHandle >= 0 && iHandle < ASURA_WII_MAX_CONCURRENT_SOUNDSTREAMS, "invalid handle");
    if (iHandle < 0 || iHandle >= ASURA_WII_MAX_CONCURRENT_SOUNDSTREAMS) return true;    // don't block.
    // (MH - 30.11.2002) todo
    return true;
}


void Asura_Sound_System::Platform_StartPreloadedStreamingSound(Asura_Handle iHandle)
{
    Asura_Assert(iHandle >= 0 && iHandle < ASURA_WII_MAX_CONCURRENT_SOUNDSTREAMS, "invalid handle");
    if (iHandle < 0 || iHandle >= ASURA_WII_MAX_CONCURRENT_SOUNDSTREAMS) return;
    // (MH - 30.11.2002) todo
}


void Asura_Sound_System::Platform_SetStreamingSoundOutput(Asura_Handle iHandle, SoundOutputType eOutputType)
{
    Asura_Assert(iHandle >= 0 && iHandle < ASURA_WII_MAX_CONCURRENT_SOUNDSTREAMS, "invalid handle");
    if (iHandle < 0 || iHandle >= ASURA_WII_MAX_CONCURRENT_SOUNDSTREAMS) return;
    // (MH - 12.12.2002) todo
}


int Asura_Sound_System::Platform_GetMaxStreamingSounds()
{
    return ASURA_WII_MAX_CONCURRENT_SOUNDSTREAMS;
}


void Asura_Sound_System::Platform_EnsureAllStreamingSoundsAreStopped()
{
	// todo
}

void Asura_Sound_System::Platform_SetStreamingSoundVolume(Asura_Handle iHandle, float fVolume)
{
    Asura_Assert(iHandle >= 0 && iHandle < ASURA_WII_MAX_CONCURRENT_SOUNDSTREAMS, "invalid handle");
    if (iHandle < 0 || iHandle >= ASURA_WII_MAX_CONCURRENT_SOUNDSTREAMS) return;
    Asura_Assert(fVolume >= 0.0f && fVolume <= 1.0f, "invalid volume");
    
    Asura_Wii_Streaming_Sound_System::SetVolume(iHandle, fVolume);
}

float Asura_Sound_System::Platform_GetStreamingSoundLength(Asura_Handle iHandle) 
{ 
	return Asura_Wii_Streaming_Sound_System::GetLength(iHandle);
}

float Asura_Sound_System::Platform_GetStreamingSoundPosition(Asura_Handle iHandle) 
{ 
	return Asura_Wii_Streaming_Sound_System::GetPosition(iHandle);
}

bool Asura_Sound_System::Platform_StreamingSoundIsFinished(Asura_Handle iHandle)
{
	return Asura_Wii_Streaming_Sound_System::IsFinished(iHandle);
}

bool Asura_Sound_System::Platform_SetSoundUsesReverb(Asura_Handle iSoundHandle, bool bUseReverb)
{
	return false;
}

bool Asura_Sound_System::Platform_SetSoundUsesDistanceFilter(Asura_Handle iSoundHandle, bool bUseReverb)
{
	return false;
}

bool Asura_Sound_System::Platform_SetSoundDistanceFilterGain (Asura_Handle iPlayingSoundHandle, float fNormalisedGain)
{
	//Platform does not use this function
	return true;
}
