Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.Motion

Namespace Nexamas.UI.Components

    Partial Public NotInheritable Class MASPivotTable

#Region "Motion"

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

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

            Dim previousRow As String = If(previousRowKey, String.Empty).Trim()
            Dim previousColumn As String = If(previousColumnKey, String.Empty).Trim()
            Dim nextRow As String = If(nextRowKey, String.Empty).Trim()
            Dim nextColumn As String = If(nextColumnKey, String.Empty).Trim()
            If previousRow.Length = 0 OrElse previousColumn.Length = 0 OrElse nextRow.Length = 0 OrElse nextColumn.Length = 0 Then Return
            If String.Equals(previousRow, nextRow, StringComparison.Ordinal) AndAlso String.Equals(previousColumn, nextColumn, StringComparison.Ordinal) Then Return

            _selectionMotionFromRowKey = previousRow
            _selectionMotionFromColumnKey = previousColumn
            _selectionMotionToRowKey = nextRow
            _selectionMotionToColumnKey = nextColumn
            _selectionMotionProgress = 0.0F
            _hasSelectionMotion = True

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

            _selectionMotionRunner = MASMotionSystem.CreateTimelineRunner(
                owner:=Me,
                consumerName:="MASPivotTable.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
            _selectionMotionFromColumnKey = String.Empty
            _selectionMotionToRowKey = String.Empty
            _selectionMotionToColumnKey = 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, columnKey As String) As Boolean
            If Not IsSelectionMotionActive() Then Return False
            Return (String.Equals(If(rowKey, String.Empty), _selectionMotionFromRowKey, StringComparison.Ordinal) AndAlso
                    String.Equals(If(columnKey, String.Empty), _selectionMotionFromColumnKey, StringComparison.Ordinal)) OrElse
                   (String.Equals(If(rowKey, String.Empty), _selectionMotionToRowKey, StringComparison.Ordinal) AndAlso
                    String.Equals(If(columnKey, String.Empty), _selectionMotionToColumnKey, StringComparison.Ordinal))
        End Function

#End Region

    End Class

End Namespace
