swathingPlus = {}
local modDirectory = g_currentModDirectory
local g_modName = g_currentModName

-- fillType Extender
if FillTypeManager.SEND_NUM_BITS < 9 then
    FillTypeManager.SEND_NUM_BITS = 9
    printf("%s: FillType limit increased to 511", g_modName)
end

-- Load our fill types right after game default fill types so they exist when fruit type converters load
function swathingPlus.registerFillTypes(self, superFunc)
    superFunc(self)
    local fillTypesPath = modDirectory .. "xml/fillTypes.xml"
    if fileExists(fillTypesPath) then
        local xml = loadXMLFile("swathingPlusFillTypes", fillTypesPath)
        if xml ~= nil then
            g_fillTypeManager:loadFillTypes(xml, modDirectory, false, nil)
            delete(xml)
            printf("%s: Loaded fill types (early) from xml/fillTypes.xml", g_modName)
        end
    end
end

FillTypeManager.loadDefaultTypes = Utils.overwrittenFunction(FillTypeManager.loadDefaultTypes, swathingPlus.registerFillTypes)

-- register fruitTypes.xml (runs after fill types; COMBINE_MOWER needs SORGHUM_CUT etc. to exist)
function swathingPlus.registerFruitTypes(self, superFunc)
    superFunc(self)

    local xml = loadXMLFile("swathingPlus", modDirectory.."xml/fruitTypes.xml")
    g_fruitTypeManager:loadFruitTypes(xml, nil, false)
    delete(xml)
end

FruitTypeManager.loadDefaultTypes = Utils.overwrittenFunction(FruitTypeManager.loadDefaultTypes, swathingPlus.registerFruitTypes)

-- add swath to crops
FruitTypeGroundTypeFix = {}

function FruitTypeGroundTypeFix:loadMap(name)
    local function configureFruit(fruitName, cutFillTypeName, patchGroundType)
        local fruit = g_fruitTypeManager:getFruitTypeByName(fruitName)
        if fruit == nil then
            fruit = g_fruitTypeManager:getFruitTypeByName(string.upper(fruitName))
        end
        if fruit == nil then
            print(string.format("%s: Fruit '%s' not found", g_modName, fruitName))
            return
        end

        -- Windrow setup (try lowercase then uppercase; XML defines e.g. SORGHUM_CUT)
        local windrowCutFillType = g_fillTypeManager:getFillTypeByName(cutFillTypeName)
        if windrowCutFillType == nil then
            windrowCutFillType = g_fillTypeManager:getFillTypeByName(string.upper(cutFillTypeName))
        end
        if windrowCutFillType == nil then
            print(string.format("%s: Fill type '%s' not found - cannot set swath for %s", g_modName, cutFillTypeName, fruitName))
            return
        end
        fruit.windrowLiterPerSqm = 3.68
        fruit.windrowCutFillType = windrowCutFillType
        fruit.windrowCutFactor = 1.25
        g_fruitTypeManager.windrowCutFillTypeIndexToFruitTypeIndex[windrowCutFillType.index] = fruit.index
        print(string.format("%s: Swath configured %s -> %s", g_modName, fruitName, cutFillTypeName))

        -- GroundType patch
        if patchGroundType then
            fruit.groundTypeChangeType = FieldGroundType.HARVEST_READY
            print(string.format("Patched groundTypeChangeType for %s → HARVEST_READY", fruitName))
        end
    end

					-- fruitType,swath_cut,push HARVEST_READY
    configureFruit("sorghum", "sorghum_cut", true)
    configureFruit("ricelonggrain", "ricelonggrain_cut", true)
end

addModEventListener(FruitTypeGroundTypeFix)
