Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.Components

    Friend NotInheritable Class MASDatePickerCalendarLayoutPlan
        Friend Shared ReadOnly Thresholds As New MASResponsiveLayoutThresholds(
            fullWidthDip:=DatePickerTokens.CalendarPopupWidthDip,
            denseWidthDip:=260.0F,
            compactWidthDip:=220.0F,
            minimalWidthDip:=172.0F,
            fullHeightDip:=DatePickerTokens.CalendarPopupHeightDip,
            denseHeightDip:=284.0F,
            compactHeightDip:=226.0F,
            minimalHeightDip:=156.0F)

        Friend ReadOnly Property ResponsiveProfile As MASResponsiveLayoutProfile
        Friend ReadOnly Property Dpi As Single
        Friend ReadOnly Property MetricDpi As Single
        Friend ReadOnly Property OuterPadPx As Single
        Friend ReadOnly Property InnerPadPx As Single
        Friend ReadOnly Property HeaderHeightPx As Single
        Friend ReadOnly Property WeekdayHeightPx As Single
        Friend ReadOnly Property CellGapPx As Single
        Friend ReadOnly Property CellMinHeightPx As Single
        Friend ReadOnly Property NavButtonSizePx As Single
        Friend ReadOnly Property SelectedRadiusPx As Single
        Friend ReadOnly Property RingStrokePx As Single
        Friend ReadOnly Property IconStrokePx As Single
        Friend ReadOnly Property MonthSelectorHeightPx As Single
        Friend ReadOnly Property SelectorLabelHeightPx As Single
        Friend ReadOnly Property ShowWeekdays As Boolean
        Friend ReadOnly Property IsCollapsed As Boolean

        Private Sub New(profile As MASResponsiveLayoutProfile)
            Dim safeProfile As MASResponsiveLayoutProfile = If(profile, MASResponsiveLayoutResolver.FromLogicalSize(MASSizeLayoutIntegrationContext.Empty, SKSize.Empty, MASSize.Default, Thresholds))
            Dim chrome As Single = ClampScale(safeProfile.ChromeScale, 0.62F, 1.24F)
            Dim gap As Single = ClampScale(safeProfile.GapScale, 0.54F, 1.18F)
            Dim content As Single = ClampScale(safeProfile.ContentScale, 0.62F, 1.22F)
            If safeProfile.Mode = MASResponsiveLayoutMode.Collapsed Then
                chrome = Math.Min(chrome, 0.66F)
                gap = Math.Min(gap, 0.56F)
                content = Math.Min(content, 0.72F)
            End If
            Me.ResponsiveProfile = safeProfile
            Me.Dpi = safeProfile.DpiScale
            Me.MetricDpi = safeProfile.DpiScale * content
            Me.OuterPadPx = Math.Max(2.0F, DatePickerTokens.CalendarOuterPadDip * safeProfile.DpiScale * chrome)
            Me.InnerPadPx = Math.Max(1.0F, DatePickerTokens.CalendarInnerPadDip * safeProfile.DpiScale * gap)
            Me.HeaderHeightPx = Math.Max(16.0F * safeProfile.DpiScale, DatePickerTokens.CalendarHeaderHeightDip * safeProfile.DpiScale * chrome)
            Me.WeekdayHeightPx = Math.Max(12.0F * safeProfile.DpiScale, DatePickerTokens.CalendarWeekdayHeightDip * safeProfile.DpiScale * content)
            Me.CellGapPx = Math.Max(0.0F, DatePickerTokens.CalendarCellGapDip * safeProfile.DpiScale * gap)
            Me.CellMinHeightPx = Math.Max(12.0F * safeProfile.DpiScale, DatePickerTokens.CalendarCellMinHeightDip * safeProfile.DpiScale * content)
            Me.NavButtonSizePx = Math.Max(16.0F * safeProfile.DpiScale, DatePickerTokens.CalendarNavButtonDip * safeProfile.DpiScale * chrome)
            Me.SelectedRadiusPx = Math.Max(3.0F, DatePickerTokens.CalendarSelectedRadiusDip * safeProfile.DpiScale * chrome)
            Me.RingStrokePx = Math.Max(1.0F, DatePickerTokens.CalendarTodayRingDip * safeProfile.DpiScale * chrome)
            Me.IconStrokePx = Math.Max(1.0F, DatePickerTokens.CalendarIconStrokeDip * safeProfile.DpiScale * chrome)
            Me.MonthSelectorHeightPx = Math.Max(52.0F * safeProfile.DpiScale, 88.0F * safeProfile.DpiScale * content)
            Me.SelectorLabelHeightPx = Math.Max(10.0F * safeProfile.DpiScale, 16.0F * safeProfile.DpiScale * content)
            Me.ShowWeekdays = safeProfile.Mode <> MASResponsiveLayoutMode.Collapsed
            Me.IsCollapsed = safeProfile.Mode = MASResponsiveLayoutMode.Collapsed
        End Sub

        Friend Shared Function Create(ctx As MASThemeContext,
                                      availableBoundsPx As SKRect,
                                      sizeIntent As MASSize) As MASDatePickerCalendarLayoutPlan
            Return New MASDatePickerCalendarLayoutPlan(MASResponsiveLayoutResolver.FromTheme(ctx, availableBoundsPx, sizeIntent, Thresholds))
        End Function

        Friend Shared Function Create(profile As MASResponsiveLayoutProfile) As MASDatePickerCalendarLayoutPlan
            Return New MASDatePickerCalendarLayoutPlan(profile)
        End Function

        Private Shared Function ClampScale(value As Single, minimum As Single, maximum As Single) As Single
            If Single.IsNaN(value) OrElse Single.IsInfinity(value) OrElse value <= 0.0F Then Return 1.0F
            Return Math.Min(maximum, Math.Max(minimum, value))
        End Function
    End Class

End Namespace
