Option Strict On
Option Explicit On

Imports System
Imports System.Linq
Imports System.Windows.Forms
Imports Nexamas.UI.Application
Imports Nexamas.UI.Verification

''' <summary>
''' Public-consumer compile and runtime-smoke host for the advanced Render Verification recipe.
''' Internal runners, baselines, target catalogs, and capture handlers are intentionally not referenced.
''' </summary>
Friend Module Program
    <STAThread>
    Public Sub Main(args As String())
        Global.System.Windows.Forms.Application.EnableVisualStyles()
        Global.System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(False)

        Dim smokeMode As Boolean = args IsNot Nothing AndAlso
            args.Any(Function(value As String) String.Equals(value, "--smoke", StringComparison.OrdinalIgnoreCase))
        Dim snapshotMode As Boolean = args IsNot Nothing AndAlso
            args.Any(Function(value As String) String.Equals(value, "--snapshot-smoke", StringComparison.OrdinalIgnoreCase))

        Global.System.Windows.Forms.Application.Run(New RenderVerificationRecipeForm(smokeMode, snapshotMode))
    End Sub
End Module

Friend NotInheritable Class RenderVerificationRecipeForm
    Inherits Form

    Private Shared ReadOnly RuntimeRouteIds As String() = New String() {"render-verification"}

    Private ReadOnly _application As MASApplication
    Private ReadOnly _window As MASApplicationWindow
    Private _renderDashboard As IDisposable
    Private ReadOnly _smokeTimer As Timer
    Private _smokeRouteIndex As Integer

    Public Sub New(smokeMode As Boolean, snapshotMode As Boolean)
        Text = "Nexamas UI Batch 5 Render Verification"
        Width = 1460
        Height = 960
        MinimumSize = New System.Drawing.Size(1180, 780)
        StartPosition = FormStartPosition.CenterScreen
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi
        Me.DoubleBuffered = True

        _application = MASApplication.Create()
        _window = _application.CreateWindow(owner:=Me)
        ConfigureRuntimePage()

        If snapshotMode Then
            CreateRenderSnapshot()
        End If

        If smokeMode OrElse snapshotMode Then
            _smokeTimer = New Timer() With {.Interval = 1500}
            AddHandler _smokeTimer.Tick, AddressOf HandleSmokeTick
            _smokeTimer.Start()
        End If
    End Sub

    ' Recipe: Verification.RenderSnapshot
    Private Shared Function CreateRenderSnapshot() As MASRenderVerificationSnapshot
        Dim progress As New Progress(Of MASRenderVerificationProgressInfo)(
            Sub(info As MASRenderVerificationProgressInfo)
                If info IsNot Nothing Then
                    ' Use info.Percent and info.StatusText in a consumer progress UI.
                End If
            End Sub)

        Dim snapshot As MASRenderVerificationSnapshot = MASRenderVerification.CreateSnapshot(
            outputDirectory:=Nothing,
            progress:=progress,
            themeId:="premium-office-mas-signature")

        For Each entry As MASRenderVerificationEntry In snapshot.Entries
            ' Read public entry facts and artifact paths for a report or dashboard.
        Next

        Return snapshot
    End Function

    Private Sub ConfigureRuntimePage()
        Dim pages As MASApplicationPageHost = _window.Pages
        pages.ClearPages()
        pages.RegisterPage(
            pageId:="render-verification",
            title:="Render Verification",
            groupName:="Batch 5",
            description:="The official Render Verification dashboard is hosted inside the PageHost scroll workspace so every proof surface keeps its measured height.",
            buildPage:=Sub(page As MASApplicationLayoutPageBuilder)
                           AttachRenderDashboard(_window, page)
                       End Sub)
        pages.WithNavigationVisible(False).
            WithBreadcrumbVisible(False).
            WithRegionBackgrounds(True).
            WithRhythm(MASApplicationSurfaceRhythm.Comfortable)
        pages.Show()
    End Sub

    ' Preserved direct overload as the exact one-call recipe documented for consumers.
    Private Sub AttachRenderDashboard(window As MASApplicationWindow)
        If _renderDashboard IsNot Nothing Then
            _renderDashboard.Dispose()
            _renderDashboard = Nothing
        End If

        _renderDashboard = MASRenderVerification.AttachDashboard(window)
    End Sub

    Private Sub AttachRenderDashboard(window As MASApplicationWindow,
                                      page As MASApplicationLayoutPageBuilder)
        If _renderDashboard IsNot Nothing Then
            _renderDashboard.Dispose()
            _renderDashboard = Nothing
        End If

        _renderDashboard = MASRenderVerification.AttachDashboard(window, page)
    End Sub

    Private Sub HandleSmokeTick(sender As Object, e As EventArgs)
        If _smokeRouteIndex < RuntimeRouteIds.Length - 1 Then
            _smokeRouteIndex += 1
            _window.Pages.NavigateTo(RuntimeRouteIds(_smokeRouteIndex))
            Return
        End If

        _smokeTimer.Stop()
        Close()
    End Sub

    Protected Overrides Sub OnFormClosed(e As FormClosedEventArgs)
        If _smokeTimer IsNot Nothing Then
            RemoveHandler _smokeTimer.Tick, AddressOf HandleSmokeTick
            _smokeTimer.Dispose()
        End If
        If _renderDashboard IsNot Nothing Then
            _renderDashboard.Dispose()
            _renderDashboard = Nothing
        End If
        If _window IsNot Nothing Then _window.Dispose()
        If _application IsNot Nothing Then _application.Dispose()
        MyBase.OnFormClosed(e)
    End Sub
End Class
