--[[--------------------------------------------------------------------
  PaladinHolyPower - ui/components/colorpicker.lua
  Custom Color Picker Button (ElvUI Style, tight hitbox)
----------------------------------------------------------------------]]

local PHP = _G.PaladinHolyPower
if not PHP then return end

local CONST = {
  WIDTH          = 150,
  HEIGHT         = 25,
  SWATCH_WIDTH   = 28,
  SWATCH_HEIGHT  = 18,
  BG_TEXTURE     = "Interface\\Buttons\\WHITE8x8",
  BG_COLOR       = { 0.102, 0.102, 0.102, 1 },
  BORDER_TEXTURE = "Interface\\Buttons\\WHITE8x8",
  BORDER_SIZE    = 1,
  BORDER_COLOR   = { 0, 0, 0, 1 },
  LABEL_TEMPLATE = "GameFontNormal",
}

function PHP.CreateColorPicker(parent, label, initialColor, x, y, callback)
  -- Parse color
  local r, g, b
  if type(initialColor) == "table" and #initialColor >= 3 then
    r, g, b = initialColor[1], initialColor[2], initialColor[3]
  elseif type(initialColor) == "table" and initialColor.r and initialColor.g and initialColor.b then
    r, g, b = initialColor.r, initialColor.g, initialColor.b
  elseif type(initialColor) == "number" then
    r, g, b = initialColor, initialColor, initialColor
  else
    r, g, b = 1, 1, 1
  end

  -- Main container (anchor only)
  local f = CreateFrame("Frame", nil, parent)
  f:SetSize(CONST.WIDTH, CONST.HEIGHT)
  f:SetPoint("TOPLEFT", parent, "TOPLEFT", x or 0, y or 0)

  -- Swatch (left, 1px border)
  f.swatch = CreateFrame("Button", nil, f, "BackdropTemplate")
  f.swatch:SetSize(CONST.SWATCH_WIDTH, CONST.SWATCH_HEIGHT)
  f.swatch:SetPoint("LEFT", f, "LEFT", 0, 0)
  f.swatch:SetBackdrop({
    bgFile   = CONST.BG_TEXTURE,
    edgeFile = CONST.BORDER_TEXTURE,
    edgeSize = CONST.BORDER_SIZE,
  })
  f.swatch:SetBackdropColor(r, g, b, 1)
  f.swatch:SetBackdropBorderColor(unpack(CONST.BORDER_COLOR))

  -- Label right of the swatch
  f.label = f:CreateFontString(nil, "OVERLAY", CONST.LABEL_TEMPLATE)
  f.label:SetPoint("LEFT", f.swatch, "RIGHT", 8, 0)
  f.label:SetText(label or "")
  f.label:SetTextColor(1, 1, 1)

  -- Store color
  f.currentColor = { r, g, b }

  local function UpdateColor(r, g, b)
    f.currentColor = { r, g, b }
    f.swatch:SetBackdropColor(r, g, b, 1)
    if callback then callback(r, g, b) end
  end

  local function ColorPicker_Live()
    local newR, newG, newB
    if ColorPickerFrame.GetColorRGB then
      newR, newG, newB = ColorPickerFrame:GetColorRGB()
    else
      newR, newG, newB = ColorPickerFrame.r, ColorPickerFrame.g, ColorPickerFrame.b
    end
    UpdateColor(newR, newG, newB)
  end
  local function ColorPicker_Finished() ColorPicker_Live() end
  local function ColorPicker_Canceled(previous)
    UpdateColor(unpack(previous))
  end

  f.swatch:SetScript("OnClick", function()
    local prev = { unpack(f.currentColor) }
    if ColorPickerFrame.SetColorRGB then
      ColorPickerFrame:SetColorRGB(unpack(f.currentColor))
    else
      ColorPickerFrame.r, ColorPickerFrame.g, ColorPickerFrame.b = unpack(f.currentColor)
    end
    ColorPickerFrame.hasOpacity = false
    ColorPickerFrame.func = ColorPicker_Finished
    ColorPickerFrame.opacityFunc = ColorPicker_Live
    ColorPickerFrame.swatchFunc = ColorPicker_Live
    ColorPickerFrame.cancelFunc = function() ColorPicker_Canceled(prev) end
    ColorPickerFrame:Hide() -- Workaround
    ColorPickerFrame:Show()
  end)

  -- Unified tight hitbox (covers only swatch+label)
  local labelWidth = f.label:GetStringWidth() or 0
  local hitboxWidth = CONST.SWATCH_WIDTH + 8 + labelWidth + 2
  local hitbox = CreateFrame("Button", nil, f)
  hitbox:SetSize(hitboxWidth, CONST.SWATCH_HEIGHT)
  hitbox:SetPoint("LEFT", f.swatch, "LEFT", 0, 0)
  hitbox:EnableMouse(true)
  hitbox:SetScript("OnClick", function() f.swatch:Click() end)
  hitbox:SetScript("OnEnter", function()
    if PHP.Tooltip and PHP.Tooltip.Show then
      PHP.Tooltip.Show(f.swatch, "Font Color", "Click to pick a color.")
    end
  end)
  hitbox:SetScript("OnLeave", function()
    if PHP.Tooltip and PHP.Tooltip.Hide then
      PHP.Tooltip.Hide()
    end
  end)

  -- Remove events from label and swatch (handled by hitbox now)
  f.label:EnableMouse(false)
  f.swatch:EnableMouse(false)

  function f:SetColor(r, g, b) UpdateColor(r, g, b) end

  return f
end