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

local cache = {}

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

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

        W:CreatePageHeader(sc,
            {{"MARKER ", C.BLUE}, {"FRAMES", C.ORANGE}},
            "Unified bar: left-click = raid mark · modifier+click = world marker")

        local function Apply()
            if ns.Markers then ns.Markers:Refresh() end
        end

        local function CB(parent, label, key, x, y, onChange)
            return W:CreateCheckbox(parent, {
                label = label, db = BrightQoL.markers, key = key,
                x = x, y = y,
                onChange = function(v) if onChange then onChange(v) end Apply() end,
            })
        end

        local function Sl(parent, label, key, mn, mx, step, x, y, w)
            return W:CreateSlider(parent, {
                label = label, db = BrightQoL.markers, key = key,
                min = mn, max = mx, step = step,
                x = x, y = y, width = w or 190,
                onChange = function() Apply() end,
            })
        end

        local C1, C2, ROW, STEP = 10, 250, -85, -30

        -- ── Master enable area ────────────────────────────────────────────────
        local killArea = CreateFrame("Frame", nil, sc, "BackdropTemplate")
        killArea:SetSize(460, 40)
        killArea:SetPoint("TOPLEFT", 10, -75)
        killArea:SetBackdrop({ bgFile = [[Interface\Buttons\WHITE8x8]] })
        killArea:SetBackdropColor(0.01, 0.56, 0.91, 0.08)

        CB(killArea, "Enable Marker Bar", "enabled", C1, -8)

        W:CreateRestoreDefaultsButton({
            moduleName = "markers",
            parent = sc,
            fixedParent = killArea,
            yOffset = -6,
            initFunc = function() ns:InitMarker() end,
            onRestore = function()
                if cache.markerFrame then
                    cache.markerFrame:Hide()
                    cache.markerFrame:SetParent(nil)
                    cache.markerFrame = nil
                end
                if ns.Markers then ns.Markers:Refresh() end
            end,
        })

        -- ── Content ───────────────────────────────────────────────────────────
        ROW = ROW + STEP

        CB(sc, "Lock Position",     "locked",  C1, ROW)
        ROW = ROW + STEP

        CB(sc, "Fade on Mouseover", "mouseOver",    C1, ROW)
        CB(sc, "Show Tooltips",     "tooltips",     C2, ROW)
        ROW = ROW + STEP

        CB(sc, "Show Backdrop",     "showBackdrop", C1, ROW)
        CB(sc, "Show Border",       "showBorder",   C2, ROW)

        W:CreateColorPicker(sc, {
            label    = "Border Color",
            x = C2 + 150, y = ROW + 10,
            db       = BrightQoL.markers,
            rKey     = "borderColorR", gKey = "borderColorG", bKey = "borderColorB", classColorKey = "borderUseClassColor",
            onChange = function() if ns.Markers then ns.Markers:ApplySettings() end end,
        })
        ROW = ROW + STEP

        Sl(sc, "Opacity", "alpha", 0.1, 1.0, 0.05, C1, ROW, 180)
        ROW = ROW + STEP - 20

        Sl(sc, "Icon Size", "iconSize", 16, 48, 2, C1, ROW, 180)
        ROW = ROW + STEP - 10

        -- ── Modifier key ─────────────────────────────────────────────────────
        W:CreateSectionHeader(sc, "World Marker Modifier Key", ROW)
        ROW = ROW + STEP

        local modNote = sc:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
        modNote:SetPoint("TOPLEFT", sc, "TOPLEFT", C1, ROW)
        modNote:SetTextColor(0.7, 0.7, 0.7, 0.9)
        modNote:SetText("Hold this key while clicking a marker to place/clear the matching world marker.")
        ROW = ROW + STEP

        -- Modifier dropdown
        W:CreateDropdown(sc, {
            x = C1, y = ROW,
            width = 130,
            db = BrightQoL.markers, key = "modifier",
            options = {
                { text = "Shift", value = "shift" },
                { text = "Alt",   value = "alt"   },
                { text = "Ctrl",  value = "ctrl"  },
            },
            onChange = function() Apply() end,
        })
        ROW = ROW + STEP

        -- ── Control buttons ───────────────────────────────────────────────────
        W:CreateSectionHeader(sc, "Control Buttons", ROW)
        ROW = ROW + STEP

        local ctrlNote = sc:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
        ctrlNote:SetPoint("TOPLEFT", sc, "TOPLEFT", C1, ROW)
        ctrlNote:SetTextColor(0.7, 0.7, 0.7, 0.9)
        ctrlNote:SetText("These buttons appear after the 8 marker icons in the bar.")
        ROW = ROW + STEP

        CB(sc, "Ready Check",      "readyCheckEnabled", C1, ROW)
        CB(sc, "Role Check",       "roleCheckEnabled",  C2, ROW)
        ROW = ROW + STEP
        CB(sc, "Pull Countdown",   "timerEnabled",      C1, ROW)
        CB(sc, "Main Tank",        "mainTankEnabled",   C2, ROW)
        ROW = ROW + STEP
        CB(sc, "Main Assist",      "mainAssistEnabled", C1, ROW)
        ROW = ROW + STEP

        Sl(sc, "Countdown Seconds", "countdownTime", 1, 30, 1, C1, ROW, 180)
        ROW = ROW + STEP - 20

        -- ── How it works ──────────────────────────────────────────────────────
        W:CreateSectionHeader(sc, "How It Works", ROW)
        ROW = ROW + STEP

        local howLines = {
            "|cff00cfff Left-click|r a marker icon  >>  set raid mark on target",
            "|cff00cfff Right-click|r a marker icon  >>  remove raid mark",
            "|cff00cfff Modifier+Left|r  >>  place matching world marker",
            "|cff00cfff Modifier+Right|r  >>  clear that world marker",
            "|cff00cfff Drag|r the bar to reposition it (unlock first)",
        }
        for _, line in ipairs(howLines) do
            local t = sc:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
            t:SetPoint("TOPLEFT", sc, "TOPLEFT", C1, ROW)
            t:SetText(line)
            t:SetTextColor(1, 1, 1, 0.85)
            ROW = ROW + -20
        end

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