#include "GameData.h"
#include <string>

class LoadedModFinder
{
	const char * m_stringToFind;

public:
	LoadedModFinder(const char * str) : m_stringToFind(str) { }

	bool Accept(ModEntry::Data* data)
	{
		return _stricmp(data->name, m_stringToFind) == 0;
	}
};

const ModEntry * DataHandler::LookupModByName(const char * modName)
{
	return ModEntryVisitor(&modList).Find(LoadedModFinder(modName));
}

const ModEntry ** DataHandler::GetActiveModList()
{
	static const ModEntry* activeModList[0x100] = { 0 };

	if (!(*activeModList))
	{
		UInt8 index = 0;
		for (ModEntry* entry = &(*g_dataHandler)->modList; entry; entry = entry->next)
		{
			if (entry->IsLoaded())
				activeModList[index++] = entry;
		}
	}

	return activeModList;
}

UInt8 DataHandler::GetModIndex(const char* modName)
{
	UInt8 modIndex = 0xFF;
	const ModEntry** activeModList = GetActiveModList();

	for (UInt8 idx = 0; idx < 0x100 && activeModList[idx] && modIndex == 0xFF; idx++)
		if (!_stricmp(activeModList[idx]->data->name, modName))
			modIndex = idx;

	return modIndex;
}

UInt8 DataHandler::GetActiveModCount()
{
	UInt8 count = 0;
	const ModEntry** activeModList = GetActiveModList();

	while (activeModList[count])
		count++;

	return count;
}

const char* DataHandler::GetNthModName(UInt32 modIndex)
{
	const ModEntry** activeModList = GetActiveModList();
	if (modIndex < GetActiveModCount() && activeModList[modIndex]->data)
		return activeModList[modIndex]->data->name;
	else
		return "";
}

TESGlobal* DataHandler::GetGlobalVarByName(const char* varName, UInt32 nameLen)
{
	if (nameLen == -1)
		nameLen = strlen(varName);

	for (Node<TESGlobal>* cur = &globals; cur; cur = cur->next)
	{
		if (cur->data && cur->data->name.m_dataLen == nameLen && !_stricmp(cur->data->name.m_data, varName))
			return cur->data;
	}

	return NULL;
}

TESQuest* DataHandler::GetQuestByEditorName(const char* questName, UInt32 nameLen)
{
	if (nameLen == -1)
		nameLen = strlen(questName);

	for (Node<TESQuest>* cur = &quests; cur; cur = cur->next)
		if (cur->data && cur->data->editorName.m_dataLen == nameLen && !_stricmp(cur->data->editorName.m_data, questName))
			return cur->data;

	return NULL;
}

#if OBLIVION_VERSION == OBLIVION_VERSION_1_1

FileFinder** g_FileFinder = (FileFinder**)0xAEBE0C;

#elif OBLIVION_VERSION == OBLIVION_VERSION_1_2

FileFinder** g_FileFinder = (FileFinder**)0xB33A04;

#elif OBLIVION_VERSION == OBLIVION_VERSION_1_2_416

FileFinder** g_FileFinder = (FileFinder**)0xB33A04;

#else

#error unsupported Oblivion version

#endif
