//
// Tools_Sound_PS2Settings.cpp
//
// Created by Richard Rice - 18/06/2002
// 
// Copyright (C) 2002 Rebellion, All Rights Reserved.
//

#include "Core/Asura.h"
#include "Core/Asura_String.h"

#include "Sound/Tools_Sound_PS2Settings.h"
#include "Load&Save/Tools_SaveAsura.h"
#include "Misc/Tools_IniFile.h"
#include "PC/FileSystem/Tools_FileSystem.h"

#include "Misc/list_tem.hpp"



////////////////////////////////////////////////////////////////////////////////////////////////
struct PS2SoundSettings
{
	Asura_String Filename;
	int Frequency;
};

static List<PS2SoundSettings*> SoundList;

bool Tools_Sound_PS2Settings::Modified = false;


////////////////////////////////////////////////////////////////////////////////////////////////
void Tools_Sound_PS2Settings::Initialise()
{
	//read in the contents of the text file
	Tools_IniFile iniFile(PS2SettingsFilename);
	if(!iniFile.LoadIniFile()) return;

	while(iniFile.GetNextIniTextLine())
	{
		//read filename and frequency on this line
		Asura_String filename;
		int frequency;

		if(!iniFile.GetNextOnLine(filename)) continue;
		if(!iniFile.GetNextOnLine(frequency)) continue;

		SetFrequency(filename.GetString(),frequency,false);
	}
	Modified = false;

}

void Tools_Sound_PS2Settings::Shutdown()
{
	while(SoundList.size())
	{
		delete SoundList.first_entry();
		SoundList.delete_first_entry();
	}
}

bool Tools_Sound_PS2Settings::Save()
{
	FILE* file = fopen(PS2SettingsFilename,"wt");
	if(!file) return false;

	fprintf(file,"#Frequencies to use for vag file creation\n\n");

	
	for(LIF<PS2SoundSettings*> slif(&SoundList);!slif.done();slif.next())
	{
		if(!slif()->Frequency) continue;
		fprintf(file,"\"%s\" , %d\n",slif()->Filename.GetString(),slif()->Frequency);
	}

	fclose(file);

	Modified = false;
	return true;

}


int Tools_Sound_PS2Settings::GetFrequency(const char* filename)
{
	PS2SoundSettings* settings = GetSoundSettings(filename,false);
	return settings ? settings->Frequency : 0;
}

void Tools_Sound_PS2Settings::SetFrequency(const char* filename,int frequency,bool bSetModified /*=true*/)
{
	PS2SoundSettings* settings = GetSoundSettings(filename,true);
	settings->Frequency = frequency;
	if(bSetModified)
	{
		if(!Modified)
		{
			Tools_SaveAsura::WarnIfReadOnly(PS2SettingsFilename);
		}
		Modified = true;
	}
}

PS2SoundSettings* Tools_Sound_PS2Settings::GetSoundSettings(const char* filename,bool create)
{
	//get the filename without path
	filename = Tools_FileSystem::StripPath(filename);

	//search in the list
	LIF<PS2SoundSettings*> slif(&SoundList);
	for(;!slif.done();slif.next())
	{
		int compare = stricmp(slif()->Filename.GetString(),filename);
		if(compare==0)
		{
			return slif();
		}
		else if(compare>0)
		{
			break;
		}
	}

	//failed to find an entry
	if(!create) return 0;

	PS2SoundSettings* settings = new PS2SoundSettings;
	settings->Filename = filename;
	settings->Frequency = 0;

	//add entry so that list is sorted alphabetically
	if(!slif.done())
	{
		SoundList.add_entry_before(settings,slif());
	}
	else
	{
		SoundList.add_entry(settings);
	}

	return settings;
}
