-- TOG Tools — Settings panel
-- AceConfig-3.0 options table registered with the Blizzard Interface Options
-- (ESC → Options → Addons → TOG Tools).  Also opened directly via
-- addon:OpenSettings() — called by right-click on the minimap button.

local _, addon = ...
local Ace       = addon.lib

local AceConfig = LibStub("AceConfig-3.0",       true)
local AceDialog = LibStub("AceConfigDialog-3.0", true)

if not AceConfig or not AceDialog then return end

-- ---------------------------------------------------------------------------
-- Options table
-- ---------------------------------------------------------------------------
local OPTIONS = {
    name    = "TOG Tools",
    handler = addon,
    type    = "group",
    args    = {

        -- ---- Minimap button ------------------------------------------------
        minimapHeader = {
            name  = "Minimap Button",
            type  = "header",
            order = 1,
        },

        minimapButton = {
            name  = "Show minimap button",
            desc  = "Toggle the minimap button on or off.",
            type  = "toggle",
            order = 2,
            get   = function()
                return not (addon.DB and addon.DB.char.minimapHide)
            end,
            set   = function(_, val)
                if val then
                    addon:ShowMinimapButton()
                else
                    addon:HideMinimapButton()
                end
            end,
        },
    },
}

-- ---------------------------------------------------------------------------
-- Registration — hook OnInitialize so the DB is ready when we register.
-- Store the panel reference so OpenSettings() can navigate directly to it.
-- ---------------------------------------------------------------------------
local _blizPanel = nil
hooksecurefunc(Ace, "OnInitialize", function()
    AceConfig:RegisterOptionsTable("TOGTools", OPTIONS)
    _blizPanel = AceDialog:AddToBlizOptions("TOGTools", "TOG Tools")
end)

-- ---------------------------------------------------------------------------
-- Direct open — opens the native Blizzard Interface Options panel directly
-- to the TOG Tools page.  Called by RMB on the minimap button.
--
-- Detection order (API presence, not version flag):
--   1. Settings.OpenToCategory  — Retail 10.0+ AND modern Classic Era (1.15+)
--   2. InterfaceOptionsFrame_OpenToCategory — older Classic/TBC/Wrath builds
--   3. InterfaceOptionsFrame:Show()         — last-resort fallback
-- ---------------------------------------------------------------------------
local gv = addon.gameVersion
function addon:OpenSettings()
    if not _blizPanel then return end
    if Settings and Settings.OpenToCategory then
        -- Retail 10.0+ and modern Classic Era 1.15+
        Settings.OpenToCategory(_blizPanel.categoryID or "TOG Tools")
    elseif InterfaceOptionsFrame_OpenToCategory then
        -- Older Classic / TBC / Wrath builds
        InterfaceOptionsFrame_OpenToCategory(_blizPanel)
        InterfaceOptionsFrame_OpenToCategory(_blizPanel)
    elseif InterfaceOptionsFrame then
        InterfaceOptionsFrame:Show()
    end
end
