---@diagnostic disable: undefined-field, undefined-global
local _, namespace = ...

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

-- Compact pill dimensions (Claude's discretion within the wild-things-design tokens).
local PILL_HEIGHT = 22
local PAD_LEFT = 6
local PAD_RIGHT = 6
local GAP_INNER = 4
local X_SIZE = 12
local X_HIT = 16 -- comfortable hit area, larger than the visible glyph

-- namespace:CreateCooldownPill(parent, spellID, onRemove)
-- Compact icon + name + X pill (READY-05 Settings redesign). Brand-skinned per
-- .claude/skills/wild-things-design/README.md: 1px BORDER_STRONG border + SURFACE_2 fill
-- (WoW textures cannot truly round corners with no rounded-corner asset in the repo, so
-- this IS the practical "pill" look here — matches Dropdown.lua/SpellIcon.lua skinning).
-- The X tints Theme.TEXT_DIM -> Theme.DANGER (#FF5262) on hover and calls onRemove() on
-- click. Sizes itself to fit its own content; callers read pill:GetWidth()/GetHeight()
-- for wrap math (SpecCooldownPicker.lua's picker:Layout()).
function namespace:CreateCooldownPill(parent, spellID, onRemove)
    local pill = CreateFrame("Frame", nil, parent)
    pill:SetHeight(PILL_HEIGHT)
    pill.spellID = spellID

    -- EnableMouse is the hard prerequisite for the spell tooltip below: a plain Frame
    -- receives no OnEnter/OnLeave (and so WTUI:AddTooltip never fires) without it
    -- (same gotcha documented at GroupGrid.lua:387-391).
    pill:EnableMouse(true)

    -- Background fill (BACKGROUND layer, inset 1px so the rounded border ring below shows
    -- through as a 1px band).
    pill.bg = pill:CreateTexture(nil, "BACKGROUND")
    pill.bg:SetPoint("TOPLEFT", pill, "TOPLEFT", 1, -1)
    pill.bg:SetPoint("BOTTOMRIGHT", pill, "BOTTOMRIGHT", -1, 1)
    pill.bg:SetColorTexture(Theme.SURFACE_2.r, Theme.SURFACE_2.g, Theme.SURFACE_2.b, Theme.SURFACE_2.a)

    -- Rounded overlay-clip primitive (ROUND-04, spike-002): flat 4px radius per the locked
    -- control-radius decision (NOT a true height/2 capsule -- Assumption A2).
    WTUI:AddRoundedMask(pill, Theme.RADIUS_SM)
    pill.bg:AddMaskTexture(pill.roundedMask)

    -- Rounded border ring (ROUND-BORDER-01): follows the corner arcs instead of a square stroke.
    WTUI:AddRoundedBorder(pill, Theme.RADIUS_SM, 1)
    pill:SetBorderColor(Theme.BORDER_STRONG.r, Theme.BORDER_STRONG.g, Theme.BORDER_STRONG.b, Theme.BORDER_STRONG.a)

    -- Spell info (nil-guarded per SpellIcon.lua's invalid-spell fallback -- a candidate's
    -- spellID could go stale before the Python generator is re-run).
    local info = WTUI.GetSpellInfo(spellID)
    local iconID = info and info.iconID or 134400
    local spellName = (info and info.name) or tostring(spellID)

    -- 1. Icon.
    pill.icon = pill:CreateTexture(nil, "ARTWORK")
    pill.icon:SetSize(Theme.ICON_SM, Theme.ICON_SM)
    pill.icon:SetPoint("LEFT", pill, "LEFT", PAD_LEFT, 0)
    pill.icon:SetTexCoord(0.08, 0.92, 0.08, 0.92)
    pill.icon:SetTexture(iconID)
    pill.icon:SetSnapToPixelGrid(false)
    pill.icon:SetTexelSnappingBias(0)

    -- 2. Name.
    pill.name = pill:CreateFontString(nil, "ARTWORK")
    pill.name:SetFontObject(WildThingsFont13)
    pill.name:SetTextColor(Theme.TEXT.r, Theme.TEXT.g, Theme.TEXT.b, Theme.TEXT.a)
    pill.name:SetText(spellName)
    pill.name:SetPoint("LEFT", pill.icon, "RIGHT", GAP_INNER, 0)

    -- 3. X remove button.
    pill.removeBtn = CreateFrame("Button", nil, pill)
    pill.removeBtn:SetSize(X_HIT, X_HIT)
    pill.removeBtn:SetPoint("LEFT", pill.name, "RIGHT", GAP_INNER, 0)

    pill.removeBtn.tex = pill.removeBtn:CreateTexture(nil, "ARTWORK")
    pill.removeBtn.tex:SetSize(X_SIZE, X_SIZE)
    pill.removeBtn.tex:SetPoint("CENTER")
    pill.removeBtn.tex:SetTexture(Theme.GetIconPath("x"))
    pill.removeBtn.tex:SetVertexColor(Theme.TEXT_DIM.r, Theme.TEXT_DIM.g, Theme.TEXT_DIM.b, Theme.TEXT_DIM.a)
    pill.removeBtn.tex:SetSnapToPixelGrid(false)
    pill.removeBtn.tex:SetTexelSnappingBias(0)

    pill.removeBtn:SetScript("OnEnter", function()
        pill.removeBtn.tex:SetVertexColor(Theme.DANGER.r, Theme.DANGER.g, Theme.DANGER.b, Theme.DANGER.a)
    end)
    pill.removeBtn:SetScript("OnLeave", function()
        pill.removeBtn.tex:SetVertexColor(Theme.TEXT_DIM.r, Theme.TEXT_DIM.g, Theme.TEXT_DIM.b, Theme.TEXT_DIM.a)
    end)
    pill.removeBtn:SetScript("OnClick", function() onRemove() end)

    -- Size the pill to fit its content: leftPad + icon + gap + name + gap + X + rightPad.
    local width = PAD_LEFT + Theme.ICON_SM + GAP_INNER + pill.name:GetStringWidth()
        + GAP_INNER + X_HIT + PAD_RIGHT
    pill:SetWidth(width)

    -- Real WoW spell tooltip on hover (like hovering a spell in the spellbook):
    -- GameTooltip:SetSpellByID renders the full name/cast/range/cooldown/description block,
    -- not just a plain-text label. EnableMouse above is the prerequisite for OnEnter/OnLeave.
    pill:SetScript("OnEnter", function()
        GameTooltip:SetOwner(pill, "ANCHOR_RIGHT")
        GameTooltip:SetSpellByID(spellID)
        GameTooltip:Show()
    end)
    pill:SetScript("OnLeave", function()
        GameTooltip:Hide()
    end)

    return pill
end

-- namespace:CreateItemPill(parent, itemID, onRemove)
-- Compact icon + name + X pill (WW1-BANK-ORGANIZE) -- a sibling of CreateCooldownPill
-- above, identical layout/skinning, but resolving display data from the ITEM rather
-- than the spell.
function namespace:CreateItemPill(parent, itemID, onRemove)
    local pill = CreateFrame("Frame", nil, parent)
    pill:SetHeight(PILL_HEIGHT)
    pill.itemID = itemID

    -- EnableMouse is the hard prerequisite for the tooltip below (same gotcha
    -- documented on CreateCooldownPill above / GroupGrid.lua:387-391).
    pill:EnableMouse(true)

    pill.bg = pill:CreateTexture(nil, "BACKGROUND")
    pill.bg:SetPoint("TOPLEFT", pill, "TOPLEFT", 1, -1)
    pill.bg:SetPoint("BOTTOMRIGHT", pill, "BOTTOMRIGHT", -1, 1)
    pill.bg:SetColorTexture(Theme.SURFACE_2.r, Theme.SURFACE_2.g, Theme.SURFACE_2.b, Theme.SURFACE_2.a)

    WTUI:AddRoundedMask(pill, Theme.RADIUS_SM)
    pill.bg:AddMaskTexture(pill.roundedMask)

    -- Rounded border ring (ROUND-BORDER-01): follows the corner arcs instead of a square stroke.
    WTUI:AddRoundedBorder(pill, Theme.RADIUS_SM, 1)
    pill:SetBorderColor(Theme.BORDER_STRONG.r, Theme.BORDER_STRONG.g, Theme.BORDER_STRONG.b, Theme.BORDER_STRONG.a)

    -- Item info (nil-guarded -- GetItemInfoInstant resolves the icon even for an
    -- item the client hasn't fully cached yet; the name falls back to a raw
    -- "item:<id>" label until the client caches it, per the plan's ASSUMED default).
    local _, _, _, _, iconID = GetItemInfoInstant(itemID)
    iconID = iconID or 134400
    local itemName = GetItemInfo(itemID) or ("item:" .. tostring(itemID))

    -- 1. Icon.
    pill.icon = pill:CreateTexture(nil, "ARTWORK")
    pill.icon:SetSize(Theme.ICON_SM, Theme.ICON_SM)
    pill.icon:SetPoint("LEFT", pill, "LEFT", PAD_LEFT, 0)
    pill.icon:SetTexCoord(0.08, 0.92, 0.08, 0.92)
    pill.icon:SetTexture(iconID)
    pill.icon:SetSnapToPixelGrid(false)
    pill.icon:SetTexelSnappingBias(0)

    -- 2. Name.
    pill.name = pill:CreateFontString(nil, "ARTWORK")
    pill.name:SetFontObject(WildThingsFont13)
    pill.name:SetTextColor(Theme.TEXT.r, Theme.TEXT.g, Theme.TEXT.b, Theme.TEXT.a)
    pill.name:SetText(itemName)
    pill.name:SetPoint("LEFT", pill.icon, "RIGHT", GAP_INNER, 0)

    -- 3. X remove button.
    pill.removeBtn = CreateFrame("Button", nil, pill)
    pill.removeBtn:SetSize(X_HIT, X_HIT)
    pill.removeBtn:SetPoint("LEFT", pill.name, "RIGHT", GAP_INNER, 0)

    pill.removeBtn.tex = pill.removeBtn:CreateTexture(nil, "ARTWORK")
    pill.removeBtn.tex:SetSize(X_SIZE, X_SIZE)
    pill.removeBtn.tex:SetPoint("CENTER")
    pill.removeBtn.tex:SetTexture(Theme.GetIconPath("x"))
    pill.removeBtn.tex:SetVertexColor(Theme.TEXT_DIM.r, Theme.TEXT_DIM.g, Theme.TEXT_DIM.b, Theme.TEXT_DIM.a)
    pill.removeBtn.tex:SetSnapToPixelGrid(false)
    pill.removeBtn.tex:SetTexelSnappingBias(0)

    pill.removeBtn:SetScript("OnEnter", function()
        pill.removeBtn.tex:SetVertexColor(Theme.DANGER.r, Theme.DANGER.g, Theme.DANGER.b, Theme.DANGER.a)
    end)
    pill.removeBtn:SetScript("OnLeave", function()
        pill.removeBtn.tex:SetVertexColor(Theme.TEXT_DIM.r, Theme.TEXT_DIM.g, Theme.TEXT_DIM.b, Theme.TEXT_DIM.a)
    end)
    pill.removeBtn:SetScript("OnClick", function() onRemove() end)

    -- Size the pill to fit its content: leftPad + icon + gap + name + gap + X + rightPad.
    local width = PAD_LEFT + Theme.ICON_SM + GAP_INNER + pill.name:GetStringWidth()
        + GAP_INNER + X_HIT + PAD_RIGHT
    pill:SetWidth(width)

    WTUI:AddTooltip(pill, itemName)

    return pill
end
