using UnityEngine;

namespace Passer.Humanoid {
    using Passer.Tracking;

    [System.Serializable]
    public class UnityXRHead : HeadSensor {
#if pUNITYXR
        public override string name {
            get { return "Unity XR"; }
        }

        protected UnityXRTracker unityXR {
            get { return humanoid.unityXR; }
        }

        #region Manage

        public override void CheckSensor(HeadTarget headTarget) {
            if (this.headTarget == null)
                this.target = headTarget;
            if (this.headTarget == null)
                return;

            if (unityXR.trackerComponent == null) {
                unityXR.CheckTracker(humanoid);
            }

            if (enabled && unityXR.trackerComponent != null && unityXR.trackerComponent.enabled) {
                if (hmd == null) {
                    Vector3 position = headTarget.transform.TransformPoint(headTarget.head2eyes);
                    Quaternion rotation = headTarget.transform.rotation;
                    UnityXR unityXRtracker = unityXR.trackerComponent as UnityXR;
                    hmd = unityXRtracker.GetHmd(position, rotation);
                }
                if (hmd != null)
                    sensorTransform = hmd.transform;

                if (!Application.isPlaying)
                    SetSensor2Target();
            }
            else {
#if UNITY_EDITOR
                if (!Application.isPlaying) {
                    if (hmd != null)
                        Object.DestroyImmediate(hmd.gameObject, true);
                }
#endif
                hmd = null;
                sensorTransform = null;
            }
        }

        #endregion

        #region Start

        public override void Start(HumanoidControl _humanoid, Transform targetTransform) {
            base.Start(_humanoid, targetTransform);

            tracker = headTarget.humanoid.unityXR;
            if (tracker == null || !tracker.enabled || !enabled)
                return;

            Vector3 position = headTarget.transform.TransformPoint(headTarget.head2eyes);
            Quaternion rotation = headTarget.transform.rotation;

#if hSTEAMVR && (UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX)
            if (UnityXRDevice.xrDevice == UnityXRDevice.XRDeviceType.OpenVR) {
                SteamVR steamVRtracker = unityXR.tracker as SteamVR;
                hmd = steamVRtracker.GetHmd(position, rotation);
            }
            else
#endif 
            {
                UnityXR unityXRtracker = unityXR.trackerComponent as UnityXR;
                hmd = unityXRtracker.GetHmd(position, rotation);
            }
            if (hmd != null)
                sensorTransform = hmd.transform;

            SetSensor2Target();
            CheckSensorTransform();
            sensor2TargetPosition = -headTarget.head2eyes;

            if (hmd != null)
                hmd.StartComponent(unityXR.trackerComponent.transform);
        }

        #endregion

        #region Update

        protected bool calibrated = false;
        public override void Update() {
            status = Tracker.Status.Unavailable;
            if (unityXR.trackerComponent == null || !unityXR.trackerComponent.enabled || !enabled)
                return;

            if (hmd == null)
                return;

            hmd.UpdateComponent();
            status = hmd.status;
            if (status != Tracker.Status.Tracking)
                return;

            UpdateTarget(headTarget.head.target, hmd);
            UpdateNeckTargetFromHead();

            if (!calibrated) {
                if (tracker.humanoid.calibrateAtStart)
                    tracker.humanoid.Calibrate();
                calibrated = true;
            }
        }

        #endregion
#endif
    }
}