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 MASTimelineLayoutPlan
        Friend Shared ReadOnly Thresholds As New MASResponsiveLayoutThresholds(
            fullWidthDip:=TimelineTokens.PreferredWidthDip,
            denseWidthDip:=520.0F,
            compactWidthDip:=TimelineTokens.MinWidthDip,
            minimalWidthDip:=180.0F,
            fullHeightDip:=360.0F,
            denseHeightDip:=TimelineTokens.MinHeightDip,
            compactHeightDip:=96.0F,
            minimalHeightDip:=48.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
        Friend ReadOnly Property ForceCompactRows As Boolean

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

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