Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.Motion

Namespace Nexamas.UI.Components

    Partial Public NotInheritable Class MASTreeGrid

#Region "Motion"

        Private _selectionMotionRunner As MASMotionTimelineRunner
        Private _selectionMotionFromRowKey As String = String.Empty
        Private _selectionMotionToRowKey As String = String.Empty
        Private _selectionMotionProgress As Single = 1.0F
        Private _hasSelectionMotion As Boolean

        Private Sub StartSelectionMotion(previousRowKey As String,
                                         nextRowKey As String)
            StopSelectionMotion()

            Dim previousKey As String = If(previousRowKey, String.Empty).Trim()
            Dim nextKey As String = If(nextRowKey, String.Empty).Trim()
            If previousKey.Length = 0 OrElse nextKey.Length = 0 Then Return
            If String.Equals(previousKey, nextKey, StringComparison.Ordinal) Then Return

            _selectionMotionFromRowKey = previousKey
            _selectionMotionToRowKey = nextKey
            _selectionMotionProgress = 0.0F
            _hasSelectionMotion = True

            Dim plan As MASMotionTransitionPlan = MASMotionSystem.CreateTransitionPlan(MASMotionTokenId.RowSelectionMove, 0.0F, 1.0F)
            If Not plan.ShouldAnimate Then
                CompleteSelectionMotionFrame(Nothing)
                Return
            End If

            _selectionMotionRunner = MASMotionSystem.CreateTimelineRunner(
                owner:=Me,
                consumerName:="MASTreeGrid.Selection",
                plan:=plan,
                requestFrame:=AddressOf InvalidateVisual,
                applyFrame:=AddressOf ApplySelectionMotionFrame,
                completed:=AddressOf CompleteSelectionMotionFrame)
            _selectionMotionRunner.Start()
        End Sub

        Private Sub ApplySelectionMotionFrame(frame As MASMotionFrame)
            If frame Is Nothing Then Return
            _selectionMotionProgress = MASMotionSystem.ClampProgress(frame.Value)
            InvalidateVisual()
        End Sub

        Private Sub CompleteSelectionMotionFrame(frame As MASMotionFrame)
            _selectionMotionProgress = 1.0F
            _hasSelectionMotion = False
            _selectionMotionRunner = Nothing
            InvalidateVisual()
        End Sub

        Private Sub StopSelectionMotion()
            If _selectionMotionRunner IsNot Nothing Then
                _selectionMotionRunner.Dispose()
                _selectionMotionRunner = Nothing
            End If

            _hasSelectionMotion = False
            _selectionMotionProgress = 1.0F
            _selectionMotionFromRowKey = String.Empty
            _selectionMotionToRowKey = String.Empty
        End Sub

        Private Function IsSelectionMotionActive() As Boolean
            Return _hasSelectionMotion AndAlso _selectionMotionRunner IsNot Nothing AndAlso _selectionMotionRunner.IsRunning
        End Function

        Private Function IsSelectionMotionEndpoint(rowKey As String) As Boolean
            If Not IsSelectionMotionActive() Then Return False
            Dim key As String = If(rowKey, String.Empty)
            Return String.Equals(key, _selectionMotionFromRowKey, StringComparison.Ordinal) OrElse
                   String.Equals(key, _selectionMotionToRowKey, StringComparison.Ordinal)
        End Function

#End Region

    End Class

End Namespace
