local _, namespace = ...

local WTUI = namespace.WildThingsUI
local Theme = namespace.Theme

-- namespace:CreateIconButton(parent, stem, OnClick, opts)
-- Borderless icon-only button (WIDGET-02): white-8% hover wash (create-once, via the
-- shared motion helper), a persistent "active" wash toggled via SetActive, and an opacity
-- press-dip to 0.7 (stronger than Button's 0.85). Composes the Icon widget for its
-- glyph rather than an inline texture.
function namespace:CreateIconButton(parent, stem, OnClick, opts)
    opts = opts or {}

    local iconSize = opts.iconSize or Theme.ICON_MD
    local pad = 6
    local size = iconSize + pad * 2

    local iconButton = CreateFrame("Button", nil, parent)
    iconButton:SetSize(size, size)

    -- .OnEnter/.OnLeave stub-field trampoline (mirrors Button.lua) so WTUI:AddTooltip and
    -- the motion helper's HookScript wiring compose additively.
    iconButton.OnEnter = function() end
    iconButton.OnLeave = function() end

    iconButton:SetScript("OnEnter", function(_self) _self.OnEnter() end)
    iconButton:SetScript("OnLeave", function(_self) _self.OnLeave() end)

    iconButton.OnClick = OnClick
    iconButton:SetScript("OnClick", iconButton.OnClick)

    -- Borderless: no border helper is invoked at all — this is IconButton's defining trait.

    -- Persistent active wash — structurally identical to Button's selectedOverlay pattern.
    iconButton.activeOverlay = iconButton:CreateTexture(nil, "ARTWORK")
    iconButton.activeOverlay:SetAllPoints()
    iconButton.activeOverlay:SetColorTexture(Theme.WHITE_A08.r, Theme.WHITE_A08.g, Theme.WHITE_A08.b, Theme.WHITE_A08.a)
    iconButton.activeOverlay:Hide()

    function iconButton:SetActive(isActive)
        iconButton.active = isActive and true or false
        if iconButton.active then
            iconButton.activeOverlay:Show()
        else
            iconButton.activeOverlay:Hide()
        end
    end

    -- Animated hover wash via the shared motion helper (white-8%, not accent).
    iconButton.hoverOverlay = iconButton:CreateTexture(nil, "OVERLAY")
    iconButton.hoverOverlay:SetAllPoints()
    WTUI:AddHoverFade(iconButton, iconButton.hoverOverlay, {color = Theme.WHITE_A08})

    -- Rounded overlay-clip primitive (ROUND-04/05, spike-002): one shared mask clips both
    -- overlay layers to the same 4px rounded shape. No base fill exists to clip (IconButton
    -- is deliberately transparent at rest).
    WTUI:AddRoundedMask(iconButton, Theme.RADIUS_SM)
    iconButton.activeOverlay:AddMaskTexture(iconButton.roundedMask)
    iconButton.hoverOverlay:AddMaskTexture(iconButton.roundedMask)

    -- Press feedback: opacity dip, a bit stronger than Button's (mirrors its old 0.92 scale).
    WTUI:AddPressDim(iconButton, {pressAlpha = 0.7})

    -- Glyph: compose Icon, centered.
    iconButton.icon = namespace:CreateIcon(iconButton, stem, iconSize)
    iconButton.icon:SetPoint("CENTER")

    if opts.tint then
        iconButton.icon:SetTint(opts.tint)
    else
        -- Seed to the idle tinted state at construction so the button reads muted-grey
        -- at rest from the first frame, not only after the first hover/leave cycle.
        iconButton.icon:SetIconState("idle")
    end

    -- Glyph brightens on hover — additive HookScript so it composes with the trampoline
    -- above and the motion helper's own HookScript wiring. When opts.tint was supplied,
    -- only alpha is driven here — SetIconState would silently overwrite the caller's
    -- custom tint with Theme.ICON_TINT_*.
    iconButton:HookScript("OnEnter", function()
        if not iconButton:IsEnabled() then return end
        if opts.tint then
            iconButton.icon:SetAlpha(iconButton.icon.hoverAlpha)
        else
            iconButton.icon:SetIconState("hover")
        end
    end)

    iconButton:HookScript("OnLeave", function()
        if not iconButton:IsEnabled() then return end
        if opts.tint then
            iconButton.icon:SetAlpha(iconButton.icon.idleAlpha)
            iconButton.icon:SetTint(opts.tint)
        else
            iconButton.icon:SetIconState("idle")
        end
    end)

    -- Disabled: mirror Button's Enable/Disable hooksecurefunc pattern. Same opts.tint
    -- guard as above — Enable/Disable must not clobber a caller-supplied custom tint.
    hooksecurefunc(iconButton, "Enable", function()
        if opts.tint then
            iconButton.icon:SetAlpha(iconButton.icon.idleAlpha)
        else
            iconButton.icon:SetIconState("idle")
        end
    end)

    hooksecurefunc(iconButton, "Disable", function()
        if opts.tint then
            iconButton.icon:SetAlpha(iconButton.icon.disabledAlpha)
        else
            iconButton.icon:SetIconState("disabled")
        end
    end)

    iconButton:SetActive(opts.active)

    return iconButton
end
