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

local cache = {}

StaticPopupDialogs["BRIGHTQOL_CHAT_RELOAD"] = {
    text      = "|cff00ccffBrightChat|r requires a UI reload to activate.",
    button1   = "Reload Now",
    button2   = "Later",
    OnAccept  = function() ReloadUI() end,
    timeout   = 0, whileDead = true, hideOnEscape = true, preferredIndex = 3,
}

function ns:InitChat()
    local p  = ns.MainFrame.Content
    local db = BrightQoL and BrightQoL.brightChat
    if not db then return end

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

        W:CreatePageHeader(sc,
            {{"Bright", C.BLUE}, {"Chat", C.ORANGE}},
            W.Colorize("Clean chat skin for BrightQoL.", C.GRAY))

        -- Enable / master switch
        local killArea = CreateFrame("Frame", nil, sc, "BackdropTemplate")
        killArea:SetSize(460, 50)
        killArea:SetPoint("TOPLEFT", 10, -75)
        killArea:SetBackdrop({bgFile="Interface\\Buttons\\WHITE8x8"})
        killArea:SetBackdropColor(0.01, 0.56, 0.91, 0.08)

        local content = CreateFrame("Frame", nil, sc)
        content:SetPoint("TOPLEFT",  sc, "TOPLEFT",  10, -135)
        content:SetPoint("TOPRIGHT", sc, "TOPRIGHT", -10, 0)
        content:SetHeight(1500)
        content:SetShown(db.enabled)

        W:CreateCheckbox(killArea, {
            label="Enable BrightChat",
            db=db, key="enabled", x=15, y=-8, isMaster=true,
            onChange=function(v)
                content:SetShown(v)
                if ns.RefreshSidebarDots then ns.RefreshSidebarDots() end
                if v then StaticPopup_Show("BRIGHTQOL_CHAT_RELOAD") end
            end,
        })
        W:CreateRestoreDefaultsButton({
            moduleName="brightChat",
            parent=sc, fixedParent=killArea, yOffset=-6,
            initFunc=function() ns:InitChat() end,
        })

        local G   = ns.Layout:New(2, {rowStart=-10, rowHeight=44, width=560})
        local row = 1

        -- ── BACKGROUND ────────────────────────────────────────────────
        W:CreateSectionHeader(content, "Background", G:Row(row)-8, {x=0}); row=row+1

        W:CreateColorPicker(content, {
            label="Color",
            db=db, rKey="bgColorR", gKey="bgColorG", bKey="bgColorB",
            noClassColor=true, x=G:Col(1), y=G:Row(row),
            onChange=function()
                if ns.BrightChat then ns.BrightChat.RefreshColors() end
            end,
        })
        W:CreateSlider(content, {
            label="Alpha",
            min=0, max=100, step=5, isPercent=true,
            x=G:Col(2), y=G:Row(row), width=220,
            value=(db.bgAlpha or 0.8) * 100,
            onChange=function(v)
                db.bgAlpha = v / 100
                if ns.BrightChat then ns.BrightChat.RefreshColors() end
            end,
        })
        row=row+1

        W:CreateCheckbox(content, {
            label="Show border",
            db=db, key="showBorder",
            x=G:Col(1), y=G:Row(row), template="ChatConfigCheckButtonTemplate",
            onChange=function(v)
                if ns.BrightChat then ns.BrightChat.RefreshColors() end
            end,
        })
        row=row+1

        -- ── PADDING ───────────────────────────────────────────────────
        W:CreateSectionHeader(content, "Padding", G:Row(row)-8, {x=0}); row=row+1

        W:CreateSlider(content, {
            label="Left",  db=db, key="padLeft",  min=0, max=20, step=1,
            x=G:Col(1), y=G:Row(row), width=200,
            onChange=function() if ns.BrightChat then ns.BrightChat.RefreshColors() end end,
        })
        W:CreateSlider(content, {
            label="Right", db=db, key="padRight", min=0, max=20, step=1,
            x=G:Col(2), y=G:Row(row), width=200,
            onChange=function() if ns.BrightChat then ns.BrightChat.RefreshColors() end end,
        })
        row=row+1

        W:CreateSlider(content, {
            label="Top",    db=db, key="padTop",    min=0, max=20, step=1,
            x=G:Col(1), y=G:Row(row), width=200,
            onChange=function() if ns.BrightChat then ns.BrightChat.RefreshColors() end end,
        })
        W:CreateSlider(content, {
            label="Bottom", db=db, key="padBottom", min=0, max=20, step=1,
            x=G:Col(2), y=G:Row(row), width=200,
            onChange=function() if ns.BrightChat then ns.BrightChat.RefreshColors() end end,
        })
        row=row+1

        -- ── TABS ──────────────────────────────────────────────────────
        W:CreateSectionHeader(content, "Chat Tabs", G:Row(row)-8, {x=0}); row=row+1

        W:CreateColorPicker(content, {
            label="Active Color",
            db=db, rKey="tabColorR", gKey="tabColorG", bKey="tabColorB",
            classColorKey="tabUseClassColor",
            x=G:Col(1), y=G:Row(row),
            onChange=function() if ns.BrightChat then ns.BrightChat.RefreshTabs() end end,
        })
        W:CreateColorPicker(content, {
            label="Inactive Color",
            db=db, rKey="tabInactiveColorR", gKey="tabInactiveColorG", bKey="tabInactiveColorB",
            noClassColor=true, x=G:Col(2), y=G:Row(row),
            onChange=function() if ns.BrightChat then ns.BrightChat.RefreshTabs() end end,
        })
        row=row+1

        W:CreateSlider(content, {
            label="Inactive Alpha",
            min=0, max=100, step=5, isPercent=true,
            x=G:Col(1), y=G:Row(row), width=220,
            value=(db.tabInactiveAlpha or 0.7) * 100,
            onChange=function(v)
                db.tabInactiveAlpha = v / 100
                if ns.BrightChat then ns.BrightChat.RefreshTabs() end
            end,
        })
        W:CreateSlider(content, {
            label="Tab Height", db=db, key="tabHeight",
            min=14, max=32, step=1,
            x=G:Col(2), y=G:Row(row), width=220,
            onChange=function() if ns.BrightChat then ns.BrightChat.RefreshTabFont() end end,
        })
        row=row+1

        W:CreateDropdown(content, {
            label="Active Tab Style",
            db=db, key="tabSelectorStyle",
            x=G:Col(1), y=G:Row(row), width=200,
            options={
                {text="Name",     value="NONE"},
                {text=">Name<",   value="ARROW"},
                {text=">>Name<<", value="ARROW2"},
                {text="[Name]",   value="BOX"},
                {text="{Name}",   value="CURLY"},
                {text="(Name)",   value="CURVE"},
            },
            onChange=function() if ns.BrightChat then ns.BrightChat.RefreshTabs() end end,
        })
        W:CreateColorPicker(content, {
            label="Selector Color",
            db=db, rKey="tabSelectorColorR", gKey="tabSelectorColorG", bKey="tabSelectorColorB",
            noClassColor=true, x=G:Col(2), y=G:Row(row),
            onChange=function() if ns.BrightChat then ns.BrightChat.RefreshTabs() end end,
        })
        row=row+1

        local fontLbl = content:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
        fontLbl:SetPoint("TOPLEFT", G:Col(1), G:Row(row)-2)
        fontLbl:SetTextColor(0.7,0.7,0.7); fontLbl:SetText("Tab Font:")
        W:CreateFontPicker(content, G:Col(1)+60, G:Row(row),
            db.tabFont or "Friz Quadrata TT", function(path)
            local LSM = LibStub and LibStub("LibSharedMedia-3.0", true)
            db.tabFont = (LSM and LSM:Fetch("font", path, true)) or path
            if ns.BrightChat then ns.BrightChat.RefreshAll() end
        end)
        row=row+1

        W:CreateSlider(content, {
            label="Tab Font Size", db=db, key="tabFontSize",
            min=8, max=18, step=1,
            x=G:Col(1), y=G:Row(row), width=220,
            onChange=function()
                if ns.BrightChat then ns.BrightChat.RefreshTabFont() end
            end,
        })
        row=row+1

        W:CreateDropdown(content, {
            label="Font Outline",
            db=db, key="tabFontOutline",
            x=G:Col(1), y=G:Row(row), width=200,
            options={
                {text="None",       value=""},
                {text="Outline",    value="OUTLINE"},
                {text="Thick",      value="THICKOUTLINE"},
            },
            onChange=function() if ns.BrightChat then ns.BrightChat.RefreshTabFont() end end,
        })
        W:CreateCheckbox(content, {
            label="Flash tab on alerts",
            db=db, key="tabGlow",
            x=G:Col(2), y=G:Row(row), template="ChatConfigCheckButtonTemplate",
        })
        row=row+1

        -- ── MOVEMENT ──────────────────────────────────────────────────
        W:CreateSectionHeader(content, "Movement", G:Row(row)-8, {x=0}); row=row+1

        W:CreateCheckbox(content, {
            label="Lock chat position",
            db=db, key="chatLocked",
            x=G:Col(1), y=G:Row(row), template="ChatConfigCheckButtonTemplate",
            onChange=function() if ns.BrightChat then ns.BrightChat.ApplyLock() end end,
        })
        row=row+1

        -- ── MESSAGES ──────────────────────────────────────────────────
        W:CreateSectionHeader(content, "Messages", G:Row(row)-8, {x=0}); row=row+1

        W:CreateCheckbox(content, {
            label="Class color player names",
            db=db, key="classColorNames",
            x=G:Col(1), y=G:Row(row), template="ChatConfigCheckButtonTemplate",
            onChange=function(v)
                if ns.BrightChat then ns.BrightChat.ApplyClassColorNames(v) end
            end,
        })
        row=row+1

        -- ── EDITBOX ───────────────────────────────────────────────────
        W:CreateSectionHeader(content, "EditBox", G:Row(row)-8, {x=0}); row=row+1

        W:CreateCheckbox(content, {
            label="Editbox at top",
            db=db, key="editBoxTop",
            x=G:Col(1), y=G:Row(row), template="ChatConfigCheckButtonTemplate",
            onChange=function() if ns.BrightChat then ns.BrightChat.RefreshAll() end end,
        })
        W:CreateCheckbox(content, {
            label="Hide side buttons",
            db=db, key="hideButtons",
            x=G:Col(2), y=G:Row(row), template="ChatConfigCheckButtonTemplate",
            onChange=function() if ns.BrightChat then ns.BrightChat.RefreshButtons() end end,
        })
        row=row+1

    end)
end
