Option Strict On
Option Explicit On

Imports System.Windows.Forms

Namespace Nexamas.UI.TextInput

    Friend Enum MASTextShortcutAction
        None = 0
        Copy = 1
        Cut = 2
        Paste = 3
        Undo = 4
        Redo = 5
        SelectAll = 6
        MovePreviousWord = 7
        MoveNextWord = 8
        MoveVisualLeft = 9
        MoveVisualRight = 10
        Home = 11
        EndKey = 12
        Backspace = 13
        DeleteForward = 14
    End Enum

    ' CONTRACT:
    ' MASTextShortcutRouter maps keyboard keys + modifiers to semantic text actions.
    '
    ' It must not execute actions, mutate state, access clipboard, render, or invalidate UI.
    Friend NotInheritable Class MASTextShortcutRouter

        Friend Function Resolve(keyCode As Keys, modifiers As Keys) As MASTextShortcutAction
            Dim ctrl As Boolean = IsControlDown(modifiers)

            Select Case keyCode
                Case Keys.C
                    If ctrl Then Return MASTextShortcutAction.Copy

                Case Keys.X
                    If ctrl Then Return MASTextShortcutAction.Cut

                Case Keys.V
                    If ctrl Then Return MASTextShortcutAction.Paste

                Case Keys.Z
                    If ctrl Then Return MASTextShortcutAction.Undo

                Case Keys.Y
                    If ctrl Then Return MASTextShortcutAction.Redo

                Case Keys.A
                    If ctrl Then Return MASTextShortcutAction.SelectAll

                Case Keys.Left
                    If ctrl Then Return MASTextShortcutAction.MovePreviousWord
                    Return MASTextShortcutAction.MoveVisualLeft

                Case Keys.Right
                    If ctrl Then Return MASTextShortcutAction.MoveNextWord
                    Return MASTextShortcutAction.MoveVisualRight

                Case Keys.Home
                    Return MASTextShortcutAction.Home

                Case Keys.End
                    Return MASTextShortcutAction.EndKey

                Case Keys.Back
                    Return MASTextShortcutAction.Backspace

                Case Keys.Delete
                    Return MASTextShortcutAction.DeleteForward
            End Select

            Return MASTextShortcutAction.None
        End Function

        Friend Function IsControlDown(modifiers As Keys) As Boolean
            Return (modifiers And Keys.Control) = Keys.Control
        End Function

        Friend Function IsShiftDown(modifiers As Keys) As Boolean
            Return (modifiers And Keys.Shift) = Keys.Shift
        End Function

    End Class

End Namespace
