-- Lampovo Project ^ω^ Average Enjoyer

local function inj_mixerWagon_onLoad(vehicle, savegame)
    local spec = vehicle.spec_mixerWagon

    for _, fillUnit in pairs(vehicle:getFillUnits()) do
        if fillUnit.supportedFillTypes[FillType.DRYGRASS_WINDROW] then
            fillUnit.supportedFillTypes[FillType.MAIZESTALKS] = true
            fillUnit.supportedFillTypes[FillType.MAIZESTALKS_PELLETS] = true
        end
    end

    for _, mixerWagonFillType in ipairs(spec.mixerWagonFillTypes) do
        if mixerWagonFillType.fillTypes[FillType.DRYGRASS_WINDROW] ~= nil then
            mixerWagonFillType.fillTypes[FillType.MAIZESTALKS] = true
            spec.fillTypeToMixerWagonFillType[FillType.MAIZESTALKS] = mixerWagonFillType
            mixerWagonFillType.fillTypes[FillType.MAIZESTALKS_PELLETS] = true
            spec.fillTypeToMixerWagonFillType[FillType.MAIZESTALKS_PELLETS] = mixerWagonFillType
        end
    end
end

MixerWagon.onLoad = Utils.appendedFunction(MixerWagon.onLoad, inj_mixerWagon_onLoad)

local function inj_mixerWagon_addFillUnitFillLevel(vehicle, injSuperFunc, superFunc, farmId, fillUnitIndex, fillLevelDelta, fillTypeIndex, toolType, fillPositionData)
    local delta = injSuperFunc(vehicle, superFunc, farmId, fillUnitIndex, fillLevelDelta, fillTypeIndex, toolType, fillPositionData)

    if fillTypeIndex == FillType.MAIZESTALKS or fillTypeIndex == FillType.MAIZESTALKS_PELLETS then
        delta = delta * 0.25
    end

    return delta
end

MixerWagon.addFillUnitFillLevel = Utils.overwrittenFunction(MixerWagon.addFillUnitFillLevel, inj_mixerWagon_addFillUnitFillLevel)

local function inj_unloadingStation_addFillLevelFromTool(unloadingStation, superFunc, farmId, deltaFillLevel, fillType, fillInfo, toolType, extraAttributes)
    local delta = superFunc(unloadingStation, farmId, deltaFillLevel, fillType, fillInfo, toolType, extraAttributes)

    if unloadingStation.owningPlaceable == nil then
        return delta
    end

    if unloadingStation.owningPlaceable.spec_husbandryFood ~= nil and (fillType == FillType.MAIZESTALKS or fillType == FillType.MAIZESTALKS_PELLETS or fillType == FillType.MAIZECOBWASTE) then
        delta = delta * 0.25
    end

    return delta
end

UnloadingStation.addFillLevelFromTool = Utils.overwrittenFunction(UnloadingStation.addFillLevelFromTool, inj_unloadingStation_addFillLevelFromTool)

local function inj_placeableHusbandryFood_addFood(placeableHusbandryFood, superFunc, farmId, deltaFillLevel, fillTypeIndex, fillPositionData, toolType, extraAttributes)
    local delta = superFunc(placeableHusbandryFood, farmId, deltaFillLevel, fillTypeIndex, fillPositionData, toolType, extraAttributes)

    if fillTypeIndex == FillType.MAIZESTALKS or fillTypeIndex == FillType.MAIZESTALKS_PELLETS or fillTypeIndex == FillType.MAIZECOBWASTE then
        delta = delta * 0.25
    end

    return delta
end

PlaceableHusbandryFood.addFood = Utils.overwrittenFunction(PlaceableHusbandryFood.addFood, inj_placeableHusbandryFood_addFood)

local function inj_getFillTypesFromXML(animalFoodSystem, superFunc, fillTypes, usedFillTypes, xmlFile, key)
    if not superFunc(animalFoodSystem, fillTypes, usedFillTypes, xmlFile, key) then
        return false
    end

    for _, fillTypeIndex in ipairs(fillTypes) do

        if fillTypeIndex == FillType.DRYGRASS_WINDROW then
            table.addElement(fillTypes, FillType.MAIZESTALKS)
            table.addElement(fillTypes, FillType.MAIZESTALKS_PELLETS)
        end

        if fillTypeIndex == FillType.GRASS_WINDROW then
            table.addElement(fillTypes, FillType.MAIZECOBWASTE)
        end
    end

    return true
end

AnimalFoodSystem.getFillTypesFromXML = Utils.overwrittenFunction(AnimalFoodSystem.getFillTypesFromXML, inj_getFillTypesFromXML)