local _, ns = ...
local BDT = ns.DataTexts
local L = BDT.L
BDT.FontManager = {}

----------------------------------------------------
-- Apply Font
----------------------------------------------------
function BDT.FontManager:ApplyFont()
    local globalFontPath = BDT.LSM:Fetch("font", BDT.db.profile.font) or STANDARD_TEXT_FONT
    local globalFontSize = BDT.db.profile.fontSize or 12
    local globalFontOutline = BDT.db.profile.fontOutline or "NONE"
    local globalOutline = (globalFontOutline == "NONE") and "" or globalFontOutline

    for _, bar in pairs(BDT.bars) do
        local barName = bar:GetName()
        local savedBar = BDT.db.profile.bars[barName]
        for i, slot in ipairs(bar.slots) do
            -- Check for per-slot overrides
            local slotData = savedBar and savedBar.slots and savedBar.slots[i]
            local slotOpts = type(slotData) == "table" and slotData or {}
            -- Use per-slot font if set, else global
            local fp = (slotOpts.fontPath and slotOpts.fontPath ~= "") and slotOpts.fontPath or globalFontPath
            local fs = slotOpts.fontSize or globalFontSize
            local fo = slotOpts.fontOutline and ((slotOpts.fontOutline == "NONE") and "" or slotOpts.fontOutline) or globalOutline
            if slot.text then
                pcall(function() slot.text:SetFont(fp, fs, fo) end)
                -- Tag with resolved settings so ApplyModuleFont respects them
                slot.text._slotFont = fp
                slot.text._slotSize = fs
                slot.text._slotOutline = fo
                -- Apply per-slot color override
                if slotOpts.colorUseClassColor then
                    local cc = C_ClassColor and C_ClassColor.GetClassColor and
                               C_ClassColor.GetClassColor(select(2, UnitClass("player")))
                    if cc then pcall(function() slot.text:SetTextColor(cc:GetRGB()) end) end
                elseif slotOpts.colorR then
                    slot.text:SetTextColor(slotOpts.colorR, slotOpts.colorG or 1, slotOpts.colorB or 1, 1)
                end
                if BDT.db.profile.fontShadowEnabled then
                    slot.text:SetShadowOffset(1, -1); slot.text:SetShadowColor(0, 0, 0, 1)
                else
                    slot.text:SetShadowOffset(0, 0)
                end
            end
            if slot.moduleFrame and slot.moduleFrame.text and slot.moduleFrame.text.SetFont then
                pcall(function() slot.moduleFrame.text:SetFont(fp, fs, fo) end)
                slot.moduleFrame.text._slotFont = fp
                slot.moduleFrame.text._slotSize = fs
                slot.moduleFrame.text._slotOutline = fo
            end
        end
    end
end

----------------------------------------------------
-- Apply Module Font
----------------------------------------------------
function BDT.FontManager:ApplyModuleFont(moduleName, textObject)
    if not moduleName or not textObject then return end
    -- Per-slot override takes priority over everything
    if textObject._slotFont then
        pcall(function() textObject:SetFont(textObject._slotFont,
            textObject._slotSize, textObject._slotOutline) end)
        return true
    end
    local overrideFont = BDT:GetModuleSetting(moduleName, "overrideFont", false)
    if overrideFont then
        -- Apply module font when override is enabled
        local fontName = BDT:GetModuleSetting(moduleName, "font", "Friz Quadrata TT")
        local fontSize = BDT:GetModuleSetting(moduleName, "fontSize", 12)
        local fontOutline = BDT:GetModuleSetting(moduleName, "fontOutline", "NONE")
        
        local fontPath = BDT.LSM:Fetch("font", fontName) or STANDARD_TEXT_FONT
        local outline = (fontOutline == "NONE") and "" or fontOutline
        
        textObject:SetFont(fontPath, fontSize, outline)
        return true
    else
        -- Apply global font when override is disabled
        local fontPath = BDT.LSM:Fetch("font", BDT.db.profile.font) or STANDARD_TEXT_FONT
        local fontSize = BDT.db.profile.fontSize or 12
        local fontOutline = BDT.db.profile.fontOutline or "NONE"
        local outline = (fontOutline == "NONE") and "" or fontOutline
        
        textObject:SetFont(fontPath, fontSize, outline)
        return false
    end
    
    return false
end

----------------------------------------------------
-- Register Fonts
----------------------------------------------------
function BDT.FontManager:RegisterFonts()
    BDT.LSM:Register("font", "Action Man", [[Interface\AddOns\BrightQoL\Assets\DataTexts\fonts\ActionMan.ttf]])
    BDT.LSM:Register("font", "Continuum Medium", [[Interface\AddOns\BrightQoL\Assets\DataTexts\fonts\ContinuumMedium.ttf]])
    BDT.LSM:Register("font", "Die Die Die!", [[Interface\AddOns\BrightQoL\Assets\DataTexts\fonts\DieDieDie.ttf]])
    BDT.LSM:Register("font", "Expressway", [[Interface\AddOns\BrightQoL\Assets\DataTexts\fonts\Expressway.ttf]])
    BDT.LSM:Register("font", "Homespun", [[Interface\AddOns\BrightQoL\Assets\DataTexts\fonts\Homespun.ttf]])
    BDT.LSM:Register("font", "PT Sans Narrow", [[Interface\AddOns\BrightQoL\Assets\DataTexts\fonts\PTSansNarrow.ttf]])
end
