Option Strict On
Option Explicit On

Imports Nexamas.UI.Motion
Imports SkiaSharp

Namespace Nexamas.UI.Controls

    Partial Public NotInheritable Class MASTabControl

#Region "Selection Indicator Motion"

        Friend ReadOnly Property SelectionIndicatorMotionFromIndex As Integer
            Get
                Return _selectionIndicatorFromIndex
            End Get
        End Property

        Friend ReadOnly Property SelectionIndicatorMotionToIndex As Integer
            Get
                Return _selectionIndicatorToIndex
            End Get
        End Property

        Friend ReadOnly Property SelectionIndicatorMotionProgress As Single
            Get
                Return _selectionIndicatorProgress
            End Get
        End Property

        Friend Function IsSelectionIndicatorMotionActive() As Boolean
            Return _hasSelectionIndicatorMotion AndAlso
                   _selectionIndicatorRunner IsNot Nothing AndAlso
                   _selectionIndicatorRunner.IsRunning
        End Function

        Private Sub StartSelectionIndicatorMotion(previousIndex As Integer,
                                                  nextIndex As Integer)
            StopSelectionIndicatorMotion()

            If previousIndex < 0 OrElse nextIndex < 0 OrElse previousIndex = nextIndex Then
                _hasSelectionIndicatorMotion = False
                _selectionIndicatorProgress = 1.0F
                Return
            End If

            _selectionIndicatorFromIndex = previousIndex
            _selectionIndicatorToIndex = nextIndex
            _selectionIndicatorProgress = 0.0F
            _hasSelectionIndicatorMotion = True

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

            _selectionIndicatorRunner = MASMotionSystem.CreateTimelineRunner(
                owner:=Me,
                consumerName:="MASTabControl.SelectedIndicator",
                plan:=plan,
                requestFrame:=AddressOf InvalidateVisual,
                applyFrame:=AddressOf ApplySelectionIndicatorMotionFrame,
                completed:=AddressOf CompleteSelectionIndicatorMotionFrame)
            _selectionIndicatorRunner.Start()
        End Sub

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

        Private Sub CompleteSelectionIndicatorMotionFrame(frame As MASMotionFrame)
            _selectionIndicatorProgress = 1.0F
            _hasSelectionIndicatorMotion = False
            _selectionIndicatorRunner = Nothing
            InvalidateVisual()
        End Sub

        Private Sub StopSelectionIndicatorMotion()
            If _selectionIndicatorRunner IsNot Nothing Then
                _selectionIndicatorRunner.Dispose()
                _selectionIndicatorRunner = Nothing
            End If

            _hasSelectionIndicatorMotion = False
            _selectionIndicatorFromIndex = -1
            _selectionIndicatorToIndex = -1
            _selectionIndicatorProgress = 1.0F
        End Sub

        Friend Function ResolveSelectionIndicatorMotionRect(lay As Nexamas.UI.Components.MASTabLayout.Metrics) As SKRect
            If Not IsSelectionIndicatorMotionActive() Then Return SKRect.Empty
            If lay.ItemRects Is Nothing OrElse lay.ItemRects.Length <= 0 Then Return SKRect.Empty

            If _selectionIndicatorFromIndex < 0 OrElse _selectionIndicatorFromIndex >= lay.ItemRects.Length Then Return SKRect.Empty
            If _selectionIndicatorToIndex < 0 OrElse _selectionIndicatorToIndex >= lay.ItemRects.Length Then Return SKRect.Empty

            Dim fromRect As SKRect = Nexamas.UI.Components.MASTabLayout.ComputeItemRect(lay, _selectionIndicatorFromIndex)
            Dim toRect As SKRect = Nexamas.UI.Components.MASTabLayout.ComputeItemRect(lay, _selectionIndicatorToIndex)
            If fromRect.IsEmpty AndAlso toRect.IsEmpty Then Return SKRect.Empty
            If fromRect.IsEmpty Then fromRect = toRect
            If toRect.IsEmpty Then toRect = fromRect

            Dim p As Single = MASMotionSystem.ClampProgress(_selectionIndicatorProgress)
            Return New SKRect(
                fromRect.Left + ((toRect.Left - fromRect.Left) * p),
                fromRect.Top + ((toRect.Top - fromRect.Top) * p),
                fromRect.Right + ((toRect.Right - fromRect.Right) * p),
                fromRect.Bottom + ((toRect.Bottom - fromRect.Bottom) * p))
        End Function

#End Region

    End Class

End Namespace
