//
// Tools_Flag_IniFile.cpp
//
// Created by Richard Rice - 09/07/2002
// 
// Copyright (C) 2002 Rebellion, All Rights Reserved.
//


#include "Core/Asura.h"
#include "Debugging/Asura_Assert.h"

#include "Misc/Tools_Flag_IniFile.h"
#include "Misc/Tools_IniFile.h"
#include "Misc/Tools_Modal_Overlay.h"

///////////////////////////////////////////////////////////////////////////////
/*--------------------**
** Tools_Flag_IniFile **
**--------------------*/

Tools_Flag_IniFile::Tools_Flag_IniFile(const char* fileName)
{
	IniFile = new Tools_IniFile(fileName);
}

Tools_Flag_IniFile::~Tools_Flag_IniFile()
{
	delete IniFile;

	while(AllFlagLists.size())
	{
		delete AllFlagLists.first_entry();
		AllFlagLists.delete_first_entry();
	}
}

Tools_Flag_List* Tools_Flag_IniFile::GetFlagList(const char* sectionName)
{
	Asura_Assert(sectionName,"");
	
	//search for an existing flag list entry
	for(LIF<Tools_Flag_List*> alif(&AllFlagLists);!alif.done();alif.next())
	{
		if(!_stricmp(alif()->SectionName.GetString(),sectionName))
		{
			return alif()->ValueList.size() ? alif() : 0;
		}
	}

	//need to create a new entry then
	Tools_Flag_List* flagList = new Tools_Flag_List(sectionName,IniFile);
	AllFlagLists.add_entry(flagList);

	return flagList->ValueList.size() ? flagList : 0;
}

///////////////////////////////////////////////////////////////////////////////
/*------------------**
** Tools_Value_List **
**------------------*/

Tools_Value_List::Tools_Value_List(const char* sectionName)
{
	SectionName = sectionName;
}

Tools_Value_List::Tools_Value_List(const char* sectionName,struct Tools_IniFile* IniFile)
{
	SectionName = sectionName;

	//find the section in the ini file
	IniFile->GetIniTextSection(sectionName);

	//add the flags to the list
	while(IniFile->GetNextIniTextLine())
	{
		NameAndValue val ;
		if(IniFile->GetNextOnLine(val.Value) &&	
		   IniFile->GetNextOnLine(val.Name))
		{
			ValueList.add_entry(val);			
		}	
	}
}


const char* Tools_Value_List::GetName(int value,const char* unknownString /*= "<Unknown>"*/)
{
	for(LIF<NameAndValue> vlif(&ValueList);!vlif.done();vlif.next())
	{
		if(vlif().Value == value)
		{
			return vlif().Name.GetString();
		}
	}

	return unknownString;
}

bool Tools_Value_List::GetValueFromName( const char* szName, int& iOutput) const
{
	Asura_Assert(szName, "");
	if(!szName) return false;

	for(CLIF<NameAndValue> xValueLIF(&ValueList);!xValueLIF.done();xValueLIF.next())
	{
		if(!stricmp(xValueLIF().Name.GetString(),szName))
		{
			iOutput = xValueLIF().Value;
			return true;
		}
	}
	return false;
}

bool Tools_Value_List::SelectValue(const char* title,int* value)
{
	Asura_Assert(value,"");

	ListSelection_Overlay overlay(title);
	for(LIF<NameAndValue> vlif(&ValueList);!vlif.done();vlif.next())
	{
		overlay.AddItem(vlif().Name.GetString(),vlif().Value);
	}

	if(!overlay.DoModal()) return false;

	*value = overlay.GetSelectionData();
	
	return true;
}

///////////////////////////////////////////////////////////////////////////////
/*-----------------**
** Tools_Flag_List **
**-----------------*/

Tools_Flag_List::Tools_Flag_List(const char* sectionName,const char** strings)
:Tools_Value_List(sectionName)
{
	for(int i=0;strings[i];i++)
	{
		if(!*strings[i]) continue;//ignore blank strings
		NameAndValue val;
		val.Value = 1<<i;
		val.Name = strings[i];

		ValueList.add_entry(val);
	}
}


const char* Tools_Flag_List::GetFlagName(int flagValue)
{
	return GetName(flagValue,"<Unknown Flag>");
}

///////////////////////////////////////////////////////////////////////////////
/*------------------------**
** Tools_Enumeration_List **
**------------------------*/

Tools_Enumeration_List::Tools_Enumeration_List(const char* sectionName,const char** strings)
:Tools_Value_List(sectionName)
{
	for(int i=0;strings[i];i++)
	{
		if(!*strings[i]) continue;//ignore blank strings
		NameAndValue val;
		val.Value = i;
		val.Name = strings[i];
		
		ValueList.add_entry(val);
	}
}
