--[[
Script to change the mod title in the mod view

Author:		Ifko[nator]
Date:		13.06.2021
Version:	2.2

History:	v1.0 @11.05.2018 - initial release in FS 17
			-------------------------------------------
			v2.0 @07.08.2019 - initial release in FS 19
			-------------------------------------------
			v2.1 @14.02.2021 - minior bug fix
			-------------------------------------------
			v2.2 @13.06.2021 - minior adjustments

<changeModTitle modTitle="_modTitle_"/>
]]

ChangeModTitle = {};

ChangeModTitle.modDirectory = g_currentModDirectory;
ChangeModTitle.currentModName = g_currentModName;
ChangeModTitle.languageShort = g_languageShort;

local function changeModTitle()
	local modDesc = loadXMLFile("modDesc", ChangeModTitle.modDirectory .. "modDesc.xml");
	local modTitle = Utils.getNoNil(getXMLString(modDesc, "modDesc.changeModTitle#modTitle"), "");
	
	ChangeModTitle.modManager = g_modManager;
	ChangeModTitle.storeManager = g_storeManager;

	if modTitle == "" then
		for _, mods in pairs(ChangeModTitle.modManager.mods) do
			for modTypeName, modDescFilename in pairs(mods) do
				if tostring(modTypeName) == "modFile" and mods.modName == ChangeModTitle.currentModName then
					local modDesc = loadXMLFile("modDesc", modDescFilename);
					local title = getXMLString(modDesc, "modDesc.title." .. ChangeModTitle.languageShort);
	
					if title == nil then
						title = getXMLString(modDesc, "modDesc.title.en");
	
						if title == nil then
							title = getXMLString(modDesc, "modDesc.title");

							if title == nil then
								title = mods.modName;
							end;
						end;
					end;

					if string.sub(title, 1, 6) == "$l10n_" then
						title = g_i18n:getText(string.sub(title, 7));
					elseif g_i18n:hasText(title) then
						title = g_i18n:getText(title);
					end;
					
					modTitle = title;
				end;
			end;
		end;
	elseif string.sub(modTitle, 1, 6) == "$l10n_" then
		modTitle = g_i18n:getText(string.sub(modTitle, 7));
	elseif g_i18n:hasText(modTitle) then
		modTitle = g_i18n:getText(modTitle);
	end;

	for _, storeItem in pairs(ChangeModTitle.storeManager.modStoreItems) do
		if storeItem.customEnvironment == ChangeModTitle.currentModName then	
			storeItem.isMod = false;
			
			storeItem.dlcTitle = modTitle;
		end;
	end;

	delete(modDesc);
end;

Vehicle.init = Utils.appendedFunction(Vehicle.init, changeModTitle);