/**********************************************************************************
* Blueprint Reality Inc. CONFIDENTIAL
* 2020 Blueprint Reality Inc.
* All Rights Reserved.
*
* NOTICE:  All information contained herein is, and remains, the property of
* Blueprint Reality Inc. and its suppliers, if any.  The intellectual and
* technical concepts contained herein are proprietary to Blueprint Reality Inc.
* and its suppliers and may be covered by Patents, pending patents, and are
* protected by trade secret or copyright law.
*
* Dissemination of this information or reproduction of this material is strictly
* forbidden unless prior written permission is obtained from Blueprint Reality Inc.
***********************************************************************************/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_2017_2_OR_NEWER
using UnityEngine.XR;
#else
using XRSettings = UnityEngine.VR.VRSettings;
#endif

namespace BlueprintReality.MixCast
{
    public class XrPlatformInfo : MonoBehaviour
    {
        public enum XrPlatform
        {
            None, SteamVR, Oculus
        }

        private const string OPENVR_DEVICE_NAME = "OpenVR";
        private const string OCULUS_DEVICE_NAME = "Oculus";

        public static readonly Vector3 VIVE_HMD_TO_HEAD = new Vector3(0, 0, -0.075f);
        public static readonly Vector3 OCULUS_RIFT_HMD_TO_HEAD = new Vector3(0, 0, -0.075f);

        
        public static XrPlatform ActivePlatform
        {
            get
            {
                if (destroyed)
                    return XrPlatform.None;
                if (instance == null)
                {
                    GameObject instanceObj = new GameObject("XrInfo") { hideFlags = HideFlags.HideAndDontSave };
                    DontDestroyOnLoad(instanceObj);
                    instance = instanceObj.AddComponent<XrPlatformInfo>();
                }
                return instance.activePlatform;
            }
        }
        public static event System.Action OnPlatformLoaded;

        private static XrPlatformInfo instance;
        private static bool destroyed = false;


        private XrPlatform activePlatform;

        private void Start()
        {
            RefreshRuntimeInfo();
        }
        private void OnDestroy()
        {
            destroyed = true;
        }

        private bool lastEnabled;
        private void Update()
        {
            if (lastEnabled != XRSettings.enabled)
            {
                RefreshRuntimeInfo();
            }
        }
        void RefreshRuntimeInfo()
        {
            if (!XRSettings.enabled)
                activePlatform = XrPlatform.None;
            else
            {
                if (XRSettings.loadedDeviceName == OPENVR_DEVICE_NAME)
                    activePlatform = XrPlatform.SteamVR;
                else if (XRSettings.loadedDeviceName == OCULUS_DEVICE_NAME)
                    activePlatform = XrPlatform.Oculus;
            }
            lastEnabled = XRSettings.enabled;

            if (OnPlatformLoaded != null)
                OnPlatformLoaded();
        }
    }
}
