Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.Values

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Public immutable split-view layout snapshot. It captures visual pane text,
    ''' bounded ratios, and collapse state without becoming a dock engine, child host,
    ''' native panel wrapper, shell router, or external storage service.
    ''' </summary>
    Public NotInheritable Class MASSplitViewLayoutSnapshot
        Public Sub New(version As Integer,
                       title As String,
                       leftPaneTitle As String,
                       contentPaneTitle As String,
                       rightPaneTitle As String,
                       leftPaneText As String,
                       contentPaneText As String,
                       rightPaneText As String,
                       leftPaneRatio As Single,
                       rightPaneRatio As Single,
                       leftPaneCollapsed As Boolean,
                       rightPaneCollapsed As Boolean)
            Me.Version = Math.Max(1, version)
            Me.Title = MASSplitViewPolicy.NormalizeTitle(title)
            Me.LeftPaneTitle = MASSplitViewPolicy.NormalizePaneTitle(leftPaneTitle, SplitViewTokens.DefaultLeftPaneTitle)
            Me.ContentPaneTitle = MASSplitViewPolicy.NormalizePaneTitle(contentPaneTitle, SplitViewTokens.DefaultContentPaneTitle)
            Me.RightPaneTitle = MASSplitViewPolicy.NormalizePaneTitle(rightPaneTitle, SplitViewTokens.DefaultRightPaneTitle)
            Me.LeftPaneText = MASSplitViewPolicy.NormalizePaneText(leftPaneText, SplitViewTokens.DefaultLeftPaneText)
            Me.ContentPaneText = MASSplitViewPolicy.NormalizePaneText(contentPaneText, SplitViewTokens.DefaultContentPaneText)
            Me.RightPaneText = MASSplitViewPolicy.NormalizePaneText(rightPaneText, SplitViewTokens.DefaultRightPaneText)
            Dim leftRatio As Single = MASSplitViewPolicy.ClampRatio(leftPaneRatio)
            Dim rightRatio As Single = MASSplitViewPolicy.ClampRatio(rightPaneRatio)
            MASSplitViewPolicy.NormalizeSideRatios(leftRatio, rightRatio)
            Me.LeftPaneRatio = leftRatio
            Me.RightPaneRatio = rightRatio
            Me.LeftPaneCollapsed = leftPaneCollapsed
            Me.RightPaneCollapsed = rightPaneCollapsed
        End Sub

        Public ReadOnly Property Version As Integer
        Public ReadOnly Property Title As String
        Public ReadOnly Property LeftPaneTitle As String
        Public ReadOnly Property ContentPaneTitle As String
        Public ReadOnly Property RightPaneTitle As String
        Public ReadOnly Property LeftPaneText As String
        Public ReadOnly Property ContentPaneText As String
        Public ReadOnly Property RightPaneText As String
        Public ReadOnly Property LeftPaneRatio As Single
        Public ReadOnly Property RightPaneRatio As Single
        Public ReadOnly Property LeftPaneCollapsed As Boolean
        Public ReadOnly Property RightPaneCollapsed As Boolean
    End Class

    Partial Public NotInheritable Class MASSplitView

        Public Function CreateLayoutSnapshot() As MASSplitViewLayoutSnapshot
            Return New MASSplitViewLayoutSnapshot(
                1,
                _title,
                _leftPaneTitle,
                _contentPaneTitle,
                _rightPaneTitle,
                _leftPaneText,
                _contentPaneText,
                _rightPaneText,
                _leftPaneRatio,
                _rightPaneRatio,
                _leftPaneCollapsed,
                _rightPaneCollapsed)
        End Function

        Public Function RestoreLayoutSnapshot(snapshot As MASSplitViewLayoutSnapshot) As Boolean
            If snapshot Is Nothing Then Return False
            _title = snapshot.Title
            _leftPaneTitle = snapshot.LeftPaneTitle
            _contentPaneTitle = snapshot.ContentPaneTitle
            _rightPaneTitle = snapshot.RightPaneTitle
            _leftPaneText = snapshot.LeftPaneText
            _contentPaneText = snapshot.ContentPaneText
            _rightPaneText = snapshot.RightPaneText
            _leftPaneRatio = snapshot.LeftPaneRatio
            _rightPaneRatio = snapshot.RightPaneRatio
            MASSplitViewPolicy.NormalizeSideRatios(_leftPaneRatio, _rightPaneRatio)
            _leftPaneCollapsed = snapshot.LeftPaneCollapsed
            _rightPaneCollapsed = snapshot.RightPaneCollapsed
            _activeDivider = SplitDividerSide.None
            _hoverDivider = SplitDividerSide.None
            RequestSizeLayoutRefreshForSizeAffectingChange("MASSplitView.RestoreLayoutSnapshot")
            InvalidateVisual()
            RaiseSplitViewChangedSafe()
            Return True
        End Function

        Public Function ResetPaneLayout() As MASSplitView
            _leftPaneRatio = SplitViewTokens.DefaultLeftRatio
            _rightPaneRatio = SplitViewTokens.DefaultRightRatio
            MASSplitViewPolicy.NormalizeSideRatios(_leftPaneRatio, _rightPaneRatio)
            _leftPaneCollapsed = False
            _rightPaneCollapsed = False
            _activeDivider = SplitDividerSide.None
            _hoverDivider = SplitDividerSide.None
            RequestSizeLayoutRefreshForSizeAffectingChange("MASSplitView.ResetPaneLayout")
            InvalidateVisual()
            RaiseSplitViewChangedSafe()
            Return Me
        End Function

        Friend Function HasLayoutSnapshotForTests() As Boolean
            Dim snapshot As MASSplitViewLayoutSnapshot = CreateLayoutSnapshot()
            Return snapshot IsNot Nothing AndAlso
                   snapshot.LeftPaneRatio >= SplitViewTokens.MinPaneRatio AndAlso
                   snapshot.RightPaneRatio >= SplitViewTokens.MinPaneRatio
        End Function
    End Class

End Namespace
