Option Strict On
Option Explicit On

Imports Nexamas.UI.Layout

Namespace Nexamas.UI.Application

    ''' <summary>
    ''' Immutable presentation profile for a host application window.
    ''' It does not arrange child controls; it only describes the outer window scale that Nexamas UI should plan safely.
    ''' </summary>
    Friend NotInheritable Class MASWindowPresentationProfile

        Private Shared ReadOnly _utility As New MASWindowPresentationProfile(MASWindowPresentationProfileKind.Utility,
                                                                             MASSizeProfileKind.Compact,
                                                                             MASWindowPresentationSizeTokens.UtilityWidthRatio,
                                                                             MASWindowPresentationSizeTokens.UtilityHeightRatio,
                                                                             MASWindowPresentationSizeTokens.UtilityMinWidth,
                                                                             MASWindowPresentationSizeTokens.UtilityMinHeight)

        Private Shared ReadOnly _compact As New MASWindowPresentationProfile(MASWindowPresentationProfileKind.Compact,
                                                                             MASSizeProfileKind.Compact,
                                                                             MASWindowPresentationSizeTokens.CompactWidthRatio,
                                                                             MASWindowPresentationSizeTokens.CompactHeightRatio,
                                                                             MASWindowPresentationSizeTokens.CompactMinWidth,
                                                                             MASWindowPresentationSizeTokens.CompactMinHeight)

        Private Shared ReadOnly _standard As New MASWindowPresentationProfile(MASWindowPresentationProfileKind.Standard,
                                                                              MASSizeProfileKind.[Default],
                                                                              MASWindowPresentationSizeTokens.StandardWidthRatio,
                                                                              MASWindowPresentationSizeTokens.StandardHeightRatio,
                                                                              MASWindowPresentationSizeTokens.StandardMinWidth,
                                                                              MASWindowPresentationSizeTokens.StandardMinHeight)

        Private Shared ReadOnly _spacious As New MASWindowPresentationProfile(MASWindowPresentationProfileKind.Spacious,
                                                                              MASSizeProfileKind.Comfortable,
                                                                              MASWindowPresentationSizeTokens.SpaciousWidthRatio,
                                                                              MASWindowPresentationSizeTokens.SpaciousHeightRatio,
                                                                              MASWindowPresentationSizeTokens.SpaciousMinWidth,
                                                                              MASWindowPresentationSizeTokens.SpaciousMinHeight)

        Private Shared ReadOnly _productive As New MASWindowPresentationProfile(MASWindowPresentationProfileKind.Productive,
                                                                               MASSizeProfileKind.Comfortable,
                                                                               MASWindowPresentationSizeTokens.ProductiveWidthRatio,
                                                                               MASWindowPresentationSizeTokens.ProductiveHeightRatio,
                                                                               MASWindowPresentationSizeTokens.ProductiveMinWidth,
                                                                               MASWindowPresentationSizeTokens.ProductiveMinHeight)

        Private Shared ReadOnly _immersive As New MASWindowPresentationProfile(MASWindowPresentationProfileKind.Immersive,
                                                                              MASSizeProfileKind.Large,
                                                                              MASWindowPresentationSizeTokens.ImmersiveWidthRatio,
                                                                              MASWindowPresentationSizeTokens.ImmersiveHeightRatio,
                                                                              MASWindowPresentationSizeTokens.ImmersiveMinWidth,
                                                                              MASWindowPresentationSizeTokens.ImmersiveMinHeight)

        Private Sub New(kind As MASWindowPresentationProfileKind,
                        sizeProfileKind As MASSizeProfileKind,
                        widthRatio As Single,
                        heightRatio As Single,
                        minimumWidth As Integer,
                        minimumHeight As Integer)
            Me.Kind = kind
            Me.SizeProfileKind = sizeProfileKind
            Me.WidthRatio = SanitizeRatio(widthRatio)
            Me.HeightRatio = SanitizeRatio(heightRatio)
            Me.MinimumWidth = Math.Max(MASWindowPresentationSizeTokens.MinimumVisibleWidth, minimumWidth)
            Me.MinimumHeight = Math.Max(MASWindowPresentationSizeTokens.MinimumVisibleHeight, minimumHeight)
        End Sub

        Friend Shared ReadOnly Property Utility As MASWindowPresentationProfile
            Get
                Return _utility
            End Get
        End Property

        Friend Shared ReadOnly Property Compact As MASWindowPresentationProfile
            Get
                Return _compact
            End Get
        End Property

        Friend Shared ReadOnly Property Standard As MASWindowPresentationProfile
            Get
                Return _standard
            End Get
        End Property

        Friend Shared ReadOnly Property Spacious As MASWindowPresentationProfile
            Get
                Return _spacious
            End Get
        End Property

        Friend Shared ReadOnly Property Productive As MASWindowPresentationProfile
            Get
                Return _productive
            End Get
        End Property

        Friend Shared ReadOnly Property Immersive As MASWindowPresentationProfile
            Get
                Return _immersive
            End Get
        End Property

        Friend ReadOnly Property Kind As MASWindowPresentationProfileKind
        Friend ReadOnly Property SizeProfileKind As MASSizeProfileKind

        Friend ReadOnly Property WidthRatio As Single
        Friend ReadOnly Property HeightRatio As Single
        Friend ReadOnly Property MinimumWidth As Integer
        Friend ReadOnly Property MinimumHeight As Integer

        Friend Shared Function FromKind(kind As MASWindowPresentationProfileKind) As MASWindowPresentationProfile
            Select Case kind
                Case MASWindowPresentationProfileKind.Utility
                    Return Utility
                Case MASWindowPresentationProfileKind.Compact
                    Return Compact
                Case MASWindowPresentationProfileKind.Spacious
                    Return Spacious
                Case MASWindowPresentationProfileKind.Productive
                    Return Productive
                Case MASWindowPresentationProfileKind.Immersive
                    Return Immersive
                Case Else
                    Return Standard
            End Select
        End Function

        Friend Shared Function Normalize(profile As MASWindowPresentationProfile) As MASWindowPresentationProfile
            If profile Is Nothing Then Return Standard
            Return profile
        End Function

        Private Shared Function SanitizeRatio(value As Single) As Single
            If Single.IsNaN(value) OrElse Single.IsInfinity(value) OrElse value <= 0.0F Then Return 0.7F
            Return Math.Max(0.2F, Math.Min(0.98F, value))
        End Function

    End Class

End Namespace
