-- FeedingRobot_Extension
-- Version: 1.0.0.0
-- Author: Tidde

if g_feedingRobotExtension ~= nil then
    FeedingRobotExtension = g_feedingRobotExtension
else
    FeedingRobotExtension = {}
    g_feedingRobotExtension = FeedingRobotExtension
end
if FeedingRobotExtension._initialized then
    return
end
FeedingRobotExtension._initialized = true

FeedingRobotExtension.debug = false

FeedingRobotExtension.config = {
    SILAGE = "GRASS_FERMENTED ALFALFA_FERMENTED CLOVER_FERMENTED",
    STRAW = "",
    DRYGRASS_WINDROW = "DRYALFALFA_WINDROW DRYCLOVER_WINDROW",
    MINERAL_FEED = ""
}

local function onFeedingRobotLoad(self, superFunc, linkNode, filename, onLoaded, target, args)
    local xmlFile = XMLFile.load("tempFeedingRobot", filename, FeedingRobot.xmlSchema)
    
    if xmlFile ~= nil then
        xmlFile:iterate("feedingRobot.unloadingSpots.unloadingSpot", function(_, spotKey)
            local currentFillTypes = xmlFile:getString(spotKey .. "#fillTypes") or ""
            local upperCurrentFillTypes = string.upper(currentFillTypes)
            
            for baseType, additions in pairs(FeedingRobotExtension.config) do
                if additions ~= "" and string.find(upperCurrentFillTypes, string.upper(baseType)) then
                    
                    local trimmedAdditions = string.trim(additions)
                    xmlFile:setString(spotKey .. "#fillTypes", string.trim(currentFillTypes .. " " .. trimmedAdditions))
                    
                    local baleTriggerKey = spotKey .. ".baleTrigger"
                    if xmlFile:hasProperty(baleTriggerKey) then
                        local baleTypes = xmlFile:getString(baleTriggerKey .. "#fillTypes") or ""
                        xmlFile:setString(baleTriggerKey .. "#fillTypes", string.trim(baleTypes .. " " .. trimmedAdditions))
                    end
                    
                    local conversionIndex = 0
                    while xmlFile:hasProperty(string.format("%s.fillTypeConversion(%d)", spotKey, conversionIndex)) do
                        conversionIndex = conversionIndex + 1
                    end
                    
                    local additionTypes = string.split(additions, " ")
                    for _, additionType in ipairs(additionTypes) do
                        local cleanedType = string.trim(additionType)
                        if cleanedType ~= "" then
                            local conversionKey = string.format("%s.fillTypeConversion(%d)", spotKey, conversionIndex)
                            xmlFile:setString(conversionKey .. "#incomingFillType", cleanedType)
                            xmlFile:setString(conversionKey .. "#outgoingFillType", baseType)
                            xmlFile:setFloat(conversionKey .. "#ratio", 1.0)
                            conversionIndex = conversionIndex + 1
                        end
                    end
                end
            end
        end)
        
        local originalXMLLoad = XMLFile.load
        XMLFile.load = function(name, fname, schema)
            if fname == filename then
                XMLFile.load = originalXMLLoad 
                return xmlFile
            end
            return originalXMLLoad(name, fname, schema)
        end
    end

    return superFunc(self, linkNode, filename, onLoaded, target, args)
end

FeedingRobot.load = Utils.overwrittenFunction(FeedingRobot.load, onFeedingRobotLoad)