// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License

using System;
using System.Runtime.InteropServices;
using UnityEngine.Bindings;
using UnityEngine.Scripting;
using UnityEngine.Scripting.APIUpdating;


namespace UnityEngine.XR.WSA.Input
{
    [MovedFrom("UnityEngine.VR.WSA.Input")]
    public enum InteractionSourceKind
    {
        Other,
        Hand,
        Voice,
        Controller
    }

    internal enum InteractionSourceFlags
    {
        None = 0,
        SupportsTouchpad =  1 << 0,
        SupportsThumbstick = 1 << 1,
        SupportsPointing = 1 << 2,
        SupportsGrasp = 1 << 3,
        SupportsMenu = 1 << 4
    }

    [RequiredByNativeCode]
    [MovedFrom("UnityEngine.VR.WSA.Input")]
    [NativeHeader("Modules/VR/HoloLens/Gestures/GestureCommon.h")]
    public struct InteractionSource : IEquatable<InteractionSource>
    {
        public override bool Equals(object obj)
        {
            InteractionSource? source = obj as InteractionSource ? ;
            if (source == null)
                return false;

            return Equals(source.Value);
        }

        public bool Equals(InteractionSource other)
        {
            return other.m_Id == m_Id;
        }

        public override int GetHashCode()
        {
            return (int)m_Id;
        }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public uint id { get { return m_Id; } }
        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public InteractionSourceKind kind { get { return m_SourceKind; } }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public InteractionSourceHandedness handedness
        { get { return m_Handedness; } }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public bool supportsGrasp
        { get { return (m_Flags & InteractionSourceFlags.SupportsGrasp) != 0; } }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public bool supportsMenu
        { get { return (m_Flags & InteractionSourceFlags.SupportsMenu) != 0; } }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public bool supportsPointing
        { get { return (m_Flags & InteractionSourceFlags.SupportsPointing) != 0; } }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public bool supportsThumbstick
        { get { return (m_Flags & InteractionSourceFlags.SupportsThumbstick) != 0; } }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public bool supportsTouchpad
        { get { return (m_Flags & InteractionSourceFlags.SupportsTouchpad) != 0; } }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public ushort vendorId
        { get { return m_VendorId; } }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public ushort productId
        { get { return m_ProductId; } }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public ushort productVersion
        { get { return m_ProductVersion; } }

        internal uint m_Id;
        internal InteractionSourceKind m_SourceKind;
        internal InteractionSourceHandedness m_Handedness;
        internal InteractionSourceFlags m_Flags;

        internal ushort m_VendorId;
        internal ushort m_ProductId;
        internal ushort m_ProductVersion;
    }

    internal enum InteractionSourcePoseFlags
    {
        None = 0,
        HasGripPosition = 1 << 0,
        HasGripRotation = 1 << 1,
        HasPointerPosition = 1 << 2,
        HasPointerRotation = 1 << 3,
        HasVelocity = 1 << 4,
        HasAngularVelocity = 1 << 5,
    }

    internal enum InteractionSourceStateFlags
    {
        None = 0,
        Grasped = 1 << 0,
        AnyPressed = 1 << 1,
        TouchpadPressed = 1 << 2,
        ThumbstickPressed = 1 << 3,
        SelectPressed = 1 << 4,
        MenuPressed = 1 << 5,
        TouchpadTouched = 1 << 6,
    }

    public enum InteractionSourceHandedness
    {
        Unknown,
        Left,
        Right
    }

    [MovedFrom("UnityEngine.VR.WSA.Input")]
    public enum InteractionSourcePressType
    {
        None,
        Select,
        Menu,
        Grasp,
        Touchpad,
        Thumbstick
    }

    public enum InteractionSourceNode
    {
        Grip,
        Pointer
    }

    public enum InteractionSourcePositionAccuracy
    {
        None,
        Approximate,
        High
    }

    [RequiredByNativeCode]
    [NativeHeader("Modules/VR/HoloLens/Gestures/GestureCommon.h")]
    public partial struct InteractionSourcePose
    {
        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public bool TryGetPosition(out Vector3 position)
        {
            return TryGetPosition(out position, InteractionSourceNode.Grip);
        }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public bool TryGetPosition(out Vector3 position, InteractionSourceNode node)
        {
            if (node == InteractionSourceNode.Grip)
            {
                position = m_GripPosition;
                return (m_Flags & InteractionSourcePoseFlags.HasGripPosition) != 0;
            }
            else
            {
                position = m_PointerPosition;
                return (m_Flags & InteractionSourcePoseFlags.HasPointerPosition) != 0;
            }
        }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public bool TryGetRotation(out Quaternion rotation, InteractionSourceNode node = InteractionSourceNode.Grip)
        {
            if (node == InteractionSourceNode.Grip)
            {
                rotation = m_GripRotation;
                return (m_Flags & InteractionSourcePoseFlags.HasGripRotation) != 0;
            }
            else
            {
                rotation = m_PointerRotation;
                return (m_Flags & InteractionSourcePoseFlags.HasPointerRotation) != 0;
            }
        }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public bool TryGetForward(out Vector3 forward, InteractionSourceNode node = InteractionSourceNode.Grip)
        {
            Quaternion rotation;
            bool ret = TryGetRotation(out rotation, node);
            forward = rotation * Vector3.forward;
            return ret;
        }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public bool TryGetRight(out Vector3 right, InteractionSourceNode node = InteractionSourceNode.Grip)
        {
            Quaternion rotation;
            bool ret = TryGetRotation(out rotation, node);
            right = rotation * Vector3.right;
            return ret;
        }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public bool TryGetUp(out Vector3 up, InteractionSourceNode node = InteractionSourceNode.Grip)
        {
            Quaternion rotation;
            bool ret = TryGetRotation(out rotation, node);
            up = rotation * Vector3.up;
            return ret;
        }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public bool TryGetVelocity(out Vector3 velocity)
        {
            velocity = m_Velocity;
            return (m_Flags & InteractionSourcePoseFlags.HasVelocity) != 0;
        }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public bool TryGetAngularVelocity(out Vector3 angularVelocity)
        {
            angularVelocity = m_AngularVelocity;
            return (m_Flags & InteractionSourcePoseFlags.HasAngularVelocity) != 0;
        }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public InteractionSourcePositionAccuracy positionAccuracy
        {
            get
            {
                return m_PositionAccuracy;
            }
        }

        internal Quaternion m_GripRotation;
        internal Quaternion m_PointerRotation;

        internal Vector3 m_GripPosition;
        internal Vector3 m_PointerPosition;
        internal Vector3 m_Velocity;
        internal Vector3 m_AngularVelocity;

        internal InteractionSourcePositionAccuracy m_PositionAccuracy;
        internal InteractionSourcePoseFlags m_Flags;
    }

    [RequiredByNativeCode]
    [NativeHeader("Modules/VR/HoloLens/Gestures/GestureCommon.h")]
    [MovedFrom("UnityEngine.VR.WSA.Input")]
    public partial struct InteractionSourceProperties
    {
        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public double sourceLossRisk { get { return m_SourceLossRisk; } }
        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public Vector3 sourceLossMitigationDirection { get { return m_SourceLossMitigationDirection; } }

        internal double m_SourceLossRisk;
        internal Vector3 m_SourceLossMitigationDirection;
        internal InteractionSourcePose m_SourcePose;
    }

    [RequiredByNativeCode]
    [NativeHeader("Modules/VR/HoloLens/Gestures/GestureSource.h")]
    [NativeHeader("VRScriptingClasses.h")]
    [MovedFrom("UnityEngine.VR.WSA.Input")]
    public partial struct InteractionSourceState
    {
        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public bool anyPressed
        { get { return (m_Flags & InteractionSourceStateFlags.AnyPressed) != 0; } }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public Pose headPose
        { get { return m_HeadPose; } }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public InteractionSourceProperties properties
        { get { return m_Properties; } }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public InteractionSource source
        { get { return m_Source; } }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public InteractionSourcePose sourcePose
        { get { return m_Properties.m_SourcePose; } }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public float selectPressedAmount
        { get { return m_SelectPressedAmount; } }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public bool selectPressed
        { get { return (m_Flags & InteractionSourceStateFlags.SelectPressed) != 0; } }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public bool menuPressed
        { get { return (m_Flags & InteractionSourceStateFlags.MenuPressed) != 0; } }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public bool grasped
        { get { return (m_Flags & InteractionSourceStateFlags.Grasped) != 0; } }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public bool touchpadTouched
        { get { return (m_Flags & InteractionSourceStateFlags.TouchpadTouched) != 0; } }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public bool touchpadPressed
        { get { return (m_Flags & InteractionSourceStateFlags.TouchpadPressed) != 0; } }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public Vector2 touchpadPosition
        { get { return m_TouchpadPosition; } }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public Vector2 thumbstickPosition
        { get { return m_ThumbstickPosition; } }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public bool thumbstickPressed
        { get { return (m_Flags & InteractionSourceStateFlags.ThumbstickPressed) != 0; } }

        internal InteractionSourceProperties m_Properties;
        internal InteractionSource m_Source;
        internal Pose m_HeadPose;

        internal Vector2 m_ThumbstickPosition;
        internal Vector2 m_TouchpadPosition;

        internal float m_SelectPressedAmount;

        internal InteractionSourceStateFlags m_Flags;
    }

    public struct InteractionSourceDetectedEventArgs
    {
        public InteractionSourceState state
        { get; private set; }

        public InteractionSourceDetectedEventArgs(InteractionSourceState state) : this()
        {
            this.state = state;
        }
    }

    public struct InteractionSourceLostEventArgs
    {
        public InteractionSourceState state
        { get; private set; }

        public InteractionSourceLostEventArgs(InteractionSourceState state) : this()
        {
            this.state = state;
        }
    }

    public struct InteractionSourcePressedEventArgs
    {
        public InteractionSourceState state
        { get; private set; }

        public InteractionSourcePressType pressType
        { get; private set; }

        public InteractionSourcePressedEventArgs(InteractionSourceState state, InteractionSourcePressType pressType) : this()
        {
            this.state = state;
            this.pressType = pressType;
        }
    }

    public struct InteractionSourceReleasedEventArgs
    {
        public InteractionSourceState state
        { get; private set; }

        public InteractionSourcePressType pressType
        { get; private set; }

        public InteractionSourceReleasedEventArgs(InteractionSourceState state, InteractionSourcePressType pressType) : this()
        {
            this.state = state;
            this.pressType = pressType;
        }
    }

    public struct InteractionSourceUpdatedEventArgs
    {
        public InteractionSourceState state
        { get; private set; }

        public InteractionSourceUpdatedEventArgs(InteractionSourceState state) : this()
        {
            this.state = state;
        }
    }

    [StaticAccessor("SpatialInput", StaticAccessorType.DoubleColon)]
    [NativeHeader("Modules/VR/HoloLens/Gestures/GestureSource.h")]
    public partial class InteractionManager
    {
        static InteractionManager()
        {
        }

        public static event Action<InteractionSourceDetectedEventArgs> InteractionSourceDetected;
        public static event Action<InteractionSourceLostEventArgs> InteractionSourceLost;
        public static event Action<InteractionSourcePressedEventArgs> InteractionSourcePressed;
        public static event Action<InteractionSourceReleasedEventArgs> InteractionSourceReleased;
        public static event Action<InteractionSourceUpdatedEventArgs> InteractionSourceUpdated;

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public static int GetCurrentReading(InteractionSourceState[] sourceStates)
        {
            return GetCurrentReading_Internal(sourceStates);
        }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public static InteractionSourceState[] GetCurrentReading()
        {
            InteractionSourceState[] sourceStates = new InteractionSourceState[numSourceStates];
            GetCurrentReading_Internal(sourceStates);
            return sourceStates;
        }

        [NativeConditional("ENABLE_HOLOLENS_MODULE")]
        [NativeName("GetCurrentReading")]
        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        private extern static int GetCurrentReading_Internal([NotNull] InteractionSourceState[] sourceStates);

        // In sync with Runtime/HoloLens/Gestures/GestureSource.h
        private enum EventType
        {
            SourceDetected,
            SourceLost,
            SourceUpdated,
            SourcePressed,
            SourceReleased
        }

        [System.Obsolete("Support for built-in VR will be removed in Unity 2020.2. Please update to the new Unity XR Plugin System. More information about the new XR Plugin System can be found at https://docs.unity3d.com/2019.3/Documentation/Manual/XR.html.", false)]
        public extern static int numSourceStates
        {
            [NativeConditional("ENABLE_HOLOLENS_MODULE")]
            [NativeName("GetNumSourceStates")]
            get;
        }

#pragma warning disable 0618
        [RequiredByNativeCode]

        unsafe private static void OnSourceEvent(EventType eventType, IntPtr statePtr, InteractionSourcePressType pressType)
        {
            InteractionSourceState state = *(InteractionSourceState*)statePtr;
            switch (eventType)
            {
                case EventType.SourceDetected:
                {
                    var deprecatedEventHandler = InteractionSourceDetectedLegacy;
                    if (deprecatedEventHandler != null)
                        deprecatedEventHandler(state);

                    var eventHandler = InteractionSourceDetected;
                    if (eventHandler != null)
                        eventHandler(new InteractionSourceDetectedEventArgs(state));
                }
                break;

                case EventType.SourceLost:
                {
                    var deprecatedEventHandler = InteractionSourceLostLegacy;
                    if (deprecatedEventHandler != null)
                        deprecatedEventHandler(state);

                    var eventHandler = InteractionSourceLost;
                    if (eventHandler != null)
                        eventHandler(new InteractionSourceLostEventArgs(state));
                }
                break;

                case EventType.SourceUpdated:
                {
                    var deprecatedEventHandler = InteractionSourceUpdatedLegacy;
                    if (deprecatedEventHandler != null)
                        deprecatedEventHandler(state);

                    var eventHandler = InteractionSourceUpdated;
                    if (eventHandler != null)
                        eventHandler(new InteractionSourceUpdatedEventArgs(state));
                }
                break;

                case EventType.SourcePressed:
                {
                    var deprecatedEventHandler = InteractionSourcePressedLegacy;
                    if (deprecatedEventHandler != null)
                        deprecatedEventHandler(state);

                    var eventHandler = InteractionSourcePressed;
                    if (eventHandler != null)
                        eventHandler(new InteractionSourcePressedEventArgs(state, pressType));
                }
                break;

                case EventType.SourceReleased:
                {
                    var deprecatedEventHandler = InteractionSourceReleasedLegacy;
                    if (deprecatedEventHandler != null)
                        deprecatedEventHandler(state);

                    var eventHandler = InteractionSourceReleased;
                    if (eventHandler != null)
                        eventHandler(new InteractionSourceReleasedEventArgs(state, pressType));
                }
                break;

                default:
                    throw new ArgumentException("OnSourceEvent: Invalid EventType");
            }
        }

#pragma warning restore 0618
    }
}
