// KeyboardHookKeyEvent.cs
// Copyright (c) 2017 Bentley Systems, Incorporated. All Rights Reserved.

using System.Windows.Input;
using Haestad.Support.Win32Api;

namespace Haestad.Framework.UI.Integration
{
    /// <summary>
    /// Class manages information about key presses coming from the low level keyboard hook.
    /// </summary>
    internal class KeyboardHookKeyEvent
    {
        #region Constructor
        /// <summary>
        /// Instantiates a new KeyboardHookKeyEvent instance.
        /// </summary>
        /// <param name="vkCode">The virtual key code from the hook callback.</param>
        /// <param name="wParam">The wParam value from the hook callback.</param>
        public KeyboardHookKeyEvent(int vkCode, int wParam)
        {
            ModifierKeys = Keyboard.Modifiers;
            Initialize(vkCode, wParam);
        }
        #endregion

        #region Public Properties
        /// <summary>
        /// The Key that is is associated with this event.
        /// </summary>
        public Key Key
        {
            get;
            private set;
        }

        /// <summary>
        /// The type of event.
        /// </summary>
        public KeyEventType KeyEventType
        {
            get;
            private set;
        }

        /// <summary>
        /// The set of modifier keys at the time of the key event.
        /// </summary>
        public ModifierKeys ModifierKeys
        {
            get;
            private set;
        }
        #endregion

        #region Private Methods
        private void Initialize(int vkCode, int wParam)
        {
            Key = KeyInterop.KeyFromVirtualKey(vkCode);

            if (wParam == User32Dll.WM_KEYDOWN)
                KeyEventType = KeyEventType.KeyDown;
            else if (wParam == User32Dll.WM_KEYUP)
                KeyEventType = KeyEventType.KeyUp;
            else if (wParam == WINDOWS_MESSAGES.WM_SYSKEYDOWN)
                KeyEventType = KeyEventType.SysKeyDown;
            else
                KeyEventType = KeyEventType.Other;
        }
        #endregion
    }
}