--[[--------------------------------------------------------------------
  PaladinHolyPower – ui/settings_panel/home_tab.lua
  Home Tab: "PlayerName" [Spec Icon] "Paladin"

  Fix: spec icon now updates when you change spec.
  How: we listen for spec/talent events and re-apply the correct icon.
----------------------------------------------------------------------]]

local PHP = _G.PaladinHolyPower
if not PHP then return end

local M = {}
_G.HomeTab = M

-- === Palette ===
local COL_WHITE = { 1, 1, 1, 1 }
local PALADIN_PINK = (RAID_CLASS_COLORS and RAID_CLASS_COLORS.PALADIN)
  and { RAID_CLASS_COLORS.PALADIN.r, RAID_CLASS_COLORS.PALADIN.g, RAID_CLASS_COLORS.PALADIN.b, 1 }
  or  { 0.9608, 0.5490, 0.7294, 1 }

-- === Defaults (editable) ===
M.ICON_COLOR = { 0.95, 0.90, 0.60, 1 } -- default tint (Paladin-ish)
M.ICON_SIZE  = { 15, 15 }              -- width, height

-- === Spec constants + assets ===
local SPEC = { HOLY = 65, PROTECTION = 66, RETRIBUTION = 70 }
local SPEC_ICONS = {
  [SPEC.HOLY]        = "Interface\\AddOns\\PaladinHolyPower\\media\\icons\\paladin_spec\\paladin_holy.png",
  [SPEC.PROTECTION]  = "Interface\\AddOns\\PaladinHolyPower\\media\\icons\\paladin_spec\\paladin_protection.png",
  [SPEC.RETRIBUTION] = "Interface\\AddOns\\PaladinHolyPower\\media\\icons\\paladin_spec\\paladin_retribution.png",
}

-- === Internal ===
M._icon    = nil
M._nameFS  = nil
M._classFS = nil
M._ev      = nil

-- === Helpers ===
local function get_identity()
  local playerName = (UnitName and UnitName("player")) or _G.UNKNOWN or "Unknown"
  if not playerName or playerName == "" then playerName = _G.UNKNOWN or "Unknown" end

  local className = _G.UNKNOWN or "Unknown"
  if UnitClass then
    local n = UnitClass("player")
    if n and n ~= "" then className = n end
  end

  local specIcon
  if GetSpecialization and GetSpecializationInfo then
    local idx = GetSpecialization()
    if idx then
      local specID = GetSpecializationInfo(idx)
      specIcon = SPEC_ICONS[specID]
    end
  end

  return playerName, className, specIcon
end

local function rgba(argsOrR, g, b, a)
  if type(argsOrR) == "table" then
    return argsOrR[1] or 1, argsOrR[2] or 1, argsOrR[3] or 1, (argsOrR[4] == nil and 1 or argsOrR[4])
  end
  return argsOrR or 1, g or 1, b or 1, (a == nil and 1 or a)
end

-- Re-apply the current name / class / spec icon to the UI elements.
function M.Refresh()
  if not (M._icon or M._nameFS or M._classFS) then return end
  local playerName, className, specIcon = get_identity()

  if M._nameFS then
    M._nameFS:SetText(playerName or "Unknown")
  end

  if M._classFS then
    M._classFS:SetText(className or "Paladin")
  end

  if M._icon then
    if specIcon then
      M._icon:SetTexture(specIcon)
    else
      -- Fallback gray block if spec data not ready yet; will be replaced on next event tick
      M._icon:SetColorTexture(0.2, 0.2, 0.2, 1)
    end
    M._icon:SetVertexColor(M.ICON_COLOR[1], M.ICON_COLOR[2], M.ICON_COLOR[3], M.ICON_COLOR[4] or 1)
  end
end

-- === Public API (safe pre/post Build) ===
function M.ColorizeSpecIcon(r, g, b, a)
  local R, G, B, A = rgba(r, g, b, a)
  M.ICON_COLOR = { R, G, B, A }
  if M._icon then M._icon:SetVertexColor(R, G, B, A) end
end

function M.ResizeSpecIcon(w, h)
  local W = tonumber(w); if not W or W <= 0 then return end
  local H = tonumber(h) or W; if H <= 0 then H = W end
  M.ICON_SIZE = { W, H }
  if M._icon then M._icon:SetSize(W, H) end
end

-- === Build ===
function M.Build(parent)
  local P = 18
  local playerName, className, specIcon = get_identity()

  -- Player name
  local nameFS = parent:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
  nameFS:SetPoint("TOPLEFT", parent, "TOPLEFT", P, -P)
  nameFS:SetJustifyH("LEFT")
  nameFS:SetTextColor(unpack(COL_WHITE))
  nameFS:SetText(playerName)
  M._nameFS = nameFS

  -- Spec icon
  local icon = parent:CreateTexture(nil, "ARTWORK")
  icon:SetPoint("LEFT", nameFS, "RIGHT", 8, -1)
  icon:SetSize(M.ICON_SIZE[1], M.ICON_SIZE[2])
  if specIcon then
    icon:SetTexture(specIcon)
  else
    icon:SetColorTexture(0.2, 0.2, 0.2, 1)
  end
  icon:SetVertexColor(M.ICON_COLOR[1], M.ICON_COLOR[2], M.ICON_COLOR[3], M.ICON_COLOR[4] or 1)
  M._icon = icon

  -- Class text
  local classFS = parent:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
  classFS:SetPoint("LEFT", icon, "RIGHT", 8, 0)
  classFS:SetJustifyH("LEFT")
  classFS:SetTextColor(unpack(PALADIN_PINK))
  classFS:SetText(className or "Paladin")
  M._classFS = classFS

  -- Events to keep the spec icon in sync with your current spec
  if M._ev then
    M._ev:UnregisterAllEvents()
    M._ev:SetScript("OnEvent", nil)
  end
  local ev = CreateFrame("Frame", nil, parent)
  ev:SetScript("OnEvent", function(_, evt, unit)
    if evt == "PLAYER_SPECIALIZATION_CHANGED" and unit and unit ~= "player" then return end
    -- Refresh all identity bits (icon, name, class) safely
    M.Refresh()
  end)
  -- Cover both modern and legacy spec/talent update events
  ev:RegisterEvent("PLAYER_ENTERING_WORLD")
  ev:RegisterEvent("PLAYER_SPECIALIZATION_CHANGED") -- fires with unit arg
  ev:RegisterEvent("PLAYER_TALENT_UPDATE")          -- sometimes fires on spec swap
  if C_EventUtils and C_EventUtils.IsEventValid then
    if C_EventUtils.IsEventValid("TRAIT_CONFIG_UPDATED") then
      ev:RegisterEvent("TRAIT_CONFIG_UPDATED")      -- DF+ talent system
    end
  end
  M._ev = ev

  -- Also refresh when the panel becomes visible (handles delayed data)
  if parent and parent.HookScript then
    parent:HookScript("OnShow", function() M.Refresh() end)
  end

  -- Initial pass (in case spec data already available)
  M.Refresh()
end

return M