-- rMinimap: GUI-Palette aus Klassenfarbe + Custom-Klassenlogo (media/CustomClassIcons)

rMinimapGuiTheme = rMinimapGuiTheme 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 rMinimapGuiTheme.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 aus Klassenfarbe.
function rMinimapGuiTheme.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 }

  C.bg = {
    clamp01(0.048 + r * 0.028),
    clamp01(0.048 + g * 0.028),
    clamp01(0.048 + b * 0.032),
    0.98,
  }
  C.sidebar = {
    clamp01(0.038 + r * 0.042),
    clamp01(0.038 + g * 0.042),
    clamp01(0.038 + b * 0.048),
    1.0,
  }
  C.content = {
    clamp01(0.056 + r * 0.048),
    clamp01(0.056 + g * 0.048),
    clamp01(0.056 + b * 0.056),
    0.97,
  }
  C.panel = {
    C.content[1], C.content[2], C.content[3], C.content[4],
  }
  C.toggleOff = {
    clamp01(0.088 + r * 0.032),
    clamp01(0.088 + g * 0.032),
    clamp01(0.088 + b * 0.036),
  }
  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.14 + r * 0.28),
    clamp01(0.12 + g * 0.24),
    clamp01(0.12 + b * 0.24),
    0.55,
  }
  C.botFill = {
    clamp01(0.032 + r * 0.05),
    clamp01(0.032 + g * 0.05),
    clamp01(0.032 + b * 0.055),
    1,
  }
  C.botEdge = {
    clamp01(0.055 + r * 0.09),
    clamp01(0.052 + g * 0.09),
    clamp01(0.052 + b * 0.10),
    1,
  }
end

--- media/CustomClassIcons/<KLASSE>/*.blp
function rMinimapGuiTheme.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\\rMinimap\\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
