Option Strict On
Option Explicit On

Namespace Nexamas.UI.Virtualization

    ''' <summary>
    ''' Friend-only proof snapshot for fast-scroll planning allocation pressure. It does not expose memory counters to the public
    ''' SDK; it records whether repeated viewport updates reused controller scratch structures and container leases within budget.
    ''' </summary>
    Friend NotInheritable Class MASVirtualizationPlanAllocationBudgetSnapshot

        Friend Sub New(updateCount As Integer,
                       maxRealizedCount As Integer,
                       createdLeaseCount As Integer,
                       reusedLeaseCount As Integer,
                       staleScratchCapacity As Integer,
                       requestScratchCapacity As Integer)
            Me.UpdateCount = Math.Max(0, updateCount)
            Me.MaxRealizedCount = Math.Max(0, maxRealizedCount)
            Me.CreatedLeaseCount = Math.Max(0, createdLeaseCount)
            Me.ReusedLeaseCount = Math.Max(0, reusedLeaseCount)
            Me.StaleScratchCapacity = Math.Max(0, staleScratchCapacity)
            Me.RequestScratchCapacity = Math.Max(0, requestScratchCapacity)
        End Sub

        Friend ReadOnly Property UpdateCount As Integer
        Friend ReadOnly Property MaxRealizedCount As Integer
        Friend ReadOnly Property CreatedLeaseCount As Integer
        Friend ReadOnly Property ReusedLeaseCount As Integer
        Friend ReadOnly Property StaleScratchCapacity As Integer
        Friend ReadOnly Property RequestScratchCapacity As Integer

        Friend Function IsWithinBudget(requiredUpdates As Integer, maxLeaseBudget As Integer, maxScratchCapacity As Integer) As Boolean
            Return UpdateCount >= Math.Max(1, requiredUpdates) AndAlso
                   MaxRealizedCount > 0 AndAlso
                   CreatedLeaseCount <= Math.Max(MaxRealizedCount, maxLeaseBudget) AndAlso
                   StaleScratchCapacity <= Math.Max(MaxRealizedCount + 8, maxScratchCapacity) AndAlso
                   RequestScratchCapacity <= Math.Max(MaxRealizedCount + 8, maxScratchCapacity)
        End Function

    End Class

End Namespace
