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 MASTimePickerClockLayoutPlan
        Friend Shared ReadOnly Thresholds As New MASResponsiveLayoutThresholds(
            fullWidthDip:=TimePickerTokens.ClockPopupWidthDip,
            denseWidthDip:=260.0F,
            compactWidthDip:=220.0F,
            minimalWidthDip:=172.0F,
            fullHeightDip:=TimePickerTokens.ClockPopupHeightDip,
            denseHeightDip:=220.0F,
            compactHeightDip:=184.0F,
            minimalHeightDip:=126.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 InnerGapPx As Single
        Friend ReadOnly Property HeaderHeightPx As Single
        Friend ReadOnly Property DisplayHeightPx As Single
        Friend ReadOnly Property StepperHeightPx As Single
        Friend ReadOnly Property ColumnGapPx As Single
        Friend ReadOnly Property StepperButtonHeightPx As Single
        Friend ReadOnly Property StepperLabelHeightPx As Single
        Friend ReadOnly Property StepperGapPx As Single
        Friend ReadOnly Property FooterGapPx As Single
        Friend ReadOnly Property FooterButtonHeightPx As Single
        Friend ReadOnly Property ShowHeaderMeta As Boolean
        Friend ReadOnly Property ShowFooterSecondary 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, TimePickerTokens.ClockOuterPadDip * safeProfile.DpiScale * chrome)
            Me.InnerGapPx = Math.Max(1.0F, TimePickerTokens.ClockInnerGapDip * safeProfile.DpiScale * gap)
            Me.HeaderHeightPx = Math.Max(18.0F * safeProfile.DpiScale, TimePickerTokens.ClockHeaderHeightDip * safeProfile.DpiScale * chrome)
            Me.DisplayHeightPx = Math.Max(28.0F * safeProfile.DpiScale, TimePickerTokens.ClockDisplayHeightDip * safeProfile.DpiScale * content)
            Me.StepperHeightPx = Math.Max(48.0F * safeProfile.DpiScale, TimePickerTokens.ClockStepperHeightDip * safeProfile.DpiScale * content)
            Me.ColumnGapPx = Math.Max(2.0F * safeProfile.DpiScale, TimePickerTokens.ClockColumnGapDip * safeProfile.DpiScale * gap)
            Me.StepperButtonHeightPx = Math.Max(18.0F * safeProfile.DpiScale, TimePickerTokens.StepperButtonHeightDip * safeProfile.DpiScale * chrome)
            Me.StepperLabelHeightPx = Math.Max(10.0F * safeProfile.DpiScale, TimePickerTokens.StepperLabelHeightDip * safeProfile.DpiScale * content)
            Me.StepperGapPx = Math.Max(0.0F, TimePickerTokens.StepperGapDip * safeProfile.DpiScale * gap)
            Me.FooterGapPx = Math.Max(1.0F, TimePickerTokens.FooterGapDip * safeProfile.DpiScale * gap)
            Me.FooterButtonHeightPx = Math.Max(20.0F * safeProfile.DpiScale, TimePickerTokens.FooterButtonHeightDip * safeProfile.DpiScale * chrome)
            Me.ShowHeaderMeta = CInt(safeProfile.Mode) <= CInt(MASResponsiveLayoutMode.Compact)
            Me.ShowFooterSecondary = 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 MASTimePickerClockLayoutPlan
            Return New MASTimePickerClockLayoutPlan(MASResponsiveLayoutResolver.FromTheme(ctx, availableBoundsPx, sizeIntent, Thresholds))
        End Function

        Friend Shared Function Create(profile As MASResponsiveLayoutProfile) As MASTimePickerClockLayoutPlan
            Return New MASTimePickerClockLayoutPlan(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
