--[[
Script to add the sound configuration to an vehicle

Author:		Ifko[nator]
Date:		07.09.2020
Version:	1.0

History:	V 1.0 @ 06709.2020 - initial release in FS 19
]]

local currentModDirectory = g_currentModDirectory;

function registerConfiguration()
    if g_configurationManager.configurations["soundExtension"] == nil then    
        g_configurationManager:addConfigurationType("soundExtension", g_i18n:getText("configuration_soundExtension"), nil, nil, nil, nil, ConfigurationUtil.SELECTOR_MULTIOPTION);
    end;

	StoreItemUtil.getConfigurationsFromXML = Utils.overwrittenFunction(StoreItemUtil.getConfigurationsFromXML, addConfiguration);
end;

function addConfiguration(xmlFile, superFunc, baseXMLName, baseDir, customEnvironment, isMod, storeItem)
	local configurations = superFunc(xmlFile, baseXMLName, baseDir, customEnvironment, isMod, storeItem);
    
    if configurations ~= nil then
        local soundExtensionKey = "soundExtension";
        local vehicleXML = loadXMLFile("vehicle", storeItem.xmlFilename);
        local i3dFilenameBase = getXMLString(vehicleXML, "vehicle.base.filename");
        
        local modDesc = loadXMLFile("modDesc", currentModDirectory .. "modDesc.xml");
        local vehicleNumber = 0;
        local allowAddSoundConfiguration = false;

        while true do
            local vehicleKey = "modDesc.newMotorSounds.newMotorSound(" .. tostring(vehicleNumber) .. ")";
    
            if not hasXMLProperty(modDesc, vehicleKey) then
                break;
            end;
    
            local i3dFilename = Utils.getNoNil(getXMLString(modDesc, vehicleKey .. "#i3dFilename"), "");
            local modName = Utils.getNoNil(getXMLString(modDesc, vehicleKey .. "#modName"), "");
            
            if i3dFilename ~= "" and i3dFilenameBase ~= nil then
                if string.find(i3dFilenameBase, i3dFilename) then	
                    allowAddSoundConfiguration = true;
                    
                    if modName ~= "" then
                        allowAddSoundConfiguration = string.find(storeItem.xmlFilename, modName);
                    end;

                    if g_configurationManager.configurations[soundExtensionKey] ~= nil then
                        if hasXMLProperty(vehicleXML, "vehicle.motorized") and allowAddSoundConfiguration then
                            --## Dirty stuff.. but the only "solid" way.
                            if configurations[soundExtensionKey] == nil then
                                local entryNoSoundExtension = {
                                    desc = "",
                                    price = 0,
                                    dailyUpkeep = 0,
                                    isDefault = false,
                                    index = 1,
                                    name = g_i18n:getText("ui_no"),
                                    enabled = true
                                };
                            
                                local entrySoundExtension = {
                                    desc = "",
                                    price = 0,
                                    dailyUpkeep = 0,
                                    isDefault = true,
                                    index = 2,
                                    name = g_i18n:getText("ui_yes"),
                                    enabled = false
                                };
                                configurations[soundExtensionKey] = {};
                            
                                table.insert(configurations[soundExtensionKey], entryNoSoundExtension);
                                table.insert(configurations[soundExtensionKey], entrySoundExtension);
                            else
                                --## Add enabled values to added xml configurations
                                for id, configuration in pairs(configurations[soundExtensionKey]) do
                                    configuration.enabled = id > 1;
                                end;
                            end;
                        end;
                    end;
                end;
            end;
    
            vehicleNumber = vehicleNumber + 1;
        end;
        
        delete(modDesc);
        delete(vehicleXML);
    end;

    return configurations;
end;

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