-- rCastbar: GUI palette from class color + custom class icon

rCastbarGuiTheme = rCastbarGuiTheme 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 rCastbarGuiTheme.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

--- Player class color and derived surfaces (bg, sidebar, border, …)
function rCastbarGuiTheme.ApplyPalette(C)
  if type(C) ~= "table" then return end

  local class = UnitClassBase("player") or select(2, UnitClass("player"))
  local r, g, b = 0.682, 0.271, 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 }

  C.bg = {
    clamp01(0.045 + r * 0.028),
    clamp01(0.045 + g * 0.028),
    clamp01(0.048 + b * 0.032),
    0.98,
  }
  C.sidebar = {
    clamp01(0.036 + r * 0.040),
    clamp01(0.036 + g * 0.040),
    clamp01(0.038 + b * 0.046),
    1.0,
  }
  C.content = {
    clamp01(0.052 + r * 0.046),
    clamp01(0.052 + g * 0.046),
    clamp01(0.056 + b * 0.052),
    0.97,
  }
  C.panel = {
    C.content[1], C.content[2], C.content[3], 0.95,
  }
  C.toggleOff = {
    clamp01(0.085 + r * 0.030),
    clamp01(0.085 + g * 0.030),
    clamp01(0.088 + b * 0.034),
  }
  C.border = {
    clamp01(0.14 + r * 0.20),
    clamp01(0.14 + g * 0.20),
    clamp01(0.14 + b * 0.22),
    1.0,
  }
  C.separator = {
    clamp01(0.12 + r * 0.26),
    clamp01(0.12 + g * 0.24),
    clamp01(0.12 + b * 0.26),
    0.55,
  }
  C.botFill = {
    clamp01(0.028 + r * 0.048),
    clamp01(0.026 + g * 0.048),
    clamp01(0.028 + b * 0.052),
    1,
  }
  C.botEdge = {
    clamp01(0.050 + r * 0.10),
    clamp01(0.048 + g * 0.10),
    clamp01(0.050 + b * 0.11),
    1,
  }
end

--- media/CustomClassIcons/<CLASS>/*.blp
function rCastbarGuiTheme.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\\rCastbar\\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
