using System;
using System.Collections;
using Leap.Unity;
using UnityEngine;

public class OvrMgr : MonoBehaviour
{
	public OvrCamera OvrCamera { get; private set; }

	public Transform EyeAnchor
	{
		get
		{
			return this.m_trEyeAnchor;
		}
	}

	public RenderTexture UIRTarget
	{
		get
		{
			return this.m_rtRTarget;
		}
	}

	public OvrMgr.OvrObject ovr_obj
	{
		get
		{
			return this.ovr_obj_;
		}
	}

	public Transform GetVRControllerTransform(bool f_bLeft)
	{
		return (!f_bLeft) ? this.ovr_obj_.right_controller.hand_trans : this.ovr_obj_.left_controller.hand_trans;
	}

	public AVRControllerButtons GetVRControllerButtons(bool f_bLeft)
	{
		return (!f_bLeft) ? this.ovr_obj_.right_controller.controller_buttons : this.ovr_obj_.left_controller.controller_buttons;
	}

	public UICamera SystemUICamera
	{
		get
		{
			return this.m_SysUiCam;
		}
	}

	public AVRTrackingObjectsMgr TrackingObjectsMgr
	{
		get
		{
			return this.m_TrackingObjectsMgr;
		}
	}

	public void Init()
	{
		this.ovr_obj_ = new OvrMgr.OvrObject();
		GameMain.Instance.MainCamera.GetComponent<AudioListener>().enabled = false;
		GameMain.Instance.MainCamera.enabled = false;
		GameMain.Instance.MainCamera.GetComponent<Camera>().enabled = false;
		GameMain.Instance.MainCamera.GetComponent<CameraMain>().enabled = false;
		GameMain.Instance.MainCamera.GetComponent<UltimateOrbitCamera>().enabled = false;
		GameMain.Instance.MainCamera.GetComponent<Bloom>().enabled = false;
		GameMain.Instance.MainCamera.GetComponent<ScreenOverlay>().enabled = false;
		GameMain.Instance.MainCamera.GetComponent<FadeInAndFadeOutOnGUI>().enabled = false;
		if (this.m_rtRTarget == null)
		{
			this.m_rtRTarget = Resources.Load<RenderTexture>("OVR/rtOVRUI");
		}
		Transform transform = base.gameObject.transform.Find("SystemUI Root");
		this.m_SysUiCam = transform.Find("Camera").GetComponent<UICamera>();
		this.m_SysUiCam.CreateVirtualCursorObj();
		Transform f_trParent = null;
		if (GameMain.Instance.VRFamily == GameMain.VRFamilyType.Oculus)
		{
			this.ovr_obj_.right_controller = new OvrMgr.OvrObject.Controller();
			this.ovr_obj_.left_controller = new OvrMgr.OvrObject.Controller();
			this.m_goOvrRig = (UnityEngine.Object.Instantiate(Resources.Load("OVR/OVRCameraRig")) as GameObject);
			this.m_goOvrRig.transform.parent = GameMain.Instance.gameObject.transform;
			this.OvrCamera = this.m_goOvrRig.GetComponent<OvrCamera>();
			this.ovr_obj_.right_controller.grip_collider.pair_object = this.ovr_obj_.left_controller.grip_collider;
			this.ovr_obj_.left_controller.grip_collider.pair_object = this.ovr_obj_.right_controller.grip_collider;
			this.m_trEyeAnchor = this.OvrCamera.m_trCenterEye;
			f_trParent = this.m_trEyeAnchor;
		}
		else if (GameMain.Instance.VRFamily == GameMain.VRFamilyType.HTC)
		{
			this.ovr_obj_.right_controller = new OvrMgr.OvrObject.Controller();
			this.ovr_obj_.left_controller = new OvrMgr.OvrObject.Controller();
			GameObject gameObject = UnityEngine.Object.Instantiate(Resources.Load("SteamVR/[SteamVR]")) as GameObject;
			gameObject.transform.parent = GameMain.Instance.gameObject.transform;
			GameObject gameObject2 = UnityEngine.Object.Instantiate(Resources.Load("SteamVR/[Status]")) as GameObject;
			gameObject2.transform.parent = GameMain.Instance.gameObject.transform;
			this.m_goOvrRig = (UnityEngine.Object.Instantiate(Resources.Load("SteamVR/ViveCameraRig")) as GameObject);
			this.m_goOvrRig.transform.parent = GameMain.Instance.gameObject.transform;
			this.OvrCamera = this.m_goOvrRig.GetComponent<ViveCamera>();
			this.ovr_obj_.right_controller.grip_collider.pair_object = this.ovr_obj_.left_controller.grip_collider;
			this.ovr_obj_.left_controller.grip_collider.pair_object = this.ovr_obj_.right_controller.grip_collider;
			this.m_trEyeAnchor = this.OvrCamera.m_trCenterEye;
			f_trParent = this.m_goOvrRig.transform.Find("Main Camera (head)");
			this.m_TrackingObjectsMgr = this.m_goOvrRig.GetComponentInChildren<ViveBaseStationHide>();
		}
		else if (GameMain.Instance.VRFamily == GameMain.VRFamilyType.FOVE)
		{
			this.ovr_obj_.right_controller = new OvrMgr.OvrObject.Controller();
			this.ovr_obj_.left_controller = new OvrMgr.OvrObject.Controller();
			this.m_goOvrRig = (UnityEngine.Object.Instantiate(Resources.Load("Fove/Fove Rig")) as GameObject);
			this.m_goOvrRig.transform.parent = GameMain.Instance.gameObject.transform;
			this.OvrCamera = this.m_goOvrRig.GetComponent<FoveCamera>();
			this.m_trEyeAnchor = this.m_goOvrRig.transform.Find("Fove Interface");
			f_trParent = this.m_trEyeAnchor;
		}
		else if (GameMain.Instance.VRFamily == GameMain.VRFamilyType.NON)
		{
			this.ovr_obj_.right_controller = new OvrMgr.OvrObject.Controller();
			this.ovr_obj_.left_controller = new OvrMgr.OvrObject.Controller();
			this.m_goOvrRig = (UnityEngine.Object.Instantiate(Resources.Load("DummyVR/DummyVRCameraRig")) as GameObject);
			this.m_goOvrRig.transform.parent = GameMain.Instance.gameObject.transform;
			this.OvrCamera = this.m_goOvrRig.GetComponent<DummyVRCamera>();
			this.m_trEyeAnchor = this.OvrCamera.m_trCenterEye;
			f_trParent = this.m_trEyeAnchor;
			this.m_SysUiCam.HideVirtualCursorObj();
		}
		UICamera[] array = UnityEngine.Object.FindObjectsOfType<UICamera>();
		for (int i = 0; i < array.Length; i++)
		{
			if (!array[i].VRExclusionUI)
			{
				array[i].GetComponent<Camera>().targetTexture = this.m_rtRTarget;
			}
		}
		GameObject gameObject3 = GameObject.Find("UI Root");
		if (gameObject3 != null)
		{
			UICamera[] componentsInChildren = gameObject3.GetComponentsInChildren<UICamera>(true);
			if (componentsInChildren != null)
			{
				foreach (UICamera uicamera in componentsInChildren)
				{
					if (uicamera.GetComponent<Camera>() != null)
					{
						uicamera.GetComponent<Camera>().clearFlags = CameraClearFlags.Color;
					}
				}
			}
		}
		UIRoot[] array3 = UnityEngine.Object.FindObjectsOfType<UIRoot>();
		for (int k = 0; k < array3.Length; k++)
		{
			array3[k].scalingStyle = UIRoot.Scaling.Constrained;
			array3[k].minimumHeight = 1080;
			array3[k].maximumHeight = 1080;
			array3[k].fitWidth = false;
			array3[k].fitHeight = true;
		}
		this.m_goAudioMgr = UTY.GetChildObject(GameMain.Instance.gameObject, "Camera/CameraMain/CameraMain/AudioMgr", false);
		this.m_goAudioMgr.transform.SetParent(this.m_trEyeAnchor, false);
		this.m_coLeapConnectWait = base.StartCoroutine(this.CoLeapLoad(f_trParent));
	}

	private IEnumerator CoLeapLoad(Transform f_trParent)
	{
		UnityEngine.Object objLeap = Resources.Load("LeapMotion/LMHeadMountedRig");
		this.m_goLeapMotion = (UnityEngine.Object.Instantiate(objLeap) as GameObject);
		this.m_goLeapMotion.transform.SetParent(GameMain.Instance.gameObject.transform);
		this.m_LeapMgr = this.m_goLeapMotion.GetComponent<LeapMgr>();
		this.m_trLeapHandL = this.m_LeapMgr.m_trHandHitL;
		this.m_trLeapHandR = this.m_LeapMgr.m_trHandHitR;
		LeapServiceProvider rsp = this.m_goLeapMotion.GetComponentInChildren<LeapServiceProvider>(true);
		float time = Time.realtimeSinceStartup;
		bool bTimeout = false;
		Debug.Log("Leap Connect wait1 loop start.");
		while (rsp.GetLeapController() == null)
		{
			yield return null;
			if (5f < Time.realtimeSinceStartup - time)
			{
				Debug.Log("Leap Unconnected timeout");
				bTimeout = true;
				break;
			}
		}
		if (!bTimeout)
		{
			time = Time.realtimeSinceStartup;
			Debug.Log("Leap Connect wait2 loop start.");
			while (!rsp.IsConnected())
			{
				yield return null;
				if (5f < Time.realtimeSinceStartup - time)
				{
					Debug.Log("Leap Unconnected timeout");
					bTimeout = true;
					break;
				}
			}
			if (!bTimeout)
			{
				Debug.Log("Leap Connected !!");
				this.m_goLeapMotion.transform.SetParent(f_trParent, false);
				this.m_goLeapMotion.transform.localPosition = new Vector3(0f, 0f, 0.1f);
				this.m_bEnableLeapMotion = true;
			}
		}
		if (bTimeout)
		{
			UnityEngine.Object.Destroy(this.m_goLeapMotion);
			this.m_goLeapMotion = null;
		}
		this.m_coLeapConnectWait = null;
		yield break;
	}

	public void Uninit()
	{
		if (GameMain.Instance == null)
		{
			return;
		}
		this.OvrCamera.Uninit();
		Transform transform = base.gameObject.transform.Find("SystemUI Root");
		if (transform != null)
		{
			UIRoot component = transform.GetComponent<UIRoot>();
			component.scalingStyle = UIRoot.Scaling.Constrained;
			component.minimumHeight = 1080;
			component.maximumHeight = 1080;
			component.fitWidth = true;
			component.fitHeight = true;
			Transform transform2 = transform.Find("Camera");
			UICamera component2 = transform2.GetComponent<UICamera>();
			component2.DeleteVirtualCursorObj();
			component2.GetComponent<Camera>().targetTexture = null;
		}
		UICamera[] array = UnityEngine.Object.FindObjectsOfType<UICamera>();
		for (int i = 0; i < array.Length; i++)
		{
			array[i].GetComponent<Camera>().targetTexture = null;
		}
		GameObject gameObject = GameObject.Find("UI Root");
		if (gameObject != null)
		{
			UICamera[] componentsInChildren = gameObject.GetComponentsInChildren<UICamera>(true);
			if (componentsInChildren != null)
			{
				foreach (UICamera uicamera in componentsInChildren)
				{
					if (uicamera.GetComponent<Camera>() != null)
					{
						uicamera.GetComponent<Camera>().clearFlags = CameraClearFlags.Depth;
					}
				}
			}
		}
		UIRoot[] array3 = UnityEngine.Object.FindObjectsOfType<UIRoot>();
		for (int k = 0; k < array3.Length; k++)
		{
			array3[k].scalingStyle = UIRoot.Scaling.Constrained;
			array3[k].minimumHeight = 1080;
			array3[k].maximumHeight = 1080;
			array3[k].fitWidth = true;
			array3[k].fitHeight = true;
		}
		Debug.Log("OvrMgr Uninit");
		if (this.m_goAudioMgr != null)
		{
			Transform transform3 = GameMain.Instance.transform.Find("Camera/CameraMain/CameraMain");
			if (transform3 != null)
			{
				this.m_goAudioMgr.transform.SetParent(transform3, false);
			}
			this.m_goAudioMgr = null;
		}
		if (this.m_coLeapConnectWait != null)
		{
			base.StopCoroutine(this.m_coLeapConnectWait);
			this.m_coLeapConnectWait = null;
		}
		if (this.m_goLeapMotion != null)
		{
			UnityEngine.Object.DestroyImmediate(this.m_goLeapMotion);
			this.m_goLeapMotion = null;
		}
		if (this.m_goOvrRig != null)
		{
			UnityEngine.Object.DestroyImmediate(this.m_goOvrRig);
		}
		this.OvrCamera = null;
		this.m_trEyeAnchor = null;
		this.ovr_obj_ = null;
		if (GameMain.Instance.MainCamera != null)
		{
			GameMain.Instance.MainCamera.enabled = true;
			GameMain.Instance.MainCamera.GetComponent<Camera>().enabled = true;
			GameMain.Instance.MainCamera.GetComponent<CameraMain>().enabled = true;
			GameMain.Instance.MainCamera.GetComponent<UltimateOrbitCamera>().enabled = true;
			GameMain.Instance.MainCamera.GetComponent<FadeInAndFadeOutOnGUI>().enabled = true;
			GameMain.Instance.MainCamera.GetComponent<AudioListener>().enabled = true;
		}
	}

	public void OnLevelWasLoaded(int level)
	{
		Debug.Log("OvrMgr OnLevelWasLoaded!");
	}

	private GameObject m_goOvrRig;

	private Transform m_trEyeAnchor;

	private RenderTexture m_rtRTarget;

	public GameObject m_goLeapMotion;

	public bool m_bEnableLeapMotion;

	public LeapMgr m_LeapMgr;

	public Transform m_trLeapHandL;

	public Transform m_trLeapHandR;

	private AVRTrackingObjectsMgr m_TrackingObjectsMgr;

	private GameObject m_goAudioMgr;

	private Coroutine m_coLeapConnectWait;

	private UICamera m_SysUiCam;

	private OvrMgr.OvrObject ovr_obj_;

	public class OvrObject
	{
		public OvrMgr.OvrObject.Controller right_controller;

		public OvrMgr.OvrObject.Controller left_controller;

		public class Controller
		{
			public OvrGripCollider grip_collider;

			public Transform hand_trans;

			public Transform hand_trans_hit;

			public AVRController controller;

			public AVRControllerButtons controller_buttons;
		}
	}
}
