Option Strict On
Option Explicit On

Imports System
Imports System.Reflection
Imports Nexamas.UI.Localization
Imports Nexamas.UI.Verification

Namespace Nexamas.UI.Quality

    ''' <summary>
    ''' Proves LOC-05: Localization snapshot readiness is not treated as public AttachPage
    ''' lifecycle proof, and render-verification snapshot scenes are not advertised as RealControl
    ''' evidence unless they are backed by the public MASLocalizationRtl.AttachPage route.
    ''' </summary>
    Friend NotInheritable Class MASLocalizationAttachPageLifecycleGate
        Private Sub New()
        End Sub

        Friend Shared Function Passes() As Boolean
            Return ProbeSnapshotReadinessDoesNotImplyAttachLifecycle() AndAlso
                   ProbeRenderVerificationSeparatesSnapshotFromRealControl() AndAlso
                   ProbePublicGatewaySurfaceIsAttachOnly()
        End Function

        Private Shared Function ProbeSnapshotReadinessDoesNotImplyAttachLifecycle() As Boolean
            Try
                Dim readiness As MASLocalizationReadinessReport = MASLocalizationReadinessGate.Evaluate()
                Dim snapshot As MASLocalizationSnapshot = MASLocalization.CreateSnapshot()

                Return readiness IsNot Nothing AndAlso
                       snapshot IsNot Nothing AndAlso
                       readiness.ReadyForPhase6Foundation AndAlso
                       snapshot.ReadyForProductLayer AndAlso
                       ProbePublicGatewaySurfaceIsAttachOnly()
            Catch ex As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowDiagnosticOnly(ex, "MASLocalizationAttachPageLifecycleGate.SnapshotSeparation")
                Return False
            End Try
        End Function

        Private Shared Function ProbeRenderVerificationSeparatesSnapshotFromRealControl() As Boolean
            Try
                Dim localizationScenarioCount As Integer = 0

                For Each target As MASRenderVerificationTarget In MASRenderVerificationTargetCatalog.CreateAllTargets()
                    If target Is Nothing OrElse target.Scenarios Is Nothing Then Continue For

                    For Each scenario As MASRenderVerificationScenario In target.Scenarios
                        If scenario Is Nothing Then Continue For
                        If Not String.Equals(scenario.ComponentKey, "MASLocalizationRtl", StringComparison.OrdinalIgnoreCase) Then Continue For

                        localizationScenarioCount += 1

                        If HasTag(scenario, "RealControl") Then Return False
                        If Not HasTag(scenario, "ProductSnapshot") Then Return False
                        If Not HasTag(scenario, "SnapshotReadiness") Then Return False
                        If Not HasTag(scenario, "AttachPageLifecycleSeparate") Then Return False
                    Next
                Next

                Return localizationScenarioCount > 0
            Catch ex As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowDiagnosticOnly(ex, "MASLocalizationAttachPageLifecycleGate.RenderScenarioClassification")
                Return False
            End Try
        End Function

        Private Shared Function ProbePublicGatewaySurfaceIsAttachOnly() As Boolean
            Try
                Const flags As BindingFlags = BindingFlags.Public Or BindingFlags.Static Or BindingFlags.DeclaredOnly
                Dim methods As MethodInfo() = GetType(MASLocalizationRtl).GetMethods(flags)
                Dim attachPageMethodCount As Integer = 0

                For Each method As MethodInfo In methods
                    If method Is Nothing OrElse method.IsSpecialName Then Continue For

                    If Not String.Equals(method.Name, "AttachPage", StringComparison.Ordinal) Then Return False
                    If method.ReturnType IsNot GetType(IDisposable) Then Return False

                    attachPageMethodCount += 1
                Next

                Return attachPageMethodCount = 2
            Catch ex As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowDiagnosticOnly(ex, "MASLocalizationAttachPageLifecycleGate.PublicGatewaySurface")
                Return False
            End Try
        End Function

        Private Shared Function HasTag(scenario As MASRenderVerificationScenario, tag As String) As Boolean
            If scenario Is Nothing OrElse scenario.Tags Is Nothing OrElse String.IsNullOrWhiteSpace(tag) Then Return False

            For Each item As String In scenario.Tags
                If String.Equals(item, tag, StringComparison.OrdinalIgnoreCase) Then Return True
            Next

            Return False
        End Function
    End Class

End Namespace
