-- BaleFermentation_Grass_fermented.lua
-- Version 1.2.0.0
-- Autor: Tidde23

BaleFermentationOverride = {}

local BaleFermentationOverride_mt = Class(BaleFermentationOverride)

function BaleFermentationOverride.copyFillTypeInfo(sourceFillTypeInfo)
    local newFillTypeInfo = {}

    for key, value in pairs(sourceFillTypeInfo) do
        if type(value) == "table" then
            newFillTypeInfo[key] = {}

            for subKey, subValue in pairs(value) do
                newFillTypeInfo[key][subKey] = subValue
            end
        else
            newFillTypeInfo[key] = value
        end
    end

    return newFillTypeInfo
end

function BaleFermentationOverride.loadFillTypesFromXML(fillTypes, xmlFile, baseDirectory)
    local grassFermentedIndex = g_fillTypeManager:getFillTypeIndexByName("GRASS_FERMENTED")
    if grassFermentedIndex == nil then
        return
    end

    local newFillTypeInfo = nil
    local grassWindrowFillTypeInfo = nil

    for _, fillTypeInfo in ipairs(fillTypes) do
        local fillTypeName = g_fillTypeManager:getFillTypeNameByIndex(fillTypeInfo.fillTypeIndex)

        if fillTypeName == "GRASS_WINDROW" and fillTypeInfo.fermenting ~= nil then
            grassWindrowFillTypeInfo = fillTypeInfo
            local originalOutputFillTypeIndex = fillTypeInfo.fermenting.outputFillTypeIndex

            fillTypeInfo.fermenting.outputFillTypeIndex = grassFermentedIndex

            if originalOutputFillTypeIndex ~= nil and newFillTypeInfo == nil then
                for _, sourceFillTypeInfo in ipairs(fillTypes) do
                    if sourceFillTypeInfo.fillTypeIndex == originalOutputFillTypeIndex then
                        newFillTypeInfo = BaleFermentationOverride.copyFillTypeInfo(sourceFillTypeInfo)
                        newFillTypeInfo.fillTypeIndex = grassFermentedIndex
                        newFillTypeInfo.fermenting = nil
                        break
                    end
                end
            end
        end
    end

    if newFillTypeInfo == nil and grassWindrowFillTypeInfo ~= nil then
        newFillTypeInfo = BaleFermentationOverride.copyFillTypeInfo(grassWindrowFillTypeInfo)
        newFillTypeInfo.fillTypeIndex = grassFermentedIndex
        newFillTypeInfo.fermenting = nil
    end

    if newFillTypeInfo ~= nil and grassWindrowFillTypeInfo ~= nil then
        newFillTypeInfo.supportsWrapping = grassWindrowFillTypeInfo.supportsWrapping

        if grassWindrowFillTypeInfo.wrapDiffuse ~= nil then
            newFillTypeInfo.wrapDiffuse = grassWindrowFillTypeInfo.wrapDiffuse
        end

        if grassWindrowFillTypeInfo.wrapNormal ~= nil then
            newFillTypeInfo.wrapNormal = grassWindrowFillTypeInfo.wrapNormal
        end
    end

    if newFillTypeInfo ~= nil then
        local alreadyExists = false

        for _, fillTypeInfo in ipairs(fillTypes) do
            if fillTypeInfo.fillTypeIndex == grassFermentedIndex then
                alreadyExists = true
                break
            end
        end

        if not alreadyExists then
            table.insert(fillTypes, newFillTypeInfo)
        end
    end
end

Bale.loadFillTypesFromXML = Utils.appendedFunction(Bale.loadFillTypesFromXML, BaleFermentationOverride.loadFillTypesFromXML)
