Option Strict On
Option Explicit On


Imports SkiaSharp

Namespace Nexamas.UI.SkiaScroll

    Friend NotInheritable Class MASScrollInteractionController

        Private _isHover As Boolean = False
        Private _isDragging As Boolean = False

        Friend ReadOnly Property IsHover As Boolean
            Get
                Return _isHover
            End Get
        End Property

        Friend ReadOnly Property IsDragging As Boolean
            Get
                Return _isDragging
            End Get
        End Property

        Friend Function HandleMove(scroll As SkiaScrollAdapter,
                                   railRectPx As SKRect,
                                   ptPx As SKPoint) As Boolean

            If scroll Is Nothing Then Return False

            If (railRectPx.Width > 1.0F AndAlso railRectPx.Contains(ptPx.X, ptPx.Y)) OrElse _isDragging Then
                _isHover = True
                scroll.MouseMove(ptPx)
                Return True
            End If

            If _isHover Then
                _isHover = False
                Return False
            End If

            Return False
        End Function

        Friend Function HandleMouseDown(scroll As SkiaScrollAdapter,
                                        railRectPx As SKRect,
                                        ptPx As SKPoint,
                                        stepPx As Single,
                                        friction As Single) As Boolean

            If scroll Is Nothing Then Return False
            If railRectPx.Width <= 1.0F OrElse railRectPx.Height <= 1.0F Then Return False
            If Not railRectPx.Contains(ptPx.X, ptPx.Y) Then Return False

            _isDragging = scroll.MouseDown(ptPx, stepPx, friction)
            _isHover = True

            Return True
        End Function

        Friend Function HandleMouseUp(scroll As SkiaScrollAdapter) As Boolean
            If Not _isDragging Then Return False

            _isDragging = False

            If scroll Is Nothing Then Return True

            Return scroll.MouseUp()
        End Function

        Friend Function HandleMouseLeave(scroll As SkiaScrollAdapter) As Boolean
            If _isDragging Then Return False

            Dim changed As Boolean = False

            If _isHover Then
                _isHover = False
                changed = True
            End If

            If scroll IsNot Nothing Then
                Try
                    scroll.MouseLeave()
                Catch masCaughtException1 As Exception
                    Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowInputBoundary(masCaughtException1)
                End Try
            End If

            Return changed
        End Function

        Friend Function Cancel(scroll As SkiaScrollAdapter) As Boolean
            Dim changed As Boolean = _isDragging OrElse _isHover

            If scroll IsNot Nothing Then
                Try
                    scroll.CancelInteraction()
                Catch masCaughtException2 As Exception
                    Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowInputBoundary(masCaughtException2)
                End Try
            End If

            _isDragging = False
            _isHover = False

            Return changed
        End Function

    End Class

End Namespace
