-- UI/Theme/Theme.lua
--
-- A centralized module that provides all styling information for the Toolkit UI.
-- This includes colors, spacing, fonts, sizes, and helper functions to apply
-- standard styles to UI elements.

local addonName, SinsToolkit = ...

-- Ensure the namespace exists.
SinsToolkit.UI = SinsToolkit.UI or {}

local Theme = {}
SinsToolkit.UI.Theme = Theme

--[[---------------------------------------------------------------------------
    Private Theme Data
---------------------------------------------------------------------------]]--

local data = {
    colors = {
        accent = { r = 0.72, g = 0.41, b = 0.88 },
        background = { r = 0.02, g = 0.02, b = 0.025, a = 0.92 },
        border = { r = 0.72, g = 0.41, b = 0.88, a = 0.90 },
        divider = { r = 0.25, g = 0.25, b = 0.25, a = 0.8 },

        success = { r = 0.10, g = 0.95, b = 0.35 },
        warning = { r = 0.85, g = 0.66, b = 0.29 },
        error = { r = 1.00, g = 0.28, b = 0.28 },
        
        font = { r = 1, g = 1, b = 1 },
        font_header = { r = 0.72, g = 0.65, b = 0.84 },
        font_highlight = { r = 0.85, g = 0.85, b = 0.85 },
    },
    spacing = {
        card_padding = 16,
        section_divider_top = -6,
        section_content_top = -8,
        toolbar_gap = 10,
        control_vertical_gap = 8,
    },
    sizes = {
        button = { width = 160, height = 30 },
        checkbox = { width = 26, height = 26 },
    },
    fonts = {
        header = "GameFontNormal",
        text = "GameFontHighlight",
        small = "GameFontHighlightSmall",
    },
}

--[[---------------------------------------------------------------------------
    Public API
---------------------------------------------------------------------------]]--

--[[
    Retrieves a color table by name.
    @param name The name of the color (e.g., "accent", "success").
    @return A table with r, g, b, (a) values, or the accent color if not found.
--]]
function Theme.GetColor(name)
    return data.colors[name] or data.colors.accent
end

--[[
    Retrieves a spacing/padding value by name.
    @param name The name of the spacing value.
    @return A number, or 0 if not found.
--]]
function Theme.GetSpacing(name)
    return data.spacing[name] or 0
end

--[[
    Retrieves a standard size table by name.
    @param name The name of the size entry (e.g., "button", "checkbox").
    @return A table with width and height, or a default.
--]]
function Theme.GetSize(name)
    return data.sizes[name] or { width = 0, height = 0 }
end

--[[
    Retrieves the standard size for a button.
    @return A table with width and height.
--]]
function Theme.GetButtonSize()
    -- DEPRECATED: Use GetSize("button") instead.
    return Theme.GetSize("button")
end

--[[
    Retrieves a font object name.
    @param name The name of the font type ("header", "text", "small").
    @return A string with the font object name.
--]]
function Theme.GetFont(name)
    return data.fonts[name] or data.fonts.text
end

--[[
    Applies the standard Toolkit panel backdrop style to a frame.
    This is for main window containers.
    @param frame The frame to style.
--]]
function Theme.ApplyPanel(frame)
    frame:SetBackdrop({
        bgFile = "Interface\Buttons\WHITE8x8",
        edgeFile = "Interface\Buttons\WHITE8x8",
        tile = true, tileSize = 8, edgeSize = 1,
        insets = { left = 1, right = 1, top = 1, bottom = 1 },
    })
    local bg = data.colors.background
    local border = data.colors.border
    frame:SetBackdropColor(bg.r, bg.g, bg.b, bg.a)
    frame:SetBackdropBorderColor(border.r, border.g, border.b, border.a)
    SinsToolkit.RegisterOpacityFrame(frame, bg.r, bg.g, bg.b, border.r, border.g, border.b, border.a)
end

--[[
    Applies the standard Toolkit card backdrop style to a frame.
    This is for content containers inside pages.
    @param frame The frame to style.
--]]
function Theme.ApplyCard(frame)
    Theme.ApplyPanel(frame) -- For now, cards and panels share the same style.
end

--[[
    Creates and applies the style for a standard section header.
    @param parent The parent frame.
    @param text The header text.
    @return The header FontString and the divider Texture.
--]]
function Theme.ApplySection(parent, text)
    local header = parent:CreateFontString(nil, "OVERLAY", Theme.GetFont("header"))
    header:SetPoint("TOPLEFT", Theme.GetSpacing("card_padding"), -Theme.GetSpacing("card_padding"))
    header:SetText(text or "")
    
    local divider = parent:CreateTexture(nil, "BACKGROUND")
    divider:SetPoint("TOPLEFT", header, "BOTTOMLEFT", 0, Theme.GetSpacing("section_divider_top"))
    divider:SetPoint("RIGHT", parent, "RIGHT", -Theme.GetSpacing("card_padding"), 0)
    divider:SetHeight(1)
    local divColor = Theme.GetColor("divider")
    divider:SetColorTexture(divColor.r, divColor.g, divColor.b, divColor.a)
    
    return header, divider
end
