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 TreeGrid row editing contracts. It proves
    ''' begin/update/commit/cancel lifecycle, label/value mutation, keyboard-friendly
    ''' selected-row editing, and source-projection detachment for manual edits without
    ''' pretending that IMASItemsSource has write-back support yet.
    ''' </summary>
    Friend NotInheritable Class MASTreeGridEditingGate
        Private Sub New()
        End Sub

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

                Dim tree As MASTreeGrid = MASTreeGrid.Create("Editing gate")
                tree.AddColumn("status", "Status")
                tree.AddRootRow("root", "Root", New String() {"Draft"}, expanded:=True)
                tree.AddChildRow("root", "child", "Child", New String() {"Ready"}, expanded:=True)

                If Not tree.SelectRow("child") Then Return False
                If Not tree.BeginEditSelectedLabel() Then Return False
                If Not tree.IsEditingRow Then Return False
                If Not String.Equals(tree.EditingRowKey, "child", StringComparison.OrdinalIgnoreCase) Then Return False
                If Not tree.UpdateEditDraft("Updated child") Then Return False
                If Not tree.CommitEdit() Then Return False
                If tree.IsEditingRow Then Return False
                If Not String.Equals(tree.GetRowLabel("child"), "Updated child", StringComparison.Ordinal) Then Return False

                If Not tree.BeginEdit("child", "status") Then Return False
                If Not String.Equals(tree.EditingColumnKey, "status", StringComparison.OrdinalIgnoreCase) Then Return False
                If Not tree.UpdateEditDraft("Done") Then Return False
                If Not tree.CommitEdit() Then Return False
                If Not String.Equals(tree.GetRowValue("child", "status"), "Done", StringComparison.Ordinal) Then Return False

                If Not tree.BeginEdit("child") Then Return False
                If Not tree.UpdateEditDraft("Canceled child") Then Return False
                If Not tree.CancelEdit() Then Return False
                If Not String.Equals(tree.GetRowLabel("child"), "Updated child", StringComparison.Ordinal) Then Return False

                Dim rows As New List(Of Object) From {
                    New MASTreeGridSourceRow("source-root", "Source root", New String() {"A"}, expanded:=True)
                }
                Dim source As MASListItemsSource = MASListItemsSource.FromEnumerable(rows, AddressOf ResolveProbeKey)
                Dim sourcedTree As MASTreeGrid = MASTreeGrid.Create("Editing sourced gate")
                sourcedTree.AddColumn("state", "State")
                sourcedTree.SetItemsSource(source, "MASTreeGridEditingGate")
                If Not sourcedTree.HasItemsSource Then Return False
                If Not sourcedTree.TrySetRowLabel("source-root", "Manual source edit") Then Return False
                If sourcedTree.HasItemsSource Then Return False
                If Not String.Equals(sourcedTree.GetRowLabel("source-root"), "Manual source edit", StringComparison.Ordinal) Then Return False

                Return True
            Catch ex As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowDiagnosticOnly(ex, "MASTreeGridEditingGate")
                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
