-- Lampovo Project ^ω^ Average Enjoyer

StrawHarvestPackExtension = {}

local modDir = g_currentModDirectory
local modName = g_currentModName

function StrawHarvestPackExtension.addParticleTypeIfNotExists(name)
    if g_particleSystemManager:getParticleSystem(name) == nil then
        g_particleSystemManager:addParticleType(name)
    end
end

Mission00.load = Utils.prependedFunction(Mission00.load, function (self, ...)
    StrawHarvestPackExtension.addParticleTypeIfNotExists("PELLET_SMOKE")
    StrawHarvestPackExtension.addParticleTypeIfNotExists("PELLET")
    StrawHarvestPackExtension.addParticleTypeIfNotExists("PELLET_MOVE")
end)

if g_modIsLoaded["pdlc_strawHarvestPack"] then
    FillUnit.onLoad = Utils.appendedFunction(FillUnit.onLoad, function (self, savegame, ...)
        local test = string.upper(self.xmlFile:getValue("vehicle#type", ""))
        if test ~= nil then
            if string.upper(test) == "KRONEPREMOS5000" then
                for _, fillUnit in pairs(self:getFillUnits()) do
                    if fillUnit.supportedFillTypes[FillType.STRAW_PELLETS] or fillUnit.supportedFillTypes[FillType.HAY_PELLETS] then
                        fillUnit.supportedFillTypes[FillType.MAIZESTALKS_PELLETS] = true
                    end
                end
            end
        end
    end)

    local StrawHarvestStorageBuffer = g_modIsLoaded["pdlc_strawHarvestPack"] and _G['pdlc_strawHarvestPack'].StrawHarvestStorageBuffer
    StrawHarvestStorageBuffer.load = Utils.overwrittenFunction(StrawHarvestStorageBuffer.load, function (self, superFunc, nodeId, xmlFile, key, i3dMappings, target, ...)
        superFunc(self, nodeId, xmlFile, key, i3dMappings, target, ...)
        if self.supportedFillTypes ~= nil then
            local hasStrawPellets = self.supportedFillTypes[FillType.STRAW_PELLETS] == true
            local hasHayPellets   = self.supportedFillTypes[FillType.HAY_PELLETS] == true

            if hasStrawPellets or hasHayPellets then
                if not self.supportedFillTypes[FillType.MAIZESTALKS_PELLETS] then
                    self.supportedFillTypes[FillType.MAIZESTALKS_PELLETS] = true
                    table.insert(self.sortedFillTypes, FillType.MAIZESTALKS_PELLETS)
                    table.sort(self.sortedFillTypes)
                end
            end
        end
        self:empty()
        return true
    end)

    local StrawHarvestPalletizer = g_modIsLoaded["pdlc_strawHarvestPack"] and _G['pdlc_strawHarvestPack'].StrawHarvestPalletizer
    StrawHarvestPalletizer.loadBagCreations = Utils.appendedFunction(StrawHarvestPalletizer.loadBagCreations, function (self, xmlFile, baseKey, ...)
        local maizeIndex = FillType.MAIZESTALKS_PELLETS
        if maizeIndex == nil then
            return
        end

        if self.fillTypeToCloneableBag[maizeIndex] ~= nil then
            return
        end

        local sourceIndex = nil

        if self.fillTypeToCloneableBag[FillType.STRAW_PELLETS] then
            sourceIndex = FillType.STRAW_PELLETS
        elseif self.fillTypeToCloneableBag[FillType.HAY_PELLETS] then
            sourceIndex = FillType.HAY_PELLETS
        end

        if sourceIndex == nil then
            return
        end

        local cloneNode = self.fillTypeToCloneableBag[sourceIndex]
        self.fillTypeToCloneableBag[maizeIndex] = cloneNode

        local decisionWeight = self.fillTypeToDecisionWeight[sourceIndex] or 1
        self.fillTypeToDecisionWeight[maizeIndex] = decisionWeight

        local palletPath = Utils.getFilename("objects/pallets/pelletPallets/maizeStalksPelletsPallet.xml", modDir)
        self.fillTypeToPalletXML[maizeIndex] = palletPath
    end)
end