local addonName, ns = ...

-- Bar hover glow — a single nine-slice rounded-rectangle halo baked in
-- Textures/BarGlow.tga (full strength at the bar edge, exponential falloff
-- outward, slight inner bleed). Slice corners stay crisp while the edges
-- stretch, so one texture fits every user-configured bar size. Replaces the
-- old two-IconAlert-overlay approximation.
local GLOW_TEX = "Interface\\AddOns\\BlitzFramesTargets\\Textures\\BarGlow.tga"
local GLOW_PAD = 10  -- matches the baked boundary inset (texels of glow reach)
local GLOW_MARGIN = 18  -- slice margin: inset + corner radius + AA

local function GetOrCreateGlow(bar)
    if bar._glow then return bar._glow end
    -- PARENTED TO THE CONTAINER, ANCHORED TO bar.visual -- the two are separate
    -- decisions and only the parent is about layering.
    --
    -- The container parent is what keeps the glow BEHIND the cards: a frame's
    -- own textures draw below its child frames whatever the draw layer says, and
    -- every bar is a child of the container. Parenting to the bar (or to
    -- bar.visual) would put the glow in front of the card it is meant to sit
    -- behind. Keep it here.
    --
    -- The anchor is in ShowGlow and deliberately does NOT use `bar`. This used
    -- to, with the note "so the bar's scale animation doesn't affect the glow" --
    -- correct when the scale was a brief press animation to be escaped, wrong now
    -- that hover and the card dock hold a zoom. bar is the secure button and
    -- never scales; ns.ApplyBarScale scales bar.visual, so anchoring to bar left
    -- the halo at the unzoomed size while the card grew out of it.
    local container = bar:GetParent()
    local t = container:CreateTexture(nil, "OVERLAY", nil, 7)
    t:SetTexture(GLOW_TEX)
    t:SetTextureSliceMargins(GLOW_MARGIN, GLOW_MARGIN, GLOW_MARGIN, GLOW_MARGIN)
    t:SetTextureSliceMode(Enum.UITextureSliceMode.Stretched)
    t:SetBlendMode("ADD")
    t:Hide()
    bar._glow = t
    return t
end

-- color: optional {r, g, b, a} table; defaults to cool blue-white gold
function ns.ShowGlow(bar, color)
    local t = GetOrCreateGlow(bar)
    local c = color or { 0.9, 0.88, 0.72, 0.6 }
    -- bar.visual is the frame ns.ApplyBarScale zooms; bar is the secure button
    -- and is always full size. Anchoring to a scaled frame resolves in screen
    -- space, so the halo tracks the card's zoomed edges while GLOW_PAD stays a
    -- constant reach -- the border keeps its thickness instead of fattening with
    -- the zoom. Falls back to bar for anything built without a visual.
    local anchor = bar.visual or bar
    t:ClearAllPoints()
    t:SetPoint("TOPLEFT", anchor, "TOPLEFT", -GLOW_PAD, GLOW_PAD)
    t:SetPoint("BOTTOMRIGHT", anchor, "BOTTOMRIGHT", GLOW_PAD, -GLOW_PAD)
    t:SetVertexColor(c[1], c[2], c[3], c[4] or 0.6)
    t:Show()
end

function ns.HideGlow(bar)
    if bar._glow then bar._glow:Hide() end
end
