'========================================================
' File: MASTileBox.vb  (SSOT + Fixes + Fluent SDK API)
'========================================================
Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports Nexamas.UI.Controls
Imports Nexamas.UI.Composition
Imports Nexamas.UI.General
Imports Nexamas.UI.Layout
Imports Nexamas.UI.Rendering
Imports Nexamas.UI.Scroll
Imports Nexamas.UI.SkiaScroll
Imports Nexamas.UI.Theming
Imports Nexamas.UI.Architecture
Imports Nexamas.UI.Values
Imports Nexamas.UI.Virtualization
Imports SkiaSharp


Namespace Nexamas.UI.Components

    Partial Public NotInheritable Class MASTileBox

        Protected Overrides Sub OnMouseMove(ctx As MASThemeContext, ptPx As SKPoint)

            Try
                EnsureScroll(ctx)

                If _scroll Is Nothing Then Return

                If (Not _lastContent.IsEmpty) AndAlso ptPx.X >= _lastContent.Right Then
                    _scroll.MouseMove(ptPx)
                    _isOverScrollbar = True

                    If _hoverIndex <> -1 Then
                        _hoverIndex = -1
                        SafeInvalidate()
                    End If

                    Return
                End If

                Dim handled As Boolean = _scroll.MouseMove(ptPx)
                _isOverScrollbar = handled

                If handled Then
                    If _hoverIndex <> -1 Then
                        _hoverIndex = -1
                        SafeInvalidate()
                    End If

                    Return
                End If

                Dim idx As Integer = HitTestIndex(ptPx)

                If idx <> _hoverIndex Then
                    _hoverIndex = idx
                    SafeInvalidate()
                End If

            Catch masCaughtException6 As Exception
                MASVirtualizationConsumerFaultBoundary.SwallowRenderFallback(masCaughtException6, MASVirtualizationConsumerDiagnosticCode.TileBoxPointerMoveFallback, "MASTileBox", MASVirtualizationConsumerDiagnosticContexts.TileBoxOnMouseMove)
            End Try

        End Sub

        Protected Overrides Function OnMouseDown(ctx As MASThemeContext, ptPx As SKPoint, button As Integer) As Boolean

            Nexamas.UI.Performance.MASPerformanceSystem.RecordConsumerInput(
                Nexamas.UI.Performance.MASPerformanceConsumerKind.TileBox,
                "MASTileBox.OnMouseDown")

            Try
                If ctx Is Nothing Then Return False
                If Not Me.Enabled Then Return False
                If button <> CInt(MouseButtons.Left) Then Return False

                EnsureScroll(ctx)

                If _scroll Is Nothing Then Return False

                Dim overScrollbar As Boolean =
                    (Not _lastContent.IsEmpty) AndAlso (ptPx.X >= _lastContent.Right)

                Dim stepPx As Single = ComputeRowStepPx(ctx)

                If overScrollbar Then
                    If _scroll.MouseDown(ptPx, rowStepPx:=stepPx, pageFactor:=CollectionTokens.ScrollDragFriction) Then
                        _pressedIndex = -1
                        _isOverScrollbar = True
                        SafeInvalidate()
                        Return True
                    End If

                    _pressedIndex = -1
                    _isOverScrollbar = True
                    SafeInvalidate()
                    Return False
                End If

                _isOverScrollbar = False

                Dim idx As Integer = HitTestIndex(ptPx)
                _pressedIndex = idx

                SafeInvalidate()

                Return (idx >= 0 AndAlso idx < _items.Count)

            Catch masCaughtException18 As Exception
                MASVirtualizationConsumerFaultBoundary.SwallowRenderFallback(masCaughtException18, MASVirtualizationConsumerDiagnosticCode.TileBoxPointerDownFallback, "MASTileBox", MASVirtualizationConsumerDiagnosticContexts.TileBoxOnMouseDown)
                _pressedIndex = -1
                _isOverScrollbar = False
                Return False
            End Try

        End Function

        Protected Overrides Function OnMouseUp(ctx As MASThemeContext, ptPx As SKPoint, button As Integer) As Boolean

            Nexamas.UI.Performance.MASPerformanceSystem.RecordConsumerInput(
                Nexamas.UI.Performance.MASPerformanceConsumerKind.TileBox,
                "MASTileBox.OnMouseUp")

            Try
                If ctx Is Nothing Then Return False
                If button <> CInt(MouseButtons.Left) Then Return False
                If _scroll Is Nothing Then Return False

                If _scroll.MouseUp() Then
                    Return True
                End If

                Dim wasPressed As Integer = _pressedIndex
                _pressedIndex = -1

                If wasPressed >= 0 Then
                    Dim upIdx As Integer = HitTestIndex(ptPx)

                    If upIdx = wasPressed AndAlso upIdx < _items.Count Then
                        Dim it As MASTileItem = _items(upIdx)

                        If it Is Nothing Then
                            SetSelectionInternal(Guid.Empty, Nothing)
                            Return True
                        End If

                        Dim isSameSelection As Boolean = False

                        If it.IconId <> Guid.Empty Then
                            isSameSelection = (_selectedId <> Guid.Empty AndAlso it.IconId = _selectedId)
                        Else
                            isSameSelection = (_selectedRef IsNot Nothing AndAlso Object.ReferenceEquals(it, _selectedRef))
                        End If

                        If isSameSelection Then
                            SetSelectionInternal(Guid.Empty, Nothing)
                        Else
                            If it.IconId <> Guid.Empty Then
                                SetSelectionInternal(it.IconId, Nothing)
                            Else
                                SetSelectionInternal(Guid.Empty, it)
                            End If
                        End If

                        RaiseEvent ItemClicked(Me, New MASTileClickedEventArgs(wasPressed))
                        Return True
                    End If

                    SafeInvalidate()
                End If

                Return False

            Catch masCaughtException19 As Exception
                MASVirtualizationConsumerFaultBoundary.SwallowRenderFallback(masCaughtException19, MASVirtualizationConsumerDiagnosticCode.TileBoxPointerUpFallback, "MASTileBox", MASVirtualizationConsumerDiagnosticContexts.TileBoxOnMouseUp)
                _pressedIndex = -1
                Return False
            End Try

        End Function

        Friend Overrides Function WantsPointerWheel(ctx As MASThemeContext, ptPx As SKPoint) As Boolean
            If ctx Is Nothing OrElse Not Me.Enabled Then Return False
            EnsureScroll(ctx)
            Return _scroll IsNot Nothing
        End Function

        Friend Overrides Function CanHandlePointerWheel(ctx As MASThemeContext, delta As Integer, ptPx As SKPoint) As Boolean
            If Not WantsPointerWheel(ctx, ptPx) Then Return False
            EnsureScroll(ctx)
            If _scroll Is Nothing Then Return False
            Dim stepPx As Single = ComputeRowStepPx(ctx)
            Return _scroll.CanMouseWheel(delta, stepPx)
        End Function

        Protected Overrides Sub OnMouseWheel(ctx As MASThemeContext, delta As Integer, ptPx As SKPoint)

            Nexamas.UI.Performance.MASPerformanceSystem.RecordConsumerInput(
                Nexamas.UI.Performance.MASPerformanceConsumerKind.TileBox,
                "MASTileBox.OnMouseWheel")

            Try
                EnsureScroll(ctx)

                If _scroll Is Nothing Then Return

                Dim stepPx As Single = ComputeRowStepPx(ctx)

                If _scroll.MouseWheel(delta, stepPx) Then
                    SafeInvalidate()
                End If

            Catch masCaughtException7 As Exception
                MASVirtualizationConsumerFaultBoundary.SwallowRenderFallback(masCaughtException7, MASVirtualizationConsumerDiagnosticCode.TileBoxWheelFallback, "MASTileBox", MASVirtualizationConsumerDiagnosticContexts.TileBoxOnMouseWheel)
            End Try

        End Sub

        Protected Overrides Sub OnMouseLeave(ctx As MASThemeContext)

            Try
                Dim visualChanged As Boolean = False

                If _hoverIndex <> -1 OrElse _pressedIndex <> -1 Then
                    _hoverIndex = -1
                    _pressedIndex = -1
                    visualChanged = True
                End If

                If _scroll IsNot Nothing Then
                    _scroll.MouseLeave()
                    visualChanged = True
                End If

                If visualChanged Then
                    MarkFrameCacheDirty()
                    InvalidateVisual()
                End If

            Catch masCaughtException8 As Exception
                MASVirtualizationConsumerFaultBoundary.SwallowRenderFallback(masCaughtException8, MASVirtualizationConsumerDiagnosticCode.TileBoxPointerLeaveFallback, "MASTileBox", MASVirtualizationConsumerDiagnosticContexts.TileBoxOnMouseLeave)
            End Try

        End Sub

        Protected Overrides Sub OnMouseCancel(ctx As MASThemeContext)

            Try
                _pressedIndex = -1
                _hoverIndex = -1
                _isOverScrollbar = False
                _scroll?.CancelInteraction()
                MarkFrameCacheDirty()
                InvalidateVisual()
            Catch masCaughtException9 As Exception
                MASVirtualizationConsumerFaultBoundary.SwallowRenderFallback(masCaughtException9, MASVirtualizationConsumerDiagnosticCode.TileBoxPointerCancelFallback, "MASTileBox", MASVirtualizationConsumerDiagnosticContexts.TileBoxOnMouseCancel)
            End Try

        End Sub

        Protected Overrides Sub OnHostLostFocus(ctx As MASThemeContext)
            OnMouseCancel(ctx)
        End Sub

    End Class

End Namespace
