Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.DiagnosticsDashboard

    ''' <summary>
    ''' Internal host gateway for the Nexamas UI-owned Diagnostics Dashboard surface.
    ''' </summary>
    ''' <remarks>
    ''' This gateway exposes a ready read-only page to Nexamas UI-owned surfaces only. It does not expose counters, mutable telemetry,
    ''' benchmark execution, snapshot contracts, or a public diagnostics API. Runtime metrics remain owned
    ''' by PerformanceSystem, VirtualizationSystem, and layout/application owners.
    ''' </remarks>
    Friend NotInheritable Class MASDiagnosticsDashboard
        Private Sub New()
        End Sub

        ''' <summary>
        ''' Attaches the official Nexamas UI-owned Diagnostics Dashboard to a MASApplicationWindow.
        ''' External hosts receive a ready page and never read diagnostics contracts or invent telemetry rows.
        ''' </summary>
        Public Shared Function AttachDashboard(window As MASApplicationWindow) As IDisposable
            If window Is Nothing Then Throw New ArgumentNullException(NameOf(window))

            Dim dashboard As New MASDiagnosticsDashboardPage()
            Try
                dashboard.Build(window)
            Catch ex As Exception
                ShowAttachDashboardError(window, ex)
            End Try
            Return dashboard
        End Function

        ''' <summary>
        ''' Attaches the official Nexamas UI-owned Diagnostics Dashboard 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
        ''' metric aggregation, product-readable rows, refresh behavior, MAS controls, and page layout.
        ''' </summary>
        Public Shared Function AttachDashboard(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 dashboard As New MASDiagnosticsDashboardPage()
            Try
                dashboard.Build(window, page)
            Catch ex As Exception
                ShowAttachDashboardError(window, ex)
            End Try
            Return dashboard
        End Function

        Private Shared Sub ShowAttachDashboardError(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 Diagnostics Dashboard could not be attached.",
                        title:="Nexamas UI Diagnostics Dashboard",
                        detail:=If(ex Is Nothing, String.Empty, ex.Message),
                        ownerKey:="MAS.DiagnosticsDashboard.AttachDashboard.Error")
                End If
            Catch dialogException As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowDiagnosticOnly(dialogException, "DiagnosticsDashboard.AttachDashboard.DialogBoundary")
            End Try
        End Sub
    End Class

End Namespace
