Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.Components

    Friend NotInheritable Class MASSplitViewLayoutPlan
        Friend Shared ReadOnly Thresholds As New MASResponsiveLayoutThresholds(
            fullWidthDip:=SplitViewTokens.PreferredWidthDip,
            denseWidthDip:=720.0F,
            compactWidthDip:=SplitViewTokens.MinWidthDip,
            minimalWidthDip:=260.0F,
            fullHeightDip:=SplitViewTokens.PreferredHeightDip,
            denseHeightDip:=SplitViewTokens.MinHeightDip,
            compactHeightDip:=190.0F,
            minimalHeightDip:=110.0F)

        Friend ReadOnly Property ResponsiveProfile As MASResponsiveLayoutProfile
        Friend ReadOnly Property PixelDpi As Single
        Friend ReadOnly Property MetricDpi As Single
        Friend ReadOnly Property PaddingPx As Single

        Private Sub New(profile As MASResponsiveLayoutProfile,
                        metricDpi As Single,
                        paddingPx As Single)
            Dim safeProfile As MASResponsiveLayoutProfile = If(profile, MASResponsiveLayoutResolver.FromLogicalSize(MASSizeLayoutIntegrationContext.Empty, SKSize.Empty, MASSize.Default, Thresholds))
            Me.ResponsiveProfile = safeProfile
            Me.PixelDpi = safeProfile.DpiScale
            Me.MetricDpi = Math.Max(0.25F, metricDpi)
            Me.PaddingPx = Math.Max(0.0F, paddingPx)
        End Sub

        Friend Shared Function Create(ctx As MASThemeContext,
                                      boundsPx As SKRect,
                                      sizeIntent As MASSize) As MASSplitViewLayoutPlan
            Dim profile As MASResponsiveLayoutProfile = MASResponsiveLayoutResolver.FromTheme(ctx, boundsPx, sizeIntent, Thresholds)
            Dim metricDpi As Single = profile.DpiScale * ClampScale(profile.ContentScale, 0.66F, 1.24F)
            Dim paddingPx As Single = SplitViewTokens.PaddingDip * profile.DpiScale * ClampScale(profile.ChromeScale, 0.62F, 1.22F)
            If profile.IsCollapsed Then paddingPx = Math.Min(paddingPx, 4.0F * profile.DpiScale)
            Return New MASSplitViewLayoutPlan(profile, metricDpi, paddingPx)
        End Function

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

End Namespace
