
local _, ns = ...

--- Fill accent/class-tinted color slots in a palette table (e.g. settings.lua `C`
--- or Click-Cast `PC`). Neutral surfaces (bg, border, text, tabIdle) unchanged.
function ns.ApplyGuiClassTheme(t)
  if type(t) ~= "table" then return end

  local class = UnitClassBase("player") or select(2, UnitClass("player"))
  local r, g, b = 0.9, 0.18, 0.18

  if class then
    local got = false
    if C_ClassColor and C_ClassColor.GetClassColor then
      local cc = C_ClassColor.GetClassColor(class)
      if cc then
        if type(cc.GetRGB) == "function" then
          local r2, g2, b2 = cc:GetRGB()
          if type(r2) == "number" then
            r, g, b = r2, g2, b2
            got = true
          end
        end
        if not got and type(cc.r) == "number" then
          r, g, b = cc.r, cc.g, cc.b
          got = true
        end
      end
    end
    if not got and RAID_CLASS_COLORS and RAID_CLASS_COLORS[class] then
      local c = RAID_CLASS_COLORS[class]
      if c and type(c.r) == "number" then
        r, g, b = c.r, c.g, c.b
      end
    end
  end

  local function clamp01(x)
    if x < 0 then return 0 end
    if x > 1 then return 1 end
    return x
  end

  local function mix(ca, cb, f)
    return ca * (1 - f) + cb * f
  end

  t.accent = { r, g, b, 1 }
  t.accentHi = {
    clamp01(mix(r, 1, 0.42)),
    clamp01(mix(g, 1, 0.42)),
    clamp01(mix(b, 1, 0.42)),
    1,
  }
  t.accentDim = { r * 0.62, g * 0.62, b * 0.62, 1 }
  t.red = { r, g, b, 1 }

  t.header = {
    clamp01(0.04 + r * 0.22),
    clamp01(0.04 + g * 0.22),
    clamp01(0.04 + b * 0.22),
    1,
  }

  t.sectionHdr = {
    clamp01(mix(r, 1, 0.34)),
    clamp01(mix(g, 1, 0.34)),
    clamp01(mix(b, 1, 0.34)),
    1,
  }

  t.tabActive = {
    clamp01(0.06 + r * 0.52),
    clamp01(0.06 + g * 0.52),
    clamp01(0.06 + b * 0.52),
    1,
  }
  t.tabHover = {
    clamp01(0.05 + r * 0.34),
    clamp01(0.05 + g * 0.34),
    clamp01(0.05 + b * 0.34),
    1,
  }

  t.sliderFill = { r, g, b, 0.85 }
  t.sliderThumb = { r, g, b, 1 }
  t.section = { r, g, b, 1 }

  -- Was fixed gold (dropdown highlight etc.) → brighter class tint
  t.yellow = {
    clamp01(mix(r, 1, 0.30)),
    clamp01(mix(g, 1, 0.30)),
    clamp01(mix(b, 1, 0.30)),
    1,
  }
end

--- Find a .blp under media/CustomClassIcons/<CLASS>/ and apply to `tex`.
--- Filenames tried (order): icon.blp, <CLASS>.blp, <class>.blp, ClassIcon.blp, class.blp
--- Folders: class file first (e.g. WARRIOR), then lowercase.
--- @return boolean true if a texture was set
function ns.ApplyCustomClassIconForClass(tex, classFile)
  if not tex or type(tex.SetTexture) ~= "function" then return false end
  tex:SetTexture(nil)

  if not classFile or classFile == "" then return false end

  local root = "Interface\\AddOns\\rOrbs\\media\\CustomClassIcons\\"
  local dirs = { classFile, string.lower(classFile) }
  local names = {
    "icon.blp",
    classFile .. ".blp",
    string.lower(classFile) .. ".blp",
    "ClassIcon.blp",
    "class.blp",
  }

  for _, dir in ipairs(dirs) do
    for _, name in ipairs(names) do
      local path = root .. dir .. "\\" .. name
      local ok, err = pcall(function() tex:SetTexture(path) end)
      if ok then
        local tid = tex:GetTexture()
        if tid and tid ~= 0 and tid ~= "" then
          return true
        end
      end
    end
  end

  tex:SetTexture(nil)
  return false
end

function ns.ApplyCustomClassIconToTexture(tex)
  local classFile = UnitClassBase("player") or select(2, UnitClass("player"))
  return ns.ApplyCustomClassIconForClass(tex, classFile)
end

local CLASS_ICON_FILE = {
  DEATHKNIGHT = "deathknight",
  DEMONHUNTER = "demonhunter",
  DRUID       = "druid",
  EVOKER      = "evoker",
  HUNTER      = "hunter",
  MAGE        = "mage",
  MONK        = "monk",
  PALADIN     = "paladin",
  PRIEST      = "priest",
  ROGUE       = "rogue",
  SHAMAN      = "shaman",
  WARLOCK     = "warlock",
  WARRIOR     = "warrior",
}

local JIBERISH_ICON_SETS = {
  jiberish_fabled            = "Fabled",
  jiberish_fabledcore        = "Fabledcore",
  jiberish_fableddimension   = "Fableddimension",
  jiberish_fabledmyth        = "Fabledmyth",
  jiberish_fabledintothevoid = "fabledintothevoid",
  jiberish_fabledpixels      = "fabledpixels",
  jiberish_fabledpixelsv2    = "fabledpixelsv2",
  jiberish_fabledrealm       = "fabledrealm",
  jiberish_fabledrealmv2     = "fabledrealmv2",
}

ns.TargetNameClassIconModes = {
  { value = "custom",                     label = "TARGET_NAME_ICON_CUSTOM" },
  { value = "jiberish_fabled",            label = "TARGET_NAME_ICON_JIBERISH_FABLED" },
  { value = "jiberish_fabledcore",        label = "TARGET_NAME_ICON_JIBERISH_FABLEDCORE" },
  { value = "jiberish_fableddimension",   label = "TARGET_NAME_ICON_JIBERISH_FABLEDDIMENSION" },
  { value = "jiberish_fabledmyth",        label = "TARGET_NAME_ICON_JIBERISH_FABLEDMYTH" },
  { value = "jiberish_fabledintothevoid", label = "TARGET_NAME_ICON_JIBERISH_FABLEDINTOTHEVOID" },
  { value = "jiberish_fabledpixels",      label = "TARGET_NAME_ICON_JIBERISH_FABLEDPIXELS" },
  { value = "jiberish_fabledpixelsv2",    label = "TARGET_NAME_ICON_JIBERISH_FABLEDPIXELSV2" },
  { value = "jiberish_fabledrealm",       label = "TARGET_NAME_ICON_JIBERISH_FABLEDREALM" },
  { value = "jiberish_fabledrealmv2",     label = "TARGET_NAME_ICON_JIBERISH_FABLEDREALMV2" },
  { value = "class",                      label = "TARGET_NAME_ICON_CLASS" },
}

function ns.ResolveTargetNameClassIconMode()
  local mode = rOrbsDB and rOrbsDB.targetNameClassIconMode
  if not mode or mode == "" then return "custom" end
  return mode
end

local function applyBlizzardClassIcon(tex, classFile)
  local texCoords = CLASS_ICON_TCOORDS and CLASS_ICON_TCOORDS[classFile]
  if not texCoords then return false end
  tex:SetTexture("Interface\\WorldStateFrame\\Icons-Classes")
  tex:SetTexCoord(texCoords[1], texCoords[2], texCoords[3], texCoords[4])
  return true
end

local function applyJiberishClassIcon(tex, mode, classFile)
  local setName = JIBERISH_ICON_SETS[mode]
  local classLower = CLASS_ICON_FILE[classFile]
  if not setName or not classLower then return false end
  local path = "Interface\\AddOns\\rOrbs\\media\\JiberishUI Class Icons\\"
    .. setName .. "\\" .. classLower .. ".tga"
  tex:SetTexture(nil)
  local ok = pcall(function() tex:SetTexture(path) end)
  if not ok then return false end
  local tid = tex:GetTexture()
  if tid and tid ~= 0 and tid ~= "" then
    tex:SetTexCoord(0, 1, 0, 1)
    return true
  end
  tex:SetTexture(nil)
  return false
end

--- Apply target-name class icon texture for `classFile` using saved or given `mode`.
--- @return boolean true if a texture was set
function ns.ApplyTargetNameClassIconTexture(tex, classFile, mode)
  if not tex or type(tex.SetTexture) ~= "function" or not classFile or classFile == "" then
    return false
  end
  mode = mode or ns.ResolveTargetNameClassIconMode()

  if mode == "class" then
    return applyBlizzardClassIcon(tex, classFile)
  end

  if mode:find("^jiberish_") then
    return applyJiberishClassIcon(tex, mode, classFile)
  end

  if ns.ApplyCustomClassIconForClass(tex, classFile) then
    tex:SetTexCoord(0, 1, 0, 1)
    return true
  end
  return applyBlizzardClassIcon(tex, classFile)
end
