Option Strict On
Option Explicit On

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

Partial Friend Class NexamasUIShowcase

    Private _guidedInlineRow As List(Of MASControlBase)

    Private 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 = ResolveSmartShowcaseSize(control, requestedSize)
        ApplySmartShowcaseSize(control, resolvedSize)
        page.Add(control, resolvedSize)
    End Sub

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

        If IsSmartNaturalControl(control) Then
            If requestedSize IsNot Nothing AndAlso requestedSize.Kind <> MASSizeIntentKind.FillWidth Then
                Return requestedSize
            End If
            Return MASSize.[Default]
        End If

        If IsSmartSelfSizedSurface(control) Then
            If TypeOf control Is MASDataGrid OrElse TypeOf control Is MASTreeGrid OrElse TypeOf control Is MASPivotTable Then
                Return If(requestedSize, MASSize.FillWidth)
            End If
            Return If(requestedSize, MASSize.[Default])
        End If

        If requestedSize IsNot Nothing Then Return requestedSize
        Return MASSize.FillWidth
    End Function

    Private Sub ApplySmartShowcaseSize(control As MASControlBase,
                                       resolvedSize As MASSize)
        If control Is Nothing OrElse resolvedSize Is Nothing Then Return

        If IsSmartNaturalControl(control) Then
            control.WithSize(resolvedSize)
            Return
        End If

        If TypeOf control Is MASStatusBanner Then
            If resolvedSize.Kind = MASSizeIntentKind.FillWidth Then Return
            control.WithSize(resolvedSize)
            Return
        End If

        If IsSmartSelfSizedSurface(control) AndAlso Not (TypeOf control Is MASDataGrid OrElse TypeOf control Is MASTreeGrid OrElse TypeOf control Is MASPivotTable) Then
            control.WithSize(resolvedSize)
        End If
    End Sub

    Private Function IsSmartNaturalControl(control As MASControlBase) As Boolean
        Return TypeOf control Is MASButton OrElse
               TypeOf control Is MASSplitButton OrElse
               TypeOf control Is MASCheckBox OrElse
               TypeOf control Is MASRadioButton OrElse
               TypeOf control Is MASToggleSwitch OrElse
               TypeOf control Is MASTextBox OrElse
               TypeOf control Is MASPasswordTextBox OrElse
               TypeOf control Is MASNumericTextBox OrElse
               TypeOf control Is MASPathTextBox OrElse
               TypeOf control Is MASSearchTextBox OrElse
               TypeOf control Is MASComboBox 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 OrElse
               TypeOf control Is MASSlider OrElse
               TypeOf control Is MASTagInput OrElse
               TypeOf control Is MASDatePicker OrElse
               TypeOf control Is MASDateRangePicker OrElse
               TypeOf control Is MASTimePicker OrElse
               TypeOf control Is MASSelectorItemControl
    End Function

    Private Function IsSmartInlineControl(control As MASControlBase) As Boolean
        If control Is Nothing Then Return False
        If TypeOf control Is MASLabel Then Return False
        Return IsSmartNaturalControl(control)
    End Function

    Private Function IsSmartSelfSizedSurface(control As MASControlBase) As Boolean
        Return TypeOf control Is MASStatusBanner OrElse
               TypeOf control Is MASExpander OrElse
               TypeOf control Is MASDataGrid OrElse
               TypeOf control Is MASListBox OrElse
               TypeOf control Is MASListView OrElse
               TypeOf control Is MASTreeView OrElse
               TypeOf control Is MASFormValidationSummary OrElse
               TypeOf control Is MASDataForm OrElse
               TypeOf control Is MASDashboardGrid OrElse
               TypeOf control Is MASMetricCard 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 MASFilePickerControl 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 MASOperationProgressBox OrElse
               TypeOf control Is MASCommandPalette OrElse
               TypeOf control Is MASNavigationRail OrElse
               TypeOf control Is MASAccordion OrElse
               TypeOf control Is MASLoadingSkeleton OrElse
               TypeOf control Is MASCalendarView
    End Function

    Private Sub BeginGuidedStageSmartLayout()
        _guidedInlineRow = New List(Of MASControlBase)()
    End Sub

    Private Sub EndGuidedStageSmartLayout(page As MASApplicationLayoutPageBuilder)
        FlushGuidedStageInlineRow(page)
        _guidedInlineRow = Nothing
    End Sub

    Private Sub RegisterSmartGuidedStageControl(page As MASApplicationLayoutPageBuilder,
                                                controls As List(Of MASControlBase),
                                                control As MASControlBase,
                                                Optional sizeIntent As MASSize = Nothing)
        If page Is Nothing OrElse controls Is Nothing OrElse control Is Nothing Then Return

        Dim resolvedSize As MASSize = ResolveSmartGuidedStageSize(control, sizeIntent)

        If sizeIntent Is Nothing AndAlso IsSmartInlineControl(control) Then
            ApplySmartShowcaseSize(control, resolvedSize)
            controls.Add(control)
            If _guidedInlineRow Is Nothing Then _guidedInlineRow = New List(Of MASControlBase)()
            _guidedInlineRow.Add(control)
            Return
        End If

        FlushGuidedStageInlineRow(page)
        ApplySmartShowcaseSize(control, resolvedSize)
        controls.Add(control)
        page.Add(control, resolvedSize)
    End Sub

    Private Function ResolveSmartGuidedStageSize(control As MASControlBase,
                                                 sizeIntent As MASSize) As MASSize
        If control Is Nothing Then Return MASSize.[Default]

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

        If TypeOf control Is MASLabel Then Return MASSize.FillWidth
        If IsSmartNaturalControl(control) Then Return MASSize.[Default]
        If TypeOf control Is MASDataGrid OrElse TypeOf control Is MASTreeGrid OrElse TypeOf control Is MASPivotTable Then Return MASSize.FillWidth
        If IsSmartSelfSizedSurface(control) Then Return MASSize.[Default]
        Return MASSize.FillWidth
    End Function

    Private Sub FlushGuidedStageInlineRow(page As MASApplicationLayoutPageBuilder)
        If page Is Nothing OrElse _guidedInlineRow Is Nothing OrElse _guidedInlineRow.Count = 0 Then Return

        Dim flow As MASApplicationLayoutFlowBuilder = page.Flow().Gap(MASLayoutSpacing.Small).IntrinsicItemSize()
        For Each item As MASControlBase In _guidedInlineRow
            If item Is Nothing Then Continue For
            flow.Add(item, MASSize.[Default])
        Next
        _guidedInlineRow.Clear()
    End Sub

    Private Sub RegisterGuidedStageFieldRow(page As MASApplicationLayoutPageBuilder,
                                            controls As List(Of MASControlBase),
                                            labelText As String,
                                            input As MASControlBase,
                                            Optional inputSize As MASSize = Nothing,
                                            Optional trailing As MASControlBase = Nothing)
        If page Is Nothing OrElse controls Is Nothing OrElse input Is Nothing Then Return
        FlushGuidedStageInlineRow(page)

        Dim label As MASLabel = CreateMuted(If(labelText, String.Empty))
        controls.Add(label)
        controls.Add(input)
        If trailing IsNot Nothing Then controls.Add(trailing)

        page.FormRow(Sub(row As MASApplicationFormRowLayoutBuilder)
                         row.Label(label).Input(input, If(inputSize, MASSize.Compact)).Gap(MASLayoutSpacing.Medium)
                         If trailing IsNot Nothing Then row.Add(trailing, MASSize.[Default])
                     End Sub)
    End Sub

End Class
