local addonName, ns = ...

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 }

  -- Neutrale rOrbs-Shell: Klassenfarbe nur als Akzent, nicht als Flächentint.
  C.bg = { 0.055, 0.055, 0.062, 0.98 }
  C.sidebar = { 0.045, 0.045, 0.052, 1.0 }
  C.content = { 0.068, 0.068, 0.075, 0.97 }
  C.panel = { 0.10, 0.10, 0.112, 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
