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 MASExpanderLayoutPlan
        Friend Shared ReadOnly Thresholds As New MASResponsiveLayoutThresholds(
            fullWidthDip:=320.0F,
            denseWidthDip:=240.0F,
            compactWidthDip:=160.0F,
            minimalWidthDip:=84.0F,
            fullHeightDip:=180.0F,
            denseHeightDip:=130.0F,
            compactHeightDip:=78.0F,
            minimalHeightDip:=36.0F)

        Friend ReadOnly Property ResponsiveProfile As MASResponsiveLayoutProfile
        Friend ReadOnly Property Dpi As Single
        Friend ReadOnly Property HeaderHeightPx As Single
        Friend ReadOnly Property HeaderHeightDip As Single
        Friend ReadOnly Property HeaderPaddingLeftPx As Single
        Friend ReadOnly Property HeaderPaddingRightPx As Single
        Friend ReadOnly Property GlyphSlotPx As Single
        Friend ReadOnly Property GlyphTextGapPx As Single
        Friend ReadOnly Property GlyphSizePx As Single
        Friend ReadOnly Property GlyphStrokePx As Single
        Friend ReadOnly Property PaddingLeftDip As Single
        Friend ReadOnly Property PaddingTopDip As Single
        Friend ReadOnly Property PaddingRightDip As Single
        Friend ReadOnly Property PaddingBottomDip As Single
        Friend ReadOnly Property SeparatorInsetPx As Single
        Friend ReadOnly Property SeparatorStrokePx As Single

        Private Sub New(profile As MASResponsiveLayoutProfile)
            Dim safeProfile As MASResponsiveLayoutProfile = If(profile, MASResponsiveLayoutResolver.FromLogicalSize(MASSizeLayoutIntegrationContext.Empty, SKSize.Empty, MASSize.Default, Thresholds))
            Dim chromeScale As Single = Clamp(safeProfile.ChromeScale, 0.64F, 1.26F)
            Dim gapScale As Single = Clamp(safeProfile.GapScale, 0.5F, 1.18F)
            Dim contentScale As Single = Clamp(safeProfile.ContentScale, 0.64F, 1.22F)
            Me.ResponsiveProfile = safeProfile
            Me.Dpi = safeProfile.DpiScale
            Me.HeaderHeightDip = ExpanderTokens.HeaderHeightDip * chromeScale
            Me.HeaderHeightPx = Math.Max(22.0F * safeProfile.DpiScale, Me.HeaderHeightDip * safeProfile.DpiScale)
            Me.HeaderPaddingLeftPx = ExpanderTokens.HeaderPaddingLeftDip * safeProfile.DpiScale * chromeScale
            Me.HeaderPaddingRightPx = ExpanderTokens.HeaderPaddingRightDip * safeProfile.DpiScale * chromeScale
            Me.GlyphSlotPx = ExpanderTokens.GlyphSlotDip * safeProfile.DpiScale * contentScale
            Me.GlyphTextGapPx = ExpanderTokens.GlyphTextGapDip * safeProfile.DpiScale * gapScale
            Me.GlyphSizePx = Math.Max(6.0F * safeProfile.DpiScale, ExpanderTokens.GlyphSizeDip * safeProfile.DpiScale * contentScale)
            Me.GlyphStrokePx = Math.Max(1.0F, ExpanderTokens.GlyphStrokeDip * safeProfile.DpiScale * chromeScale)
            Me.PaddingLeftDip = ExpanderTokens.PaddingLeftDip * chromeScale
            Me.PaddingTopDip = ExpanderTokens.PaddingTopDip * chromeScale
            Me.PaddingRightDip = ExpanderTokens.PaddingRightDip * chromeScale
            Me.PaddingBottomDip = ExpanderTokens.PaddingBottomDip * chromeScale
            Me.SeparatorInsetPx = ExpanderTokens.SeparatorInsetDip * safeProfile.DpiScale * gapScale
            Me.SeparatorStrokePx = Math.Max(1.0F, ExpanderTokens.SeparatorStrokeDip * safeProfile.DpiScale * chromeScale)
        End Sub

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

        Friend Shared Function CreateForLogical(availableSizeDip As SKSize,
                                                sizeIntent As MASSize) As MASExpanderLayoutPlan
            Dim integration As MASSizeLayoutIntegrationContext = MASSizeLayoutIntegrationGateway.Create(Nothing, MASRuntimeSizeProfileService.Current, 1.0F)
            Return New MASExpanderLayoutPlan(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
