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 MASCalendarView. It translates the
    ''' official runtime size profile and current bounds into inner month-grid chrome.
    ''' </summary>
    Friend NotInheritable Class MASCalendarViewLayoutPlan
        Friend Shared ReadOnly Thresholds As New MASResponsiveLayoutThresholds(
            fullWidthDip:=CalendarViewTokens.PreferredWidthDip,
            fullHeightDip:=CalendarViewTokens.PreferredHeightDip,
            denseWidthDip:=340.0F,
            denseHeightDip:=320.0F,
            compactWidthDip:=260.0F,
            compactHeightDip:=250.0F,
            minimalWidthDip:=190.0F,
            minimalHeightDip:=190.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 WeekdayHeightPx As Single
        Friend ReadOnly Property GridGapPx As Single
        Friend ReadOnly Property CellRadiusPx As Single
        Friend ReadOnly Property NavButtonSizePx As Single
        Friend ReadOnly Property ShowHeader As Boolean
        Friend ReadOnly Property ShowHeaderTitle As Boolean
        Friend ReadOnly Property ShowWeekdays As Boolean
        Friend ReadOnly Property UseShortWeekdayLabels As Boolean

        Private Sub New(profile As MASResponsiveLayoutProfile,
                        paddingPx As Single,
                        headerHeightPx As Single,
                        weekdayHeightPx As Single,
                        gridGapPx As Single,
                        cellRadiusPx As Single,
                        navButtonSizePx As Single,
                        showHeader As Boolean,
                        showHeaderTitle As Boolean,
                        showWeekdays As Boolean,
                        useShortWeekdayLabels 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.WeekdayHeightPx = PositiveOrZero(weekdayHeightPx)
            Me.GridGapPx = PositiveOrZero(gridGapPx)
            Me.CellRadiusPx = PositiveOrZero(cellRadiusPx)
            Me.NavButtonSizePx = PositiveOrZero(navButtonSizePx)
            Me.ShowHeader = showHeader
            Me.ShowHeaderTitle = showHeaderTitle
            Me.ShowWeekdays = showWeekdays
            Me.UseShortWeekdayLabels = useShortWeekdayLabels
        End Sub

        Friend Shared Function Create(profile As MASResponsiveLayoutProfile) As MASCalendarViewLayoutPlan
            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 showHeader As Boolean = safeProfile.Mode <> MASResponsiveLayoutMode.Collapsed
            Dim showTitle As Boolean = safeProfile.Mode = MASResponsiveLayoutMode.Full OrElse safeProfile.Mode = MASResponsiveLayoutMode.Dense OrElse safeProfile.AvailableSizeDip.Width >= 230.0F
            Dim showWeekdays As Boolean = safeProfile.Mode <> MASResponsiveLayoutMode.Collapsed
            Dim shortLabels As Boolean = safeProfile.Mode <> MASResponsiveLayoutMode.Full

            Return New MASCalendarViewLayoutPlan(
                profile:=safeProfile,
                paddingPx:=CalendarViewTokens.PaddingDip * gapDpi,
                headerHeightPx:=If(showHeader, CalendarViewTokens.HeaderHeightDip * chromeDpi, 0.0F),
                weekdayHeightPx:=If(showWeekdays, CalendarViewTokens.WeekdayHeightDip * contentDpi, 0.0F),
                gridGapPx:=CalendarViewTokens.GridGapDip * gapDpi,
                cellRadiusPx:=CalendarViewTokens.CellRadiusDip * chromeDpi,
                navButtonSizePx:=CalendarViewTokens.NavButtonSizeDip * chromeDpi,
                showHeader:=showHeader,
                showHeaderTitle:=showTitle,
                showWeekdays:=showWeekdays,
                useShortWeekdayLabels:=shortLabels)
        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
