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 MASRating. It keeps the public rating
    ''' size contract in MASSizeResolver while adapting header, value text, star pitch,
    ''' and hit rectangles to the runtime size profile and available bounds.
    ''' </summary>
    Friend NotInheritable Class MASRatingLayoutPlan
        Friend Shared ReadOnly Thresholds As New MASResponsiveLayoutThresholds(
            fullWidthDip:=RatingTokens.PreferredWidthDip,
            fullHeightDip:=RatingTokens.PreferredHeightDip,
            denseWidthDip:=210.0F,
            denseHeightDip:=74.0F,
            compactWidthDip:=160.0F,
            compactHeightDip:=58.0F,
            minimalWidthDip:=112.0F,
            minimalHeightDip:=42.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 HeaderHeightPx As Single
        Friend ReadOnly Property StarSizePx As Single
        Friend ReadOnly Property StarGapPx As Single
        Friend ReadOnly Property StarStrokePx As Single
        Friend ReadOnly Property ShowHeader As Boolean
        Friend ReadOnly Property ShowValueText As Boolean

        Private Sub New(profile As MASResponsiveLayoutProfile,
                        paddingPx As Single,
                        headerHeightPx As Single,
                        starSizePx As Single,
                        starGapPx As Single,
                        starStrokePx As Single,
                        showHeader As Boolean,
                        showValueText 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.HeaderHeightPx = PositiveOrZero(headerHeightPx)
            Me.StarSizePx = Math.Max(12.0F, PositiveOrDefault(starSizePx, 12.0F))
            Me.StarGapPx = PositiveOrZero(starGapPx)
            Me.StarStrokePx = Math.Max(1.0F, PositiveOrDefault(starStrokePx, 1.0F))
            Me.ShowHeader = showHeader
            Me.ShowValueText = showValueText
        End Sub

        Friend Shared Function Create(profile As MASResponsiveLayoutProfile,
                                      maximum As Integer) As MASRatingLayoutPlan
            Dim safeProfile As MASResponsiveLayoutProfile = If(profile, MASResponsiveLayoutResolver.FromLogicalSize(MASSizeLayoutIntegrationContext.Empty, SKSize.Empty, MASSize.Default, Thresholds))
            Dim safeMax As Integer = MASRatingPolicy.NormalizeMaximum(maximum)
            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 showHeader As Boolean = safeProfile.Mode <> MASResponsiveLayoutMode.Collapsed AndAlso safeProfile.AvailableSizeDip.Height >= 52.0F
            Dim gapPx As Single = RatingTokens.StarGapDip * gapDpi
            Dim baseStarPx As Single = RatingTokens.StarSizeDip * contentDpi
            Dim availableTrackPx As Single = Math.Max(1.0F, safeProfile.AvailableBoundsPx.Width - 2.0F * RatingTokens.PaddingDip * gapDpi)
            Dim fittingStarPx As Single = (availableTrackPx - CSng(Math.Max(0, safeMax - 1)) * gapPx) / CSng(Math.Max(1, safeMax))
            Dim starPx As Single = Math.Max(12.0F, Math.Min(baseStarPx, fittingStarPx))

            Return New MASRatingLayoutPlan(
                profile:=safeProfile,
                paddingPx:=RatingTokens.PaddingDip * gapDpi,
                headerHeightPx:=If(showHeader, RatingTokens.HeaderHeightDip * chromeDpi, 0.0F),
                starSizePx:=starPx,
                starGapPx:=gapPx,
                starStrokePx:=RatingTokens.StarStrokeDip * chromeDpi,
                showHeader:=showHeader,
                showValueText:=safeProfile.Mode = MASResponsiveLayoutMode.Full OrElse safeProfile.Mode = MASResponsiveLayoutMode.Dense)
        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
