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 MASFilePickerResponsiveLayoutPlan
        Friend Shared ReadOnly Thresholds As New MASResponsiveLayoutThresholds(
            fullWidthDip:=720.0F,
            denseWidthDip:=520.0F,
            compactWidthDip:=360.0F,
            minimalWidthDip:=220.0F,
            fullHeightDip:=520.0F,
            denseHeightDip:=360.0F,
            compactHeightDip:=240.0F,
            minimalHeightDip:=140.0F)

        Friend ReadOnly Property ResponsiveProfile As MASResponsiveLayoutProfile
        Friend ReadOnly Property LayoutScale As Single

        Private Sub New(profile As MASResponsiveLayoutProfile,
                        layoutScale As Single)
            Dim safeProfile As MASResponsiveLayoutProfile = If(profile, MASResponsiveLayoutResolver.FromLogicalSize(MASSizeLayoutIntegrationContext.Empty, SKSize.Empty, MASSize.Default, Thresholds))
            Me.ResponsiveProfile = safeProfile
            Me.LayoutScale = ClampScale(layoutScale, 0.62F, 1.22F)
        End Sub

        Friend Shared Function Create(ctx As MASThemeContext,
                                      boundsPx As SKRect,
                                      sizeIntent As MASSize) As MASFilePickerResponsiveLayoutPlan
            Dim profile As MASResponsiveLayoutProfile = MASResponsiveLayoutResolver.FromTheme(ctx, boundsPx, sizeIntent, Thresholds)
            Dim scale As Single = (profile.ChromeScale + profile.GapScale + profile.ContentScale) / 3.0F
            If profile.IsCollapsed Then scale = Math.Min(scale, 0.66F)
            Return New MASFilePickerResponsiveLayoutPlan(profile, scale)
        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
