﻿Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports System.Globalization
Imports System.IO
Imports Nexamas.UI.Application
Imports Nexamas.UI.Components
Imports Nexamas.UI.Components.DropDownMenu
Imports Nexamas.UI.Controls
Imports Nexamas.UI.FileSystem
Imports Nexamas.UI.Icons
Imports Nexamas.UI.Layout
Imports Nexamas.UI.Localization
Imports Nexamas.UI.Output
Imports Nexamas.UI.Theming
Imports Nexamas.UI.TextInput
Imports Nexamas.UI.Verification
Imports SkiaSharp

Partial Friend NotInheritable Class ShowcasePageHostContentBuilder

    Private Shared Sub BuildUnknownPage(page As MASApplicationLayoutPageBuilder,
                                        window As MASApplicationWindow,
                                        entry As ShowcaseNavigationEntry)
        AddShowcaseContent(page, Body(window, entry.Summary), MASSize.FillWidth)
        AddShowcaseContent(page, Muted(window, "This entry is registered in MASApplicationWindow.Pages and will receive a dedicated real control recipe when the corresponding Nexamas.UI surface is promoted."), MASSize.FillWidth)
    End Sub

    Private Shared Sub AddShowcaseContent(page As MASApplicationLayoutPageBuilder,
                                             control As MASControlBase,
                                             Optional requestedSize As MASSize = Nothing)
        If page Is Nothing OrElse control Is Nothing Then Return

        Dim resolvedSize As MASSize = ResolveShowcaseContentSize(control, requestedSize)
        page.Add(control, resolvedSize)
    End Sub

    Private Shared Function ResolveShowcaseContentSize(control As MASControlBase,
                                                       requestedSize As MASSize) As MASSize
        If control Is Nothing Then Return MASSize.[Default]

        If IsShowcaseSelfSizedSurface(control) Then
            Return MASSize.[Default]
        End If

        If requestedSize IsNot Nothing Then
            If IsShowcaseNaturalControl(control) AndAlso requestedSize.Kind = MASSizeIntentKind.FillWidth Then
                Return MASSize.[Default]
            End If
            Return requestedSize
        End If

        If IsShowcaseNaturalControl(control) Then
            Return MASSize.[Default]
        End If

        Return MASSize.FillWidth
    End Function

    Private Shared Function IsShowcaseNaturalControl(control As MASControlBase) As Boolean
        Return TypeOf control Is MASButton OrElse
               TypeOf control Is MASSplitButton OrElse
               TypeOf control Is MASComboBox OrElse
               TypeOf control Is MASSearchTextBox OrElse
               TypeOf control Is MASTextBox OrElse
               TypeOf control Is MASToggleSwitch OrElse
               TypeOf control Is MASSegmentedControl OrElse
               TypeOf control Is MASTabControl OrElse
               TypeOf control Is MASBreadcrumb OrElse
               TypeOf control Is MASPagination OrElse
               TypeOf control Is MASBadge OrElse
               TypeOf control Is MASRating
    End Function

    Private Shared Function IsShowcaseSelfSizedSurface(control As MASControlBase) As Boolean
        Return TypeOf control Is MASDataGrid OrElse
               TypeOf control Is MASListView OrElse
               TypeOf control Is MASFormValidationSummary OrElse
               TypeOf control Is MASDashboardGrid OrElse
               TypeOf control Is MASChart OrElse
               TypeOf control Is MASChartLegend OrElse
               TypeOf control Is MASFilterBuilder OrElse
               TypeOf control Is MASAppliedFiltersBar OrElse
               TypeOf control Is MASTreeGrid OrElse
               TypeOf control Is MASPivotTable OrElse
               TypeOf control Is MASMasterDetailView OrElse
               TypeOf control Is MASKanbanBoard OrElse
               TypeOf control Is MASAgendaView OrElse
               TypeOf control Is MASContentCarousel OrElse
               TypeOf control Is MASEmptyState OrElse
               TypeOf control Is MASTileBox OrElse
               TypeOf control Is MASTimeline OrElse
               TypeOf control Is MASNotificationPanel OrElse
               TypeOf control Is MASFileDropZone OrElse
               TypeOf control Is MASFileExplorerView OrElse
               TypeOf control Is MASTransferList OrElse
               TypeOf control Is MASPropertyGrid OrElse
               TypeOf control Is MASStepper OrElse
               TypeOf control Is MASWizard OrElse
               TypeOf control Is MASProgressBar OrElse
               TypeOf control Is MASStatusBanner OrElse
               TypeOf control Is MASExpander
    End Function

    Private Shared Sub AddShowcaseFieldRow(page As MASApplicationLayoutPageBuilder,
                                           label As MASLabel,
                                           input As MASControlBase,
                                           Optional trailing As MASControlBase = Nothing)
        If page Is Nothing OrElse label Is Nothing OrElse input Is Nothing Then Return
        page.FormRow(Sub(row As MASApplicationFormRowLayoutBuilder)
                         row.Label(label).Input(input, MASSize.Compact).Gap(MASLayoutSpacing.Medium)
                         If trailing IsNot Nothing Then
                             row.Add(trailing, MASSize.[Default])
                         End If
                     End Sub)
    End Sub


    Private Const ShowcaseMatrixHeaderHeightDip As Single = 42.0F
    Private Const ShowcaseMatrixRowHeightDip As Single = 32.0F
    Private Const ShowcaseMatrixOuterChromeHeightDip As Single = 18.0F
    Private Const ShowcaseMatrixMaximumVisibleRows As Integer = 8

    Private Shared Function MatrixSizeForRows(rowCount As Integer,
                                              Optional maximumVisibleRows As Integer = ShowcaseMatrixMaximumVisibleRows) As MASSize
        Dim safeRows As Integer = Math.Max(0, rowCount)
        Dim safeMaximum As Integer = Math.Max(0, maximumVisibleRows)
        Dim visibleRows As Integer = Math.Min(safeRows, safeMaximum)
        Dim heightDip As Single = ShowcaseMatrixHeaderHeightDip + (CSng(visibleRows) * ShowcaseMatrixRowHeightDip) + ShowcaseMatrixOuterChromeHeightDip
        Return MASSize.FillWidth.OffsetHeight(heightDip)
    End Function

    Private Shared Sub FitDataGridToRows(grid As MASDataGrid,
                                         Optional maximumVisibleRows As Integer = ShowcaseMatrixMaximumVisibleRows)
        If grid Is Nothing Then Return
        grid.WithSize(MatrixSizeForRows(grid.VisibleRowCount, maximumVisibleRows))
    End Sub

    Private Shared Sub AddNaturalActionGroup(page As MASApplicationLayoutPageBuilder,
                                             ParamArray buttons As MASButton())
        If page Is Nothing OrElse buttons Is Nothing Then Return
        page.ActionGroup(Sub(actions As MASApplicationActionGroupLayoutBuilder)
                             actions.Gap(MASLayoutSpacing.Medium).AlignCenter()
                             For Each button As MASButton In buttons
                                 If button Is Nothing Then Continue For
                                 actions.Add(button, MASSize.[Default])
                             Next
                         End Sub)
    End Sub

    Private Shared Function Heading(window As MASApplicationWindow, text As String) As MASLabel
        Dim label As MASLabel = window.Controls.AddLabel(If(text, String.Empty), Sub(control As MASLabel)
                                                                                     control.LabelVariant = MASLabelVariant.Heading
                                                                                     control.TextRole = MASLabelTextRole.Primary
                                                                                     control.MultiLine = True
                                                                                     control.WordWrap = True
                                                                                     control.VerticalAlignment = MASVerticalAlignment.Top
                                                                                 End Sub)
        label.WithDefaultSize()
        Return label
    End Function

    Private Shared Function Body(window As MASApplicationWindow, text As String) As MASLabel
        Dim label As MASLabel = window.Controls.AddLabel(If(text, String.Empty), Sub(control As MASLabel)
                                                                                     control.LabelVariant = MASLabelVariant.Body
                                                                                     control.TextRole = MASLabelTextRole.Primary
                                                                                     control.MultiLine = True
                                                                                     control.WordWrap = True
                                                                                     control.VerticalAlignment = MASVerticalAlignment.Top
                                                                                 End Sub)
        label.WithDefaultSize()
        Return label
    End Function

    Private Shared Function Muted(window As MASApplicationWindow, text As String) As MASLabel
        Dim label As MASLabel = window.Controls.AddLabel(If(text, String.Empty), Sub(control As MASLabel)
                                                                                     control.LabelVariant = MASLabelVariant.Caption
                                                                                     control.TextRole = MASLabelTextRole.Muted
                                                                                     control.MultiLine = True
                                                                                     control.WordWrap = True
                                                                                     control.VerticalAlignment = MASVerticalAlignment.Top
                                                                                 End Sub)
        label.WithDefaultSize()
        Return label
    End Function

    Private Shared Function InfoCard(window As MASApplicationWindow,
                                           title As String,
                                           text As String) As MASLabel
        Dim label As MASLabel = window.Controls.AddLabel(If(title, String.Empty) & Environment.NewLine & Environment.NewLine & If(text, String.Empty), Sub(control As MASLabel)
                                                                                     control.LabelVariant = MASLabelVariant.Body
                                                                                     control.TextRole = MASLabelTextRole.Secondary
                                                                                     control.MultiLine = True
                                                                                     control.WordWrap = True
                                                                                     control.VerticalAlignment = MASVerticalAlignment.Top
                                                                                 End Sub)
        label.WithDefaultSize()
        Return label
    End Function

End Class
