-- rChat: GUI-Palette aus Klassenfarbe + Custom-Klassenlogo (core/CustomClassIcons)

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

--- Setzt C.primary / primaryHi / primaryDim und abgeleitete Flächen (Fallback: rChat-Rot #af1917)
function rChatGuiTheme.ApplyPalette(C)
  if type(C) ~= "table" then return end

  local class = UnitClassBase("player") or select(2, UnitClass("player"))
  local r, g, b = 0.686, 0.098, 0.090

  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.042 + r * 0.022),
    clamp01(0.038 + g * 0.022),
    clamp01(0.038 + b * 0.024),
    0.98,
  }
  C.sidebar = {
    clamp01(0.034 + r * 0.048),
    clamp01(0.030 + g * 0.048),
    clamp01(0.030 + b * 0.052),
    1.0,
  }
  C.content = {
    clamp01(0.050 + r * 0.036),
    clamp01(0.044 + g * 0.036),
    clamp01(0.044 + b * 0.040),
    0.97,
  }
  C.toggleOff = {
    clamp01(0.092 + r * 0.026),
    clamp01(0.088 + g * 0.026),
    clamp01(0.088 + b * 0.028),
  }
  C.border = {
    clamp01(0.18 + r * 0.15),
    clamp01(0.18 + g * 0.15),
    clamp01(0.18 + b * 0.15),
    1.0,
  }
  C.separator = {
    clamp01(0.16 + r * 0.24),
    clamp01(0.12 + g * 0.20),
    clamp01(0.12 + b * 0.20),
    0.45,
  }
  C.botFill = {
    clamp01(0.028 + r * 0.045),
    clamp01(0.024 + g * 0.045),
    clamp01(0.024 + b * 0.048),
    1,
  }
  C.botEdge = {
    clamp01(0.055 + r * 0.08),
    clamp01(0.048 + g * 0.08),
    clamp01(0.048 + b * 0.085),
    1,
  }
end

--- core/CustomClassIcons/<KLASSE>/*.blp
function rChatGuiTheme.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\\rChat\\core\\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
