//
// Tools_GUIMenu_Audio_Overlay.h
//
// Created by Steven Keen - 12/04/2007
// 
// Copyright (C) 2007 Rebellion, All Rights Reserved.
//

#include "Core/Asura.h"
#include "GUIMenu/Tools_GUIMenu_Audio_Overlay.h"

#ifdef ASURA_USE_GUI_MENU_SYSTEM

//#include "Core/Asura_NameTree.h"
//#include "Entity/Asura_StaticMessage_System.h"
#include "GUIMenu/Asura_GUIMenu_Audio.h"
#include "Interface/Asura_Overlay_System.h"

#include "Rendering/Asura_Text_Output.h"
#include "Rendering/Asura_Viewport.h"
#include "Sound/Asura_Sound_Event.h"
#include "Sound/Asura_Sound_Event_Storage.h"
//#include "Sound/Asura_Sound_Mixer.h"

#include "Tools_Overlays.h"
#include "GUIMenu/Tools_GUIMenu_System.h"
#include "Load&Save/Tools_ModeDataFile.h"
#include "Misc/Tools_Dialog.h"
#include "Misc/Tools_DynamicArray.h"
#include "Misc/Tools_MessageLog.h"
#include "Misc/Tools_Modal_Overlay.h"
#include "Sound/Tools_PickSoundEvent_Overlay.h"
//#include "Misc/Tools_Timed_Messages.h"
//#include "Sound/Tools_SoundEvent_System.h"
//#include "Sound/Tools_SoundEventTab_Event.h"
//#include "Sound/Tools_SurroundPan_Dialog.h"

// G L O B A L S /////////////////////////////////////////////////////////////
//static const char* g_szNewPreset = "*New Preset*";
//Asura_String Tools_SoundMixer_Overlay::s_xPresetName( "default" );

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

struct Tools_GUIMenu_Audio_Info
{
	Asura_Char* pcText;
	Asura_Char* pcHelpText;
	Asura_GUIMenu_Audio_Type eAudioType;
	void (*pfnCallback)( int);
};

static Tools_GUIMenu_Audio_Info g_xAudioInfo[] = 
{
	"Navigation", "Played when changing selection via key/button presses",ASURA_GUIMENU_AUDIO_TYPE_ON_NAVIGATE, Tools_GUIMenu_Audio_Overlay::SetNavigateEvent,
	"Cursor Navigation", "Played when changing selection via mouse",ASURA_GUIMENU_AUDIO_TYPE_ON_CURSOR_NAVIGATE, Tools_GUIMenu_Audio_Overlay::SetCursorNavigateEvent,
	"Accept", "", ASURA_GUIMENU_AUDIO_TYPE_ON_ACCEPT, Tools_GUIMenu_Audio_Overlay::SetAcceptEvent,
	"Cancel", "", ASURA_GUIMENU_AUDIO_TYPE_ON_CANCEL, Tools_GUIMenu_Audio_Overlay::SetCancelEvent,
	"Value Change",	"",ASURA_GUIMENU_AUDIO_TYPE_ON_VALUE_CHANGE, Tools_GUIMenu_Audio_Overlay::SetValueChangeEvent,
	"Fail",	"",ASURA_GUIMENU_AUDIO_TYPE_FAILURE, Tools_GUIMenu_Audio_Overlay::SetFailEvent
};

static u_int g_uAudioInfoSize = sizeof( g_xAudioInfo) / sizeof( Tools_GUIMenu_Audio_Info);

/*--------------------------**
** Tools_GUIMenu_Audio_Overlay **
**--------------------------*/
Asura_Vector_2 Tools_GUIMenu_Audio_Overlay::s_xOverlayPosition(10.0f,10.0f);
Tools_GUIMenu_Audio_Overlay* Tools_GUIMenu_Audio_Overlay::s_pxThis = 0;

bool Tools_GUIMenu_Audio_Overlay::s_bDataChanged = false;
float Tools_GUIMenu_Audio_Overlay::s_fOffsetX = 0.0f;

Tools_GUIMenu_Audio_Overlay::Tools_GUIMenu_Audio_Overlay()
:Asura_Overlay("GUIMenu_Audio", false, &s_xOverlayPosition)
,m_pxSubwindow(0)
{
	Asura_Assert(!s_pxThis,"Should only be one instance of the GUIMenu Audio overlay");
	s_pxThis = this;

	m_xPosition = s_xOverlayPosition;

	//	UpdateTitle( s_xPresetName.GetString() );
}

Tools_GUIMenu_Audio_Overlay::~Tools_GUIMenu_Audio_Overlay()
{
	if(s_pxThis == this)
	{
		s_pxThis = 0;
	}
}



void Tools_GUIMenu_Audio_Overlay::EditAudio( void)
{
	if(!s_pxThis)
	{
		s_pxThis = new Tools_GUIMenu_Audio_Overlay();
		s_pxThis->ShowOverlay();
	}
	Asura_Overlay_System::BringOverlayToTop( TOOLS_OVERLAY_GUIMENU_AUDIO);
}

void Tools_GUIMenu_Audio_Overlay::CloseOverlay()
{
	Asura_Overlay_System::CloseOverlays( TOOLS_OVERLAY_GUIMENU_AUDIO);
	s_pxThis = 0;
}

void Tools_GUIMenu_Audio_Overlay::ShowOverlay()
{
	u_int u;
	Asura_Vector_2 xOffset(1.0f,3.0f);
	Asura_Dialog* pxDialog;
	Asura_Char acText[256];
	Asura_Vector_2 xRightOffset(2.0f,3.0f);
	Asura_Sound_Event* pxEvent = NULL;

	//Create the subwindow control for containing all the mixers
	float fSubwindowWidth = Asura_Maths::Min(50.0f,(Asura_Viewport::GetWidth()-100)/ASURA_TEXT_OUTPUT_FONT_WIDTH);
	float fSubwindowHeight = Asura_Maths::Min(40.0f,(Asura_Viewport::GetHeight()-100)/ASURA_TEXT_OUTPUT_FONT_HEIGHT);
	pxDialog = m_pxSubwindow = new Asura_Subwindow_Dialog(xOffset,fSubwindowWidth,fSubwindowHeight,true);
	AddDialog(pxDialog);
	xOffset.y += Asura_Dialog::DefaultSpacing();

	for( u=0; u<g_uAudioInfoSize; u++)
	{
		Asura_PrintfToBfr( &acText[0], "Set %s", 256, true, g_xAudioInfo[u].pcText);
		pxDialog = new Asura_Function_Dialog( acText, xOffset, g_xAudioInfo[u].pfnCallback,(int)this);
		pxDialog->SetHelpText(g_xAudioInfo[u].pcHelpText);
		AddDialog(pxDialog);
		pxDialog->SetRelativePosition(m_pxSubwindow,true,false);

		xRightOffset.x = pxDialog->m_xMaxCoord.x + 1.0f;
		xRightOffset.y = xOffset.y;

		if( u == 0)
		{
			s_fOffsetX = xRightOffset.x;
		}

		Asura_PrintfToBfr( &acText[0], ": *None*", 256, true);

		pxEvent = Asura_Sound_Event_Storage::GetEventByHash( Asura_GUIMenu_Audio::GetAudioHash( g_xAudioInfo[u].eAudioType));
		if( pxEvent != NULL)
		{
			Asura_PrintfToBfr( &acText[0], " : %s", 256, true, pxEvent->GetName());
		}

		pxDialog = new Asura_StringDisplay_Dialog( acText, xRightOffset);
		AddDialog(pxDialog);
		pxDialog->m_iControlID = Asura_GetHashID( g_xAudioInfo[u].pcText);

		xOffset.y += Asura_Dialog::DefaultSpacing();
	}

	m_uID = TOOLS_OVERLAY_GUIMENU_AUDIO;
	Asura_Overlay_System::SubmitNewOverlay(this);
}

// -----------------------------------------------------------------------------------

void Tools_GUIMenu_Audio_Overlay::SetNavigateEvent( void)
{
	Asura_Hash_ID uSoundEventID;

	uSoundEventID = ChooseAudioEvent(Asura_GUIMenu_Audio::GetAudioHash( ASURA_GUIMENU_AUDIO_TYPE_ON_NAVIGATE));
	Asura_GUIMenu_Audio::SetAudioType( ASURA_GUIMENU_AUDIO_TYPE_ON_NAVIGATE, uSoundEventID);
}

// -----------------------------------------------------------------------------------

void Tools_GUIMenu_Audio_Overlay::SetAcceptEvent( void)
{
	Asura_Hash_ID uSoundEventID;

	uSoundEventID = ChooseAudioEvent(Asura_GUIMenu_Audio::GetAudioHash( ASURA_GUIMENU_AUDIO_TYPE_ON_ACCEPT));
	Asura_GUIMenu_Audio::SetAudioType( ASURA_GUIMENU_AUDIO_TYPE_ON_ACCEPT, uSoundEventID);
}

// -----------------------------------------------------------------------------------

void Tools_GUIMenu_Audio_Overlay::SetCancelEvent( void)
{
	Asura_Hash_ID uSoundEventID;

	uSoundEventID = ChooseAudioEvent(Asura_GUIMenu_Audio::GetAudioHash( ASURA_GUIMENU_AUDIO_TYPE_ON_CANCEL));
	Asura_GUIMenu_Audio::SetAudioType( ASURA_GUIMENU_AUDIO_TYPE_ON_CANCEL, uSoundEventID);

}

// -----------------------------------------------------------------------------------

void Tools_GUIMenu_Audio_Overlay::SetValueChangeEvent( void)
{
	Asura_Hash_ID uSoundEventID;

	uSoundEventID = ChooseAudioEvent(Asura_GUIMenu_Audio::GetAudioHash( ASURA_GUIMENU_AUDIO_TYPE_ON_VALUE_CHANGE));
	Asura_GUIMenu_Audio::SetAudioType( ASURA_GUIMENU_AUDIO_TYPE_ON_VALUE_CHANGE, uSoundEventID);

}

// -----------------------------------------------------------------------------------

void Tools_GUIMenu_Audio_Overlay::SetFailEvent( void)
{
	Asura_Hash_ID uSoundEventID;

	uSoundEventID = ChooseAudioEvent(Asura_GUIMenu_Audio::GetAudioHash( ASURA_GUIMENU_AUDIO_TYPE_FAILURE));
	Asura_GUIMenu_Audio::SetAudioType( ASURA_GUIMENU_AUDIO_TYPE_FAILURE, uSoundEventID);

}

// -----------------------------------------------------------------------------------
void Tools_GUIMenu_Audio_Overlay::SetCursorNavigateEvent( void)
{
	Asura_Hash_ID uSoundEventID;

	uSoundEventID = ChooseAudioEvent(Asura_GUIMenu_Audio::GetAudioHash( ASURA_GUIMENU_AUDIO_TYPE_ON_CURSOR_NAVIGATE));
	Asura_GUIMenu_Audio::SetAudioType( ASURA_GUIMENU_AUDIO_TYPE_ON_CURSOR_NAVIGATE, uSoundEventID);
}

// -----------------------------------------------------------------------------------

Asura_Hash_ID Tools_GUIMenu_Audio_Overlay::ChooseAudioEvent( Asura_Hash_ID uInitialID)
{
	Asura_Hash_ID uRetSoundEvent = ASURA_HASH_ID_UNSET;

	//For sound events this gets used when creating the phonon
	Tools_PickSoundEvent_Overlay xOverlay("Pick event for new phonon");
	uRetSoundEvent = xOverlay.DoModal(uInitialID);

	Asura_Sound_Event* pxEvent = Asura_Sound_Event_Storage::GetEventByHash( uRetSoundEvent);
	if( pxEvent)
	{
		if( !pxEvent->IsGlobalSound())
		{
			Tools_MessageLog::AddMessage( Tools_MessageLog::GUI_MENU,
											"Audio",
											"%s may not play in retail builds.  This sound event is not set as a global sound.", pxEvent->GetName());
		}
		
	}

	s_bDataChanged = true;

	return uRetSoundEvent;
}

// -----------------------------------------------------------------------------------

void Tools_GUIMenu_Audio_Overlay::PerFrameUpdate()
{
	Asura_Overlay::PerFrameUpdate();

	if( s_bDataChanged)
	{
		u_int u;
		Asura_Vector_2 xOffset( s_fOffsetX, 3.0f);
		Asura_Sound_Event* pxEvent = NULL;
		Asura_Char acText[256];

		for( u=0; u<g_uAudioInfoSize; u++)
		{
			Asura_PrintfToBfr( &acText[0], ": *None*", 256, true);

			pxEvent = Asura_Sound_Event_Storage::GetEventByHash( Asura_GUIMenu_Audio::GetAudioHash( g_xAudioInfo[u].eAudioType));
			if( pxEvent != NULL)
			{
				Asura_PrintfToBfr( &acText[0], " : %s", 256, true, pxEvent->GetName());
			}
			
			UpdateDialogLabel( Asura_GetHashID( g_xAudioInfo[u].pcText), acText);
		}

		Tools_GUIMenu_System::SetAudioModified();
		s_bDataChanged = false;
	}
}

// -----------------------------------------------------------------------------------


//bool Tools_SoundMixer_Overlay::CanDeleteMixerChannel()
//{
//	return Asura_Sound_Mixer_System::GetNumberOfChannels()>0;
//}


// JAT 26/02/07 update the name to show the chosen preset
//void Tools_SoundMixer_Overlay::UpdateTitle( const char* const szPresetName )
//{
//	// JAT 26/02/07 update the name to show the chosen preset
//	Asura_String xTitle( "Sound Mixer: " );
//	xTitle += szPresetName;
//	SetTitle( xTitle.GetString() );
//	s_xPresetName = szPresetName;
//}

#endif //ASURA_USE_GUI_MENU_SYSTEM
