--[[
    Commander 3.0 - TUI Color System

    Semantic color mappings for TUI widgets.
    All colors are resolved through C3.Colors to respect the theme system.
]]

local ADDON_NAME, C3 = ...

C3.TUI = C3.TUI or {}

-------------------------------------------------------------------------------
-- TUI Colors
-------------------------------------------------------------------------------

local TuiColors = {}
C3.TUI.Colors = TuiColors

-- Default color values (Nord palette)
-- These are used as fallbacks when theme colors aren't available
local DEFAULTS = {
    -- Primary colors
    frost = "88C0D0",      -- Nord8 - primary accent (cyan)
    snow = "ECEFF4",       -- Nord6 - bright text
    dim = "4C566A",        -- Nord3 - dim/muted text
    polar = "3B4252",      -- Nord1 - dark background

    -- Semantic colors
    success = "A3BE8C",    -- Nord14 - green
    warning = "EBCB8B",    -- Nord13 - yellow
    error = "BF616A",      -- Nord11 - red
    info = "81A1C1",       -- Nord9 - blue
    special = "B48EAD",    -- Nord15 - purple
}

---Get a TUI color with theme fallback
---@param themeKey string Theme color key (e.g., "accent.primary")
---@param fallback string Fallback hex color if theme unavailable
---@return string hex color
local function getColor(themeKey, fallback)
    local hex = C3.Colors and C3.Colors.GetColor(themeKey)
    return hex or fallback
end

-------------------------------------------------------------------------------
-- Widget Color Getters
-------------------------------------------------------------------------------

-- Bar/Gauge colors
function TuiColors.barFill()
    return getColor("accent.primary", DEFAULTS.frost)
end

function TuiColors.barBg()
    return DEFAULTS.polar  -- Always dark for contrast
end

function TuiColors.barLabel()
    return getColor("content.fg", DEFAULTS.snow)
end

-- Text colors
function TuiColors.text()
    return getColor("content.fg", DEFAULTS.snow)
end

function TuiColors.textDim()
    return getColor("status.dim", DEFAULTS.dim)
end

function TuiColors.textBright()
    return DEFAULTS.snow
end

-- Border colors
function TuiColors.border()
    return getColor("accent.primary", DEFAULTS.frost)
end

function TuiColors.borderDim()
    return getColor("status.dim", DEFAULTS.dim)
end

-- Selection/highlight
function TuiColors.highlight()
    return getColor("accent.success", DEFAULTS.success)
end

function TuiColors.selected()
    return getColor("accent.primary", DEFAULTS.frost)
end

-- Semantic colors
function TuiColors.success()
    return getColor("accent.success", DEFAULTS.success)
end

function TuiColors.warning()
    return getColor("accent.warning", DEFAULTS.warning)
end

function TuiColors.error()
    return getColor("accent.error", DEFAULTS.error)
end

function TuiColors.info()
    return getColor("accent.info", DEFAULTS.info)
end

function TuiColors.special()
    return getColor("accent.special", DEFAULTS.special)
end

-------------------------------------------------------------------------------
-- Health/Mana Presets (game-specific colors)
-------------------------------------------------------------------------------

-- Health colors based on percentage
TuiColors.health = {
    high = DEFAULTS.success,    -- Green (> 50%)
    mid = DEFAULTS.warning,     -- Yellow (25-50%)
    low = DEFAULTS.error,       -- Red (< 25%)
}

-- Get health color for a ratio (0.0 - 1.0)
function TuiColors.healthForRatio(ratio)
    if ratio > 0.5 then
        return TuiColors.health.high
    elseif ratio > 0.25 then
        return TuiColors.health.mid
    else
        return TuiColors.health.low
    end
end

-- Mana/power colors
TuiColors.mana = DEFAULTS.info  -- Blue

-------------------------------------------------------------------------------
-- Color Resolution Helper
-------------------------------------------------------------------------------

---Resolve a color value with fallback
---@param color string|nil Explicit color (hex string)
---@param fallbackGetter function|string|nil Fallback: function returning hex, or hex string directly
---@return string|nil hex color
function TuiColors.Resolve(color, fallbackGetter)
    if color then
        return color
    end
    if fallbackGetter then
        if type(fallbackGetter) == "function" then
            return fallbackGetter()
        else
            return fallbackGetter
        end
    end
    return nil
end

return TuiColors
