P16MaterialSlots = {}

function P16MaterialSlots.prerequisitesPresent(specializations)
    return true
end

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

local function getPaintRules(configFileName)
    configFileName = configFileName or ""

    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", [1] = "PACIFIC_body_mat", [2] = "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", [2] = "PACIFIC_body_mat"},
            hdx_rad = {[0] = "PACIFIC_body_mat", [1] = "PACIFIC_body_mat"},
            bunk = {[0] = "PACIFIC_accent_mat"},
            lstake = {[0] = "PACIFIC_accent_mat"},
            lcable = {[0] = "PACIFIC_accent_mat", [1] = "PACIFIC_accent_mat"},
            rstake = {[0] = "PACIFIC_accent_mat", [1] = "PACIFIC_accent_mat"},
            rcable = {[0] = "PACIFIC_accent_mat", [1] = "PACIFIC_accent_mat"},
            reciever = {[0] = "PACIFIC_accent_mat", [1] = "PACIFIC_accent_mat"}
        }
    end

    if configFileName:find("trailer.xml", 1, true) ~= nil then
        return {
            reach = {[0] = "p16Trailer_body_mat"},
            body = {[0] = "p16Trailer_body_mat"},
            bunk = {[0] = "p16Trailer_accent_mat"},
            axles = {[0] = "p16Trailer_body_mat"},
            beams = {[0] = "p16Trailer_body_mat"},
            stabilizerbars = {[0] = "p16Trailer_body_mat"},
            torsionbars = {[0] = "p16Trailer_body_mat"},
            loadstrap = {[0] = "p16Trailer_accent_mat", [1] = "p16Trailer_accent_mat"},
            lstake = {[0] = "p16Trailer_accent_mat"},
            lcable = {[0] = "p16Trailer_accent_mat", [1] = "p16Trailer_accent_mat"},
            rstake = {[0] = "p16Trailer_accent_mat", [1] = "p16Trailer_accent_mat"},
            rcable = {[0] = "p16Trailer_accent_mat", [1] = "p16Trailer_accent_mat"}
        }
    end

    return nil
end

local function applyMaterialSlots(node, rules)
    if node == nil or node == 0 then
        return
    end

    if getHasClassId(node, ClassIds.SHAPE) then
        local materialSlots = rules[getName(node)]
        if materialSlots ~= nil then
            local numMaterials = getNumOfMaterials(node)
            for materialIndex, slotName in pairs(materialSlots) do
                if materialIndex < numMaterials then
                    setMaterialSlotName(node, materialIndex, slotName)
                end
            end
        end
    end

    local numChildren = getNumOfChildren(node)
    for i = 0, numChildren - 1 do
        applyMaterialSlots(getChildAt(node, i), rules)
    end
end

local function reapplyMaterialConfiguration(vehicle, configurationName)
    if vehicle.configurations ~= nil
        and vehicle.configurations[configurationName] ~= nil
        and vehicle.applyBaseMaterialConfiguration ~= nil then
        vehicle:applyBaseMaterialConfiguration(vehicle.xmlFile, configurationName, vehicle.configurations[configurationName])
    end
end

function P16MaterialSlots:onLoad(savegame)
    local rules = getPaintRules(self.configFileName or self.xmlFilename)
    if rules == nil then
        return
    end

    applyMaterialSlots(self.rootNode, rules)
    reapplyMaterialConfiguration(self, "baseColor")
    reapplyMaterialConfiguration(self, "designColor")
end
