﻿Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.Application
Imports Nexamas.UI.Components.DropDownMenu
Imports Nexamas.UI.Layout

Partial Friend Class NexamasUIShowcase

    Protected Overrides Sub CreateSampleControls()
        If Window Is Nothing Then Return

        Window.Pages.ClearPages().
            WithNavigationVisible(False).
            WithBreadcrumbVisible(False).
            WithBreadcrumbRootText("Showcase").
            WithRegionBackgrounds(False).
            WithRhythm(MASApplicationSurfaceRhythm.Comfortable).
            WithNavigationSize(MASApplicationSurfaceSideSize.Compact)

        AttachShowcaseMainMenu()
        RegisterShowcasePages()
    End Sub

    Protected Overrides Sub LayoutSampleControls()
        If Window Is Nothing Then Return

        If Window.Pages.IsShown Then
            Window.Pages.Refresh()
        Else
            Window.Pages.Show()
        End If
    End Sub

    Protected Overrides Sub ConfigureTopBarMenu(builder As MASDropDownMenuBuilder)
        If builder Is Nothing Then Return

        ConfigureThemeTopBarMenu(builder)
        ConfigureSurfaceMaterialTopBarMenu(builder)
        ConfigureSizeProfileTopBarMenu(builder)
        builder.AddAction("Reset Showcase route", HomePageId)
        builder.AddAction("About Showcase shell", "showcase.about")
        builder.AddSeparator()
        builder.AddAction("Minimize", "window.minimize")
        builder.AddAction("Maximize / Restore", "window.toggle_max_restore")
        builder.AddDangerAction("Close", "window.close")
    End Sub

    Protected Overrides Sub OnTopBarMenuCommand(commandId As String)
        Dim normalizedCommandId As String = If(commandId, String.Empty).Trim()

        If String.Equals(normalizedCommandId, HomePageId, StringComparison.OrdinalIgnoreCase) Then
            Window.Pages.NavigateTo(HomePageId)
            Return
        End If

        If String.Equals(normalizedCommandId, StartTourCommandId, StringComparison.OrdinalIgnoreCase) Then
            OpenGuidedJourney("controls", 0)
            Return
        End If

        Dim area As Nullable(Of ShowcaseNavigationCategory) = ShowcaseNavigationCatalog.FindCategoryByPageId(normalizedCommandId)
        If area.HasValue Then
            Window.Pages.NavigateTo(ShowcaseNavigationCatalog.ResolveCategoryPageId(area.Value))
            Return
        End If

        If String.Equals(normalizedCommandId, "showcase.about", StringComparison.OrdinalIgnoreCase) Then
            If Window IsNot Nothing Then
                Window.Services.Toasts.Info("The Showcase uses MASApplicationWindow.Pages as the official shell, but the fixed left navigation is hidden to preserve workspace width. TopBar owns global environment settings; the shell-owned MASMenuBar below the TopBar owns global route discovery, and Journeys own guided paths. Capability discovery is now demonstrated inside the DataGrid journey stage.", "Nexamas UI Showcase")
            End If
            Return
        End If

        Dim entry As ShowcaseNavigationEntry = ShowcaseNavigationCatalog.FindByCommandId(normalizedCommandId)
        If entry IsNot Nothing Then
            OpenShowcaseEntry(entry)
            Return
        End If

        MyBase.OnTopBarMenuCommand(normalizedCommandId)
    End Sub

    Private Sub RegisterShowcasePages()
        ' PageHost remains the route owner, but its description slot is not used
        ' by the commercial Showcase layout. Page summaries are rendered by the
        ' shared body composition so all descriptive copy shares one left edge
        ' with the rest of the page content.
        Window.Pages.RegisterPage(
            pageId:=HomePageId,
            title:="Overview",
            groupName:="Showcase",
            description:=String.Empty,
            buildPage:=Sub(page As MASApplicationLayoutPageBuilder)
                           PrepareShowcasePageBuild()
                           BuildHomePage(page)
                       End Sub)


        Window.Pages.RegisterPage(
            pageId:=GuidedJourneyPageId,
            title:="Guided Exploration",
            groupName:="Showcase",
            description:=String.Empty,
            buildPage:=Sub(page As MASApplicationLayoutPageBuilder)
                           PrepareShowcasePageBuild()
                           BuildGuidedJourneyPage(page)
                       End Sub)

        RegisterGuidedJourneyStepPages()

        For Each category As ShowcaseNavigationCategory In ShowcaseNavigationCatalog.GetCategories()
            Dim categoryForPage As ShowcaseNavigationCategory = category
            Window.Pages.RegisterPage(
                pageId:=ShowcaseNavigationCatalog.ResolveCategoryPageId(categoryForPage),
                title:=ShowcaseNavigationCatalog.ResolveCategoryTitle(categoryForPage) & " Overview",
                groupName:="Area overviews",
                description:=String.Empty,
                buildPage:=Sub(page As MASApplicationLayoutPageBuilder)
                               PrepareShowcasePageBuild()
                               BuildAreaPage(page, categoryForPage)
                           End Sub)
        Next

        For Each entry As ShowcaseNavigationEntry In ShowcaseNavigationCatalog.GetEntries()
            Dim entryForPage As ShowcaseNavigationEntry = entry
            Window.Pages.RegisterPage(
                pageId:=entryForPage.CommandId,
                title:=entryForPage.Title,
                groupName:=ShowcaseNavigationCatalog.ResolveCategoryTitle(entryForPage.Category),
                description:=String.Empty,
                buildPage:=Sub(page As MASApplicationLayoutPageBuilder)
                               PrepareShowcasePageBuild()
                               BuildCapabilityPage(page, entryForPage)
                           End Sub)
        Next
    End Sub

    Private Sub BuildCapabilityPage(page As MASApplicationLayoutPageBuilder,
                                    entry As ShowcaseNavigationEntry)
        If page Is Nothing OrElse entry Is Nothing Then Return

        ApplyShowcaseWorkspaceSurface(page)
        BuildShowcaseRouteSummary(page, entry.Summary)
        BuildCapabilityClassificationBanner(page, entry)

        ' Capability routes are product/evidence pages, not catalog cards.
        ' The real Nexamas.UI body must lead, while Showcase metadata stays as
        ' a secondary context section below the owned control/proof surface.
        If Not BuildSharedRouteScenarios(page, entry) Then
            ShowcasePageHostContentBuilder.Build(
                page:=page,
                window:=Window,
                entry:=entry,
                topBar:=ShowcaseTopBar,
                application:=ApplicationFacade,
                registerLifetime:=AddressOf RegisterPageLifetime)
        End If
        BuildCapabilityContextWorkspace(page, entry)
        ShowcasePageHostContentBuilder.BuildConsumerBoundaryFooter(page, Window, entry)
        BuildCapabilityBottomRouteTrail(page, entry)
    End Sub

    Private Sub OpenShowcaseEntry(entry As ShowcaseNavigationEntry)
        If entry Is Nothing OrElse Window Is Nothing Then Return
        Window.Pages.NavigateTo(entry.CommandId)
    End Sub
End Class
