Option Strict On
Option Explicit On

Imports System
Imports System.Linq
Imports System.Windows.Forms
Imports Nexamas.UI.Application
Imports Nexamas.UI.Components
Imports Nexamas.UI.Controls
Imports Nexamas.UI.Layout

''' <summary>
''' Repository-owned consumer compile host for Batch 1 documentation recipes.
''' This project must use only public package-facing APIs. It is not a Showcase helper,
''' not a Friend test harness, and not proof that the recipes are Showcase-ready.
''' </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))

        Global.System.Windows.Forms.Application.Run(New ExplicitLifetimeRecipeForm(smokeMode))
    End Sub
End Module

''' <summary>
''' Covers recipe Application.Window.Attach.
''' The form is intentionally present for compile verification; Program runs the explicit lifetime form
''' so maintainers can inspect both startup paths without changing this source file.
''' </summary>
Friend NotInheritable Class AttachWindowRecipeForm
    Inherits Form

    Private ReadOnly _window As MASApplicationWindow

    Public Sub New()
        Text = "Nexamas UI Batch 1 AttachWindow"
        Width = 1000
        Height = 700
        MinimumSize = New System.Drawing.Size(800, 560)
        StartPosition = FormStartPosition.CenterScreen
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi
        Me.DoubleBuffered = True

        _window = MASApplication.AttachWindow(
            owner:=Me,
            configure:=Sub(window As MASApplicationWindow)
                           Dim title As MASTitle = window.Controls.AddTitle("Nexamas UI")
                           Dim message As MASLabel = window.Controls.AddLabel("Ready.")
                           Dim action As MASButton = window.Controls.AddButton("Refresh")

                           AddHandler action.Click,
                               Sub(sender As Object, e As EventArgs)
                                   window.Services.Toasts.Success("The MAS window is alive.", "Quickstart")
                               End Sub

                           window.Controls.LayoutPage(
                               Sub(page As MASApplicationLayoutPageBuilder)
                                   page.Padding(MASLayoutSpacing.Spacious)
                                   page.Spacing(MASLayoutSpacing.Medium)
                                   page.FullWidth(title, MASSize.FillWidth)
                                   page.FullWidth(message, MASSize.FillWidth)
                                   page.ActionGroup(
                                       Sub(actions As MASApplicationActionGroupLayoutBuilder)
                                           actions.AlignCenter().EqualItemWidth().Add(action, MASSize.Default)
                                       End Sub)
                               End Sub)
                       End Sub)
    End Sub
End Class

''' <summary>
''' Covers recipe Application.Window.ExplicitLifetime and acts as the runtime host for the Batch 1 sample.
''' </summary>
Friend NotInheritable Class ExplicitLifetimeRecipeForm
    Inherits Form

    Private Shared ReadOnly RuntimeRouteIds As String() = New String() {"overview", "customer-page", "actions", "search"}

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

    Public Sub New(smokeMode As Boolean)
        Text = "Nexamas UI Batch 1 Recipe Verification"
        Width = 1280
        Height = 840
        MinimumSize = New System.Drawing.Size(1024, 700)
        StartPosition = FormStartPosition.CenterScreen
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi
        Me.DoubleBuffered = True

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

        Batch1RecipeSnippets.ConfigureRuntimePages(_window)

        If smokeMode Then
            _smokeTimer = New Timer() With {.Interval = 1200}
            AddHandler _smokeTimer.Tick, AddressOf HandleSmokeTick
            _smokeTimer.Start()
        End If
    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 _window IsNot Nothing Then _window.Dispose()
        If _application IsNot Nothing Then _application.Dispose()
        MyBase.OnFormClosed(e)
    End Sub
End Class

''' <summary>
''' Public-API-only snippets corresponding to Batch 1 recipe IDs.
''' These methods intentionally mirror the documentation snippets closely so a successful Windows build
''' can be recorded against the recipe build-verification ledger.
''' </summary>
Friend NotInheritable Class Batch1RecipeSnippets
    Private Sub New()
    End Sub

    Public Shared Sub BuildBatch1LandingPage(window As MASApplicationWindow)
        Dim title As MASTitle = window.Controls.AddTitle("Batch 1 recipes")
        Dim search As MASSearchTextBox = window.Controls.AddSearchTextBox("Search")
        Dim nameBox As MASTextBox = window.Controls.AddTextBox("Customer name")
        Dim activeOnly As MASCheckBox = window.Controls.AddCheckBox("Active only", checked:=True)
        Dim status As MASLabel = window.Controls.AddLabel("Ready. This page exercises the Batch 1 public recipe surface.")
        Dim saveButton As MASButton = window.Controls.AddButton("Save")
        Dim cancelButton As MASButton = window.Controls.AddButton("Cancel")

        AddHandler saveButton.Click,
            Sub(sender As Object, e As EventArgs)
                status.WithText("Save clicked.")
                window.Services.Toasts.Success("The action was handled.", "Batch 1")
            End Sub

        AddHandler cancelButton.Click,
            Sub(sender As Object, e As EventArgs)
                status.WithText("Cancelled.")
            End Sub

        window.Controls.LayoutPage(
            Sub(page As MASApplicationLayoutPageBuilder)
                page.Padding(MASLayoutSpacing.Spacious)
                page.Spacing(MASLayoutSpacing.Medium)
                page.FullWidth(title, MASSize.FillWidth)

                page.Grid(2).
                    Gap(MASLayoutSpacing.Medium).
                    EqualItemWidth().
                    Add(search, MASSize.FillWidth).
                    Add(activeOnly, MASSize.Default)

                page.FullWidth(nameBox, MASSize.FillWidth)
                page.FullWidth(status, MASSize.FillWidth)
                page.ActionGroup(
                    Sub(actions As MASApplicationActionGroupLayoutBuilder)
                        actions.AlignEnd().EqualItemWidth().Add(saveButton, MASSize.Default).Add(cancelButton, MASSize.Default)
                    End Sub)
            End Sub)
    End Sub

    ' Recipe: Layout.Page.Basic
    Public Shared Sub BuildCustomerPage(window As MASApplicationWindow)
        Dim title As MASTitle = window.Controls.AddTitle("Customer Overview")
        Dim search As MASSearchTextBox = window.Controls.AddSearchTextBox("Search")
        Dim activeOnly As MASCheckBox = window.Controls.AddCheckBox("Active only", checked:=True)
        Dim exportButton As MASButton = window.Controls.AddButton("Export")
        Dim closeButton As MASButton = window.Controls.AddButton("Close")

        window.Controls.LayoutPage(
            Sub(page As MASApplicationLayoutPageBuilder)
                page.Padding(MASLayoutSpacing.Spacious)
                page.Spacing(MASLayoutSpacing.Medium)
                page.FullWidth(title, MASSize.FillWidth)

                page.Grid(2).
                    Gap(MASLayoutSpacing.Medium).
                    EqualItemWidth().
                    Add(search, MASSize.FillWidth).
                    Add(activeOnly, MASSize.Default)

                page.ActionGroup(
                    Sub(actions As MASApplicationActionGroupLayoutBuilder)
                        actions.AlignEnd().
                            EqualItemWidth().
                            Add(exportButton, MASSize.Default).
                            Add(closeButton, MASSize.Default)
                    End Sub)
            End Sub)
    End Sub

    ' Recipe: Controls.BasicSet
    Public Shared Sub BuildControls(window As MASApplicationWindow)
        Dim title As MASTitle = window.Controls.AddTitle("Controls")
        Dim nameBox As MASTextBox = window.Controls.AddTextBox("Customer name")
        Dim status As MASLabel = window.Controls.AddLabel("Ready.")
        Dim saveButton As MASButton = window.Controls.AddButton("Save")
        Dim cancelButton As MASButton = window.Controls.AddButton("Cancel")

        AddHandler saveButton.Click,
            Sub(sender As Object, e As EventArgs)
                status.WithText("Save clicked.")
                window.Services.Toasts.Success("The action was handled.", "Controls")
            End Sub

        AddHandler cancelButton.Click,
            Sub(sender As Object, e As EventArgs)
                status.WithText("Cancelled.")
            End Sub

        window.Controls.LayoutPage(
            Sub(page As MASApplicationLayoutPageBuilder)
                page.Padding(MASLayoutSpacing.Spacious)
                page.Spacing(MASLayoutSpacing.Medium)
                page.FullWidth(title, MASSize.FillWidth)
                page.FullWidth(nameBox, MASSize.FillWidth)
                page.FullWidth(status, MASSize.FillWidth)
                page.ActionGroup(
                    Sub(actions As MASApplicationActionGroupLayoutBuilder)
                        actions.AlignEnd().EqualItemWidth().Add(saveButton, MASSize.Default).Add(cancelButton, MASSize.Default)
                    End Sub)
            End Sub)
    End Sub

    ' Recipe: Button.Primary.Basic
    Public Shared Sub BuildSaveAction(window As MASApplicationWindow)
        Dim status As MASLabel = window.Controls.AddLabel("Ready.")
        Dim saveButton As MASButton = window.Controls.AddButton("Save")

        AddHandler saveButton.Click,
            Sub(sender As Object, e As EventArgs)
                status.WithText("Saved.")
                window.Services.Toasts.Success("The action was handled.", "Save")
            End Sub

        window.Controls.LayoutPage(
            Sub(page As MASApplicationLayoutPageBuilder)
                page.Padding(MASLayoutSpacing.Spacious)
                page.Spacing(MASLayoutSpacing.Medium)
                page.FullWidth(status, MASSize.FillWidth)
                page.ActionGroup(
                    Sub(actions As MASApplicationActionGroupLayoutBuilder)
                        actions.AlignEnd().EqualItemWidth().Add(saveButton, MASSize.Default)
                    End Sub)
            End Sub)
    End Sub

    ' Recipe: Button.Group.SaveCancel
    Public Shared Sub BuildSaveCancelActions(window As MASApplicationWindow)
        Dim status As MASLabel = window.Controls.AddLabel("No action yet.")
        Dim saveButton As MASButton = window.Controls.AddButton("Save")
        Dim cancelButton As MASButton = window.Controls.AddButton("Cancel")

        AddHandler saveButton.Click,
            Sub(sender As Object, e As EventArgs)
                status.WithText("Save clicked.")
            End Sub

        AddHandler cancelButton.Click,
            Sub(sender As Object, e As EventArgs)
                status.WithText("Cancel clicked.")
            End Sub

        window.Controls.LayoutPage(
            Sub(page As MASApplicationLayoutPageBuilder)
                page.Padding(MASLayoutSpacing.Spacious)
                page.Spacing(MASLayoutSpacing.Medium)
                page.FullWidth(status, MASSize.FillWidth)
                page.ActionGroup(
                    Sub(actions As MASApplicationActionGroupLayoutBuilder)
                        actions.AlignEnd().
                            EqualItemWidth().
                            Add(saveButton, MASSize.Default).
                            Add(cancelButton, MASSize.Default)
                    End Sub)
            End Sub)
    End Sub

    ' Recipe: Input.Text.Basic
    Public Shared Sub BuildTextInput(window As MASApplicationWindow)
        Dim title As MASTitle = window.Controls.AddTitle("Customer")
        Dim nameBox As MASTextBox = window.Controls.AddTextBox("Customer name")

        window.Controls.LayoutPage(
            Sub(page As MASApplicationLayoutPageBuilder)
                page.Padding(MASLayoutSpacing.Spacious)
                page.Spacing(MASLayoutSpacing.Medium)
                page.FullWidth(title, MASSize.FillWidth)
                page.FullWidth(nameBox, MASSize.FillWidth)
            End Sub)
    End Sub

    ' Recipe: Input.Search.Basic
    Public Shared Sub BuildSearchInput(window As MASApplicationWindow)
        Dim searchBox As MASSearchTextBox = window.Controls.AddSearchTextBox("Search")
        Dim status As MASLabel = window.Controls.AddLabel("Type to search.")

        window.Controls.LayoutPage(
            Sub(page As MASApplicationLayoutPageBuilder)
                page.Padding(MASLayoutSpacing.Spacious)
                page.Spacing(MASLayoutSpacing.Medium)
                page.FullWidth(searchBox, MASSize.FillWidth)
                page.FullWidth(status, MASSize.FillWidth)
            End Sub)
    End Sub

    ''' <summary>
    ''' Configures the visual verification host as an official same-window page application.
    ''' MASApplicationWindow.Pages supplies vertical workspace scrolling, responsive relayout,
    ''' and navigation so every Batch 1 recipe surface remains separated at smaller viewports.
    ''' </summary>
    Public Shared Sub ConfigureRuntimePages(window As MASApplicationWindow)
        If window Is Nothing Then Throw New ArgumentNullException(NameOf(window))

        Dim pages As MASApplicationPageHost = window.Pages
        pages.ClearPages()
        pages.RegisterPage(
            pageId:="overview",
            title:="Overview",
            groupName:="Batch 1",
            description:="A compact customer form using the official page, grid, form-control, action-group, and toast routes.",
            buildPage:=Sub(page As MASApplicationLayoutPageBuilder)
                           BuildRuntimeOverviewPage(window, page)
                       End Sub)
        pages.RegisterPage(
            pageId:="customer-page",
            title:="Customer page",
            groupName:="Batch 1",
            description:="Search, filtering intent, a full-width input, and balanced application actions without manual coordinates.",
            buildPage:=Sub(page As MASApplicationLayoutPageBuilder)
                           BuildRuntimeCustomerPage(window, page)
                       End Sub)
        pages.RegisterPage(
            pageId:="actions",
            title:="Actions",
            groupName:="Batch 1",
            description:="Primary and secondary actions with status feedback and equal semantic sizing.",
            buildPage:=Sub(page As MASApplicationLayoutPageBuilder)
                           BuildRuntimeActionsPage(window, page)
                       End Sub)
        pages.RegisterPage(
            pageId:="search",
            title:="Search input",
            groupName:="Batch 1",
            description:="A focused search recipe with live status text in a scroll-safe workspace.",
            buildPage:=Sub(page As MASApplicationLayoutPageBuilder)
                           BuildRuntimeSearchPage(window, page)
                       End Sub)

        pages.WithNavigationVisible(True).
            WithBreadcrumbVisible(True).
            WithBreadcrumbRootText("Recipe verification").
            WithRegionBackgrounds(True).
            WithRhythm(MASApplicationSurfaceRhythm.Comfortable).
            WithNavigationSize(MASApplicationSurfaceSideSize.Compact)
        pages.Show()
    End Sub

    Private Shared Sub BuildRuntimeOverviewPage(window As MASApplicationWindow,
                                                page As MASApplicationLayoutPageBuilder)
        Dim banner As MASStatusBanner = window.Controls.AddStatusBanner(
            title:="Batch 1 is ready",
            message:="The runtime host now uses the official PageHost workspace, so controls reflow and scroll instead of competing for one fixed viewport.",
            tone:=MASToastTone.Info)
        Dim search As MASSearchTextBox = window.Controls.AddSearchTextBox("Search customers")
        Dim activeOnly As MASCheckBox = window.Controls.AddCheckBox("Active only", checked:=True)
        Dim nameBox As MASTextBox = window.Controls.AddTextBox("Customer name")
        Dim status As MASLabel = window.Controls.AddLabel("Ready. Choose an action to verify feedback and toast behavior.")
        Dim saveButton As MASButton = window.Controls.AddButton("Save")
        Dim cancelButton As MASButton = window.Controls.AddButton("Cancel")

        AddHandler saveButton.Click,
            Sub(sender As Object, e As EventArgs)
                status.WithText("Save clicked.")
                window.Services.Toasts.Success("The action was handled.", "Batch 1")
            End Sub
        AddHandler cancelButton.Click,
            Sub(sender As Object, e As EventArgs)
                status.WithText("Cancelled.")
            End Sub

        page.Spacing(MASLayoutSpacing.Large)
        page.FullWidth(banner, MASSize.FillWidth)
        page.Grid(2).
            Gap(MASLayoutSpacing.Large).
            Add(search, MASSize.FillWidth).
            Add(activeOnly, MASSize.Default)
        page.FullWidth(nameBox, MASSize.FillWidth)
        page.FullWidth(status, MASSize.FillWidth)
        page.ActionGroup(
            Sub(actions As MASApplicationActionGroupLayoutBuilder)
                actions.AlignEnd().EqualItemWidth().Gap(MASLayoutSpacing.Medium)
                actions.Add(saveButton, MASSize.Default)
                actions.Add(cancelButton, MASSize.Default)
            End Sub)
    End Sub

    Private Shared Sub BuildRuntimeCustomerPage(window As MASApplicationWindow,
                                                page As MASApplicationLayoutPageBuilder)
        Dim search As MASSearchTextBox = window.Controls.AddSearchTextBox("Search")
        Dim activeOnly As MASCheckBox = window.Controls.AddCheckBox("Active only", checked:=True)
        Dim nameBox As MASTextBox = window.Controls.AddTextBox("Customer name")
        Dim summary As MASLabel = window.Controls.AddLabel("This page keeps input rows readable at narrow widths and preserves generous spacing at large widths.")
        Dim exportButton As MASButton = window.Controls.AddButton("Export")
        Dim closeButton As MASButton = window.Controls.AddButton("Close")

        page.Spacing(MASLayoutSpacing.Large)
        page.FullWidth(summary, MASSize.FillWidth)
        page.Grid(2).
            Gap(MASLayoutSpacing.Large).
            Add(search, MASSize.FillWidth).
            Add(activeOnly, MASSize.Default)
        page.FullWidth(nameBox, MASSize.FillWidth)
        page.ActionGroup(
            Sub(actions As MASApplicationActionGroupLayoutBuilder)
                actions.AlignEnd().EqualItemWidth().Gap(MASLayoutSpacing.Medium)
                actions.Add(exportButton, MASSize.Default)
                actions.Add(closeButton, MASSize.Default)
            End Sub)
    End Sub

    Private Shared Sub BuildRuntimeActionsPage(window As MASApplicationWindow,
                                               page As MASApplicationLayoutPageBuilder)
        Dim status As MASLabel = window.Controls.AddLabel("No action yet.")
        Dim saveButton As MASButton = window.Controls.AddButton("Save")
        Dim cancelButton As MASButton = window.Controls.AddButton("Cancel")

        AddHandler saveButton.Click,
            Sub(sender As Object, e As EventArgs)
                status.WithText("Save clicked.")
                window.Services.Toasts.Success("Saved through the public toast service.", "Actions")
            End Sub
        AddHandler cancelButton.Click,
            Sub(sender As Object, e As EventArgs)
                status.WithText("Cancel clicked.")
            End Sub

        page.Spacing(MASLayoutSpacing.Large)
        page.FullWidth(status, MASSize.FillWidth)
        page.ActionGroup(
            Sub(actions As MASApplicationActionGroupLayoutBuilder)
                actions.AlignCenter().EqualItemWidth().Gap(MASLayoutSpacing.Medium)
                actions.Add(saveButton, MASSize.Default)
                actions.Add(cancelButton, MASSize.Default)
            End Sub)
    End Sub

    Private Shared Sub BuildRuntimeSearchPage(window As MASApplicationWindow,
                                              page As MASApplicationLayoutPageBuilder)
        Dim searchBox As MASSearchTextBox = window.Controls.AddSearchTextBox("Search")
        Dim status As MASLabel = window.Controls.AddLabel("Type to search.")

        AddHandler searchBox.TextChanged,
            Sub(sender As Object, e As EventArgs)
                status.WithText(If(String.IsNullOrWhiteSpace(searchBox.Text), "Type to search.", "Searching for: " & searchBox.Text))
            End Sub

        page.Spacing(MASLayoutSpacing.Large)
        page.FullWidth(searchBox, MASSize.FillWidth)
        page.FullWidth(status, MASSize.FillWidth)
    End Sub

End Class
