local addonName, addonTable = ...
local Media = {}
addonTable.Media = Media

local function AddUnique(list, seen, name, path)
    if not seen[name] then
        table.insert(list, { name = name, path = path })
        seen[name] = true
    end
end

local BASE_PATH = "Interface\\AddOns\\" .. addonName .. "\\Media\\"
local FONT_PATH = BASE_PATH .. "Fonts\\"
local TEXTURE_PATH = BASE_PATH .. "Textures\\"

local misFuentes = {}
local misTexturas = {}
local misBackgrounds = {}
local misBorders = {}
local seenFonts = {}
local seenTextures = {}
local seenBackgrounds = {}
local seenBorders = {}

-- ========================================================
-- FUENTES LOCALES DEL ADDON
-- ========================================================
AddUnique(misFuentes, seenFonts, "Naowh", FONT_PATH .. "Naowh.ttf")
AddUnique(misFuentes, seenFonts, "Gotham Ultra", FONT_PATH .. "GothamNarrowUltra.ttf")
AddUnique(misFuentes, seenFonts, "Friz Quadrata (WoW)", "Fonts\\FRIZQT__.TTF")

local extraFonts = {
    "Accidental Presidency",
    "ActionMan",
    "ALBAS__",
    "AnonymousPro-Regular",
    "ArmWrestler",
    "AtkinsonHyperlegibleNext-Regular",
    "AvantGarde_LT_Book_Regular",
    "BAARS__",
    "Bazooka",
    "Blazed",
    "BorisBlackBloxx",
    "BorisBlackBloxxDirty",
    "Cabin-Regular",
    "CelestiaMediumRedux1.55",
    "centurygothic",
    "COLLEGIA",
    "ContinuumMedium",
    "coolinbi",
    "DejaVuSans",
    "DejaVuSans-Bold",
    "DieDieDie",
    "DIOGENES",
    "Disko",
    "expressway rg",
    "Expressway-Bold",
    "FRAKS__",
    "GothamNarrowUltra",
    "GothamNarrow-Ultra",
    "GothamNarrowUltraAsia",
    "Homespun",
    "impact",
    "JetBrainsMono-Regular",
    "LiberationSans-Regular",
    "LiberationSerif-Regular",
    "MystikOrbs",
    "NanumGothic-Regular",
    "Naowh",
    "NaowhAsia",
    "Nunito-Regular",
    "OpenDyslexic3-Regular",
    "Pokemon Solid",
    "Prototype",
    "PTSansNarrow-Bold",
    "Rock Show Whiplash",
    "SF Diego Sans",
    "Solange",
    "starcine",
    "trashco",
    "Ubuntu-C",
    "Ubuntu-L",
    "Verdana",
    "waltographUI",
    "X360",
    "YanoneKaffeesatz-Regular",
}

for _, fontName in ipairs(extraFonts) do
    AddUnique(misFuentes, seenFonts, fontName, FONT_PATH .. fontName .. ".ttf")
end

-- ========================================================
-- TEXTURAS LOCALES DEL ADDON
-- Solo se registran las de statusbar para el selector de barras
-- ========================================================
AddUnique(misTexturas, seenTextures, "Naowh Melli", TEXTURE_PATH .. "Melli.tga")
AddUnique(misTexturas, seenTextures, "Naowh Gradient", TEXTURE_PATH .. "NaowhGradient.tga")
AddUnique(misTexturas, seenTextures, "Plano (Flat)", "Interface\\BUTTONS\\WHITE8X8")
AddUnique(misTexturas, seenTextures, "Blizzard Normal", "Interface\\TargetingFrame\\UI-StatusBar")

local extraTextures = {
    "Aluminium",
    "Armory",
    "BantoBar",
    "Bars",
    "Bumps",
    "Button",
    "Charcoal",
    "Cilo",
    "Cloud",
    "combo",
    "Comet",
    "Dabs",
    "DarkBottom",
    "Diagonal",
    "Empty",
    "Falumn",
    "Fifths",
    "Flat",
    "Fourths",
    "Frost",
    "Glamour",
    "Glamour2",
    "Glamour3",
    "Glamour4",
    "Glamour5",
    "Glamour6",
    "Glamour7",
    "Glass",
    "Glaze",
    "Glaze2",
    "Gloss",
    "Graphite",
    "Grid",
    "Hatched",
    "Healbot",
    "LiteStep",
    "LiteStepLite",
    "Lyfe",
    "Melli",
    "MelliDark",
    "MelliDarkRough",
    "Minimalist",
    "Otravi",
    "Outline",
    "Perl",
    "Perl2",
    "Pill",
    "Rain",
    "Rocks",
    "Round",
    "Ruben",
    "Runes",
    "Skewed",
    "Smooth",
    "Smoothv2",
    "Smudge",
    "Steel",
    "Striped",
    "Tube",
    "Water",
    "Wglass",
    "Wisps",
    "Xeon",
}

for _, textureName in ipairs(extraTextures) do
    AddUnique(misTexturas, seenTextures, textureName, TEXTURE_PATH .. "statusbar\\" .. textureName .. ".tga")
end

-- ========================================================
-- TEXTURAS DE FONDO Y BORDES DEL ADDON
-- De momento solo se registran para tenerlas disponibles
-- luego ya decidiremos dónde aplicarlas en la UI
-- ========================================================
local extraBackgrounds = {
    "bricks",
    "brushedmetal",
    "copper",
    "moo",
    "smoke",
}

for _, backgroundName in ipairs(extraBackgrounds) do
    AddUnique(misBackgrounds, seenBackgrounds, backgroundName, TEXTURE_PATH .. "background\\" .. backgroundName .. ".tga")
end

local extraBorders = {
    { name = "roth", path = TEXTURE_PATH .. "border\\roth.tga" },
    { name = "SeerahScalloped", path = TEXTURE_PATH .. "border\\SeerahScalloped.blp" },
}

for _, border in ipairs(extraBorders) do
    AddUnique(misBorders, seenBorders, border.name, border.path)
end

local DEFAULT_FONT = FONT_PATH .. "Naowh.ttf"
local DEFAULT_TEX = TEXTURE_PATH .. "Melli.tga"

local currentFontPath = DEFAULT_FONT
local currentTexPath = DEFAULT_TEX

function Media:GetAvailableFonts()
    return misFuentes
end

function Media:GetAvailableTextures()
    return misTexturas
end

function Media:GetAvailableBackgrounds()
    return misBackgrounds
end

function Media:GetAvailableBorders()
    return misBorders
end

function Media:LoadFromDB(db)
    currentFontPath = DEFAULT_FONT
    if db.fontName then
        for _, f in ipairs(misFuentes) do
            if f.name == db.fontName then
                currentFontPath = f.path
                break
            end
        end
    end

    currentTexPath = DEFAULT_TEX
    if db.textureName then
        for _, t in ipairs(misTexturas) do
            if t.name == db.textureName then
                currentTexPath = t.path
                break
            end
        end
    end
end

function Media:ApplyFont(fontString, size)
    if fontString then
        local success = fontString:SetFont(currentFontPath, size or 12, "OUTLINE")
        if not success then
            fontString:SetFont("Fonts\\FRIZQT__.TTF", size or 12, "OUTLINE")
        end
    end
end

function Media:ApplyTexture(widget)
    if widget then
        if widget.SetStatusBarTexture then
            widget:SetStatusBarTexture(currentTexPath)
        elseif widget.SetTexture then
            widget:SetTexture(currentTexPath)
        end
    end
end

function Media:ApplyNamedTexture(widget, texturePath)
    if widget and texturePath then
        if widget.SetStatusBarTexture then
            widget:SetStatusBarTexture(texturePath)
        elseif widget.SetTexture then
            widget:SetTexture(texturePath)
        end
    end
end

