P16MaterialSlots = {}

function P16MaterialSlots.prerequisitesPresent(specializations)
    return true
end

function P16MaterialSlots.registerEventListeners(vehicleType)
    SpecializationUtil.registerEventListener(vehicleType, "onLoad", P16MaterialSlots)
end

local function setShapeSlots(node, rules)
    if getHasClassId(node, ClassIds.SHAPE) then
        local slots = rules[getName(node)]

        if slots ~= nil then
            local numMaterials = getNumOfMaterials(node)

            for materialIndex, slotName in pairs(slots) do
                if materialIndex < numMaterials then
                    setMaterialSlotName(node, materialIndex, slotName)
                end
            end
        end
    end

    for i = 0, getNumOfChildren(node) - 1 do
        setShapeSlots(getChildAt(node, i), rules)
    end
end

local function reapplyColorConfiguration(vehicle, configName)
    if vehicle.configurations == nil or vehicle.configurations[configName] == nil then
        return
    end

    if vehicle.applyBaseMaterialConfiguration ~= nil then
        vehicle:applyBaseMaterialConfiguration(vehicle.xmlFile, configName, vehicle.configurations[configName])
    end
end

local function getRules(configFileName)
    if configFileName:find("PACIFIC.xml", 1, true) ~= nil then
        return {
            bumper = {[0] = "PACIFIC_accent_mat"},
            cab = {[0] = "PACIFIC_body_mat", [1] = "PACIFIC_body_mat", [2] = "PACIFIC_body_mat"},
            fenders = {[0] = "PACIFIC_body_mat"},
            filters = {[0] = "PACIFIC_body_mat"},
            guard = {[0] = "PACIFIC_body_mat"},
            hood = {[0] = "PACIFIC_body_mat"},
            frame = {[0] = "PACIFIC_accent_mat"},
            frontsusp = {[0] = "PACIFIC_accent_mat"},
            rearsusp = {[0] = "PACIFIC_accent_mat"},
            headlights = {[0] = "PACIFIC_body_mat"},
            watertank = {[0] = "PACIFIC_body_mat"},
            stack = {[0] = "PACIFIC_body_mat"},
            grill = {[0] = "PACIFIC_body_mat", [1] = "PACIFIC_body_mat"},
            guard2 = {[0] = "PACIFIC_body_mat"},
            visor = {[0] = "PACIFIC_accent_mat"},
            ["bumper.001"] = {[0] = "PACIFIC_body_mat"},
            hdx_grill = {[0] = "PACIFIC_body_mat", [1] = "PACIFIC_body_mat"},
            hdx_rad = {[0] = "PACIFIC_body_mat", [1] = "PACIFIC_body_mat"},
            reciever = {[0] = "PACIFIC_accent_mat"}
        }
    end

    if configFileName:find("p16BtrainLead", 1, true) ~= nil then
        return {
            Feet = {[0] = "p16BtrainLead_body_mat"},
            Bunk = {[0] = "p16BtrainLead_accent_mat"},
            BunkBrackets = {[0] = "p16BtrainLead_accent_mat"},
            Main = {
                [0] = "p16BtrainLead_body_mat",
                [1] = "p16BtrainLead_body_mat",
                [5] = "p16BtrainLead_body_mat"
            }
        }
    end

    if configFileName:find("p16BtrainPup", 1, true) ~= nil then
        return {
            Feet = {[0] = "p16BtrainPup_body_mat"},
            Bunk = {[0] = "p16BtrainPup_accent_mat"},
            BunkBrackets = {[0] = "p16BtrainPup_accent_mat"},
            Pup = {
                [0] = "p16BtrainPup_body_mat",
                [1] = "p16BtrainPup_body_mat",
                [3] = "p16BtrainPup_body_mat",
                [5] = "p16BtrainPup_body_mat"
            }
        }
    end

    if configFileName:find("p162Bunktrailer", 1, true) ~= nil then
        return {
            Feet = {[0] = "p162BunkTrailer_body_mat"},
            Bunk = {[0] = "p162BunkTrailer_body_mat"},
            BunkBrackets = {[0] = "p162BunkTrailer_body_mat"},
            tra = {[0] = "p162BunkTrailer_body_mat"}
        }
    end

    if configFileName:find("lowdeck.xml", 1, true) ~= nil then
        return {
            body_vis = {[0] = "p16Lowbed_body_mat"},
            supportLeg = {[0] = "p16Lowbed_body_mat"},
            hydraulic_base = {[0] = "p16Lowbed_body_mat"},
            hydraulic_punch = {[0] = "p16Lowbed_body_mat"},
            ramp_base = {[0] = "p16Lowbed_body_mat"},
            rampR1 = {[0] = "p16Lowbed_body_mat"},
            rampR2 = {[0] = "p16Lowbed_body_mat"},
            rampLock_R = {[0] = "p16Lowbed_body_mat"},
            rampL1 = {[0] = "p16Lowbed_body_mat"},
            rampL2 = {[0] = "p16Lowbed_body_mat"},
            rampLock_L = {[0] = "p16Lowbed_body_mat"},
            hitchpiece = {[0] = "p16Lowbed_body_mat", [1] = "p16Lowbed_accent_mat"}
        }
    end

    return nil
end

function P16MaterialSlots:onLoad(savegame)
    local rules = getRules(self.configFileName or "")

    if rules == nil or self.components == nil then
        return
    end

    for _, component in ipairs(self.components) do
        setShapeSlots(component.node, rules)
    end

    reapplyColorConfiguration(self, "baseColor")
    reapplyColorConfiguration(self, "designColor")
end
