Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Friend-only governance probe for the TreeGrid source-projection path. It does not
    ''' render, mutate global registries, or expose binding APIs; it proves that the new
    ''' stable-keyed source path can build a parent/child tree and preserve viewport access.
    ''' </summary>
    Friend NotInheritable Class MASTreeGridSourceProjectionGate
        Private Sub New()
        End Sub

        Friend Shared Function Passes() As Boolean
            Try
                If Not MASTreeGridPolicy.HasItemsSourceProjectionPath() Then Return False
                If Not MASTreeGridPolicy.HasViewportWindowingPath() Then Return False

                Dim rows As New List(Of Object) From {
                    New MASTreeGridSourceRow("root", "Root", New String() {"A"}, expanded:=True),
                    New MASTreeGridSourceRow("child", "Child", New String() {"B"}, parentKey:="root", expanded:=True)
                }
                Dim source As MASListItemsSource = MASListItemsSource.FromEnumerable(rows, AddressOf ResolveProbeKey)
                Dim tree As MASTreeGrid = MASTreeGrid.Create("Projection gate")
                tree.AddColumn("value", "Value")
                tree.SetItemsSource(source, "MASTreeGridSourceProjectionGate")

                If Not tree.HasItemsSource Then Return False
                If tree.ItemsSourceRevision < 0 Then Return False
                If tree.RowCount <> 2 Then Return False
                If tree.VisibleRowCount <> 2 Then Return False
                If Not tree.EnsureRowVisible("child") Then Return False
                If tree.GetVisibleSourceKeys().Count <= 0 Then Return False

                source.Add(New MASTreeGridSourceRow("child-2", "Child 2", New String() {"C"}, parentKey:="root", expanded:=True))
                If tree.RowCount <> 3 Then Return False
                If Not tree.SelectRow("child-2") Then Return False
                Return True
            Catch ex As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowDiagnosticOnly(ex, "MASTreeGridSourceProjectionGate")
                Return False
            End Try
        End Function

        Private Shared Function ResolveProbeKey(item As Object) As String
            Dim row As MASTreeGridSourceRow = TryCast(item, MASTreeGridSourceRow)
            If row Is Nothing Then Return String.Empty
            Return row.Key
        End Function
    End Class

    Friend NotInheritable Class MASTreeGridIncrementalSourceUpdateGate
        Private Sub New()
        End Sub

        Friend Shared Function Passes() As Boolean
            Try
                If Not MASTreeGridPolicy.HasSourceWriteBackAdapterPath() Then Return False
                If Not MASTreeGridPolicy.HasIncrementalSourceUpdatePath() Then Return False

                Dim rows As New List(Of Object) From {
                    New MASTreeGridSourceRow("root", "Root", New String() {"A"}, expanded:=True),
                    New MASTreeGridSourceRow("child", "Child", New String() {"B"}, parentKey:="root", expanded:=True)
                }
                Dim source As MASListItemsSource = MASListItemsSource.FromEnumerable(rows, AddressOf ResolveProbeKey)
                Dim tree As MASTreeGrid = MASTreeGrid.Create("Incremental gate")
                tree.AddColumn("value", "Value")
                tree.SetItemsSource(source, "MASTreeGridIncrementalSourceUpdateGate")

                If Not tree.SelectRow("child") Then Return False
                If Not tree.SetRowExpanded("root", True) Then Return False

                Dim selectedKey As String = tree.SelectedRowKey
                Dim revisionBeforeWrite As Integer = tree.ItemsSourceRevision
                Dim adapter As New MASTreeGridSourceWriteBackAdapter()
                If Not adapter.CommitLabel(tree, "child", "Edited child") Then Return False
                If Not String.Equals(tree.GetRowLabel("child"), "Edited child", StringComparison.Ordinal) Then Return False
                If Not String.Equals(tree.SelectedRowKey, selectedKey, StringComparison.OrdinalIgnoreCase) Then Return False
                If tree.ItemsSourceRevision <= revisionBeforeWrite Then Return False

                source.Add(New MASTreeGridSourceRow("child-2", "Child 2", New String() {"C"}, parentKey:="root", expanded:=True))
                If tree.RowCount <> 3 Then Return False
                If Not tree.EnsureRowVisible("child-2") Then Return False
                If Not String.Equals(tree.SelectedRowKey, selectedKey, StringComparison.OrdinalIgnoreCase) Then Return False

                Dim removeIndex As Integer = source.IndexOfStableKey("child-2")
                If removeIndex < 0 OrElse Not source.RemoveAt(removeIndex) Then Return False
                If tree.RowCount <> 2 Then Return False
                For Each key As String In tree.GetRowKeys()
                    If String.Equals(key, "child-2", StringComparison.OrdinalIgnoreCase) Then Return False
                Next
                If Not String.Equals(tree.SelectedRowKey, selectedKey, StringComparison.OrdinalIgnoreCase) Then Return False

                Return True
            Catch ex As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowDiagnosticOnly(ex, "MASTreeGridIncrementalSourceUpdateGate")
                Return False
            End Try
        End Function

        Private Shared Function ResolveProbeKey(item As Object) As String
            Dim row As MASTreeGridSourceRow = TryCast(item, MASTreeGridSourceRow)
            If row Is Nothing Then Return String.Empty
            Return row.Key
        End Function
    End Class

End Namespace
