Option Strict On
Option Explicit On

Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Linq

Namespace Nexamas.UI.Virtualization

    ''' <summary>
    ''' Runtime proof report for VIRT-06. It proves TileBox tile-grid virtualization uses a single shared-controller
    ''' planning pass per render-equivalent pass after scroll extent has already been resolved/clamped, instead of the old
    ''' update-plan-update-plan pattern that could do duplicate work during one render.
    ''' </summary>
    Friend NotInheritable Class MASTileBoxSinglePlanningReport
        Private ReadOnly _findings As IReadOnlyList(Of MASVirtualizationReadinessFinding)

        Friend Sub New(logicalItemCount As Integer,
                       passCount As Integer,
                       scrollExtentResolvedBeforePlanningPassCount As Integer,
                       maxPrepareGridPlanCallsPerPass As Integer,
                       duplicatePlanningPassCount As Integer,
                       maxRealizedRowsPerPass As Integer,
                       minResolvedScrollExtentPx As Single,
                       maxResolvedScrollExtentPx As Single,
                       findings As IEnumerable(Of MASVirtualizationReadinessFinding))
            Me.LogicalItemCount = Math.Max(0, logicalItemCount)
            Me.PassCount = Math.Max(0, passCount)
            Me.ScrollExtentResolvedBeforePlanningPassCount = Math.Max(0, scrollExtentResolvedBeforePlanningPassCount)
            Me.MaxPrepareGridPlanCallsPerPass = Math.Max(0, maxPrepareGridPlanCallsPerPass)
            Me.DuplicatePlanningPassCount = Math.Max(0, duplicatePlanningPassCount)
            Me.MaxRealizedRowsPerPass = Math.Max(0, maxRealizedRowsPerPass)
            Me.MinResolvedScrollExtentPx = Math.Max(0.0F, MASVirtualizationNumericGuard.NormalizeNonNegative(minResolvedScrollExtentPx))
            Me.MaxResolvedScrollExtentPx = Math.Max(0.0F, MASVirtualizationNumericGuard.NormalizeNonNegative(maxResolvedScrollExtentPx))
            _findings = New ReadOnlyCollection(Of MASVirtualizationReadinessFinding)(If(findings, Enumerable.Empty(Of MASVirtualizationReadinessFinding)()).Where(Function(finding) finding IsNot Nothing).ToList())
        End Sub

        Friend ReadOnly Property LogicalItemCount As Integer
        Friend ReadOnly Property PassCount As Integer
        Friend ReadOnly Property ScrollExtentResolvedBeforePlanningPassCount As Integer
        Friend ReadOnly Property MaxPrepareGridPlanCallsPerPass As Integer
        Friend ReadOnly Property DuplicatePlanningPassCount As Integer
        Friend ReadOnly Property MaxRealizedRowsPerPass As Integer
        Friend ReadOnly Property MinResolvedScrollExtentPx As Single
        Friend ReadOnly Property MaxResolvedScrollExtentPx As Single

        Friend ReadOnly Property Findings As IReadOnlyList(Of MASVirtualizationReadinessFinding)
            Get
                Return _findings
            End Get
        End Property

        Friend ReadOnly Property HasFindings As Boolean
            Get
                Return _findings.Count > 0
            End Get
        End Property

        Friend ReadOnly Property Proves100kTileGridSinglePlanning As Boolean
            Get
                Return LogicalItemCount >= MASTileBoxSinglePlanningGate.RequiredLogicalItemCount AndAlso
                       PassCount >= MASTileBoxSinglePlanningGate.RequiredPlanningPassCount AndAlso
                       MaxPrepareGridPlanCallsPerPass <= MASTileBoxSinglePlanningGate.MaxPrepareGridPlanCallsPerPass AndAlso
                       DuplicatePlanningPassCount = 0
            End Get
        End Property

        Friend ReadOnly Property ProvesScrollExtentResolvedBeforePlanning As Boolean
            Get
                Return ScrollExtentResolvedBeforePlanningPassCount >= MASTileBoxSinglePlanningGate.RequiredPlanningPassCount AndAlso
                       MaxResolvedScrollExtentPx > 0.0F AndAlso
                       MaxResolvedScrollExtentPx >= MinResolvedScrollExtentPx
            End Get
        End Property

        Friend ReadOnly Property ProvesBoundedTileGridRealization As Boolean
            Get
                Return MaxRealizedRowsPerPass > 0 AndAlso
                       MaxRealizedRowsPerPass <= MASTileBoxSinglePlanningGate.MaxRealizedRowsPerPass AndAlso
                       MaxRealizedRowsPerPass < LogicalItemCount
            End Get
        End Property

        Friend ReadOnly Property IsReady As Boolean
            Get
                Return Not HasFindings AndAlso
                       Proves100kTileGridSinglePlanning AndAlso
                       ProvesScrollExtentResolvedBeforePlanning AndAlso
                       ProvesBoundedTileGridRealization
            End Get
        End Property
    End Class

End Namespace
