Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Render-only responsive layout facts for MASMetricCard. The metric card keeps
    ''' its public size contract in MASSizeResolver; this plan owns inner chrome,
    ''' sparkline, subtitle, and trend visibility decisions.
    ''' </summary>
    Friend NotInheritable Class MASMetricCardLayoutPlan
        Friend Shared ReadOnly Thresholds As New MASResponsiveLayoutThresholds(
            fullWidthDip:=MetricCardTokens.PreferredWidthDip,
            fullHeightDip:=MetricCardTokens.PreferredHeightDip,
            denseWidthDip:=240.0F,
            denseHeightDip:=116.0F,
            compactWidthDip:=180.0F,
            compactHeightDip:=84.0F,
            minimalWidthDip:=128.0F,
            minimalHeightDip:=58.0F)

        Friend ReadOnly Property ResponsiveProfile As MASResponsiveLayoutProfile
        Friend ReadOnly Property Mode As MASResponsiveLayoutMode
        Friend ReadOnly Property Dpi As Single
        Friend ReadOnly Property PaddingPx As Single
        Friend ReadOnly Property TextInsetPx As Single
        Friend ReadOnly Property HeaderHeightPx As Single
        Friend ReadOnly Property BodyGapPx As Single
        Friend ReadOnly Property ValueHeightPx As Single
        Friend ReadOnly Property SubtitleHeightPx As Single
        Friend ReadOnly Property SparklineHeightPx As Single
        Friend ReadOnly Property AccentRailWidthPx As Single
        Friend ReadOnly Property TrendPillHeightPx As Single
        Friend ReadOnly Property TrendPillMinWidthPx As Single
        Friend ReadOnly Property TrendPillRadiusPx As Single
        Friend ReadOnly Property ShowHeader As Boolean
        Friend ReadOnly Property ShowTrendPill As Boolean
        Friend ReadOnly Property ShowSubtitle As Boolean
        Friend ReadOnly Property ShowSparkline As Boolean
        Friend ReadOnly Property ShowAccentMarker As Boolean

        Private Sub New(profile As MASResponsiveLayoutProfile,
                        paddingPx As Single,
                        textInsetPx As Single,
                        headerHeightPx As Single,
                        bodyGapPx As Single,
                        valueHeightPx As Single,
                        subtitleHeightPx As Single,
                        sparklineHeightPx As Single,
                        accentRailWidthPx As Single,
                        trendPillHeightPx As Single,
                        trendPillMinWidthPx As Single,
                        trendPillRadiusPx As Single,
                        showHeader As Boolean,
                        showTrendPill As Boolean,
                        showSubtitle As Boolean,
                        showSparkline As Boolean,
                        showAccentMarker As Boolean)
            Dim safeProfile As MASResponsiveLayoutProfile = If(profile, MASResponsiveLayoutResolver.FromLogicalSize(MASSizeLayoutIntegrationContext.Empty, SKSize.Empty, MASSize.Default, Thresholds))
            Me.ResponsiveProfile = safeProfile
            Me.Mode = safeProfile.Mode
            Me.Dpi = Math.Max(0.64F, PositiveOrDefault(safeProfile.DpiScale, 1.0F))
            Me.PaddingPx = PositiveOrZero(paddingPx)
            Me.TextInsetPx = PositiveOrZero(textInsetPx)
            Me.HeaderHeightPx = PositiveOrZero(headerHeightPx)
            Me.BodyGapPx = PositiveOrZero(bodyGapPx)
            Me.ValueHeightPx = Math.Max(18.0F, PositiveOrDefault(valueHeightPx, 18.0F))
            Me.SubtitleHeightPx = PositiveOrZero(subtitleHeightPx)
            Me.SparklineHeightPx = PositiveOrZero(sparklineHeightPx)
            Me.AccentRailWidthPx = PositiveOrZero(accentRailWidthPx)
            Me.TrendPillHeightPx = PositiveOrZero(trendPillHeightPx)
            Me.TrendPillMinWidthPx = PositiveOrZero(trendPillMinWidthPx)
            Me.TrendPillRadiusPx = PositiveOrZero(trendPillRadiusPx)
            Me.ShowHeader = showHeader
            Me.ShowTrendPill = showTrendPill
            Me.ShowSubtitle = showSubtitle
            Me.ShowSparkline = showSparkline
            Me.ShowAccentMarker = showAccentMarker
        End Sub

        Friend Shared Function Create(profile As MASResponsiveLayoutProfile,
                                      isExplicitCompact As Boolean,
                                      showTrend As Boolean,
                                      trendText As String,
                                      subtitle As String,
                                      sparklineSampleCount As Integer) As MASMetricCardLayoutPlan
            Dim safeProfile As MASResponsiveLayoutProfile = If(profile, MASResponsiveLayoutResolver.FromLogicalSize(MASSizeLayoutIntegrationContext.Empty, SKSize.Empty, MASSize.Default, Thresholds))
            Dim dpi As Single = Math.Max(0.64F, safeProfile.DpiScale)
            Dim chromeDpi As Single = Math.Max(0.64F, dpi * safeProfile.ChromeScale)
            Dim gapDpi As Single = Math.Max(0.64F, dpi * safeProfile.GapScale)
            Dim contentDpi As Single = Math.Max(0.64F, dpi * safeProfile.ContentScale)
            Dim compact As Boolean = isExplicitCompact OrElse safeProfile.IsCompactOrSmaller
            Dim minimal As Boolean = safeProfile.IsMinimalOrSmaller
            Dim hasTrend As Boolean = MASMetricCardPolicy.NormalizeTrendVisible(showTrend, trendText)
            Dim hasSubtitle As Boolean = Not String.IsNullOrWhiteSpace(subtitle)

            Return New MASMetricCardLayoutPlan(
                profile:=safeProfile,
                paddingPx:=MetricCardTokens.PaddingDip * gapDpi,
                textInsetPx:=If(minimal, 8.0F, 12.0F) * gapDpi,
                headerHeightPx:=If(safeProfile.Mode = MASResponsiveLayoutMode.Collapsed, 0.0F, MetricCardTokens.HeaderHeightDip * chromeDpi),
                bodyGapPx:=If(minimal, 3.0F, 8.0F) * gapDpi,
                valueHeightPx:=MetricCardTokens.ValueHeightDip * contentDpi,
                subtitleHeightPx:=If(hasSubtitle AndAlso Not minimal, MetricCardTokens.SubtitleHeightDip * contentDpi, 0.0F),
                sparklineHeightPx:=If(sparklineSampleCount >= 2 AndAlso Not compact, MetricCardTokens.SparklineHeightDip * contentDpi, 0.0F),
                accentRailWidthPx:=MetricCardTokens.AccentRailWidthDip * chromeDpi,
                trendPillHeightPx:=MetricCardTokens.TrendPillHeightDip * chromeDpi,
                trendPillMinWidthPx:=MetricCardTokens.TrendPillMinWidthDip * chromeDpi,
                trendPillRadiusPx:=MetricCardTokens.TrendPillRadiusDip * chromeDpi,
                showHeader:=safeProfile.Mode <> MASResponsiveLayoutMode.Collapsed,
                showTrendPill:=hasTrend AndAlso Not compact AndAlso safeProfile.AvailableSizeDip.Width >= 220.0F,
                showSubtitle:=hasSubtitle AndAlso Not minimal,
                showSparkline:=sparklineSampleCount >= 2 AndAlso Not compact,
                showAccentMarker:=Not compact)
        End Function

        Private Shared Function PositiveOrDefault(value As Single, fallback As Single) As Single
            If Single.IsNaN(value) OrElse Single.IsInfinity(value) OrElse value <= 0.0F Then Return fallback
            Return value
        End Function

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