-- rCompassCastbar: GUI palette from class color + custom class icon (media/CustomClassIcons)

rCompassCastbarGuiTheme = rCompassCastbarGuiTheme or {}

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

function rCompassCastbarGuiTheme.RGBToWoWColorHex(r, g, b)
  return string.format(
    "%02x%02x%02x",
    math.max(0, math.min(255, math.floor(r * 255 + 0.5))),
    math.max(0, math.min(255, math.floor(g * 255 + 0.5))),
    math.max(0, math.min(255, math.floor(b * 255 + 0.5)))
  )
end

--- Fallback addon orange; live from class color. Sets warn/warnBg/warnBorder for reload UI among others.
function rCompassCastbarGuiTheme.ApplyPalette(C)
  if type(C) ~= "table" then return end

  local class = UnitClassBase("player") or select(2, UnitClass("player"))
  local r, g, b = 0.965, 0.533, 0.000

  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

  C.primary = { r, g, b }
  C.primaryHi = {
    clamp01(mix(r, 1, 0.42)),
    clamp01(mix(g, 1, 0.42)),
    clamp01(mix(b, 1, 0.42)),
  }
  C.primaryDim = { r * 0.62, g * 0.62, b * 0.62 }

  -- Keep shell colors neutral like rOrbs/rTooltip/rThreat; only accents are class-colored.
  C.toggleOff = {
    clamp01(0.088 + r * 0.034),
    clamp01(0.088 + g * 0.034),
    clamp01(0.088 + b * 0.034),
  }
  C.border = {
    clamp01(0.16 + r * 0.18),
    clamp01(0.16 + g * 0.18),
    clamp01(0.16 + b * 0.18),
    1.0,
  }
  C.separator = {
    clamp01(0.12 + r * 0.26),
    clamp01(0.12 + g * 0.26),
    clamp01(0.12 + b * 0.26),
    0.55,
  }
  C.botFill = {
    clamp01(0.032 + r * 0.052),
    clamp01(0.032 + g * 0.052),
    clamp01(0.032 + b * 0.052),
    1,
  }
  C.botEdge = {
    clamp01(0.052 + r * 0.095),
    clamp01(0.052 + g * 0.095),
    clamp01(0.052 + b * 0.095),
    1,
  }

  C.warn = {
    clamp01(mix(r, 1, 0.30)),
    clamp01(mix(g, 1, 0.30)),
    clamp01(mix(b, 1, 0.30)),
    1.0,
  }
  C.warnBg = {
    clamp01(0.03 + r * 0.10),
    clamp01(0.03 + g * 0.10),
    clamp01(0.03 + b * 0.10),
    1.0,
  }
  C.warnBorder = {
    clamp01(0.22 + r * 0.42),
    clamp01(0.22 + g * 0.42),
    clamp01(0.22 + b * 0.42),
    1.0,
  }
end

--- media/CustomClassIcons/<CLASS>/*.blp
function rCompassCastbarGuiTheme.ApplyCustomClassIconToTexture(tex)
  if not tex or type(tex.SetTexture) ~= "function" then return false end
  tex:SetTexture(nil)

  local classFile = UnitClassBase("player") or select(2, UnitClass("player"))
  if not classFile or classFile == "" then return false end

  local root = "Interface\\AddOns\\rCompassCastbar\\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 = 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
