-- UI/Fonts.lua
-- What Farm? :: Self-contained font pairing (no ElvUI dependency)
-- Display/Headers: Expressway | Body/Tables: PT Sans Narrow

local ADDON_NAME, ns = ...
local WF = ns.WF

WF.UI = WF.UI or {}
WF.UI.Fonts = WF.UI.Fonts or {}

local BASE = "Interface\\AddOns\\" .. tostring(ADDON_NAME) .. "\\Media\\Fonts\\"
local BODY_FONT    = BASE .. "PTSansNarrow.ttf"
local DISPLAY_FONT = BASE .. "Expressway.ttf"

local function IsElvUIEnabled()
  if _G and _G.ElvUI then
    return true
  end
  if type(C_AddOns) == "table" and type(C_AddOns.IsAddOnLoaded) == "function" then
    local ok, loaded = pcall(C_AddOns.IsAddOnLoaded, "ElvUI")
    if ok and loaded then
      return true
    end
  end
  if type(IsAddOnLoaded) == "function" then
    local ok, loaded = pcall(IsAddOnLoaded, "ElvUI")
    if ok and loaded then
      return true
    end
  end
  return false
end

-- Centralized sizing (pt).
-- Tweak these to scale table text and headers everywhere without touching layout code.
WF.UI.Fonts.sizes = WF.UI.Fonts.sizes or {
  body         = 14,
  bodySmall    = 13,
  headerSmall  = 13,
  displayLarge = 17,
}

local function CloneFont(newName, baseName, fontFile, sizeOverride, flagsOverride, shadowAlpha, shadowX, shadowY)
  local base = _G[baseName]
  if not base or not base.GetFont then
    return nil
  end

  local f = CreateFont(newName)
  f:SetFontObject(base)

  local _, size, flags = base:GetFont()
  size = tonumber(size) or 12
  flags = flags or ""

  if sizeOverride ~= nil then
    local s = tonumber(sizeOverride)
    if s and s > 0 then
      size = s
    end
  end

  if flagsOverride ~= nil then
    flags = flagsOverride or ""
  end

  -- Preserve colors/shadows from the Blizzard font object; swap file and (optionally) size/flags.
  f:SetFont(fontFile, size, flags)

  if shadowAlpha ~= nil then
    f:SetShadowColor(0, 0, 0, shadowAlpha)
  end
  if shadowX ~= nil or shadowY ~= nil then
    f:SetShadowOffset(shadowX or 1, shadowY or -1)
  end

  return f
end

-- Body fonts (PT Sans Narrow)
CloneFont("WF_GameFontNormal",         "GameFontNormal",         BODY_FONT, WF.UI.Fonts.sizes.body,      "",        0.78, 1, -1)
CloneFont("WF_GameFontHighlight",      "GameFontHighlight",      BODY_FONT, WF.UI.Fonts.sizes.body,      "",        0.78, 1, -1)
CloneFont("WF_GameFontDisable",        "GameFontDisable",        BODY_FONT, WF.UI.Fonts.sizes.body,      "",        0.72, 1, -1)
CloneFont("WF_GameFontNormalSmall",    "GameFontNormalSmall",    BODY_FONT, WF.UI.Fonts.sizes.bodySmall, "",        0.74, 1, -1)
CloneFont("WF_GameFontHighlightSmall", "GameFontHighlightSmall", BODY_FONT, WF.UI.Fonts.sizes.bodySmall, "",        0.74, 1, -1)
CloneFont("WF_GameFontDisableSmall",   "GameFontDisableSmall",   BODY_FONT, WF.UI.Fonts.sizes.bodySmall, "",        0.68, 1, -1)

-- Display/headers (Expressway)
CloneFont("WF_GameFontNormalLarge",    "GameFontNormalLarge",    DISPLAY_FONT, WF.UI.Fonts.sizes.displayLarge, "", 0.72, 1, -1)
CloneFont("WF_GameFontHighlightLarge", "GameFontHighlightLarge", DISPLAY_FONT, WF.UI.Fonts.sizes.displayLarge, "", 0.72, 1, -1)

-- Chat-style editboxes (PT Sans Narrow)
CloneFont("WF_ChatFontNormal",         "ChatFontNormal",         BODY_FONT, nil, "", 0.78, 1, -1)

-- Expose for any helper usage
WF.UI.Fonts.body = BODY_FONT
WF.UI.Fonts.display = DISPLAY_FONT
WF.UI.Fonts.IsElvUIEnabled = IsElvUIEnabled
WF.UI.Fonts.UseBundledFontsOnly = function()
  return not IsElvUIEnabled()
end
WF.UI.Fonts.ResolveFontObject = function(preferredName, fallbackName)
  if preferredName and _G[preferredName] then
    return preferredName
  end
  if fallbackName and _G[fallbackName] then
    return fallbackName
  end
  return preferredName or fallbackName
end
WF.UI.Fonts.ResolveFontPath = function(kind)
  if kind == "display" then
    return DISPLAY_FONT
  end
  return BODY_FONT
end
