averageTraffic = {
    modDir = g_currentModDirectory,
    xmlDir = g_currentModSettingsDirectory
}

TrafficSystem.onCreate = Utils.overwrittenFunction(TrafficSystem.onCreate, function (self, superFunc, transformId)
    local xmlFilename = getUserAttribute(transformId, "xmlFile")

    if xmlFilename ~= nil then
        local prefix = "eu"
        local prefix2 = ""

        local trafficSystemXml = Utils.getFilename(xmlFilename, g_currentMission.loadingMapBaseDirectory)
        local trafficSystemXmlLoad = loadXMLFile("map_trafficSystem", trafficSystemXml)
        local licensePlates = getXMLString(trafficSystemXmlLoad, "trafficSystem#licensePlates")
        if licensePlates ~= nil then
            local aiLicensePlates = Utils.getFilenameInfo(licensePlates, true)
            local len = aiLicensePlates:len()
            local XX = aiLicensePlates:sub(15 - len)
        end

        local mod = g_modManager:getModByName("FS22_PolishLicensePlates")
        if mod ~= nil and g_modIsLoaded["FS22_PolishLicensePlates"] or XX == "PL" then
            prefix = "pl"
            print("Average Traffic Info: Polish License Plates will be loaded for traffic.")
        end

        local mod = g_modManager:getModByName("FS22_RussianLicensePlates")
        if mod ~= nil and g_modIsLoaded["FS22_RussianLicensePlates"] or XX == "RUS" or XX == "RU" then
            prefix = "ru"
            print("Average Traffic Info: Russian License Plates will be loaded for traffic.")
        end
    
        local mod = g_modManager:getModByName("FS22_UkrainianLicensePlates")
        if mod ~= nil and g_modIsLoaded["FS22_UkrainianLicensePlates"] or XX == "UKR" or XX == "UA" then
            prefix = "ua"
            print("Average Traffic Info: Ukrainian License Plates will be loaded for traffic.")
        end

        local mod = g_modManager:getModByName("FS22_BelarusianLicensePlates")
        if mod ~= nil and g_modIsLoaded["FS22_BelarusianLicensePlates"] or XX == "BY" then
            prefix = "by"
            print("Average Traffic Info: Belarusian License Plates will be loaded for traffic.")
        end

        local configFile = averageTraffic.xmlDir .. "averageTraffic.xml"
        if fileExists(configFile) then
            averageTraffic.configXml = loadXMLFile("averageTraffic_XML", configFile)
            -- if getXMLString(averageTraffic.configXml, "averageTraffic.aiLicensePlates.prefix#code") ~= nil then
                -- prefix = getXMLString(averageTraffic.configXml, "averageTraffic.aiLicensePlates.prefix#code")
            -- end
            if getXMLBool(averageTraffic.configXml, "averageTraffic.aiLicensePlates#lite") == true then
                prefix2 = "_lite"
            end
        else
            copyFile(averageTraffic.modDir .. "averageTraffic.xml", configFile, false)
        end

        local randomTraffic = math.random(1, 5)
        local randomFilename = string.format("%sxml/trafficSystem%s%s.xml", averageTraffic.modDir, randomTraffic, prefix2)
        local xml = loadXMLFile("mod_trafficSystems", randomFilename)

        local i = 0
        while true do
            local filename = getXMLString(xml, "trafficSystems.trafficSystem(" .. i .. ")#filename")
            if filename ~= nil and filename ~= "" then
                local xmlFilename = Utils.getFilename(filename, averageTraffic.modDir)
                local len = xmlFilename:len()
                local ext = xmlFilename:sub(len - 5)
                local name = Utils.getFilenameFromPath(xmlFilename)

                if ext == prefix .. ".xml" then
                    local lightsProfile = g_gameSettings:getValue("lightsProfile")
                    local useHighProfile = lightsProfile == GS_PROFILE_HIGH or lightsProfile == GS_PROFILE_VERY_HIGH
                    local trafficSystem = TrafficSystem.new(g_server ~= nil, g_client ~= nil)
            
                    if trafficSystem:load(xmlFilename, transformId, useHighProfile, g_server ~= nil, g_client ~= nil) then
                        trafficSystem:register(true)
                        g_currentMission.onCreateObjectSystem:add(trafficSystem)
                        trafficSystem:setEnabled(g_currentMission.missionInfo.trafficEnabled)
                        print("Average Traffic Info: TrafficSystem xml " .. tostring(name) .. " loaded!")
                    else
                        trafficSystem:delete()
                    end
            
                    g_currentMission.trafficSystem = trafficSystem
                end
            else
                break
            end
            i = i + 1
        end

        delete(xml)
    else
        print("Error: Missing xmlFile attribute for traffic system in " .. getName(transformId))
    end
end)