Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports Nexamas.UI.DataBinding
Imports Nexamas.UI.Values

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Friend-only proof that TreeGrid's source projection can hold a larger row set
    ''' while the realized drawing/input window remains bounded to the viewport.
    ''' The gate intentionally avoids timing assertions because execution hosts vary;
    ''' it verifies capacity, stable-key access, scrolling, and selection retention.
    ''' </summary>
    Friend NotInheritable Class MASTreeGridLargeDataWindowingGate

        Private Sub New()
        End Sub

        Friend Shared Function Passes() As Boolean
            Dim failures As IReadOnlyList(Of String) = Evaluate()
            Return failures.Count = 0
        End Function

        Friend Shared Function Evaluate() As IReadOnlyList(Of String)
            Dim failures As New List(Of String)()

            If Not MASTreeGridPolicy.HasLargeDataWindowingProofPath() Then failures.Add("TreeGrid large-data row-windowing proof path is missing.")

            Dim rowCount As Integer = Math.Max(1000, TreeGridTokens.LargeDataProofRows)
            Dim rows As New List(Of MASTreeGridSourceRow)(rowCount)
            For i As Integer = 0 To rowCount - 1
                Dim key As String = "node-" & i.ToString(System.Globalization.CultureInfo.InvariantCulture)
                rows.Add(New MASTreeGridSourceRow(key,
                                                  "Node " & i.ToString(System.Globalization.CultureInfo.InvariantCulture),
                                                  New String() {If(i Mod 2 = 0, "Ready", "Review")},
                                                  String.Empty,
                                                  True))
            Next

            Dim source As MASListItemsSource = MASListItemsSource.FromEnumerable(rows, Function(item As Object) DirectCast(item, MASTreeGridSourceRow).Key)
            Dim grid As MASTreeGrid = MASTreeGrid.Create("Large tree")
            grid.AddColumn("state", "State")
            grid.SetDiagnosticRowViewportCapacity(TreeGridTokens.MaxVisibleRows)
            grid.SetItemsSource(source, "MASTreeGridLargeDataWindowingGate")

            If grid.RowCount <> rowCount Then failures.Add("TreeGrid did not retain the full large source row count.")
            If grid.VisibleRowCount <> rowCount Then failures.Add("TreeGrid visible projection did not expose all expanded large-source rows.")

            Dim firstWindow As MASTreeGridRowWindowSnapshot = grid.CreateRowWindowSnapshot()
            If firstWindow.TotalVisibleRows <> rowCount Then failures.Add("Initial row-window total does not match the large source row count.")
            If Not firstWindow.IsBounded Then failures.Add("Initial row-window realizes more rows than requested capacity.")
            If firstWindow.RealizedRowCount > TreeGridTokens.MaxVisibleRows Then failures.Add("Initial row-window exceeds TreeGrid MaxVisibleRows.")
            If Not firstWindow.CoversPartialVisibleSet Then failures.Add("Initial row-window should cover only a partial visible set for large sources.")
            If Not firstWindow.ContainsKey("node-0") Then failures.Add("Initial row-window does not contain the first stable key.")

            Dim middleIndex As Integer = rowCount \ 2
            Dim middleKey As String = "node-" & middleIndex.ToString(System.Globalization.CultureInfo.InvariantCulture)
            If Not grid.EnsureRowVisible(middleKey) Then failures.Add("TreeGrid could not ensure a middle large-source row visible.")
            Dim middleWindow As MASTreeGridRowWindowSnapshot = grid.CreateRowWindowSnapshot()
            If Not middleWindow.ContainsKey(middleKey) Then failures.Add("Middle row was not realized after EnsureRowVisible.")
            If Not middleWindow.IsBounded Then failures.Add("Middle row-window realizes more rows than requested capacity.")

            Dim lastKey As String = "node-" & (rowCount - 1).ToString(System.Globalization.CultureInfo.InvariantCulture)
            If Not grid.SelectRow(lastKey) Then failures.Add("TreeGrid could not select the final large-source row.")
            Dim lastWindow As MASTreeGridRowWindowSnapshot = grid.CreateRowWindowSnapshot()
            If Not lastWindow.ContainsKey(lastKey) Then failures.Add("Final selected row was not realized in the bounded window.")
            If lastWindow.RealizedRowCount > TreeGridTokens.MaxVisibleRows Then failures.Add("Final row-window exceeds TreeGrid MaxVisibleRows.")
            If lastWindow.FirstRowIndex <= 0 Then failures.Add("Final row-window did not scroll away from the first row.")

            If Not grid.HasBoundedRowWindow(rowCount, TreeGridTokens.MaxVisibleRows) Then failures.Add("Bounded row-window helper did not recognize the large-source proof.")

            Return failures
        End Function

    End Class

End Namespace
