local RUI = LibStub("RUI-1.0")
if not RUI then return end

-- ─── RUI.Media — LSM sound/font cache; centralizes boilerplate from all addons ──
-- In OnInitialize: RUI.Media.RegisterBuiltinSounds(lsm)
-- In LSM callback: if mediatype=="sound" then RUI.Media.InvalidateSoundCache() end
--                  if mediatype=="font"  then RUI.Media.InvalidateFontCache()  end

local BUILTIN_SOUNDS = {
    ["Alarm Clock 1"]      = "Sound\\Interface\\AlarmClockWarning1.ogg",
    ["Alarm Clock 2"]      = "Sound\\Interface\\AlarmClockWarning2.ogg",
    ["Alarm Clock 3"]      = "Sound\\Interface\\AlarmClockWarning3.ogg",
    ["Achievement"]        = "Sound\\Interface\\AchievementSoundNormal.ogg",
    ["Battleground Start"] = "Sound\\Interface\\UI_BattlegroundCountdownEnd.ogg",
    ["BG Countdown Tick"]  = "Sound\\Interface\\UI_BattlegroundCountdownTick.ogg",
    ["Dungeon Ready"]      = "Sound\\Interface\\UI_LFG_DungeonReady_01.ogg",
    ["Level Up"]           = "Sound\\Interface\\LevelUp.ogg",
    ["Map Ping"]           = "Sound\\Interface\\Ping_Map_Point.ogg",
    ["Quest Complete"]     = "Sound\\Interface\\QuestComplete.ogg",
    ["Raid Warning"]       = "Sound\\Interface\\RaidWarning.ogg",
    ["Ready Check"]        = "Sound\\Interface\\ReadyCheck.ogg",
    ["Whisper"]            = "Sound\\Interface\\UI_Chat_WhisperNotification.ogg",
}

local BUILTIN_FONTS = {
    ["Arial Narrow"]     = "Fonts\\ARIALN.TTF",
    ["Friz Quadrata TT"] = "Fonts\\FRIZQT__.TTF",
    ["Morpheus"]         = "Fonts\\MORPHEUS.TTF",
    ["Skurri"]           = "Fonts\\SKURRI.TTF",
}
local BUILTIN_FONT_SORT = {"Arial Narrow", "Friz Quadrata TT", "Morpheus", "Skurri"}

local soundCache, fontCache

function RUI.Media.InvalidateSoundCache() soundCache = nil end
function RUI.Media.InvalidateFontCache()  fontCache  = nil end

-- Call once from OnInitialize to register the 13 built-in WoW alert sounds with LSM.
function RUI.Media.RegisterBuiltinSounds(lsm)
    if not lsm then return end
    for name, path in pairs(BUILTIN_SOUNDS) do
        lsm:Register("sound", name, path)
    end
end

-- Returns {{text, value}, ...} with "None" prepended and LSM's own "None" filtered out.
function RUI.Media.BuildSoundMenuValues(lsm)
    if soundCache then return soundCache end
    local t = {{text="None", value="None"}}
    if lsm then
        for _, name in ipairs(lsm:List("sound")) do
            if name ~= "None" then t[#t+1] = {text=name, value=name} end
        end
    end
    soundCache = t
    return t
end

-- Returns alphabetically sorted array of font names from LSM (or 4 builtins if no LSM).
function RUI.Media.BuildFontSort(lsm)
    if fontCache then return fontCache end
    local t = {}
    if lsm then
        for _, name in ipairs(lsm:List("font")) do t[#t+1] = name end
    else
        for _, name in ipairs(BUILTIN_FONT_SORT) do t[#t+1] = name end
    end
    fontCache = t
    return t
end

-- Returns the file path for a font name. Falls back to ARIALN if not found.
function RUI.Media.GetFontPath(lsm, name)
    if lsm then
        local p = lsm:Fetch("font", name, true)
        if p then return p end
    end
    return BUILTIN_FONTS[name] or "Fonts\\ARIALN.TTF"
end

-- Plays an LSM sound by name. No-ops on "None" or a bad path (PlaySoundFile is safe).
function RUI.Media.PlaySound(lsm, name)
    if not lsm or not name or name == "None" then return end
    local path = lsm:Fetch("sound", name, true)
    if path then PlaySoundFile(path, "Master") end
end
