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.Controls

    Friend NotInheritable Class MASStatusBannerLayoutPlan
        Friend Shared ReadOnly Thresholds As New MASResponsiveLayoutThresholds(
            fullWidthDip:=520.0F,
            denseWidthDip:=StatusBannerTokens.MinWidthDip,
            compactWidthDip:=220.0F,
            minimalWidthDip:=128.0F,
            fullHeightDip:=StatusBannerTokens.MinHeightDip,
            denseHeightDip:=72.0F,
            compactHeightDip:=48.0F,
            minimalHeightDip:=28.0F)

        Friend ReadOnly Property ResponsiveProfile As MASResponsiveLayoutProfile
        Friend ReadOnly Property PixelDpi As Single
        Friend ReadOnly Property MetricDpi As Single
        Friend ReadOnly Property ShowIcon As Boolean

        Private Sub New(profile As MASResponsiveLayoutProfile,
                        metricDpi As Single,
                        showIcon As Boolean)
            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.ShowIcon = showIcon
        End Sub

        Friend Shared Function Create(ctx As MASThemeContext,
                                      boundsPx As SKRect,
                                      sizeIntent As MASSize,
                                      requestedIcon As Boolean) As MASStatusBannerLayoutPlan
            Dim profile As MASResponsiveLayoutProfile = MASResponsiveLayoutResolver.FromTheme(ctx, boundsPx, sizeIntent, Thresholds)
            Dim metricDpi As Single = profile.DpiScale * ClampScale((profile.ChromeScale + profile.ContentScale) * 0.5F, 0.66F, 1.22F)
            Dim allowIcon As Boolean = requestedIcon AndAlso Not profile.IsMinimalOrSmaller AndAlso boundsPx.Width >= 210.0F * profile.DpiScale
            Return New MASStatusBannerLayoutPlan(profile, metricDpi, allowIcon)
        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
