Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.Controls

    Friend NotInheritable Class MASTooltipLayoutPlan
        Friend Shared ReadOnly Thresholds As New MASResponsiveLayoutThresholds(
            fullWidthDip:=TooltipTokens.MaxWidth,
            denseWidthDip:=220.0F,
            compactWidthDip:=150.0F,
            minimalWidthDip:=78.0F,
            fullHeightDip:=96.0F,
            denseHeightDip:=72.0F,
            compactHeightDip:=48.0F,
            minimalHeightDip:=24.0F)

        Friend ReadOnly Property ResponsiveProfile As MASResponsiveLayoutProfile
        Friend ReadOnly Property Dpi As Single
        Friend ReadOnly Property MetricDpi As Single
        Friend ReadOnly Property PaddingXPx As Single
        Friend ReadOnly Property PaddingYPx As Single
        Friend ReadOnly Property ScreenGapPx As Single
        Friend ReadOnly Property MinWidthPx As Single
        Friend ReadOnly Property MaxWidthPx As Single
        Friend ReadOnly Property MinTextWidthPx As Single
        Friend ReadOnly Property MaxLines As Integer
        Friend ReadOnly Property ArrowSizePx As Single

        Private Sub New(profile As MASResponsiveLayoutProfile)
            Dim safeProfile As MASResponsiveLayoutProfile = If(profile, MASResponsiveLayoutResolver.FromLogicalSize(MASSizeLayoutIntegrationContext.Empty, SKSize.Empty, MASSize.Default, Thresholds))
            Dim chrome As Single = ClampScale(safeProfile.ChromeScale, 0.66F, 1.22F)
            Dim gap As Single = ClampScale(safeProfile.GapScale, 0.56F, 1.16F)
            Dim content As Single = ClampScale(safeProfile.ContentScale, 0.66F, 1.22F)
            If safeProfile.Mode = MASResponsiveLayoutMode.Collapsed Then
                chrome = Math.Min(chrome, 0.72F)
                gap = Math.Min(gap, 0.62F)
                content = Math.Min(content, 0.74F)
            End If
            Me.ResponsiveProfile = safeProfile
            Me.Dpi = safeProfile.DpiScale
            Me.MetricDpi = safeProfile.DpiScale * content
            Me.PaddingXPx = TooltipTokens.PaddingX * safeProfile.DpiScale * chrome
            Me.PaddingYPx = TooltipTokens.PaddingY * safeProfile.DpiScale * chrome
            Me.ScreenGapPx = TooltipTokens.ScreenGap * safeProfile.DpiScale * gap
            Me.MinWidthPx = TooltipTokens.MinWidth * safeProfile.DpiScale * chrome
            Me.MaxWidthPx = TooltipTokens.MaxWidth * safeProfile.DpiScale * content
            Me.MinTextWidthPx = TooltipTokens.MinTextWidth * safeProfile.DpiScale * content
            Me.MaxLines = If(safeProfile.Mode = MASResponsiveLayoutMode.Collapsed, 2, If(safeProfile.Mode = MASResponsiveLayoutMode.Minimal, 3, TooltipTokens.MaxLines))
            Me.ArrowSizePx = PixelSnap.SafeRadius(Math.Max(4.0F, 7.0F * safeProfile.DpiScale * chrome))
        End Sub

        Friend Shared Function Create(ctx As MASThemeContext,
                                      availableBoundsPx As SKRect,
                                      sizeIntent As MASSize) As MASTooltipLayoutPlan
            Return New MASTooltipLayoutPlan(MASResponsiveLayoutResolver.FromTheme(ctx, availableBoundsPx, sizeIntent, Thresholds))
        End Function

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

End Namespace
