//
// Tools_FSFX_System.cpp
//
// Created by JGP 02/02/06
// 
// Copyright (C) 2006 Rebellion (Derby), All Rights Reserved.
//

#include "Core/Asura.h"

#ifdef ASURA_USE_FSFX
#include "Tools_FSFX_System.h"

#include "ChunkSystem/Asura_Chunk_Loaders.h"
#include "ChunkSystem/Asura_Chunk_Stream.h"
#include "Core/Asura_NameTree.h"
#include "FileAccess/Asura_FileAccess.h"
#include "FSFX/Asura_FSFX_Template.h"
#include "Rendering/Asura_Render.h"

#include "PC/FileSystem/Tools_FileSystem.h"



const Asura_Hash_ID uTOOLS_FSFX_SYSTEM_HANDLER_NAME	= 0x831D16B6;	// Asura_Guid("Tools_FSFX_System_Handler")


///////////////////////////////////////////////////////////////////////////////////////
// System
///////////////////////////////////////////////////////////////////////////////////////

Asura_FSFX_Template*					Tools_FSFX_System::s_pxCurrentTemplate		= NULL;
float									Tools_FSFX_System::s_fCurrentIntensity		= 0.0f;
bool									Tools_FSFX_System::s_bLoadingAllTemplates	= false;

Asura_FSFX_Handler*						Tools_FSFX_System::s_pxFSFXHandler = NULL;


void Tools_FSFX_System::Initialise()
{
	s_pxCurrentTemplate		= NULL;
	s_fCurrentIntensity		= 0.0f;
	s_bLoadingAllTemplates	= false;

	LoadAllTemplates();


	s_pxFSFXHandler = Asura_FSFX_System::AddHandler( uTOOLS_FSFX_SYSTEM_HANDLER_NAME );
}

void Tools_FSFX_System::Shutdown()
{
	Asura_FSFX_System::RemoveHandler( s_pxFSFXHandler );
}


///////////////////////////////////////////////////////////////////////////////////////
// Render
///////////////////////////////////////////////////////////////////////////////////////


void Tools_FSFX_System::Render()
{
	if ( Asura_Render::IsSystemEnabled( ASURA_RENDER_SYSTEM_FSFX ) )
	{
		if( s_pxCurrentTemplate )
		{
			s_pxCurrentTemplate->Render( s_fCurrentIntensity );
		}
	}
}

void Tools_FSFX_System::ResetHandler()
{
	if( s_pxFSFXHandler )
	{
		s_pxFSFXHandler->ResetAllTemplates();
	}
}


///////////////////////////////////////////////////////////////////////////////////////
// Template Functions
///////////////////////////////////////////////////////////////////////////////////////


void Tools_FSFX_System::AddTemplate( Asura_FSFX_Template* const pxTemplate )
{
	const Asura_FSFX_Template* pxExistingTemplate = Asura_FSFX_System::FindTemplate( pxTemplate->GetNameHash() );
	Asura_Assertf( !pxExistingTemplate, ("Template %s from file %s already exists in file %s. You probably copied the template file manually. Try creating a duplicate from inside tools instead.", 
		pxTemplate->GetName(), pxTemplate->GetLoadedFromFile(), pxExistingTemplate->GetLoadedFromFile() ) );

	int iAttempt = 0;
	while (pxExistingTemplate)
	{
		char acNewName[256];
		Asura_PrintfToBfr( acNewName, "(broken%i)%s", 256, true, iAttempt, pxTemplate->GetName() );
		pxTemplate->SetName( acNewName );
		pxExistingTemplate = Asura_FSFX_System::FindTemplate( pxTemplate->GetNameHash() );
		++iAttempt;
	}

	Asura_FSFX_System::AddTemplate( pxTemplate );
}


///////////////////////////////////////////////////////////////////////////////////////
// Get / set
///////////////////////////////////////////////////////////////////////////////////////


void Tools_FSFX_System::SetOverrideFSFX( Asura_FSFX_Template* const pxTemplate, const float fIntensity )
{
	s_pxCurrentTemplate = pxTemplate;
	s_fCurrentIntensity = fIntensity;
}


///////////////////////////////////////////////////////////////////////////////////////
// Load save
///////////////////////////////////////////////////////////////////////////////////////


void Tools_FSFX_System::LoadAllTemplates()
{

	// we are loading all templates

	s_bLoadingAllTemplates = true;

	List<Asura_String> xFileList;

	//find local files
	char szSearch[MAX_PATH];
	Asura_PrintfToBfr(szSearch,"%s*%s",sizeof(szSearch),true,GetTemplateDirectory(),GetTemplateExtension());
	Tools_FileSystem::FindFiles(szSearch, xFileList);

	#ifdef ASURA_PC_ALLOW_LOAD_FROM_NETWORK
	//find network files
	Asura_FileAccess::PlatformGetNetworkDir(GetTemplateDirectory(),szSearch,sizeof(szSearch)-20);
	strcat(szSearch,"*");
	strcat(szSearch,GetTemplateExtension());
	Tools_FileSystem::FindFiles(szSearch, xFileList);
	#endif //ASURA_PC_ALLOW_LOAD_FROM_NETWORK

	
	// Clear out existing templates in case they are already loaded.
	Asura_FSFX_System::GetResourceSet().Reset();


	Asura_NameTree xNameTree;

	//load the files in the list
	for(LIF<Asura_String> xFileLIF(&xFileList);!xFileLIF.done();xFileLIF.next())
	{
		//make sure this file hasn't already been dealt with
		const char* szName = Tools_FileSystem::StripPath(xFileLIF().GetString());

		int iJunkIndex;
		if(xNameTree.FindNodeIndexByName(szName,true,iJunkIndex)) continue;

		LoadAsuraFile(xFileLIF().GetString());
	}
	

	// end

	s_bLoadingAllTemplates = false;

}


const Asura_String Tools_FSFX_System::MakeFilenameForTemplate( const Asura_FSFX_Template* const pxTemplate )
{
	return MakeFilenameForTemplate( pxTemplate->GetName() );
}


const Asura_String Tools_FSFX_System::MakeFilenameForTemplate( const char* const szTemplateName )
{
	Asura_String xFn = GetTemplateDirectory();
	xFn += szTemplateName;
	xFn += GetTemplateExtension();
	return xFn;
}


const char* const Tools_FSFX_System::GetTemplateDirectory()
{
	return "FSFX\\";
}


const char* const Tools_FSFX_System::GetTemplateExtension()
{
	return ".fsx";
}


#endif // ASURA_USE_FSFX
