Option Strict On
Option Explicit On

Namespace Nexamas.UI.Virtualization

    ''' <summary>
    ''' Internal lifecycle/dispatcher contract for a virtualization consumer. This is proof metadata for the current catalog:
    ''' the actual source-contract gate verifies the matching control code routes scroll-repeat pulses through the official
    ''' owner dispatcher and cancels repeat/drag state when the owner lifecycle is detached.
    ''' </summary>
    Friend NotInheritable Class MASVirtualizationConsumerLifecycleBinding

        Friend Sub New(consumerId As String,
                       dispatcherOwner As String,
                       requiresOfficialDispatcher As Boolean,
                       requiresRuntimeUnbindCancellation As Boolean,
                       requiresOwnerDetachCancellation As Boolean,
                       requiresDisposeCancellation As Boolean,
                       requiresComposedChildLifecycleProof As Boolean)
            Me.ConsumerId = NormalizeText(consumerId)
            Me.DispatcherOwner = NormalizeText(dispatcherOwner)
            Me.RequiresOfficialDispatcher = requiresOfficialDispatcher
            Me.RequiresRuntimeUnbindCancellation = requiresRuntimeUnbindCancellation
            Me.RequiresOwnerDetachCancellation = requiresOwnerDetachCancellation
            Me.RequiresDisposeCancellation = requiresDisposeCancellation
            Me.RequiresComposedChildLifecycleProof = requiresComposedChildLifecycleProof
        End Sub

        Friend ReadOnly Property ConsumerId As String
        Friend ReadOnly Property DispatcherOwner As String
        Friend ReadOnly Property RequiresOfficialDispatcher As Boolean
        Friend ReadOnly Property RequiresRuntimeUnbindCancellation As Boolean
        Friend ReadOnly Property RequiresOwnerDetachCancellation As Boolean
        Friend ReadOnly Property RequiresDisposeCancellation As Boolean
        Friend ReadOnly Property RequiresComposedChildLifecycleProof As Boolean

        Friend ReadOnly Property IsControlRuntimeDispatcher As Boolean
            Get
                Return String.Equals(DispatcherOwner, MASVirtualizationConsumerLifecycleGate.ControlRuntimeServicesDispatcherOwner, Global.System.StringComparison.Ordinal)
            End Get
        End Property

        Friend ReadOnly Property IsFloatSessionDispatcher As Boolean
            Get
                Return String.Equals(DispatcherOwner, MASVirtualizationConsumerLifecycleGate.FloatSessionDispatcherOwner, Global.System.StringComparison.Ordinal)
            End Get
        End Property

        Friend ReadOnly Property IsComposedDispatcher As Boolean
            Get
                Return String.Equals(DispatcherOwner, MASVirtualizationConsumerLifecycleGate.ComposedChildDispatcherOwner, Global.System.StringComparison.Ordinal)
            End Get
        End Property

        Friend ReadOnly Property IsReady As Boolean
            Get
                If ConsumerId.Length = 0 Then Return False
                If DispatcherOwner.Length = 0 Then Return False
                If RequiresComposedChildLifecycleProof Then Return IsComposedDispatcher
                If Not RequiresOfficialDispatcher Then Return False
                If Not (IsControlRuntimeDispatcher OrElse IsFloatSessionDispatcher) Then Return False
                If IsControlRuntimeDispatcher AndAlso Not RequiresRuntimeUnbindCancellation Then Return False
                If IsFloatSessionDispatcher AndAlso Not RequiresOwnerDetachCancellation Then Return False
                Return RequiresDisposeCancellation OrElse RequiresOwnerDetachCancellation
            End Get
        End Property

        Private Shared Function NormalizeText(value As String) As String
            Return If(value, String.Empty).Trim()
        End Function

    End Class

End Namespace
