// Asura_Audio_Systems_Manager.cpp
// Thomas Young - 03.12.08
// Copyright (C) 2008 Rebellion, All Rights Reserved.

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

#include "Core/Asura.h"

#ifdef ASURA_USE_AUDIO_SYSTEM

#include "Debugging/Asura_CPUProfile.h"
#include "Sound/Asura_Audio_System.h"
#include "Sound/Asura_Audio_Systems_Manager.h"
#include "Sound/Asura_Dialogue.h"
#include "Sound/Asura_Dialogue_System.h"
#include "Sound/Asura_Dynamic_Music_System.h"
#include "Sound/Asura_Phonon.h"
#include "Sound/Asura_ReverbVolume_System.h"
#include "Sound/Asura_Sound_Event_Storage.h"
#include "Sound/Asura_Sound_Event_System.h"
#include "Sound/Asura_Sound_Mixer.h"
#include "Sound/Asura_StreamingBackgroundSound_System.h"

// S T A T I C   M E M B E R   V A R I A B L E S /////////////////////////////

bool	Asura_Audio_Systems_Manager::s_bIsInitialised	= false;

// F U N C T I O N   D E F I N I T I O N S ///////////////////////////////////

void Asura_Audio_Systems_Manager::Initialise ( )
{
	if( IsInitialised() )
	{
		Asura_Assert( false, "Asura_Audio_Systems_Manager has already been initialised" );
		return;
	}

	Asura_Audio_System::Initialise();

	Asura_Sound_Mixer_System::Initialise();
	Asura_Sound_Event_System::Initialise();
	#ifdef ASURA_USE_DIALOGUE_SYSTEM
	Asura_Dialogue_System::Initialise();
	#endif
	Asura_Phonon_System::Initialise();
	Asura_StreamingBackgroundSound_System::Initialise();
	#ifdef ASURA_SUPPORT_DYNAMIC_MUSIC_SYSTEM
	Asura_Dynamic_Music_System::Initialise();
	#endif
	#ifdef ASURA_USE_REVERBVOLUME_SYSTEM
	Asura_ReverbVolume_System::Initialise();
	#endif

	s_bIsInitialised = true;
}


void Asura_Audio_Systems_Manager::Update ( const u_int uUpdateFlags )
{
	if( !IsInitialised() )
	{
		Asura_Assert( false, "Should not be updating Asura_Audio_Systems_Manager without initialising first" );
		return;
	}

	ASURA_CPU_PROFILE__START_LABEL( ASURA_CPU_PROFILE__SOUND_UPDATE, "Sound update" );

	// We always update the base audio system and mixer system
	Asura_Audio_System::SetShouldUpdateOcclusion( ( uUpdateFlags & ASURA_AUDIO_UPDATE_OCCLUSION ) != 0 );

	Asura_Audio_System::Update();
	Asura_Sound_Mixer_System::Update();

	// everything else can be controlled via flags
	if( uUpdateFlags & ASURA_AUDIO_UPDATE_SOUND_EVENTS )
	{
		Asura_Sound_Event_System::Update();
	}
	
	#ifdef ASURA_USE_DIALOGUE_SYSTEM
	if( uUpdateFlags & ASURA_AUDIO_UPDATE_DIALOGUE )
	{
		Asura_Dialogue_System::Update();
	}
	#endif
	
	if( uUpdateFlags & ASURA_AUDIO_UPDATE_PHONONS )
	{
		Asura_Phonon_System::Update();
	}

	if( uUpdateFlags & ASURA_AUDIO_UPDATE_STREAMING_BGND )
	{
		Asura_StreamingBackgroundSound_System::Update();
	}
	
	#ifdef ASURA_SUPPORT_DYNAMIC_MUSIC_SYSTEM
	if( uUpdateFlags & ASURA_AUDIO_UPDATE_DYNAMIC_MUSIC )
	{
		Asura_Dynamic_Music_System::Update();
	}
	#endif

	#ifdef ASURA_USE_REVERBVOLUME_SYSTEM
	if( uUpdateFlags & ASURA_AUDIO_UPDATE_REVERB_VOLUMES )
	{
		Asura_ReverbVolume_System::Update();
	}
	#endif

	ASURA_CPU_PROFILE__STOP( ASURA_CPU_PROFILE__SOUND_UPDATE );
}


void Asura_Audio_Systems_Manager::Reset ( )
{
	if( IsInitialised() )
	{
		Asura_Sound_Mixer_System::Reset();
		#ifdef ASURA_USE_DIALOGUE_SYSTEM
		Asura_Dialogue_System::Reset();
		#endif
		Asura_Phonon_System::Reset();
		Asura_StreamingBackgroundSound_System::Reset();
		#ifdef ASURA_SUPPORT_DYNAMIC_MUSIC_SYSTEM
		Asura_Dynamic_Music_System::Reset();
		#endif
		#ifdef ASURA_USE_REVERBVOLUME_SYSTEM
		Asura_ReverbVolume_System::Reset();
		#endif
		
		// JG - 22 January 2009 - Reset this system after all the others to stop this manager
		// complaining about external handles being kept by other systems (i.e. phonons)
		Asura_Sound_Event_System::Reset();

		Asura_Audio_System::Reset();
	}
}


void Asura_Audio_Systems_Manager::Shutdown ( )
{
	if( !IsInitialised() )
	{
		Asura_Assert( false, "Should not be shutting down Asura_Audio_Systems_Manager without initialising first" );
		return;
	}

	#ifdef ASURA_USE_REVERBVOLUME_SYSTEM
	Asura_ReverbVolume_System::Shutdown();
	#endif
	#ifdef ASURA_SUPPORT_DYNAMIC_MUSIC_SYSTEM
	Asura_Dynamic_Music_System::Shutdown();
	#endif
	Asura_StreamingBackgroundSound_System::Shutdown();
	Asura_Phonon_System::Shutdown();
	#ifdef ASURA_USE_DIALOGUE_SYSTEM
	Asura_Dialogue_System::Shutdown();
	#endif
	Asura_Sound_Event_System::Shutdown();
	Asura_Sound_Mixer_System::Shutdown();

	Asura_Audio_System::Shutdown();

	s_bIsInitialised = false;
}


bool Asura_Audio_Systems_Manager::IsInitialised ( )
{
	return s_bIsInitialised;
}



#endif	// ASURA_USE_AUDIO_SYSTEM

//eof
