local _, ns = ...

local SectionFrames = {}
ns.UISectionFrames = SectionFrames

local BACKDROP_TEMPLATE = BackdropTemplateMixin and "BackdropTemplate" or nil

local UIData = ns.UIData or {}
local PREMIUM = UIData.PREMIUM or {}
local THEME = UIData.THEME or {}

local function createLabel(parent, fontObject, justify)
    local text = parent:CreateFontString(nil, "OVERLAY", fontObject)
    text:SetJustifyH(justify or "LEFT")
    text:SetJustifyV("MIDDLE")
    return text
end

local function reduceFontSize(fontString, amount)
    if not fontString or type(fontString.GetFont) ~= "function" or type(fontString.SetFont) ~= "function" then
        return
    end

    local fontFile, fontHeight, fontFlags = fontString:GetFont()
    fontHeight = tonumber(fontHeight)
    if fontFile and fontHeight and fontHeight > amount then
        fontString:SetFont(fontFile, fontHeight - amount, fontFlags)
    end
end

local function setTextureColor(texture, color, alpha)
    if not texture or type(color) ~= "table" then
        return
    end

    texture:SetVertexColor(color[1], color[2], color[3], alpha == nil and 1 or alpha)
end

local function createSectionFrame(parent, variant)
    local frame = CreateFrame("Frame", nil, parent, BACKDROP_TEMPLATE)
    variant = variant or "panel"
    local backdropAlpha = 0.08

    if frame.SetBackdrop then
        frame:SetBackdrop({
            bgFile = "Interface\\Buttons\\WHITE8X8",
            edgeFile = "Interface\\Buttons\\WHITE8X8",
            tile = false,
            edgeSize = 1,
            insets = { left = 1, right = 1, top = 1, bottom = 1 },
        })
        local borderAlpha = 0.48
        if variant == "inset" or variant == "cardInset" then
            borderAlpha = 0.62
            backdropAlpha = 0.06
        elseif variant == "hero" then
            borderAlpha = 0.82
            backdropAlpha = 0.04
        elseif variant == "card" then
            borderAlpha = 0.70
            backdropAlpha = 0.06
        end
        frame:SetBackdropColor(THEME.metal[1], THEME.metal[2], THEME.metal[3], backdropAlpha)
        frame:SetBackdropBorderColor(THEME.goldDim[1], THEME.goldDim[2], THEME.goldDim[3], borderAlpha)
    end

    local bgTexture = PREMIUM.panelBg
    local overlayTexture = PREMIUM.panelOverlay
    if variant == "inset" then
        bgTexture = PREMIUM.insetBg
        overlayTexture = PREMIUM.insetOverlay
    elseif variant == "hero" then
        bgTexture = PREMIUM.heroPlate
    elseif variant == "card" then
        bgTexture = PREMIUM.cardPlate
    elseif variant == "cardInset" then
        bgTexture = PREMIUM.cardInset
        overlayTexture = PREMIUM.insetOverlay
    end

    frame.bg = frame:CreateTexture(nil, "BACKGROUND")
    frame.bg:SetAllPoints()
    frame.bg:SetTexture(bgTexture)
    setTextureColor(frame.bg, { 1, 1, 1 }, 1)

    frame.overlay = frame:CreateTexture(nil, "BACKGROUND", nil, 1)
    frame.overlay:SetAllPoints()
    frame.overlay:SetTexture(overlayTexture)
    local overlayAlpha = 0.07
    if variant == "hero" then
        overlayAlpha = 0.05
    elseif variant == "card" then
        overlayAlpha = 0.06
    elseif variant == "inset" or variant == "cardInset" then
        overlayAlpha = 0.08
    end
    setTextureColor(frame.overlay, { 1, 1, 1 }, overlayAlpha)

    frame.tint = frame:CreateTexture(nil, "BACKGROUND", nil, 2)
    frame.tint:SetAllPoints()
    frame.tint:SetTexture("Interface\\Buttons\\WHITE8X8")
    if variant == "hero" then
        frame.tint:SetColorTexture(THEME.metalInset[1], THEME.metalInset[2], THEME.metalInset[3], 0.004)
    elseif variant == "inset" or variant == "cardInset" then
        frame.tint:SetColorTexture(THEME.metalInset[1], THEME.metalInset[2], THEME.metalInset[3], 0.006)
    else
        frame.tint:SetColorTexture(THEME.metalDeep[1], THEME.metalDeep[2], THEME.metalDeep[3], 0.006)
    end

    frame.innerShade = frame:CreateTexture(nil, "BACKGROUND", nil, 3)
    frame.innerShade:SetPoint("TOPLEFT", 1, -1)
    frame.innerShade:SetPoint("BOTTOMRIGHT", -1, 1)
    frame.innerShade:SetTexture("Interface\\Buttons\\WHITE8X8")
    frame.innerShade:SetColorTexture(THEME.shadow[1], THEME.shadow[2], THEME.shadow[3], 0.004)

    frame.topGlow = frame:CreateTexture(nil, "BORDER")
    frame.topGlow:SetAllPoints()
    frame.topGlow:SetTexture("Interface\\Buttons\\WHITE8X8")
    frame.topGlow:SetAlpha(0)

    frame.bottomShade = frame:CreateTexture(nil, "BORDER")
    frame.bottomShade:SetAllPoints()
    frame.bottomShade:SetTexture("Interface\\Buttons\\WHITE8X8")
    frame.bottomShade:SetAlpha(0)

    return frame
end

local function setSectionFrameTextures(frame, bgTexture, overlayTexture)
    if not frame then
        return
    end

    if frame.bg and bgTexture then
        frame.bg:SetTexture(bgTexture)
    end

    if frame.overlay and overlayTexture then
        frame.overlay:SetTexture(overlayTexture)
    end
end

local function createUtilityHeader(parent, text)
    local frame = CreateFrame("Frame", nil, parent, BACKDROP_TEMPLATE)
    frame:SetHeight(26)

    if frame.SetBackdrop then
        frame:SetBackdrop({
            bgFile = "Interface\\Buttons\\WHITE8X8",
            edgeFile = "Interface\\Buttons\\WHITE8X8",
            tile = false,
            edgeSize = 1,
            insets = { left = 1, right = 1, top = 1, bottom = 1 },
        })
        frame:SetBackdropColor(THEME.metalInset[1], THEME.metalInset[2], THEME.metalInset[3], 0.06)
        frame:SetBackdropBorderColor(THEME.goldDim[1], THEME.goldDim[2], THEME.goldDim[3], 0.32)
    end

    frame.bg = frame:CreateTexture(nil, "BACKGROUND")
    frame.bg:SetAllPoints()
    frame.bg:SetTexture(PREMIUM.insetBg)
    setTextureColor(frame.bg, { 1, 1, 1 }, 1)

    frame.overlay = frame:CreateTexture(nil, "BACKGROUND", nil, 1)
    frame.overlay:SetAllPoints()
    frame.overlay:SetTexture(PREMIUM.insetOverlay)
    setTextureColor(frame.overlay, { 1, 1, 1 }, 0.08)

    frame.shade = frame:CreateTexture(nil, "BACKGROUND", nil, 2)
    frame.shade:SetPoint("TOPLEFT", 1, -1)
    frame.shade:SetPoint("BOTTOMRIGHT", -1, 1)
    frame.shade:SetTexture("Interface\\Buttons\\WHITE8X8")
    frame.shade:SetColorTexture(THEME.shadow[1], THEME.shadow[2], THEME.shadow[3], 0.01)

    frame.line = frame:CreateTexture(nil, "BORDER")
    frame.line:SetPoint("BOTTOMLEFT", 10, 2)
    frame.line:SetPoint("BOTTOMRIGHT", -10, 2)
    frame.line:SetHeight(1)
    frame.line:SetTexture("Interface\\Buttons\\WHITE8X8")
    setTextureColor(frame.line, THEME.goldDim, 0)

    frame.label = createLabel(frame, "GameFontNormalLarge", "CENTER")
    reduceFontSize(frame.label, 1)
    frame.label:SetPoint("CENTER")
    frame.label:SetText(text or "")
    frame.label:SetTextColor(THEME.parchment[1], THEME.parchment[2], THEME.parchment[3], 0.92)

    return frame
end

SectionFrames.CreateSectionFrame = createSectionFrame
SectionFrames.SetSectionFrameTextures = setSectionFrameTextures
SectionFrames.CreateUtilityHeader = createUtilityHeader
