-- FS25 Working Width Options - JD v1.0.0.1
-- Shop configuration injection based directly on WorkArea XML, not vehicle type names.

JDWorkingWidth = JDWorkingWidth or {}
JDWorkingWidth.WIDTH_CONFIG_NAME = "jdWorkingWidth"
JDWorkingWidth.GUIDE_CONFIG_NAME = "jdCropGuides"

local function addConfigurationItem(items, configName, index, name, isDefault)
    local item = VehicleConfigurationItem.new(configName)
    if item.setIndex ~= nil then
        item:setIndex(index)
    else
        item.index = index
    end
    item.name = name
    item.saveId = tostring(index)
    item.price = 0
    item.isDefault = isDefault == true
    table.insert(items, item)
end

local function isWorkAreaStoreItem(manager, xmlFile, key)
    if manager ~= g_vehicleConfigurationManager or xmlFile == nil or key == nil then
        return false
    end

    -- GIANTS WorkArea reads either vehicle.workAreas.workArea(?) directly,
    -- or a configured workAreaConfiguration under vehicle.workAreas.
    return xmlFile:hasProperty(key .. ".workAreas.workArea(0)")
        or xmlFile:hasProperty(key .. ".workAreas.workAreaConfigurations.workAreaConfiguration(0)")
end

local superGetConfigurationsFromXML = ConfigurationUtil.getConfigurationsFromXML
ConfigurationUtil.getConfigurationsFromXML = function(manager, xmlFile, key, baseDir, customEnvironment, isMod, storeItem)
    local configurations, defaultConfigurationIds =
        superGetConfigurationsFromXML(manager, xmlFile, key, baseDir, customEnvironment, isMod, storeItem)

    if not isWorkAreaStoreItem(manager, xmlFile, key) then
        return configurations, defaultConfigurationIds
    end

    configurations = configurations or {}
    defaultConfigurationIds = defaultConfigurationIds or {}

    if configurations[JDWorkingWidth.WIDTH_CONFIG_NAME] == nil then
        local items = {}
        addConfigurationItem(items, JDWorkingWidth.WIDTH_CONFIG_NAME, 1, "Default", true)
        addConfigurationItem(items, JDWorkingWidth.WIDTH_CONFIG_NAME, 2, "25 m", false)
        addConfigurationItem(items, JDWorkingWidth.WIDTH_CONFIG_NAME, 3, "50 m", false)
        configurations[JDWorkingWidth.WIDTH_CONFIG_NAME] = items
        defaultConfigurationIds[JDWorkingWidth.WIDTH_CONFIG_NAME] = ConfigurationUtil.getDefaultConfigIdFromItems(items)
    end

    if configurations[JDWorkingWidth.GUIDE_CONFIG_NAME] == nil then
        local items = {}
        addConfigurationItem(items, JDWorkingWidth.GUIDE_CONFIG_NAME, 1, "Green", true)
        addConfigurationItem(items, JDWorkingWidth.GUIDE_CONFIG_NAME, 2, "White", false)
        addConfigurationItem(items, JDWorkingWidth.GUIDE_CONFIG_NAME, 3, "Red", false)
        addConfigurationItem(items, JDWorkingWidth.GUIDE_CONFIG_NAME, 4, "Orange", false)
        addConfigurationItem(items, JDWorkingWidth.GUIDE_CONFIG_NAME, 5, "Yellow", false)
        addConfigurationItem(items, JDWorkingWidth.GUIDE_CONFIG_NAME, 6, "Blue", false)
        addConfigurationItem(items, JDWorkingWidth.GUIDE_CONFIG_NAME, 7, "Purple", false)
        addConfigurationItem(items, JDWorkingWidth.GUIDE_CONFIG_NAME, 8, "Pink", false)
        configurations[JDWorkingWidth.GUIDE_CONFIG_NAME] = items
        defaultConfigurationIds[JDWorkingWidth.GUIDE_CONFIG_NAME] = ConfigurationUtil.getDefaultConfigIdFromItems(items)
    end

    local filename = storeItem ~= nil and storeItem.xmlFilename or (xmlFile.filename or "unknown")
    Logging.info("[JDWorkingWidth] SHOP OPTIONS ADDED '%s'", tostring(filename))

    return configurations, defaultConfigurationIds
end

-- Register after GIANTS has created the vehicle configuration manager.
if g_vehicleConfigurationManager ~= nil then
    g_vehicleConfigurationManager:addConfigurationType(
        JDWorkingWidth.WIDTH_CONFIG_NAME,
        "Working Width",
        JDWorkingWidth.WIDTH_CONFIG_NAME,
        VehicleConfigurationItem
    )

    g_vehicleConfigurationManager:addConfigurationType(
        JDWorkingWidth.GUIDE_CONFIG_NAME,
        "Crop Guide Color",
        JDWorkingWidth.GUIDE_CONFIG_NAME,
        VehicleConfigurationItem
    )
end

Logging.info("[JDWorkingWidth] v1.0.0.1 Register.lua loaded")
