Option Strict On
Option Explicit On

Imports System
Imports System.Diagnostics
Imports Nexamas.UI.Application
Imports Nexamas.UI.Layout

Namespace Nexamas.UI.Certification

    ''' <summary>
    ''' Internal host gateway for the Nexamas UI-owned Certification Center surface.
    ''' </summary>
    ''' <remarks>
    ''' Nexamas UI internal demos use AttachCenter to display the ready Nexamas UI-owned page.
    ''' Manifest creation remains Friend-only; this gateway does not expose public telemetry, run gates, scan source files,
    ''' or duplicate Performance/RenderVerification/Virtualization/DataGrid/FloatRuntime ownership.
    ''' </remarks>
    Friend NotInheritable Class NexamasUICertification
        Private Sub New()
        End Sub

        Friend Shared Function CreateManifest() As NexamasUICertificationManifest
            Return NexamasUICertificationManifest.CreateCurrent()
        End Function

        Friend Shared Function CreateQualityReport() As MASQualityReport
            Return MASQualityOrchestrator.Run()
        End Function

        ''' <summary>
        ''' Attaches the official Nexamas UI-owned Certification Center to a MASApplicationWindow.
        ''' External hosts receive a ready page and never read manifests, gate scripts, or certification internals.
        ''' </summary>
        Public Shared Function AttachCenter(window As MASApplicationWindow) As IDisposable
            If window Is Nothing Then Throw New ArgumentNullException(NameOf(window))

            Dim center As New NexamasUICertificationCenterPage()
            Try
                center.Build(window)
            Catch ex As Exception
                ShowAttachCenterError(window, ex)
            End Try
            Return center
        End Function

        ''' <summary>
        ''' Attaches the official Nexamas UI-owned Certification Center content to an existing
        ''' Application page-builder scope such as MASApplicationWindow.Pages. The host supplies only
        ''' the current MASApplicationWindow and MASApplicationLayoutPageBuilder; Nexamas UI still owns
        ''' the manifest, product-readable rows, refresh behavior, MAS controls, and page layout.
        ''' </summary>
        Public Shared Function AttachCenter(window As MASApplicationWindow,
                                            page As MASApplicationLayoutPageBuilder) As IDisposable
            If window Is Nothing Then Throw New ArgumentNullException(NameOf(window))
            If page Is Nothing Then Throw New ArgumentNullException(NameOf(page))

            Dim center As New NexamasUICertificationCenterPage()
            Try
                center.Build(window, page)
            Catch ex As Exception
                ShowAttachCenterError(window, ex)
            End Try
            Return center
        End Function

        Private Shared Sub ShowAttachCenterError(window As MASApplicationWindow, ex As Exception)
            Try
                If window IsNot Nothing AndAlso Not window.IsDisposed AndAlso window.Services IsNot Nothing Then
                    window.Services.Dialogs.Error(
                        message:="The Nexamas UI Certification Center could not be attached.",
                        title:="Nexamas UI Certification Center",
                        detail:=If(ex Is Nothing, String.Empty, ex.Message),
                        ownerKey:="MAS.Certification.AttachCenter.Error")
                End If
            Catch dialogException As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowDiagnosticOnly(dialogException, "Certification.AttachCenter.DialogBoundary")
            End Try
        End Sub
    End Class

End Namespace
