-- HDG_HT_Cards.lua
-- Card chrome factory. Four chrome modes:
--   "card"      — backdrop + border + title bar + body (the boxed widgets:
--                 donuts, capacity, hot picks, etc.)
--   "cardPlain" — backdrop + border + body (NO title bar). Use this when the
--                 widget already renders its own internal title and we just
--                 want the panel-tinted box around it (xpHero, trophyShelf,
--                 etc. when set to "card" duplicate the title text).
--   "section"   — chromeless flow band; thin 1px top divider only. Widget
--                 owns its own internal title/header.
--   "none"      — no chrome at all; body = full card area.
-- All three return the same shape: a frame with .body (and .titleText etc.
-- on "card" only). Anchors only to parent — no card-to-card chains.

local _, privateDB = ...
privateDB.HDG_HT = privateDB.HDG_HT or {}
local Cards = {}
privateDB.HDG_HT.Cards = Cards

local HDG = _G.HousingDecorGuide

local function GetScheme()
    if HDG and HDG.Theme and HDG.Theme.GetCurrentScheme then
        return HDG.Theme.GetCurrentScheme()
    end
    return {
        panel  = { r = 0.10, g = 0.12, b = 0.15, a = 0.95 },
        border = { r = 0.30, g = 0.35, b = 0.40, a = 1.00 },
        text   = { r = 0.95, g = 0.95, b = 0.95, a = 1.00 },
        accent = { r = 0.55, g = 0.75, b = 0.85, a = 1.00 },
    }
end

-- opts: {
--   id              = string,
--   title           = string,                     -- "card" mode only
--   width, height   = numbers,
--   chrome          = "card" | "section" | "none" (default "card"),
--   titleBarHeight  = number (default 24),        -- "card" only
--   bodyPaddingX    = number (default 10),
--   bodyPaddingY    = number (default 8),
--   showFooter      = bool (default false),       -- "card" only
--   footerHeight    = number (default 18),
-- }
function Cards.Create(parent, opts)
    opts = opts or {}
    local chrome         = opts.chrome or "card"
    local bodyPaddingX   = opts.bodyPaddingX or 10
    local bodyPaddingY   = opts.bodyPaddingY or 8
    local scheme         = GetScheme()

    local card = CreateFrame("Frame", nil, parent, "BackdropTemplate")
    card:SetSize(opts.width or 270, opts.height or 100)
    card._id = opts.id
    card._chrome = chrome

    if chrome == "card" then
        local titleBarHeight = opts.titleBarHeight or 24
        local showFooter     = opts.showFooter or false
        local footerHeight   = opts.footerHeight or 18

        card:SetBackdrop({
            bgFile   = "Interface\\Buttons\\WHITE8x8",
            edgeFile = "Interface\\Buttons\\WHITE8x8",
            edgeSize = 1,
            insets   = { left = 0, right = 0, top = 0, bottom = 0 },
        })
        -- Caller picks alpha; scheme provides hue. v2.37 uses 0.6 panel / 0.3 border.
        card:SetBackdropColor(scheme.panel.r, scheme.panel.g, scheme.panel.b, 0.6)
        card:SetBackdropBorderColor(scheme.border.r, scheme.border.g, scheme.border.b, 0.3)

        local titleBar = CreateFrame("Frame", nil, card)
        titleBar:SetHeight(titleBarHeight)
        titleBar:SetPoint("TOPLEFT",  card, "TOPLEFT",  0, 0)
        titleBar:SetPoint("TOPRIGHT", card, "TOPRIGHT", 0, 0)

        -- Vertical accent bar left of the title (v2.37 SectionTitle convention)
        local titleAccent = titleBar:CreateTexture(nil, "ARTWORK")
        titleAccent:SetTexture("Interface\\Buttons\\WHITE8x8")
        titleAccent:SetSize(2, 14)
        titleAccent:SetPoint("LEFT", titleBar, "LEFT", 6, 0)
        titleAccent:SetVertexColor(scheme.accent.r, scheme.accent.g, scheme.accent.b, 0.8)
        card.titleAccent = titleAccent

        local titleText = titleBar:CreateFontString(nil, "OVERLAY", "GameFontNormal")
        titleText:SetPoint("LEFT", titleAccent, "RIGHT", 6, 0)
        titleText:SetText(opts.title or opts.id or "")
        titleText:SetTextColor(scheme.accent.r, scheme.accent.g, scheme.accent.b)
        local CockpitTheme = privateDB.HDG_HT and privateDB.HDG_HT.Theme
        if CockpitTheme and CockpitTheme.ApplyFont then
            CockpitTheme.ApplyFont(titleText, scheme, "header")
        elseif HDG.Theme and HDG.Theme.ApplyFont then
            HDG.Theme.ApplyFont(titleText, scheme, "header")
        end
        card.titleText = titleText

        local chipSlot = CreateFrame("Frame", nil, titleBar)
        chipSlot:SetSize(120, titleBarHeight)
        chipSlot:SetPoint("RIGHT", titleBar, "RIGHT", -8, 0)
        card.titleChipSlot = chipSlot

        local body = CreateFrame("Frame", nil, card)
        local bodyTop    = -(titleBarHeight + bodyPaddingY)
        local bodyBottom = showFooter and (footerHeight + bodyPaddingY) or bodyPaddingY
        body:SetPoint("TOPLEFT", card, "TOPLEFT", bodyPaddingX, bodyTop)
        body:SetPoint("BOTTOMRIGHT", card, "BOTTOMRIGHT", -bodyPaddingX, bodyBottom)
        card.body = body

        if showFooter then
            local footer = CreateFrame("Frame", nil, card)
            footer:SetHeight(footerHeight)
            footer:SetPoint("BOTTOMLEFT", card, "BOTTOMLEFT", bodyPaddingX, bodyPaddingY)
            footer:SetPoint("BOTTOMRIGHT", card, "BOTTOMRIGHT", -bodyPaddingX, bodyPaddingY)
            card.footer = footer
        end

    elseif chrome == "cardPlain" then
        -- Same backdrop as "card" but no title bar. Body fills the whole card
        -- so widgets that own their internal header don't get a duplicate.
        card:SetBackdrop({
            bgFile   = "Interface\\Buttons\\WHITE8x8",
            edgeFile = "Interface\\Buttons\\WHITE8x8",
            edgeSize = 1,
            insets   = { left = 0, right = 0, top = 0, bottom = 0 },
        })
        card:SetBackdropColor(scheme.panel.r, scheme.panel.g, scheme.panel.b, 0.6)
        card:SetBackdropBorderColor(scheme.border.r, scheme.border.g, scheme.border.b, 0.3)

        local body = CreateFrame("Frame", nil, card)
        body:SetPoint("TOPLEFT",     card, "TOPLEFT",      bodyPaddingX, -bodyPaddingY)
        body:SetPoint("BOTTOMRIGHT", card, "BOTTOMRIGHT", -bodyPaddingX,  bodyPaddingY)
        card.body = body

    elseif chrome == "section" then
        -- 1px divider line across the top, body fills the rest. Widget renders
        -- its own header text (matches v2.37 style: "Trophy shelf 32 / 36" etc.).
        local divider = card:CreateTexture(nil, "ARTWORK")
        divider:SetColorTexture(scheme.border.r or 0.3, scheme.border.g or 0.3, scheme.border.b or 0.3, 0.5)
        divider:SetHeight(1)
        divider:SetPoint("TOPLEFT", card, "TOPLEFT", 0, 0)
        divider:SetPoint("TOPRIGHT", card, "TOPRIGHT", 0, 0)
        card._divider = divider

        local body = CreateFrame("Frame", nil, card)
        body:SetPoint("TOPLEFT", card, "TOPLEFT", bodyPaddingX, -(1 + bodyPaddingY))
        body:SetPoint("BOTTOMRIGHT", card, "BOTTOMRIGHT", -bodyPaddingX, bodyPaddingY)
        card.body = body

    else  -- "none"
        local body = CreateFrame("Frame", nil, card)
        body:SetAllPoints(card)
        card.body = body
    end

    return card
end
