Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Windows.Forms
Imports Nexamas.UI.Architecture
Imports Nexamas.UI.Composition
Imports Nexamas.UI.Controls
Imports Nexamas.UI.General
Imports Nexamas.UI.Icons
Imports Nexamas.UI.Layout
Imports Nexamas.UI.Rendering
Imports Nexamas.UI.TextRendering
Imports Nexamas.UI.Theming
Imports Nexamas.UI.Values
Imports SkiaSharp

Namespace Nexamas.UI.Components

    Partial Public NotInheritable Class MASFileDropZone

#Region "Input"

        Protected Overrides Sub OnMouseMove(ctx As MASThemeContext, ptPx As SKPoint)
            Dim hoverBrowse As Boolean = _browseRect.Contains(ptPx.X, ptPx.Y)
            Dim hoverRemove As Integer = HitTestRemoveIndex(ptPx)
            If hoverBrowse = _hoverBrowse AndAlso hoverRemove = _hoverRemoveIndex Then Return
            _hoverBrowse = hoverBrowse
            _hoverRemoveIndex = hoverRemove
            InvalidateVisual()
        End Sub

        Protected Overrides Function OnMouseDown(ctx As MASThemeContext,
                                                 ptPx As SKPoint,
                                                 button As Integer) As Boolean
            If button <> CInt(MouseButtons.Left) Then Return False

            _pressedRemoveIndex = HitTestRemoveIndex(ptPx)
            If _pressedRemoveIndex >= 0 Then
                _pressedBrowse = False
                InvalidateVisual()
                Return True
            End If

            _pressedBrowse = _browseRect.Contains(ptPx.X, ptPx.Y)
            If _pressedBrowse Then
                InvalidateVisual()
                Return True
            End If
            Return _dropRect.Contains(ptPx.X, ptPx.Y)
        End Function

        Protected Overrides Function OnMouseUp(ctx As MASThemeContext,
                                               ptPx As SKPoint,
                                               button As Integer) As Boolean
            If button <> CInt(MouseButtons.Left) Then Return False

            Dim pressedRemove As Integer = _pressedRemoveIndex
            _pressedRemoveIndex = -1
            If pressedRemove >= 0 Then
                Dim releaseRemove As Integer = HitTestRemoveIndex(ptPx)
                If releaseRemove = pressedRemove Then RemoveFileAt(pressedRemove)
                InvalidateVisual()
                Return True
            End If

            Dim wasPressed As Boolean = _pressedBrowse
            _pressedBrowse = False
            If wasPressed AndAlso _browseRect.Contains(ptPx.X, ptPx.Y) Then
                RaiseBrowseRequestedSafe()
                InvalidateVisual()
                Return True
            End If
            If wasPressed Then
                InvalidateVisual()
                Return True
            End If
            Return False
        End Function

        Protected Overrides Sub OnMouseLeave(ctx As MASThemeContext)
            If Not _hoverBrowse AndAlso Not _pressedBrowse AndAlso _hoverRemoveIndex < 0 AndAlso _pressedRemoveIndex < 0 Then Return
            _hoverBrowse = False
            _pressedBrowse = False
            _hoverRemoveIndex = -1
            _pressedRemoveIndex = -1
            InvalidateVisual()
        End Sub

        Protected Overrides Sub OnMouseCancel(ctx As MASThemeContext)
            _hoverBrowse = False
            _pressedBrowse = False
            _hoverRemoveIndex = -1
            _pressedRemoveIndex = -1
            InvalidateVisual()
        End Sub

        Protected Overrides Sub OnHostLostFocus(ctx As MASThemeContext)
            _pressedBrowse = False
            _pressedRemoveIndex = -1
            InvalidateVisual()
        End Sub

        Protected Overrides Function OnKeyDown(ctx As MASThemeContext,
                                               keyCode As Keys) As Boolean
            If Not Enabled Then Return False
            Select Case keyCode
                Case Keys.Enter, Keys.Space
                    RaiseBrowseRequestedSafe()
                    Return True
                Case Keys.Delete, Keys.Back
                    If _acceptedPaths.Count > 0 Then
                        RemoveFileAt(_acceptedPaths.Count - 1)
                        Return True
                    End If
            End Select
            Return False
        End Function

        Protected Overrides Function WantsKeyboardFocus() As Boolean
            Return MASCompositeContentFocusPolicy.CanReceiveKeyboardFocus(_contract, Visible, Enabled)
        End Function

        Protected Overrides Function WantsPointerFocus() As Boolean
            Return MASCompositeContentFocusPolicy.AllowsPointerFocusForCompositeContent(_contract, Visible, Enabled)
        End Function

#End Region

    End Class

End Namespace
