﻿Option Strict On
Option Explicit On

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

Partial Friend Class NexamasUIShowcase

    Private Sub BuildHomeBottomRouteTrail(page As MASApplicationLayoutPageBuilder)
        BuildBottomRouteTrail(
            page:=page,
            segments:=New String() {"Showcase", "Overview"},
            navigateByIndex:=Sub(index As Integer)
                                 ' The current route segment is informational only.
                             End Sub)
    End Sub

    Private Sub BuildAreaBottomRouteTrail(page As MASApplicationLayoutPageBuilder,
                                          category As ShowcaseNavigationCategory)
        Dim categoryTitle As String = ShowcaseNavigationCatalog.ResolveCategoryTitle(category)
        Dim categoryPageId As String = ShowcaseNavigationCatalog.ResolveCategoryPageId(category)

        BuildBottomRouteTrail(
            page:=page,
            segments:=New String() {"Showcase", "Area overviews", categoryTitle},
            navigateByIndex:=Sub(index As Integer)
                                 If Window Is Nothing Then Return
                                 If index <= 0 Then Window.Pages.NavigateTo(HomePageId)
                             End Sub)
    End Sub

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

        Dim categoryTitle As String = ShowcaseNavigationCatalog.ResolveCategoryTitle(entry.Category)
        Dim categoryPageId As String = ShowcaseNavigationCatalog.ResolveCategoryPageId(entry.Category)

        BuildBottomRouteTrail(
            page:=page,
            segments:=New String() {"Showcase", categoryTitle, entry.Title},
            navigateByIndex:=Sub(index As Integer)
                                 If Window Is Nothing Then Return
                                 If index <= 0 Then
                                     Window.Pages.NavigateTo(HomePageId)
                                 ElseIf index = 1 Then
                                     Window.Pages.NavigateTo(categoryPageId)
                                 End If
                             End Sub)
    End Sub

    Private Sub BuildBottomRouteTrail(page As MASApplicationLayoutPageBuilder,
                                      segments As String(),
                                      navigateByIndex As Action(Of Integer))
        If page Is Nothing OrElse Window Is Nothing OrElse segments Is Nothing OrElse segments.Length = 0 Then Return

        Dim breadcrumb As MASBreadcrumb = Window.Controls.AddBreadcrumb(
            segments,
            Sub(trail As MASBreadcrumb)
                trail.WithSelectedIndex(segments.Length - 1)
                trail.WithSize(MASSize.FillWidth)
            End Sub)

        AddHandler breadcrumb.SelectedIndexChanged,
            Sub(sender As Object, e As EventArgs)
                If navigateByIndex Is Nothing Then Return
                navigateByIndex(Math.Max(0, Math.Min(breadcrumb.SelectedIndex, segments.Length - 1)))
            End Sub

        page.FullWidth(breadcrumb, MASSize.FillWidth)
    End Sub
End Class
