﻿Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports System.Globalization
Imports Nexamas.UI.Application
Imports Nexamas.UI.Components
Imports Nexamas.UI.Controls
Imports Nexamas.UI.Layout

Partial Friend Class NexamasUIShowcase

    ' Retired from the Overview: explanatory presentation-policy tables belong in
    ' documentation/area context, not in the first live product experience.
    Private Sub BuildHomePresentationDisciplineMap(page As MASApplicationLayoutPageBuilder)
        If page Is Nothing Then Return
    End Sub

    Private Sub BuildAreaContextWorkspace(page As MASApplicationLayoutPageBuilder,
                                          category As ShowcaseNavigationCategory,
                                          entries As IReadOnlyList(Of ShowcaseNavigationEntry))
        If page Is Nothing OrElse Window Is Nothing Then Return

        Dim safeEntries As IReadOnlyList(Of ShowcaseNavigationEntry) = If(entries, New List(Of ShowcaseNavigationEntry)().AsReadOnly())
        Dim areaTitle As String = ShowcaseNavigationCatalog.ResolveCategoryTitle(category)

        Dim contextGrid As MASApplicationLayoutGridBuilder = page.Grid(4).Gap(MASLayoutSpacing.Medium).EqualItemSize()
        contextGrid.Add(CreateMetric("Area", areaTitle, ShowcaseNavigationCatalog.ResolveCategoryKind(category)), MASSize.FillWidth)
        contextGrid.Add(CreateMetric("Pages", safeEntries.Count.ToString(CultureInfo.InvariantCulture), "Direct PageHost routes"), MASSize.FillWidth)
        contextGrid.Add(CreateMetric("Navigation", "MenuBar", "No fixed left tree"), MASSize.FillWidth)
        contextGrid.Add(CreateMetric("Preview", "Curated", "Not a duplicated catalog"), MASSize.FillWidth)

        Dim surfaceGrid As MASDataGrid = Window.Controls.AddDataGrid()
        surfaceGrid.AddColumn("surface", "Surface decision")
        surfaceGrid.AddColumn("pages", "Pages")
        surfaceGrid.AddColumn("reason", "Why this surface is used here")

        For Each value As Object In [Enum].GetValues(GetType(ShowcasePresentationKind))
            Dim kind As ShowcasePresentationKind = CType(value, ShowcasePresentationKind)
            Dim count As Integer = CountPresentationKind(category, kind)
            If count <= 0 Then Continue For

            surfaceGrid.AddRow(ShowcaseNavigationCatalog.ResolvePresentationKindTitle(kind),
                               count.ToString(CultureInfo.InvariantCulture),
                               ShowcaseNavigationCatalog.ResolvePresentationDecision(kind))
        Next

        surfaceGrid.WithSize(MASSize.FillWidth.OffsetHeight(180.0F))
        AddShowcaseContent(page, surfaceGrid, MASSize.FillWidth)
    End Sub

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

        Dim presentationKind As ShowcasePresentationKind = ShowcaseNavigationCatalog.ResolvePresentationKind(entry)
        Dim kind As ShowcaseNavigationEntryKind = ShowcaseNavigationCatalog.ResolveEntryKind(entry)
        Dim metadataText As String =
            "Route: " & entry.CommandId & Environment.NewLine &
            "Area: " & ShowcaseNavigationCatalog.ResolveCategoryTitle(entry.Category) & Environment.NewLine &
            "Lane: " & ShowcaseNavigationCatalog.ResolveEntryKindTitle(kind) & Environment.NewLine &
            "Surface: " & ShowcaseNavigationCatalog.ResolvePresentationKindTitle(presentationKind) & Environment.NewLine &
            "Journey: " & ShowcaseNavigationCatalog.ResolvePrimaryJourneyTitle(entry) & Environment.NewLine &
            "Related: " & ResolveRelatedContextText(entry) & Environment.NewLine & Environment.NewLine &
            "Consumer rule: the live page body stays first. This collapsed metadata is optional developer context; it does not create navigation, duplicate a catalog, or delay the Nexamas.UI-owned control surface."

        Dim metadataExpander As MASExpander = Window.Controls.AddExpander("Developer route metadata", expanded:=False, configure:=Sub(expander As MASExpander)
                                                                                                                                              expander.WithSize(MASSize.FillWidth.OffsetHeight(175.0F))
                                                                                                                                              expander.Add(CreateMuted(metadataText))
                                                                                                                                          End Sub)
        AddShowcaseContent(page, metadataExpander, MASSize.FillWidth)
    End Sub


    ' Route-trail note: the visible path indicator is appended by
    ' NexamasUIShowcase.RouteTrail.vb at the bottom of each route. This
    ' composition file keeps only context/workspace evidence and must not
    ' create its own breadcrumbs.

    Private Function CountPresentationKind(kind As ShowcasePresentationKind) As Integer
        Dim count As Integer = 0
        For Each entry As ShowcaseNavigationEntry In ShowcaseNavigationCatalog.GetEntries()
            If ShowcaseNavigationCatalog.ResolvePresentationKind(entry) = kind Then count += 1
        Next
        Return count
    End Function

    Private Function CountPresentationKind(category As ShowcaseNavigationCategory,
                                           kind As ShowcasePresentationKind) As Integer
        Dim count As Integer = 0
        For Each entry As ShowcaseNavigationEntry In ShowcaseNavigationCatalog.GetEntries(category)
            If ShowcaseNavigationCatalog.ResolvePresentationKind(entry) = kind Then count += 1
        Next
        Return count
    End Function

    Private Function ResolveRelatedContextText(entry As ShowcaseNavigationEntry) As String
        Dim relatedEntries As IReadOnlyList(Of ShowcaseNavigationEntry) = ShowcaseNavigationCatalog.GetRelatedEntries(entry, 2)
        If relatedEntries.Count = 0 Then Return "Area overview"

        Dim titles As New List(Of String)()
        For Each relatedEntry As ShowcaseNavigationEntry In relatedEntries
            If relatedEntry IsNot Nothing Then titles.Add(relatedEntry.Title)
        Next

        If titles.Count = 0 Then Return "Area overview"
        Return String.Join(" • ", titles.ToArray())
    End Function
End Class
