local _, namespace = ...

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

-- Generic progress-bar widget (PROG-01/02): a pure repaint surface driven entirely by the
-- caller's SetValue -- no timers, no animation. Used by RosterScreen to render the invite
-- and arrange drains' live progress; kept here (not RosterScreen-local) since it has no
-- dev-only data and could be reused by any future long-running operation.
function namespace:CreateProgressBar(parent)
    local bar = CreateFrame("Frame", nil, parent)
    bar:SetHeight(Theme.H_BTN)

    -- Track background.
    bar.track = bar:CreateTexture(nil, "BACKGROUND")
    bar.track:SetAllPoints()
    bar.track:SetColorTexture(Theme.INPUT_BG.r, Theme.INPUT_BG.g, Theme.INPUT_BG.b, Theme.INPUT_BG.a)
    bar.track:SetSnapToPixelGrid(false)
    bar.track:SetTexelSnappingBias(0)

    -- Accent fill, left-anchored so it grows rightward as the value rises.
    bar.fill = bar:CreateTexture(nil, "ARTWORK")
    bar.fill:SetPoint("TOPLEFT", bar, "TOPLEFT", 0, 0)
    bar.fill:SetPoint("BOTTOMLEFT", bar, "BOTTOMLEFT", 0, 0)
    bar.fill:SetColorTexture(Theme.ACCENT_A70.r, Theme.ACCENT_A70.g, Theme.ACCENT_A70.b, Theme.ACCENT_A70.a)
    bar.fill:SetWidth(0.001)
    bar.fill:SetSnapToPixelGrid(false)
    bar.fill:SetTexelSnappingBias(0)

    -- Rounded overlay-clip primitive (ROUND-04/05, spike-002): one shared mask clips both
    -- the track (full 4-corner) and the fill; the fill's live right edge (mid-bar) falls
    -- outside the mask's right-corner region and stays a square progress cut (Pattern 3).
    WTUI:AddRoundedMask(bar, Theme.RADIUS_SM)
    bar.track:AddMaskTexture(bar.roundedMask)
    bar.fill:AddMaskTexture(bar.roundedMask)

    WTUI:AddRoundedBorder(bar, Theme.RADIUS_SM, 1)
    bar:SetBorderColor(Theme.BORDER_BTN.r, Theme.BORDER_BTN.g, Theme.BORDER_BTN.b, Theme.BORDER_BTN.a)

    bar.label = bar:CreateFontString(nil, "OVERLAY")
    bar.label:SetFontObject(WildThingsFont13)
    bar.label:SetPoint("CENTER", bar, "CENTER", 0, 0)
    bar.label:SetTextColor(Theme.TEXT.r, Theme.TEXT.g, Theme.TEXT.b, Theme.TEXT.a)

    -- fraction is clamped to 0..1; the 0.001 floor avoids a zero-width-texture warning.
    function bar:SetValue(fraction, text)
        fraction = math.max(0, math.min(1, fraction or 0))
        bar.fill:SetWidth(math.max((bar:GetWidth() or 0) * fraction, 0.001))
        bar.label:SetText(text or "")
    end

    return bar
end
