-- WarBoard_Options.lua
-- Settings panel registered under Options → AddOns → War Board, using the modern retail
-- Settings API (Settings.RegisterVerticalLayoutCategory → native checkboxes → RegisterAddOnCategory).
--
-- Options are stored per character in WarBoardCharDB.options and read live via WB.Options.Get(key),
-- so toggles take effect immediately.

local addonName, WB = ...
WB.Options = WB.Options or {}
local O = WB.Options

local DEFAULTS = {
    -- Show the TalkingHead frame for objective voice lines. When OFF, the line is printed in
    -- chat (as NPC speech) instead — the voice still plays (unless talkingHeadSound is also off).
    talkingHeadFrames = true,
    -- Play the voice/sound for objective TalkingHead lines (frame OR chat presentation).
    talkingHeadSound  = true,
    -- Reparent the shard button to UIParent so it remains visible and draggable.
    detachShardTracker = false,
}

local function DB()
    WarBoardCharDB = WarBoardCharDB or {}
    WarBoardCharDB.options = WarBoardCharDB.options or {}
    return WarBoardCharDB.options
end

function O.Get(key)
    local v = DB()[key]
    if v == nil then return DEFAULTS[key] end
    return v
end

function O.Set(key, value)
    DB()[key] = value
    if key == "detachShardTracker" and WB.ShardPill and WB.ShardPill.SetDetached then
        WB.ShardPill.SetDetached(value)
    end
end

-- ── Settings panel registration ────────────────────────────────────────────────
local function Register()
    if O._registered then return end
    if not (Settings and Settings.RegisterVerticalLayoutCategory and Settings.RegisterProxySetting) then
        return   -- pre-Settings-API client; nothing to register
    end
    O._registered = true

    local category = Settings.RegisterVerticalLayoutCategory("War Board")

    local function checkbox(key, label, tooltip)
        local default = DEFAULTS[key]
        local setting = Settings.RegisterProxySetting(
            category, "WarBoard_" .. key, type(default), label, default,
            function() return O.Get(key) end,
            function(v) O.Set(key, v) end)
        Settings.CreateCheckbox(category, setting, tooltip)
        return setting
    end

    checkbox("talkingHeadFrames", "Talking Head Frames",
        "Show the TalkingHead frame when a faction leader speaks near an objective.\n\n"
        .. "When off, the line is printed in chat as NPC speech instead — the voice still plays.")

    checkbox("talkingHeadSound", "Talking Head Sound",
        "Play the voice/sound for objective Talking Head lines.\n\n"
        .. "Turn this off (and Talking Head Frames off) for a silent chat-only message.")

    checkbox("detachShardTracker", "Detach Shard Tracker",
        "Detach the shard button from the War Board so it remains available on screen and can be moved.\n\n"
        .. "Turn this off to return it to its original position on the War Board.")

    Settings.RegisterAddOnCategory(category)
    O._category = category
end

-- Open straight to the War Board panel (used by /wb options).
function O.Open()
    if not (O._category and Settings and Settings.OpenToCategory) then return end
    Settings.OpenToCategory(O._category:GetID())
end

local f = CreateFrame("Frame")
f:RegisterEvent("PLAYER_LOGIN")   -- SavedVariables are loaded by now
f:SetScript("OnEvent", function(self)
    self:UnregisterEvent("PLAYER_LOGIN")
    Register()
end)
