-- MythicMate LFG skin: a shared accent-color system for the LFG finder + applicant sorter.
-- OAK LFG Sorter tints its whole UI (borders, bars, hover) with a single "accent" color that is
-- either your class color or a fixed preset. We mirror that: one setting drives the panel border,
-- toolbar gradient, header underline and sort arrows, so the finder can match your taste/UI.
--
-- Split file so its locals get a fresh Lua chunk. Everything is exposed as globals
-- (MythicMate_GetLFGAccent / _GetLFGAccentName / _SetLFGAccent / _LFGAccentPresets) so the
-- other split files (PremadeFilter, Applicants) can read it without sharing file-locals.

local MythicMateNS = _G.MythicMate
if not MythicMateNS then return end

-- Fixed accent presets (RGB). "CLASS" is resolved live from the player's class color.
local PRESETS = {
    VIOLET  = { 0.66, 0.40, 1.00 },
    SKY     = { 0.30, 0.76, 0.98 },
    MINT    = { 0.35, 0.90, 0.66 },
    AMBER   = { 0.98, 0.65, 0.18 },
    ROSE    = { 0.92, 0.43, 0.61 },
    EMERALD = { 0.20, 0.82, 0.58 },
}

-- Order shown in the accent picker (CLASS first so class-color is the easy default).
_G.MythicMate_LFGAccentPresets = { "CLASS", "VIOLET", "SKY", "MINT", "AMBER", "ROSE", "EMERALD" }

-- Current accent preset name (persisted account-wide under settings; VIOLET = the original look).
function _G.MythicMate_GetLFGAccentName()
    local st = MythicMateDB and MythicMateDB.settings
    local n = st and st.lfgAccent
    if type(n) == "string" and (n == "CLASS" or PRESETS[n]) then return n end
    return "VIOLET"
end

-- RGB (0..1) for any preset name. Used by the accent picker to draw each swatch, and by
-- GetLFGAccent() for the active one. CLASS → the player's class color (with fallbacks).
function _G.MythicMate_LFGAccentRGB(name)
    if name == "CLASS" then
        local _, class = UnitClass("player")
        if class and type(C_ClassColor) == "table" and type(C_ClassColor.GetClassColor) == "function" then
            local ok, col = pcall(C_ClassColor.GetClassColor, class)
            if ok and col then return col.r, col.g, col.b end
        end
        local c = class and _G.RAID_CLASS_COLORS and _G.RAID_CLASS_COLORS[class]
        if c then return c.r, c.g, c.b end
        return PRESETS.VIOLET[1], PRESETS.VIOLET[2], PRESETS.VIOLET[3]
    end
    local p = PRESETS[name] or PRESETS.VIOLET
    return p[1], p[2], p[3]
end

-- Resolve the ACTIVE accent to r,g,b (0..1).
function _G.MythicMate_GetLFGAccent()
    return _G.MythicMate_LFGAccentRGB(_G.MythicMate_GetLFGAccentName())
end

-- "|cffRRGGBB" tag for the active accent (for inline colored text).
function _G.MythicMate_LFGAccentHex()
    local r, g, b = _G.MythicMate_GetLFGAccent()
    return string.format("|cff%02x%02x%02x", (r or 1) * 255, (g or 1) * 255, (b or 1) * 255)
end

-- Persist a new accent preset. Returns true when it changed (so callers can re-skin).
function _G.MythicMate_SetLFGAccent(name)
    if type(name) ~= "string" then return false end
    if name ~= "CLASS" and not PRESETS[name] then return false end
    MythicMateDB = MythicMateDB or {}
    MythicMateDB.settings = MythicMateDB.settings or {}
    if MythicMateDB.settings.lfgAccent == name then return false end
    MythicMateDB.settings.lfgAccent = name
    return true
end
