Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.Layout
Imports Nexamas.UI.Values
Imports SkiaSharp

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Render-only responsive layout facts for MASDashboardGrid. It keeps the public
    ''' size contract in MASSizeResolver while adapting header/footer chrome, cell
    ''' density, columns, spans, and widget details to the runtime size profile.
    ''' </summary>
    Friend NotInheritable Class MASDashboardGridLayoutPlan
        Friend Shared ReadOnly Thresholds As New MASResponsiveLayoutThresholds(
            fullWidthDip:=DashboardGridTokens.PreferredWidthDip,
            fullHeightDip:=DashboardGridTokens.PreferredHeightDip,
            denseWidthDip:=560.0F,
            denseHeightDip:=380.0F,
            compactWidthDip:=360.0F,
            compactHeightDip:=250.0F,
            minimalWidthDip:=220.0F,
            minimalHeightDip:=150.0F)

        Friend ReadOnly Property ResponsiveProfile As MASResponsiveLayoutProfile
        Friend ReadOnly Property Mode As MASResponsiveLayoutMode
        Friend ReadOnly Property Dpi As Single
        Friend ReadOnly Property PaddingPx As Single
        Friend ReadOnly Property HeaderHeightPx As Single
        Friend ReadOnly Property FooterHeightPx As Single
        Friend ReadOnly Property CellHeightPx As Single
        Friend ReadOnly Property CellGapPx As Single
        Friend ReadOnly Property WidgetRadiusPx As Single
        Friend ReadOnly Property WidgetPaddingPx As Single
        Friend ReadOnly Property AccentRailWidthPx As Single
        Friend ReadOnly Property MaxVisibleWidgets As Integer
        Friend ReadOnly Property MaxColumns As Integer
        Friend ReadOnly Property MinColumns As Integer
        Friend ReadOnly Property ShowHeader As Boolean
        Friend ReadOnly Property ShowSummary As Boolean
        Friend ReadOnly Property ShowFooter As Boolean
        Friend ReadOnly Property ShowCaption As Boolean
        Friend ReadOnly Property ShowAccentRail As Boolean

        Private Sub New(profile As MASResponsiveLayoutProfile,
                        paddingPx As Single,
                        headerHeightPx As Single,
                        footerHeightPx As Single,
                        cellHeightPx As Single,
                        cellGapPx As Single,
                        widgetRadiusPx As Single,
                        widgetPaddingPx As Single,
                        accentRailWidthPx As Single,
                        maxVisibleWidgets As Integer,
                        maxColumns As Integer,
                        minColumns As Integer,
                        showHeader As Boolean,
                        showSummary As Boolean,
                        showFooter As Boolean,
                        showCaption As Boolean,
                        showAccentRail As Boolean)
            Dim safeProfile As MASResponsiveLayoutProfile = If(profile, MASResponsiveLayoutResolver.FromLogicalSize(MASSizeLayoutIntegrationContext.Empty, SKSize.Empty, MASSize.Default, Thresholds))
            Me.ResponsiveProfile = safeProfile
            Me.Mode = safeProfile.Mode
            Me.Dpi = Math.Max(0.64F, PositiveOrDefault(safeProfile.DpiScale, 1.0F))
            Me.PaddingPx = PositiveOrZero(paddingPx)
            Me.HeaderHeightPx = PositiveOrZero(headerHeightPx)
            Me.FooterHeightPx = PositiveOrZero(footerHeightPx)
            Me.CellHeightPx = Math.Max(54.0F, PositiveOrDefault(cellHeightPx, 54.0F))
            Me.CellGapPx = PositiveOrZero(cellGapPx)
            Me.WidgetRadiusPx = PositiveOrZero(widgetRadiusPx)
            Me.WidgetPaddingPx = PositiveOrZero(widgetPaddingPx)
            Me.AccentRailWidthPx = PositiveOrZero(accentRailWidthPx)
            Me.MaxVisibleWidgets = Math.Max(1, maxVisibleWidgets)
            Me.MaxColumns = Math.Max(1, maxColumns)
            Me.MinColumns = Math.Max(1, Math.Min(minColumns, Me.MaxColumns))
            Me.ShowHeader = showHeader
            Me.ShowSummary = showSummary
            Me.ShowFooter = showFooter
            Me.ShowCaption = showCaption
            Me.ShowAccentRail = showAccentRail
        End Sub

        Friend Shared Function Create(profile As MASResponsiveLayoutProfile) As MASDashboardGridLayoutPlan
            Dim safeProfile As MASResponsiveLayoutProfile = If(profile, MASResponsiveLayoutResolver.FromLogicalSize(MASSizeLayoutIntegrationContext.Empty, SKSize.Empty, MASSize.Default, Thresholds))
            Dim dpi As Single = Math.Max(0.64F, safeProfile.DpiScale)
            Dim chromeDpi As Single = Math.Max(0.64F, dpi * safeProfile.ChromeScale)
            Dim gapDpi As Single = Math.Max(0.64F, dpi * safeProfile.GapScale)
            Dim contentDpi As Single = Math.Max(0.64F, dpi * safeProfile.ContentScale)
            Dim maxColumns As Integer
            Select Case safeProfile.Mode
                Case MASResponsiveLayoutMode.Full
                    maxColumns = DashboardGridTokens.MaxColumns
                Case MASResponsiveLayoutMode.Dense
                    maxColumns = Math.Min(3, DashboardGridTokens.MaxColumns)
                Case MASResponsiveLayoutMode.Compact
                    maxColumns = 2
                Case Else
                    maxColumns = 1
            End Select

            Return New MASDashboardGridLayoutPlan(
                profile:=safeProfile,
                paddingPx:=DashboardGridTokens.PaddingDip * gapDpi,
                headerHeightPx:=If(safeProfile.Mode <> MASResponsiveLayoutMode.Collapsed, DashboardGridTokens.HeaderHeightDip * chromeDpi, 0.0F),
                footerHeightPx:=If(safeProfile.Mode = MASResponsiveLayoutMode.Full OrElse safeProfile.Mode = MASResponsiveLayoutMode.Dense, DashboardGridTokens.FooterHeightDip * chromeDpi, 0.0F),
                cellHeightPx:=DashboardGridTokens.CellHeightDip * contentDpi,
                cellGapPx:=DashboardGridTokens.CellGapDip * gapDpi,
                widgetRadiusPx:=DashboardGridTokens.WidgetRadiusDip * chromeDpi,
                widgetPaddingPx:=DashboardGridTokens.WidgetPaddingDip * gapDpi,
                accentRailWidthPx:=DashboardGridTokens.AccentRailWidthDip * chromeDpi,
                maxVisibleWidgets:=If(safeProfile.IsCompactOrSmaller, 8, DashboardGridTokens.MaxVisibleWidgets),
                maxColumns:=maxColumns,
                minColumns:=If(maxColumns = 1, 1, DashboardGridTokens.MinColumns),
                showHeader:=safeProfile.Mode <> MASResponsiveLayoutMode.Collapsed,
                showSummary:=safeProfile.Mode = MASResponsiveLayoutMode.Full AndAlso safeProfile.AvailableSizeDip.Width >= 520.0F,
                showFooter:=safeProfile.Mode = MASResponsiveLayoutMode.Full OrElse safeProfile.Mode = MASResponsiveLayoutMode.Dense,
                showCaption:=Not safeProfile.IsCompactOrSmaller,
                showAccentRail:=Not safeProfile.IsMinimalOrSmaller)
        End Function

        Friend Function ResolveColumnCount(widthPx As Single) As Integer
            If widthPx <= 1.0F Then Return 1
            Dim dipWidth As Single = widthPx / Math.Max(0.64F, Dpi)
            Dim estimated As Integer = CInt(Math.Floor(dipWidth / DashboardGridTokens.CellMinWidthDip))
            If estimated < MinColumns Then estimated = MinColumns
            If estimated > MaxColumns Then estimated = MaxColumns
            Return Math.Max(1, estimated)
        End Function

        Private Shared Function PositiveOrDefault(value As Single, fallback As Single) As Single
            If Single.IsNaN(value) OrElse Single.IsInfinity(value) OrElse value <= 0.0F Then Return fallback
            Return value
        End Function

        Private Shared Function PositiveOrZero(value As Single) As Single
            If Single.IsNaN(value) OrElse Single.IsInfinity(value) OrElse value < 0.0F Then Return 0.0F
            Return value
        End Function
    End Class

End Namespace
