local addonName, ns = ...
local W = ns.Widgets
local C = ns.COLORS

local cache = {}

-- ── Module Registry (used by EditModeDrawer and unlock-all) ──────────────
local MODULE_REGISTRY = {
    { db = "combatTimer",      unlockKeys = {"unlock"},                         tab = "combat_timer",    frameKey = "CombatTimerDisplay" },
    { db = "combatAlert",      unlockKeys = {"unlock"},                         tab = "combat_alert",    frameKey = "CombatAlertDisplay" },
    { db = "focusCastBar",     unlockKeys = {"unlock"},                         tab = "focus_cast_bar",  frameKey = "FocusCastBarDisplay" },
    { db = "movementAlert",    unlockKeys = {"unlock", "tsUnlock", "gwUnlock"}, tab = "movement_alert",  frameKey = "MovementAlertDisplay" },
    { db = "stealthReminder",  unlockKeys = {"unlock", "stanceUnlock"},         tab = "stealth",         frameKey = "StealthReminderDisplay" },
    { db = "rangeCheck",       unlockKeys = {"rangeUnlock"},                    tab = "range_check",     frameKey = "RangeCheckRangeFrame" },
    { db = "petTracker",       unlockKeys = {"unlock"},                         tab = "pet_tracker",     frameKey = "PetTrackerDisplay" },
    { db = "cRez",             unlockKeys = {"unlock"},                         tab = "crez",            frameKey = "CRezTimerDisplay" },
    { db = "buffWatcherV2",    unlockKeys = {"reportCardUnlock"},               tab = "buff_watcher",    frameKey = "BWV2ReportCard" },
    { db = "gameBar",          unlockKeys = {"locked"},                         tab = "game_bar",        frameKey = "GameBar" },
    { db = "markers",          unlockKeys = {"locked"},                         tab = "markers",         frameKey = "Markers" },
    { db = "healerMana",       unlockKeys = {"unlock"},                         tab = "healer_mana",     frameKey = "HealerManaWatch" },
    { db = "swiftStats",       unlockKeys = {"unlock"},                         tab = "swift_stats",     frameKey = "Stats" },
    { db = "combatLogger",     unlockKeys = {},                                 tab = "combat_logger",   frameKey = nil },
}
ns.MODULE_REGISTRY = MODULE_REGISTRY

local MODULE_DISPLAY_NAMES = {
    combatTimer     = "Combat Timer",
    combatAlert     = "Combat Alert",
    focusCastBar    = "Focus Cast Bar",
    movementAlert   = "Movement Alert",
    stealthReminder = "Stealth / Stance",
    rangeCheck      = "Range Check",
    petTracker      = "Pet Tracker",
    cRez            = "Combat Rez",
    buffWatcherV2   = "Buff Watcher Report Card",
    gameBar         = "Game Bar",
    markers         = "Marker Frames",
    healerMana      = "Healer Mana Watch",
    swiftStats      = "Stats Display",
    combatLogger    = "Combat Logger",
}
ns.MODULE_DISPLAY_NAMES = MODULE_DISPLAY_NAMES

local function RefreshAllModuleDisplays()
    if ns.CombatTimerDisplay    then pcall(ns.CombatTimerDisplay.UpdateDisplay, ns.CombatTimerDisplay) end
    if ns.CombatAlertDisplay    then pcall(ns.CombatAlertDisplay.UpdateDisplay, ns.CombatAlertDisplay) end
    if ns.FocusCastBarDisplay   then pcall(ns.FocusCastBarDisplay.UpdateDisplay, ns.FocusCastBarDisplay) end
    if ns.MovementAlertDisplay  then pcall(ns.MovementAlertDisplay.UpdateDisplay, ns.MovementAlertDisplay) end
    if ns.TimeSpiralDisplay     then pcall(ns.TimeSpiralDisplay.UpdateDisplay, ns.TimeSpiralDisplay) end
    if ns.GatewayShardDisplay   then pcall(ns.GatewayShardDisplay.UpdateDisplay, ns.GatewayShardDisplay) end
    if ns.StealthReminderDisplay then pcall(ns.StealthReminderDisplay.UpdateDisplay, ns.StealthReminderDisplay) end
    if ns.StanceReminderDisplay then pcall(ns.StanceReminderDisplay.UpdateDisplay, ns.StanceReminderDisplay) end
    if ns.RangeCheckRangeFrame  then pcall(ns.RangeCheckRangeFrame.UpdateDisplay, ns.RangeCheckRangeFrame) end
    if ns.PetTrackerDisplay     then pcall(ns.PetTrackerDisplay.UpdateDisplay, ns.PetTrackerDisplay) end
    if ns.GcdTrackerDisplay     then pcall(ns.GcdTrackerDisplay.UpdateDisplay, ns.GcdTrackerDisplay) end
    if ns.CrosshairDisplay      then pcall(ns.CrosshairDisplay.UpdateDisplay, ns.CrosshairDisplay) end
    if ns.CRezTimerDisplay      then pcall(ns.CRezTimerDisplay.Refresh, ns.CRezTimerDisplay) end
    if ns.GameBar               then pcall(ns.GameBar.ApplySettings, ns.GameBar) end
    if ns.Markers               then pcall(ns.Markers.ApplySettings, ns.Markers) end
    if ns.HealerManaWatch and ns.HealerManaWatch.ApplySettings then
        pcall(ns.HealerManaWatch.ApplySettings, ns.HealerManaWatch)
    end
    if ns.Stats and ns.Stats.ApplySettings then
        pcall(ns.Stats.ApplySettings, ns.Stats)
    end
end
ns.RefreshAllModuleDisplays = RefreshAllModuleDisplays

function ns:InitGeneral()
    local p = ns.MainFrame.Content

    W:CachedPanel(cache, "generalFrame", p, function(f)
        local sf, sc = W:CreateScrollFrame(f, 900)

        W:CreatePageHeader(sc,
            {{"GENERAL ", C.BLUE}, {"SETTINGS", C.ORANGE}},
            "Global appearance settings for the BrightQoL interface")

        local db = BrightQoL.general

        local function Apply()
            if ns.Theme and ns.Theme.RefreshUI then
                ns.Theme.RefreshUI()
            end
        end

        local C1, C2, ROW, STEP = 10, 300, -85, -32

        -- ── Font ──────────────────────────────────────────────────────────
        W:CreateSectionHeader(sc, "Font", ROW)
        ROW = ROW + STEP

        W:CreateFontPicker(sc, C1, ROW + 4, db.font, function(path)
            db.font = path
            Apply()
            -- Rebuild all cached panels so the new font takes effect immediately
            if cache.generalFrame then
                cache.generalFrame:Hide()
                cache.generalFrame:SetParent(nil)
                cache.generalFrame = nil
            end
            C_Timer.After(0.05, function() ns:InitGeneral() end)
        end)
        ROW = ROW + STEP + 8

        -- ── Accent Colour (cyan) ──────────────────────────────────────────
        W:CreateSectionHeader(sc, "Accent Colour", ROW - 3)
        ROW = ROW + STEP

        -- Full color picker for custom accent
        W:CreateColorPicker(sc, {
            label    = "Custom Accent Colour",
            db       = db,
            rKey     = "accentR", gKey = "accentG", bKey = "accentB",
            x        = C1, y = ROW,
            noClassColor = true,
            onChange = function() Apply() end,
        })
        ROW = ROW + STEP - 16

        local restoreBtn = W:CreateRestoreDefaultsButton({
            moduleName = "general",
            parent = sc,
            initFunc = function() ns:InitGeneral() end,
            onRestore = function() end,
        })
        restoreBtn:SetPoint("TOPLEFT", sc, "TOPLEFT", C1, ROW)
        ROW = ROW + STEP

        sc:SetHeight(math.abs(ROW) + 40)
    end)
end
