using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Runtime.InteropServices;
using System.Threading;



namespace EmbeddedDesktop
{
    /// <summary>
    /// Eventy nalezejici mysi jako periferii
    /// </summary>
    [Flags]
    public enum MouseEventFlags
    {
        LeftDown = 0x00000002,
        LeftUp = 0x00000004,
        MiddleDown = 0x00000020,
        MiddleUp = 0x00000040,
        Move = 0x00000001,
        Absolute = 0x00008000,
        RightDown = 0x00000008,
        RightUp = 0x00000010
    }

    public static class MouseControl
    {
        /// <summary>
        /// Import hlavni funkce pro ovladani mysi
        /// </summary>
        /// <param name="dwFlags">identifikator udalosti</param>
        /// <param name="dx">pozice X</param>
        /// <param name="dy">pozice Y</param>
        /// <param name="cButtons"></param>
        /// <param name="dwExtraInfo"></param>
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        private static extern void mouse_event(UInt32 dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);


        /// <summary>
        /// Provede posun mysi relativne k aktualni pozici
        /// </summary>
        /// <param name="x">zmena X</param>
        /// <param name="y">zmena Y</param>
        public static void Move(int x, int y)
        {
            mouse_event((UInt32)MouseEventFlags.Move, (uint)((x < 0 ? -1 : 1) * x * x / 250), (uint)((y < 0 ? -1 : 1) * y * y / 250), 0, 0);
        }

        /// <summary>
        /// Umisti mys na absolutni pozici
        /// </summary>
        /// <param name="x">pozice X</param>
        /// <param name="y">pozice Y</param>
        public static void Place(int x, int y)
        {
            mouse_event((UInt32)(MouseEventFlags.Absolute| MouseEventFlags.Move), (uint)x, (uint)y, 0, 0);
            mouse_event((UInt32)(MouseEventFlags.Move), (uint)x, (uint)y, 0, 0);
        }

        /// <summary>
        /// Klikne na danou pozici levou mysi
        /// </summary>
        /// <param name="x">pozice X</param>
        /// <param name="y">pozice Y</param>
        public static void Click(int x, int y)
        {
            mouse_event((UInt32)(MouseEventFlags.Absolute | MouseEventFlags.Move), (uint)x, (uint)y, 0, 0);
            mouse_event((UInt32)(MouseEventFlags.Move), (uint)x, (uint)y, 0, 0);
            mouse_event((UInt32)(MouseEventFlags.Absolute | MouseEventFlags.LeftDown), (uint)x, (uint)y, 0, 0);
            mouse_event((UInt32)(MouseEventFlags.Absolute | MouseEventFlags.LeftUp), (uint)x, (uint)y, 0, 0);
            
        }

        /// <summary>
        /// Klikne na aktualni pozici
        /// </summary>
        public static void Click()
        {
            mouse_event((UInt32)(MouseEventFlags.Absolute | MouseEventFlags.LeftDown), (uint)0, (uint)0, 0, 0);

            mouse_event((UInt32)(MouseEventFlags.Absolute | MouseEventFlags.LeftUp), (uint)0, (uint)0, 0, 0);

        }

        /// <summary>
        /// Klikne na pozici pravym tlacitkem mysi
        /// </summary>
        /// <param name="x">pozice X</param>
        /// <param name="y">pozice Y</param>
        public static void RightClick(int x, int y)
        {
            mouse_event((UInt32)(MouseEventFlags.Absolute | MouseEventFlags.Move), (uint)x, (uint)y, 0, 0);
            mouse_event((UInt32)(MouseEventFlags.Move), (uint)x, (uint)y, 0, 0);
            mouse_event((UInt32)(MouseEventFlags.Absolute | MouseEventFlags.RightDown), (uint)x, (uint)y, 0, 0);
            mouse_event((UInt32)(MouseEventFlags.Absolute | MouseEventFlags.RightUp), (uint)x, (uint)y, 0, 0);

        }

        /// <summary>
        /// Klikne na aktualni pozici pravym tlacitkem mysi
        /// </summary>
        public static void RightClick()
        {
            mouse_event((UInt32)(MouseEventFlags.Absolute | MouseEventFlags.RightDown), (uint)0, (uint)0, 0, 0);

            mouse_event((UInt32)(MouseEventFlags.Absolute | MouseEventFlags.RightUp), (uint)0, (uint)0, 0, 0);

        }

    }
}
