local addonName, ns = ...

------------------------------------------------------------
-- Settings panel (Options → AddOns → Housing Herald).
--
-- The tracker checkbox is backed by the boolean HousingHeraldDB.useBlizzardTracker,
-- but the canonical value is the trackerMode string used by InitTracker and the
-- /hherald tracker command. SetTrackerMode keeps the two in sync, so the panel
-- and the slash command never diverge.
------------------------------------------------------------

function ns:RegisterSettingsPanel()
    if not (Settings and Settings.RegisterVerticalLayoutCategory) then return end

    local category = Settings.RegisterVerticalLayoutCategory("Housing Herald")

    -- The second argument is the client-global setting *variable* name,
    -- shared across every addon's settings; namespace it so it can't collide
    -- with another addon's setting of the same generic name. The third
    -- argument (variableKey) stays "useBlizzardTracker" so ns.db.useBlizzardTracker
    -- remains the backing field.
    local trackerSetting = Settings.RegisterAddOnSetting(
        category,
        "HousingHerald_useBlizzardTracker",
        "useBlizzardTracker",
        ns.db,
        type(false),
        "Use Blizzard's objective tracker",
        false
    )
    ns._trackerSetting = trackerSetting

    -- Route the checkbox through SetTrackerMode: it writes the canonical
    -- trackerMode and applies the change (standalone immediately; Blizzard on
    -- the next /reload).
    trackerSetting:SetValueChangedCallback(function(_, value)
        ns:SetTrackerMode(value and "blizzard" or "standalone")
    end)

    Settings.CreateCheckbox(
        category,
        trackerSetting,
        "Show the shopping list inside Blizzard's objective tracker instead of "
            .. "the addon's own window. On rare occasions this can cause Lua "
            .. "errors elsewhere in the interface; leave it unticked to use the "
            .. "standalone Housing Herald tracker, which looks the same and "
            .. "cannot cause them."
    )

    Settings.RegisterAddOnCategory(category)
    ns._settingsCategoryID = category:GetID()
end

function ns:OpenSettings()
    if ns._settingsCategoryID and Settings and Settings.OpenToCategory then
        Settings.OpenToCategory(ns._settingsCategoryID)
    end
end
