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 MASStepperLayoutPlan
        Friend Shared ReadOnly Thresholds As New MASResponsiveLayoutThresholds(
            fullWidthDip:=420.0F,
            denseWidthDip:=310.0F,
            compactWidthDip:=210.0F,
            minimalWidthDip:=116.0F,
            fullHeightDip:=86.0F,
            denseHeightDip:=70.0F,
            compactHeightDip:=54.0F,
            minimalHeightDip:=32.0F)

        Friend ReadOnly Property ResponsiveProfile As MASResponsiveLayoutProfile
        Friend ReadOnly Property Dpi As Single
        Friend ReadOnly Property CircleDiameterPx As Single
        Friend ReadOnly Property RadiusPx As Single
        Friend ReadOnly Property TextGapPx As Single
        Friend ReadOnly Property TextMaxWidthPx As Single
        Friend ReadOnly Property VerticalStepHeightPx As Single
        Friend ReadOnly Property ConnectorStrokePx As Single
        Friend ReadOnly Property HorizontalEdgeInsetPx As Single
        Friend ReadOnly Property VerticalEdgeInsetPx As Single
        Friend ReadOnly Property ShowDescriptions As Boolean
        Friend ReadOnly Property ShowLabels As Boolean
        Friend ReadOnly Property IsCollapsed As Boolean

        Private Sub New(profile As MASResponsiveLayoutProfile)
            Dim safeProfile As MASResponsiveLayoutProfile = If(profile, MASResponsiveLayoutResolver.FromLogicalSize(MASSizeLayoutIntegrationContext.Empty, SKSize.Empty, MASSize.Default, Thresholds))
            Dim contentScale As Single = Clamp(safeProfile.ContentScale, 0.64F, 1.25F)
            Dim chromeScale As Single = Clamp(safeProfile.ChromeScale, 0.64F, 1.25F)
            Dim gapScale As Single = Clamp(safeProfile.GapScale, 0.5F, 1.2F)
            Me.ResponsiveProfile = safeProfile
            Me.Dpi = safeProfile.DpiScale
            Me.CircleDiameterPx = Math.Max(14.0F * safeProfile.DpiScale, StepperTokens.StepCircleDiameterDip * safeProfile.DpiScale * contentScale)
            Me.RadiusPx = Me.CircleDiameterPx * 0.5F
            Me.TextGapPx = StepperTokens.StepTextGapDip * safeProfile.DpiScale * gapScale
            Me.TextMaxWidthPx = Math.Max(56.0F * safeProfile.DpiScale, StepperTokens.StepTextMaxWidthDip * safeProfile.DpiScale * contentScale)
            Me.VerticalStepHeightPx = Math.Max(Me.CircleDiameterPx + 8.0F * safeProfile.DpiScale, StepperTokens.StepperVerticalStepHeightDip * safeProfile.DpiScale * chromeScale)
            Me.ConnectorStrokePx = Math.Max(1.0F, StepperTokens.StepConnectorStrokeDip * safeProfile.DpiScale * chromeScale)
            Me.HorizontalEdgeInsetPx = Math.Max(3.0F * safeProfile.DpiScale, 6.0F * safeProfile.DpiScale * gapScale)
            Me.VerticalEdgeInsetPx = Math.Max(3.0F * safeProfile.DpiScale, 8.0F * safeProfile.DpiScale * gapScale)
            Me.ShowDescriptions = Not safeProfile.IsCompactOrSmaller
            Me.ShowLabels = Not safeProfile.IsCollapsed
            Me.IsCollapsed = safeProfile.IsCollapsed
        End Sub

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

        Friend Shared Function CreateForLogical(availableSizeDip As SKSize,
                                                sizeIntent As MASSize) As MASStepperLayoutPlan
            Dim integration As MASSizeLayoutIntegrationContext = MASSizeLayoutIntegrationGateway.Create(Nothing, MASRuntimeSizeProfileService.Current, 1.0F)
            Return New MASStepperLayoutPlan(MASResponsiveLayoutResolver.FromLogicalSize(integration, availableSizeDip, sizeIntent, Thresholds))
        End Function

        Private Shared Function Clamp(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
            If value < minValue Then Return minValue
            If value > maxValue Then Return maxValue
            Return value
        End Function
    End Class

End Namespace
