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 MASMenuBarLayoutPlan
        Friend Shared ReadOnly Thresholds As New MASResponsiveLayoutThresholds(
            fullWidthDip:=MenuTokens.MenuBarMaxWidthDip,
            denseWidthDip:=360.0F,
            compactWidthDip:=MenuTokens.MenuBarMinWidthDip,
            minimalWidthDip:=128.0F,
            fullHeightDip:=Nexamas.UI.Layout.MASComponentSizeContractTokens.ApplicationMenuBarDefaultHeight,
            denseHeightDip:=MenuTokens.MenuBarMinHeightDip,
            compactHeightDip:=22.0F,
            minimalHeightDip:=14.0F)

        Friend ReadOnly Property ResponsiveProfile As MASResponsiveLayoutProfile
        Friend ReadOnly Property PixelDpi As Single
        Friend ReadOnly Property MetricDpi As Single
        Friend ReadOnly Property ContentRectPx As SKRect

        Private Sub New(profile As MASResponsiveLayoutProfile,
                        metricDpi As Single,
                        contentRectPx As SKRect)
            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.ContentRectPx = If(contentRectPx.Width > 1.0F AndAlso contentRectPx.Height > 1.0F, contentRectPx, SKRect.Empty)
        End Sub

        Friend Shared Function Create(ctx As MASThemeContext,
                                      boundsPx As SKRect,
                                      sizeIntent As MASSize) As MASMenuBarLayoutPlan
            Dim profile As MASResponsiveLayoutProfile = MASResponsiveLayoutResolver.FromTheme(ctx, boundsPx, sizeIntent, Thresholds)
            Dim metricDpi As Single = profile.DpiScale * ClampScale(profile.ContentScale, 0.66F, 1.2F)
            Dim chromeDpi As Single = profile.DpiScale * ClampScale(profile.ChromeScale, 0.62F, 1.18F)
            Dim content As New SKRect(boundsPx.Left + MenuTokens.MenuBarPaddingHorizontalDip * chromeDpi,
                                      boundsPx.Top + MenuTokens.MenuBarPaddingVerticalDip * chromeDpi,
                                      boundsPx.Right - MenuTokens.MenuBarPaddingHorizontalDip * chromeDpi,
                                      boundsPx.Bottom - MenuTokens.MenuBarPaddingVerticalDip * chromeDpi)
            If profile.IsCollapsed Then content = SKRect.Empty
            Return New MASMenuBarLayoutPlan(profile, metricDpi, content)
        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
