local RUI = LibStub("RUI-1.0")
if not RUI then return end

-- RUI.Widget.NumberInput(parent, label, x, y, width, get, set) → EditBox, Refresh()
-- Styled number input matching the slider's inline value box:
--   element-bg backdrop, 1px border, centered text, GameFontHighlightSmall.
-- `set` receives a number (the parsed float); callers may floor if they need integers.
-- Commits on EnterPressed; reverts to get() on EscapePressed.
-- Total height consumed: LABEL_H(14) + INPUT_H(20) + CONTROL_GAP(4) = 38px.
-- Default width: 80px.
function RUI.Widget.NumberInput(parent, label, x, y, width, get, set)
    width = width or 80

    local lbl = RUI.Draw.MakeLabel(parent)
    lbl:SetTextColor(RUI.C.TEXT.r, RUI.C.TEXT.g, RUI.C.TEXT.b)
    lbl:SetPoint("TOPLEFT", parent, "TOPLEFT", x, y)
    lbl:SetWidth(width)
    lbl:SetJustifyH("LEFT")
    lbl:SetText(label)

    local eb = RUI.Draw.MakeInputBox(parent, width)
    eb:SetPoint("TOPLEFT", parent, "TOPLEFT", x, y - RUI.LABEL_H)
    -- Numbers are short and centred; the shared chrome left-aligns with a
    -- wider inset, which suits names and messages rather than digits.
    eb:SetJustifyH("CENTER")
    eb:SetTextInsets(2, 2, 0, 0)
    eb:SetMaxLetters(10)

    eb:SetScript("OnEnterPressed", function(self)
        local n = tonumber(self:GetText())
        if n then set(n) end
        self:ClearFocus()
    end)
    eb:SetScript("OnEscapePressed", function(self)
        local v = get()
        self:SetText(v ~= nil and tostring(v) or "")
        self:ClearFocus()
    end)

    local function Refresh()
        local v = get()
        eb:SetText(v ~= nil and tostring(v) or "")
    end
    Refresh()
    return eb, Refresh
end
