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 MASToolbarLayoutPlan
        Friend Shared ReadOnly Thresholds As New MASResponsiveLayoutThresholds(
            fullWidthDip:=360.0F,
            denseWidthDip:=260.0F,
            compactWidthDip:=180.0F,
            minimalWidthDip:=96.0F,
            fullHeightDip:=44.0F,
            denseHeightDip:=36.0F,
            compactHeightDip:=28.0F,
            minimalHeightDip:=18.0F)

        Friend ReadOnly Property ResponsiveProfile As MASResponsiveLayoutProfile
        Friend ReadOnly Property Dpi As Single
        Friend ReadOnly Property MetricDpi As Single
        Friend ReadOnly Property HorizontalPaddingPx As Single
        Friend ReadOnly Property VerticalInsetPx As Single
        Friend ReadOnly Property GapPx As Single
        Friend ReadOnly Property ItemHeightPx As Single
        Friend ReadOnly Property CornerRadiusPx As Single
        Friend ReadOnly Property IsCollapsed As Boolean

        Private Sub New(profile As MASResponsiveLayoutProfile,
                        horizontalPaddingPx As Single,
                        verticalInsetPx As Single,
                        gapPx As Single,
                        itemHeightPx As Single,
                        cornerRadiusPx As Single)
            Dim safeProfile As MASResponsiveLayoutProfile = If(profile, MASResponsiveLayoutResolver.FromLogicalSize(MASSizeLayoutIntegrationContext.Empty, SKSize.Empty, MASSize.Default, Thresholds))
            Me.ResponsiveProfile = safeProfile
            Me.Dpi = safeProfile.DpiScale
            Me.MetricDpi = safeProfile.DpiScale * Math.Max(0.64F, Math.Min(1.28F, safeProfile.ContentScale))
            Me.HorizontalPaddingPx = Math.Max(0.0F, horizontalPaddingPx)
            Me.VerticalInsetPx = Math.Max(0.0F, verticalInsetPx)
            Me.GapPx = Math.Max(0.0F, gapPx)
            Me.ItemHeightPx = Math.Max(1.0F, itemHeightPx)
            Me.CornerRadiusPx = Math.Max(1.0F, cornerRadiusPx)
            Me.IsCollapsed = safeProfile.IsCollapsed
        End Sub

        Friend Shared Function Create(ctx As MASThemeContext,
                                      availableBoundsPx As SKRect,
                                      sizeIntent As MASSize,
                                      horizontalPaddingDip As Single,
                                      verticalInsetDip As Single,
                                      gapDip As Single,
                                      itemHeightDip As Single) As MASToolbarLayoutPlan
            Return Create(MASResponsiveLayoutResolver.FromTheme(ctx, availableBoundsPx, sizeIntent, Thresholds), horizontalPaddingDip, verticalInsetDip, gapDip, itemHeightDip)
        End Function

        Friend Shared Function Create(profile As MASResponsiveLayoutProfile,
                                      horizontalPaddingDip As Single,
                                      verticalInsetDip As Single,
                                      gapDip As Single,
                                      itemHeightDip As Single) As MASToolbarLayoutPlan
            Dim safeProfile As MASResponsiveLayoutProfile = If(profile, MASResponsiveLayoutResolver.FromLogicalSize(MASSizeLayoutIntegrationContext.Empty, SKSize.Empty, MASSize.Default, Thresholds))
            Dim chromeScale As Single = Math.Max(0.64F, Math.Min(1.28F, safeProfile.ChromeScale))
            Dim contentScale As Single = Math.Max(0.64F, Math.Min(1.28F, safeProfile.ContentScale))
            Dim gapScale As Single = Math.Max(0.5F, Math.Min(1.24F, safeProfile.GapScale))

            If safeProfile.Mode = MASResponsiveLayoutMode.Collapsed Then
                chromeScale = Math.Min(chromeScale, 0.68F)
                contentScale = Math.Min(contentScale, 0.68F)
                gapScale = Math.Min(gapScale, 0.58F)
            End If

            Dim itemHeightPx As Single = Math.Max(ToolbarTokens.ItemMinHeight * safeProfile.DpiScale, SanitizeDip(itemHeightDip) * safeProfile.DpiScale * contentScale)
            Return New MASToolbarLayoutPlan(
                profile:=safeProfile,
                horizontalPaddingPx:=SanitizeDip(horizontalPaddingDip) * safeProfile.DpiScale * chromeScale,
                verticalInsetPx:=SanitizeDip(verticalInsetDip) * safeProfile.DpiScale * chromeScale,
                gapPx:=SanitizeDip(gapDip) * safeProfile.DpiScale * gapScale,
                itemHeightPx:=itemHeightPx,
                cornerRadiusPx:=Math.Max(4.0F * safeProfile.DpiScale, 6.0F * safeProfile.DpiScale * chromeScale))
        End Function

        Private Shared Function SanitizeDip(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
