--[[--------------------------------------------------------------------
  PaladinHolyPower – ui/components/button.lua
  Styled Button: Minimal, ElvUI/tab-style (animated to match panel tabs, no yellow line)

  Hover behavior matches dropdown rows & sidebar tabs:
    • Background stays at base (idle), no darkening
    • Subtle white "ink" lift
    • White-glass glossy overlay (vertical gradient, ADD blend)
----------------------------------------------------------------------]]

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

local CONST = {
  WIDTH          = 80,
  HEIGHT         = 20,

  -- Match panel.lua tab palette (fallbacks provided)
  COLOR_N        = (PHP.TAB_COLOR    or { 0.22, 0.20, 0.14, 1.00 }), -- idle
  COLOR_H        = (PHP.TAB_HOVER    or { 0.10, 0.09, 0.07, 0.98 }), -- legacy hover (unused; kept for compat)
  COLOR_P        = (PHP.TAB_SELECTED or { 0.13, 0.12, 0.10, 1.00 }), -- pressed

  TEXT_N         = { 1.00, 1.00, 1.00, 0.95 }, -- same as tabs
  TEXT_A         = { 1.00, 0.95, 0.75, 1.00 }, -- warm holy tone when pressed

  BG_TEXTURE     = "Interface\\Buttons\\WHITE8x8",
  BORDER_TEXTURE = "Interface\\Buttons\\WHITE8x8",
  BORDER_SIZE    = 1,
  FONT_OBJECT    = "GameFontNormal",

  -- Animation
  SPEED          = 18,    -- higher = snappier
  INK_HOVER      = 0.06,  -- subtle wash like tabs
  INK_PRESSED    = 0.12,  -- deeper press pulse
}

local function lerp(a, b, t) return a + (b - a) * t end

function PHP.CreateStyledButton(parent, text, x, y, width, height, onClick)
  width  = width  or CONST.WIDTH
  height = height or CONST.HEIGHT

  local btn = CreateFrame("Button", nil, parent, "BackdropTemplate")
  btn:SetSize(width, height)
  btn:SetPoint("TOPLEFT", parent, "TOPLEFT", x or 0, y or 0)

  btn:SetBackdrop({
    bgFile   = CONST.BG_TEXTURE,
    edgeFile = CONST.BORDER_TEXTURE,
    edgeSize = CONST.BORDER_SIZE,
  })
  btn:SetBackdropColor(unpack(CONST.COLOR_N))
  btn:SetBackdropBorderColor(0, 0, 0, 1)

  -- Text
  btn.text = btn:CreateFontString(nil, "OVERLAY", CONST.FONT_OBJECT)
  btn.text:SetPoint("CENTER")
  btn.text:SetText(text or "Button")
  btn.text:SetTextColor(CONST.TEXT_N[1], CONST.TEXT_N[2], CONST.TEXT_N[3], CONST.TEXT_N[4])
  btn.text:SetShadowOffset(1, -1)
  btn.text:SetShadowColor(0, 0, 0, 1)

  -- Soft highlight “ink” layer (alpha-driven)
  local ink = btn:CreateTexture(nil, "BACKGROUND")
  ink:SetTexture(CONST.BG_TEXTURE)
  ink:SetAllPoints(btn)
  ink:SetVertexColor(1, 1, 1, 1)
  ink:SetAlpha(0)
  btn._ink = ink

  -- Glossy hover overlay: vertical gradient, ADD blend (same as dropdown/tabs)
  local gloss = btn:CreateTexture(nil, "OVERLAY", nil, 1)
  gloss:SetAllPoints(btn)
  gloss:SetTexture(CONST.BG_TEXTURE)
  gloss:SetBlendMode("ADD")
  if gloss.SetGradient and CreateColor then
    gloss:SetGradient("VERTICAL",
      CreateColor(1, 1, 1, 0.10),  -- top shine
      CreateColor(1, 1, 1, 0.00)) -- fade out
  else
    gloss:SetVertexColor(1, 1, 1, 0.05)
  end
  gloss:SetAlpha(0) -- only visible on hover
  btn._gloss = gloss

  -- Track pressed state explicitly (avoids colon-method/and parsing gotchas)
  btn.__pressed = false

  -- Animated state
  btn._state = {
    targetBG   = { CONST.COLOR_N[1], CONST.COLOR_N[2], CONST.COLOR_N[3], CONST.COLOR_N[4] },
    targetText = { CONST.TEXT_N[1],  CONST.TEXT_N[2],  CONST.TEXT_N[3],  CONST.TEXT_N[4]  },
    inkAlpha   = 0,
    speed      = CONST.SPEED,
  }

  -- Event-driven animator (runs only during transitions)
  local function __ButtonRunAnim(self, elapsed)
    local s = self._state
    if not s then self:SetScript("OnUpdate", nil); self._animating = false; return end
    local k = math.min(1, (elapsed or 0) * (s.speed or 18))

    -- Lerp backdrop
    local r,g,b,a = self:GetBackdropColor()
    r = lerp(r, s.targetBG[1], k)
    g = lerp(g, s.targetBG[2], k)
    b = lerp(b, s.targetBG[3], k)
    a = lerp(a, s.targetBG[4], k)
    self:SetBackdropColor(r,g,b,a)

    -- Lerp text
    local tr,tg,tb,ta = self.text:GetTextColor()
    tr = lerp(tr, s.targetText[1], k)
    tg = lerp(tg, s.targetText[2], k)
    tb = lerp(tb, s.targetText[3], k)
    ta = lerp(ta, s.targetText[4], k)
    self.text:SetTextColor(tr,tg,tb,ta)

    -- Ink
    local ia = self._ink:GetAlpha()
    self._ink:SetAlpha(lerp(ia, s.inkAlpha or 0, k))

    -- Stop when close enough
    if math.abs(r - s.targetBG[1]) < 0.001 and math.abs(g - s.targetBG[2]) < 0.001
      and math.abs(b - s.targetBG[3]) < 0.001 and math.abs(a - s.targetBG[4]) < 0.001
      and math.abs(self._ink:GetAlpha() - (s.inkAlpha or 0)) < 0.001 then
      self:SetScript("OnUpdate", nil); self._animating = false
    end
  end
  local function __ButtonKick(self)
    if self._animating then return end
    self._animating = true
    self:SetScript("OnUpdate", __ButtonRunAnim)
  end

  -- Hover: keep base bg; add ink + gloss
  btn:SetScript("OnEnter", function(self)
    local s = self._state
    -- keep s.targetBG as-is (idle or pressed); no dark hover plate
    s.targetText = { CONST.TEXT_N[1], CONST.TEXT_N[2], CONST.TEXT_N[3], CONST.TEXT_N[4] }
    s.inkAlpha   = CONST.INK_HOVER
    self._gloss:SetAlpha(1)
    __ButtonKick(self)
  end)

  btn:SetScript("OnLeave", function(self)
    local s = self._state
    if self.__pressed then
      -- pressed state preserved by OnMouseDown logic
      s.targetBG   = { CONST.COLOR_P[1], CONST.COLOR_P[2], CONST.COLOR_P[3], CONST.COLOR_P[4] }
      s.targetText = { CONST.TEXT_A[1],  CONST.TEXT_A[2],  CONST.TEXT_A[3],  CONST.TEXT_A[4]  }
      s.inkAlpha   = CONST.INK_PRESSED
    else
      -- idle look
      s.targetBG   = { CONST.COLOR_N[1], CONST.COLOR_N[2], CONST.COLOR_N[3], CONST.COLOR_N[4] }
      s.targetText = { CONST.TEXT_N[1],  CONST.TEXT_N[2],  CONST.TEXT_N[3],  CONST.TEXT_N[4]  }
      s.inkAlpha   = 0
    end
    self._gloss:SetAlpha(0)
    __ButtonKick(self)
  end)

  -- Press feedback (deeper ink + active plate; warm text pop)
  btn:SetScript("OnMouseDown", function(self)
    self.__pressed = true
    local s = self._state
    s.targetBG   = { CONST.COLOR_P[1], CONST.COLOR_P[2], CONST.COLOR_P[3], CONST.COLOR_P[4] }
    s.targetText = { CONST.TEXT_A[1],  CONST.TEXT_A[2],  CONST.TEXT_A[3],  CONST.TEXT_A[4]  }
    s.inkAlpha   = CONST.INK_PRESSED
    __ButtonKick(self)
  end)

  btn:SetScript("OnMouseUp", function(self, mouseButton)
    self.__pressed = false
    local s = self._state
    if self:IsMouseOver() then
      -- hover look: base idle bg + ink + gloss
      s.targetBG   = { CONST.COLOR_N[1], CONST.COLOR_N[2], CONST.COLOR_N[3], CONST.COLOR_N[4] }
      s.targetText = { CONST.TEXT_N[1],  CONST.TEXT_N[2],  CONST.TEXT_N[3],  CONST.TEXT_N[4]  }
      s.inkAlpha   = CONST.INK_HOVER
      self._gloss:SetAlpha(1)
    else
      -- idle look
      s.targetBG   = { CONST.COLOR_N[1], CONST.COLOR_N[2], CONST.COLOR_N[3], CONST.COLOR_N[4] }
      s.targetText = { CONST.TEXT_N[1],  CONST.TEXT_N[2],  CONST.TEXT_N[3],  CONST.TEXT_N[4]  }
      s.inkAlpha   = 0
      self._gloss:SetAlpha(0)
    end
    __ButtonKick(self)

    if onClick then onClick(self, mouseButton) end
  end)

  return btn
end