-- FS25 Working Width Options - JD v1.0.0.1
-- Adds the current working-width/guide state to the real F1 help HUD.
-- F2 toggle itself remains handled by F2Toggle.lua raw key listener.

JDWidthGuideF1HUD = JDWidthGuideF1HUD or {}
JDWidthGuideF1HUD.ACTION_NAME = "JD94_WIDTH_GUIDE_HUD"

local function jdWWF1GetAction()
    if InputAction == nil then
        return nil
    end
    return InputAction[JDWidthGuideF1HUD.ACTION_NAME]
end

local function jdWWF1FindVehicle(rootVehicle)
    if rootVehicle == nil then
        return nil
    end

    if JDWidthGuideF2 ~= nil and JDWidthGuideF2.getHudVehicle ~= nil then
        return JDWidthGuideF2.getHudVehicle(rootVehicle)
    end

    return nil
end

local function jdWWF1GuidesVisible(vehicle)
    if vehicle == nil then
        return false
    end

    if JDWidthGuideF2 ~= nil and JDWidthGuideF2.getGuideVisibilityForVehicle ~= nil then
        return JDWidthGuideF2.getGuideVisibilityForVehicle(vehicle)
    end

    return vehicle.jdWorkingWidthGuidesVisible == true
end

function JDWidthGuideF1HUD.getText(rootVehicle)
    local widthVehicle = jdWWF1FindVehicle(rootVehicle)
    if widthVehicle == nil then
        return nil
    end

    local width = widthVehicle.jdWorkingWidthCurrentWidth
    if width == nil or width <= 0 then
        return nil
    end

    local guidesOn = jdWWF1GuidesVisible(widthVehicle)
    return string.format("Working Width: %.0f m | Guides: %s", width, guidesOn and "ON" or "OFF")
end

function JDWidthGuideF1HUD.actionEventNoOp(self, actionName, inputValue, callbackState, isAnalog)
    -- Intentionally empty. F2Toggle.lua handles the actual raw F2 toggle.
end

function JDWidthGuideF1HUD.onRegisterActionEvents(self, isActiveForInput, isActiveForInputIgnoreSelection)
    if self == nil or not self.isClient or not isActiveForInputIgnoreSelection then
        return
    end

    local inputAction = jdWWF1GetAction()
    if inputAction == nil then
        return
    end

    local text = JDWidthGuideF1HUD.getText(self)
    if text == nil then
        if self.jdWorkingWidthF1ActionEvents ~= nil and self.clearActionEventsTable ~= nil then
            self:clearActionEventsTable(self.jdWorkingWidthF1ActionEvents)
        end
        return
    end

    self.jdWorkingWidthF1ActionEvents = self.jdWorkingWidthF1ActionEvents or {}
    self:clearActionEventsTable(self.jdWorkingWidthF1ActionEvents)

    local _, actionEventId = self:addActionEvent(
        self.jdWorkingWidthF1ActionEvents,
        inputAction,
        self,
        JDWidthGuideF1HUD.actionEventNoOp,
        false,
        true,
        false,
        true,
        nil
    )

    if actionEventId ~= nil then
        g_inputBinding:setActionEventText(actionEventId, text)
        g_inputBinding:setActionEventTextVisibility(actionEventId, true)
        g_inputBinding:setActionEventTextPriority(actionEventId, GS_PRIO_HIGH)
        self.jdWorkingWidthF1ActionEventId = actionEventId
    end
end

-- Motorized is present on tractors, harvesters and other self-propelled vehicles.
-- Appending here puts the line into the same F1 action list as the base-game controls.
if Motorized ~= nil and Motorized.onRegisterActionEvents ~= nil then
    Motorized.onRegisterActionEvents = Utils.appendedFunction(
        Motorized.onRegisterActionEvents,
        JDWidthGuideF1HUD.onRegisterActionEvents
    )
    Logging.info("[JDWorkingWidth] v1.0.0.1 F1 HUD hook installed")
else
    Logging.warning("[JDWorkingWidth] F1 HUD hook could not find Motorized.onRegisterActionEvents")
end
