-- Modern Status ModOptions.
-- This keeps persistent UI access preferences outside the save-specific ModernStatusConfig.lua file.

local MOD_ID = "ModernStatus"
local SHOW_BLUE_COG_ID = "showBlueCogButton"

MODERN_STATUS_MODOPTIONS_LOADED = MODERN_STATUS_MODOPTIONS_LOADED or false

local function tr(key, fallback)
    if key and getTextOrNull then
        local value = getTextOrNull(key)
        if value and value ~= key then return value end
    end
    if key and getText then
        local ok, value = pcall(getText, key)
        if ok and value and value ~= key then return value end
    end
    return fallback or tostring(key or "")
end

local function toBool(value)
    -- Accept booleans, strings, and numeric values returned by ModOptions.
    return value == true or value == 1 or value == "1" or value == "true"
end

local function loadModOptionsOnce()
    -- ModOptions.ini is normally loaded after opening the options menu.
    -- Loading it once here lets saved values apply during initial UI creation.
    if MODERN_STATUS_MODOPTIONS_LOADED then
        return
    end
    if PZAPI and PZAPI.ModOptions and type(PZAPI.ModOptions.load) == "function" then
        pcall(function()
            PZAPI.ModOptions:load()
        end)
    end
    MODERN_STATUS_MODOPTIONS_LOADED = true
end

function ModernStatus_shouldShowBlueCogButton()
    loadModOptionsOnce()

    local options = PZAPI and PZAPI.ModOptions and PZAPI.ModOptions:getOptions(MOD_ID)
    local option = options and options:getOption(SHOW_BLUE_COG_ID)
    if not option then
        -- The new Health-button context entry is the default access path.
        return false
    end
    return toBool(option:getValue())
end

function ModernStatus_applyBlueCogButtonVisibility(enabled)
    enabled = enabled == true
    if not (StatusWidget and StatusWidget.globalControls) then
        return
    end

    for _, control in pairs(StatusWidget.globalControls) do
        if control and control.setVisible then
            control:setVisible(enabled)
        end
    end
end

local function requestBlueCogButtonVisibility(enabled)
    ModernStatus_applyBlueCogButtonVisibility(enabled == true)
end

local function initModernStatusModOptions()
    if not (PZAPI and PZAPI.ModOptions) then
        return
    end

    local options = PZAPI.ModOptions:create(MOD_ID, tr("IGUI_ModernStatus_ModName", "Modern Status"))
    options:addTickBox(
        SHOW_BLUE_COG_ID,
        tr("IGUI_ModernStatus_ShowBlueGearButton", "Show blue gear button"),
        false,
        tr("IGUI_ModernStatus_ShowBlueGearButton_Tooltip", "Shows the old blue gear button. The new global settings panel can be opened by right-clicking the Health button with the heart icon on the left sidebar.")
    )

    loadModOptionsOnce()

    local option = options:getOption(SHOW_BLUE_COG_ID)
    if option then
        option.onChange = function(self, selected)
            requestBlueCogButtonVisibility(toBool(selected))
        end
        option.onChangeApply = function(self, selected)
            requestBlueCogButtonVisibility(toBool(selected))
        end
    end

    requestBlueCogButtonVisibility(ModernStatus_shouldShowBlueCogButton())
end

if Events and Events.OnGameBoot then
    Events.OnGameBoot.Add(initModernStatusModOptions)
end

return true
