-- ============================================================
-- Komo Hold'Em — UI Widgets
-- Shared factories implementing the design system from mockups:
-- gold gradient buttons, blue pill buttons, dark panels, pills,
-- toggles. Gradients approximated with Blizzard's white8x8 +
-- vertex gradient API.
-- ============================================================
local _, KH = ...
KH.UI = KH.UI or {}
local UI = KH.UI

local WHITE8 = "Interface\\Buttons\\WHITE8x8"
local TOOLTIP = "Interface\\Tooltips\\UI-Tooltip-Border"

-- rounded 9-slice backdrop used by all chrome
UI.BACKDROP = {
    bgFile = WHITE8,
    edgeFile = TOOLTIP,
    edgeSize = 12,
    insets = { left = 3, right = 3, top = 3, bottom = 3 },
}

-- gradient fill helper (top color -> bottom color)
function UI.SetGradient(tex, r1,g1,b1,a1, r2,g2,b2,a2)
    tex:SetTexture(WHITE8)
    tex:SetGradient("VERTICAL",
        CreateColor(r2, g2, b2, a2),   -- bottom
        CreateColor(r1, g1, b1, a1))   -- top
end

-- dark window panel with 1px edge
function UI.CreatePanel(parent, w, h)
    local f = CreateFrame("Frame", nil, parent, "BackdropTemplate")
    f:SetSize(w, h)
    f:SetBackdrop(UI.BACKDROP)
    f:SetBackdropColor(0.082, 0.090, 0.110, 0.96)    -- #15171c
    f:SetBackdropBorderColor(0.36, 0.38, 0.44, 1)    -- slate tint
    return f
end

-- gold gradient primary button (#FAD990 -> #D8AB48, dark text)
function UI.CreateGoldButton(parent, w, h, label)
    local b = CreateFrame("Button", nil, parent, "BackdropTemplate")
    b:SetSize(w, h)
    b:SetBackdrop(UI.BACKDROP)
    b:SetBackdropColor(0, 0, 0, 0)
    b:SetBackdropBorderColor(0.722, 0.537, 0.165, 1)

    local bg = b:CreateTexture(nil, "BACKGROUND")
    bg:SetPoint("TOPLEFT", 3, -3)
    bg:SetPoint("BOTTOMRIGHT", -3, 3)
    UI.SetGradient(bg, 0.980,0.851,0.565,1, 0.847,0.671,0.282,1)
    b.bg = bg

    local t = b:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    t:SetPoint("CENTER")
    t:SetText(label)
    t:SetTextColor(0.09, 0.075, 0.04)
    b.label = t

    b:SetScript("OnEnter", function()
        UI.SetGradient(bg, 1,0.902,0.671,1, 0.910,0.737,0.345,1)
    end)
    b:SetScript("OnLeave", function()
        UI.SetGradient(bg, 0.980,0.851,0.565,1, 0.847,0.671,0.282,1)
    end)
    return b
end

-- blue pill button (Back / Unready family)
function UI.CreateBlueButton(parent, w, h, label)
    local b = CreateFrame("Button", nil, parent, "BackdropTemplate")
    b:SetSize(w, h)
    b:SetBackdrop(UI.BACKDROP)
    b:SetBackdropColor(0.086, 0.137, 0.259, 1)
    b:SetBackdropBorderColor(0.227, 0.435, 0.784, 1)

    local t = b:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
    t:SetPoint("CENTER")
    t:SetText(label)
    t:SetTextColor(0.490, 0.706, 0.941)
    b.label = t

    b:SetScript("OnEnter", function()
        b:SetBackdropColor(0.125, 0.290, 0.541, 1)
        t:SetTextColor(1, 1, 1)
    end)
    b:SetScript("OnLeave", function()
        b:SetBackdropColor(0.086, 0.137, 0.259, 1)
        t:SetTextColor(0.490, 0.706, 0.941)
    end)
    return b
end

-- red danger button (Cancel / Leave family)
function UI.CreateRedButton(parent, w, h, label)
    local b = CreateFrame("Button", nil, parent, "BackdropTemplate")
    b:SetSize(w, h)
    b:SetBackdrop(UI.BACKDROP)
    b:SetBackdropColor(0.133, 0.075, 0.070, 1)
    b:SetBackdropBorderColor(0.659, 0.227, 0.188, 1)

    local t = b:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
    t:SetPoint("CENTER")
    t:SetText(label)
    t:SetTextColor(0.941, 0.541, 0.502)
    b.label = t

    b:SetScript("OnEnter", function()
        b:SetBackdropColor(0.659, 0.227, 0.188, 1)
        t:SetTextColor(1, 1, 1)
    end)
    b:SetScript("OnLeave", function()
        b:SetBackdropColor(0.133, 0.075, 0.070, 1)
        t:SetTextColor(0.941, 0.541, 0.502)
    end)
    return b
end

-- 50% black pill with centered text
function UI.CreatePill(parent, w, h)
    local f = CreateFrame("Frame", nil, parent, "BackdropTemplate")
    f:SetSize(w, h)
    f:SetBackdrop(UI.BACKDROP)
    f:SetBackdropColor(0, 0, 0, 0.5)
    f:SetBackdropBorderColor(0.30, 0.32, 0.38, 0.9)
    f.text = f:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
    f.text:SetPoint("CENTER")
    return f
end

-- gold on/off toggle
function UI.CreateToggle(parent, initial, onChange)
    local b = CreateFrame("Button", nil, parent, "BackdropTemplate")
    b:SetSize(38, 20)
    b:SetBackdrop(UI.BACKDROP)

    local knob = b:CreateTexture(nil, "OVERLAY")
    knob:SetSize(14, 14)
    knob:SetTexture(WHITE8)

    local state = initial and true or false
    local function paint()
        if state then
            b:SetBackdropColor(0.980, 0.851, 0.565, 1)
            b:SetBackdropBorderColor(0.722, 0.537, 0.165, 1)
            knob:ClearAllPoints(); knob:SetPoint("RIGHT", -2, 0)
            knob:SetVertexColor(0.09, 0.075, 0.04)
        else
            b:SetBackdropColor(0.137, 0.149, 0.180, 1)
            b:SetBackdropBorderColor(0.290, 0.318, 0.376, 1)
            knob:ClearAllPoints(); knob:SetPoint("LEFT", 2, 0)
            knob:SetVertexColor(0.541, 0.565, 0.612)
        end
    end
    paint()

    b:SetScript("OnClick", function()
        state = not state
        paint()
        KH.Sounds:Play(state and "READY_CLICK" or "UNREADY")
        if onChange then onChange(state) end
    end)

    function b:GetValue() return state end
    return b
end

-- ------------------------------------------------------------
-- segmented control: equal-width options, gold selected state
-- ------------------------------------------------------------
function UI.CreateSegmented(parent, w, h, options, defaultIx, onChange)
    local seg = CreateFrame("Frame", nil, parent, "BackdropTemplate")
    seg:SetSize(w, h)
    seg:SetBackdrop(UI.BACKDROP)
    seg:SetBackdropColor(0.047, 0.055, 0.075, 1)
    seg:SetBackdropBorderColor(0.36, 0.38, 0.44, 1)

    seg.buttons = {}
    seg.selected = defaultIx or 1
    local bw = (w - 2) / #options

    local function paint()
        for i, b in ipairs(seg.buttons) do
            if i == seg.selected then
                b.bg:Show()
                b.label:SetTextColor(0.09, 0.075, 0.04)
            else
                b.bg:Hide()
                b.label:SetTextColor(0.776, 0.800, 0.839)
            end
        end
    end

    for i, opt in ipairs(options) do
        local b = CreateFrame("Button", nil, seg)
        b:SetSize(bw - (#options > 1 and 0 or 0), h - 6)
        b:SetPoint("LEFT", 3 + (i - 1) * bw, 0)

        b.bg = b:CreateTexture(nil, "BACKGROUND")
        b.bg:SetAllPoints()
        UI.SetGradient(b.bg, 0.980,0.851,0.565,1, 0.847,0.671,0.282,1)

        b.label = b:CreateFontString(nil, "OVERLAY")
        b.label:SetFont("Fonts\\FRIZQT__.TTF", 12, "")
        b.label:SetPoint("CENTER")
        b.label:SetText(opt)

        b:SetScript("OnClick", function()
            seg.selected = i
            paint()
            KH.Sounds:Play("UI_CLICK")
            if onChange then onChange(i, opt) end
        end)
        seg.buttons[i] = b
    end
    paint()

    function seg:GetSelected() return self.selected end
    return seg
end

-- ------------------------------------------------------------
-- edit box: dark field, purple focus edge
-- ------------------------------------------------------------
function UI.CreateEditBox(parent, w, h, numeric)
    local box = CreateFrame("EditBox", nil, parent, "BackdropTemplate")
    box:SetSize(w, h)
    box:SetBackdrop(UI.BACKDROP)
    box:SetBackdropColor(0.047, 0.055, 0.075, 0.95)
    box:SetBackdropBorderColor(0.36, 0.38, 0.44, 1)
    box:SetFont("Fonts\\FRIZQT__.TTF", 13, "")
    box:SetTextColor(1, 1, 1)
    box:SetTextInsets(10, 10, 0, 0)
    box:SetAutoFocus(false)
    if numeric then box:SetNumeric(true) end
    box:SetScript("OnEditFocusGained", function(self)
        self:SetBackdropBorderColor(0.482, 0.361, 0.941, 1)
    end)
    box:SetScript("OnEditFocusLost", function(self)
        self:SetBackdropBorderColor(0.36, 0.38, 0.44, 1)
    end)
    box:SetScript("OnEnterPressed", function(self) self:ClearFocus() end)
    box:SetScript("OnEscapePressed", function(self) self:ClearFocus() end)
    return box
end
