#include "Localization.h"
#include "../Daodan_Config.h"
#include "../Daodan_Patch.h"
#include "../Inifile_Reader.h"
#include "Utility.h"
#include "Cheater.h"

#include <beaengine/BeaEngine.h>

void LocalizationIniCallback(const char* section, const char* name, const char* value)
{
	if (!_stricmp(name, "blam"))
	{
		char* str = _strdup(value);
		DDrPatch_Int32((int*)(OniExe + 0x0010fb73), (int)str);
	}
	else if (!_stricmp(name, "damn"))
	{
		char* str = _strdup(value);
		DDrPatch_Int32((int*)(OniExe + 0x0010fb6e), (int)str);
	}
	else if (!_stricmp(name, "savepoint"))
	{
		char* str = malloc(strlen(value) + 10);
		memset(str, ' ', 5);
		strcpy(str+5, value);
		strcpy(str + strlen(str), " %d");
		bool b1 = DDrPatch_Int32((int*)(OniExe + 0x000fd730), (int)str);
		bool b2 = DDrPatch_Int32((int*)(OniExe + 0x000fd738), (int)str);
	}
	else if (!_stricmp(name, "syndicatewarehouse"))
	{
		char* str = _strdup(value);
		DDrPatch_Int32((int*)(OniExe + 0x000fd71a), (int)str);
		DDrPatch_Int32((int*)(OniExe + 0x0010ef75), (int)str);
	}
	else if (!_stricmp(name, "shapeshifter_on"))
		DDr_CheatTable[0].message_on = _strdup(value);
	else if (!_stricmp(name, "shapeshifter_off"))
		DDr_CheatTable[0].message_off = _strdup(value);
	else if (!_stricmp(name, "liveforever_on"))
		DDr_CheatTable[1].message_on = _strdup(value);
	else if (!_stricmp(name, "liveforever_off"))
		DDr_CheatTable[1].message_off = _strdup(value);
	else if (!_stricmp(name, "touchofdeath_on"))
		DDr_CheatTable[2].message_on = _strdup(value);
	else if (!_stricmp(name, "touchofdeath_off"))
		DDr_CheatTable[2].message_off = _strdup(value);
	else if (!_stricmp(name, "canttouchthis_on"))
		DDr_CheatTable[3].message_on = _strdup(value);
	else if (!_stricmp(name, "canttouchthis_off"))
		DDr_CheatTable[3].message_off = _strdup(value);
	else if (!_stricmp(name, "fatloot_on"))
		DDr_CheatTable[4].message_on = _strdup(value);
	else if (!_stricmp(name, "glassworld_on"))
		DDr_CheatTable[5].message_on = _strdup(value);
	else if (!_stricmp(name, "glassworld_off"))
		DDr_CheatTable[5].message_off = _strdup(value);
	else if (!_stricmp(name, "winlevel_on"))
		DDr_CheatTable[6].message_on = _strdup(value);
	else if (!_stricmp(name, "loselevel_on"))
		DDr_CheatTable[7].message_on = _strdup(value);
	else if (!_stricmp(name, "bighead_on"))
		DDr_CheatTable[8].message_on = _strdup(value);
	else if (!_stricmp(name, "bighead_off"))
		DDr_CheatTable[8].message_off = _strdup(value);
	else if (!_stricmp(name, "minime_on"))
		DDr_CheatTable[9].message_on = _strdup(value);
	else if (!_stricmp(name, "minime_off"))
		DDr_CheatTable[9].message_off = _strdup(value);
	else if (!_stricmp(name, "superammo_on"))
		DDr_CheatTable[10].message_on = _strdup(value);
	else if (!_stricmp(name, "superammo_off"))
		DDr_CheatTable[10].message_off = _strdup(value);
	else if (!_stricmp(name, "devmode_on"))
	{
		char* str = _strdup(value);
		DDr_CheatTable[11].message_on = str;
		DDr_CheatTable[cheat_x].message_on = str;
	}
	else if (!_stricmp(name, "devmode_off"))
	{
		char* str = _strdup(value);
		DDr_CheatTable[11].message_off = str;
		DDr_CheatTable[cheat_x].message_off = str;
	}
	else if (!_stricmp(name, "reservoirdogs_on"))
		DDr_CheatTable[12].message_on = _strdup(value);
	else if (!_stricmp(name, "reservoirdogs_off"))
		DDr_CheatTable[12].message_off = _strdup(value);
	else if (!_stricmp(name, "roughjustice_on"))
		DDr_CheatTable[13].message_on = _strdup(value);
	else if (!_stricmp(name, "roughjustice_off"))
		DDr_CheatTable[13].message_off = _strdup(value);
	else if (!_stricmp(name, "chenille_on"))
		DDr_CheatTable[14].message_on = _strdup(value);
	else if (!_stricmp(name, "chenille_off"))
		DDr_CheatTable[14].message_off = _strdup(value);
	else if (!_stricmp(name, "behemoth_on"))
		DDr_CheatTable[15].message_on = _strdup(value);
	else if (!_stricmp(name, "behemoth_off"))
		DDr_CheatTable[15].message_off = _strdup(value);
	else if (!_stricmp(name, "elderrune_on"))
		DDr_CheatTable[16].message_on = _strdup(value);
	else if (!_stricmp(name, "elderrune_off"))
		DDr_CheatTable[16].message_off = _strdup(value);
	else if (!_stricmp(name, "moonshadow_on"))
		DDr_CheatTable[17].message_on = _strdup(value);
	else if (!_stricmp(name, "moonshadow_off"))
		DDr_CheatTable[17].message_off = _strdup(value);
	else if (!_stricmp(name, "munitionfrenzy_on"))
		DDr_CheatTable[18].message_on = _strdup(value);
	else if (!_stricmp(name, "fistsoflegend_on"))
		DDr_CheatTable[19].message_on = _strdup(value);
	else if (!_stricmp(name, "fistsoflegend_off"))
		DDr_CheatTable[19].message_off = _strdup(value);
	else if (!_stricmp(name, "killmequick_on"))
		DDr_CheatTable[20].message_on = _strdup(value);
	else if (!_stricmp(name, "killmequick_off"))
		DDr_CheatTable[20].message_off = _strdup(value);
	else if (!_stricmp(name, "carousel_on"))
		DDr_CheatTable[21].message_on = _strdup(value);
	else if (!_stricmp(name, "carousel_off"))
		DDr_CheatTable[21].message_off = _strdup(value);
}

void DD_Localization()
{
	char* lang = DDrConfig_GetOptOfType("language.language", C_STRING)->value.stringVal;
	char filename[50] = "daodan_locales/";
	strcpy(filename + strlen(filename), lang);
	strcpy(filename + strlen(filename), ".ini");
//	STARTUPMESSAGE("Locale file: %s", filename);
	if (_stricmp(lang, "en"))
	{
		STARTUPMESSAGE("Language: %s", lang);
		uint32_t attr = GetFileAttributes("daodan_locales");
		if (attr == INVALID_FILE_ATTRIBUTES)
		{
			STARTUPMESSAGE("Locales folder does not exist", 0);
			return;
		}
		if ((attr & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY)
		{
			STARTUPMESSAGE("Locales folder does not exist", 0);
			return;
		}

		if (GetFileAttributes(filename) == INVALID_FILE_ATTRIBUTES)
		{
			STARTUPMESSAGE("Locale file for language \"%s\" does not exist", lang);
			return;
		}

		if (!Inifile_Read(filename, LocalizationIniCallback))
			STARTUPMESSAGE("Error reading %s, check your syntax!", filename);
	}
}

