using System;
using System.Diagnostics;
using UnityEngine;
using UnityEngine.VR;

[ExecuteInEditMode]
public class OVRCameraRig : MonoBehaviour
{
	public Camera leftEyeCamera
	{
		get
		{
			return (!this.usePerEyeCameras) ? this._centerEyeCamera : this._leftEyeCamera;
		}
	}

	public Camera rightEyeCamera
	{
		get
		{
			return (!this.usePerEyeCameras) ? this._centerEyeCamera : this._rightEyeCamera;
		}
	}

	public Transform trackingSpace { get; private set; }

	public Transform leftEyeAnchor { get; private set; }

	public Transform centerEyeAnchor { get; private set; }

	public Transform rightEyeAnchor { get; private set; }

	public Transform leftHandAnchor { get; private set; }

	public Transform rightHandAnchor { get; private set; }

	public Transform trackerAnchor { get; private set; }

	[DebuggerBrowsable(DebuggerBrowsableState.Never)]
	public event Action<OVRCameraRig> UpdatedAnchors;

	private void Awake()
	{
		this.EnsureGameObjectIntegrity();
	}

	private void Start()
	{
		this.EnsureGameObjectIntegrity();
		if (!Application.isPlaying)
		{
			return;
		}
		this.UpdateAnchors();
	}

	private void Update()
	{
		this.EnsureGameObjectIntegrity();
		if (!Application.isPlaying)
		{
			return;
		}
		this.UpdateAnchors();
	}

	private void UpdateAnchors()
	{
		bool monoscopic = OVRManager.instance.monoscopic;
		OVRPose pose = OVRManager.tracker.GetPose(0);
		this.trackerAnchor.localRotation = pose.orientation;
		this.centerEyeAnchor.localRotation = InputTracking.GetLocalRotation(VRNode.CenterEye);
		this.leftEyeAnchor.localRotation = ((!monoscopic) ? InputTracking.GetLocalRotation(VRNode.LeftEye) : this.centerEyeAnchor.localRotation);
		this.rightEyeAnchor.localRotation = ((!monoscopic) ? InputTracking.GetLocalRotation(VRNode.RightEye) : this.centerEyeAnchor.localRotation);
		this.leftHandAnchor.localRotation = OVRInput.GetLocalControllerRotation(OVRInput.Controller.LTouch);
		this.rightHandAnchor.localRotation = OVRInput.GetLocalControllerRotation(OVRInput.Controller.RTouch);
		this.trackerAnchor.localPosition = pose.position;
		this.centerEyeAnchor.localPosition = InputTracking.GetLocalPosition(VRNode.CenterEye);
		this.leftEyeAnchor.localPosition = ((!monoscopic) ? InputTracking.GetLocalPosition(VRNode.LeftEye) : this.centerEyeAnchor.localPosition);
		this.rightEyeAnchor.localPosition = ((!monoscopic) ? InputTracking.GetLocalPosition(VRNode.RightEye) : this.centerEyeAnchor.localPosition);
		this.leftHandAnchor.localPosition = OVRInput.GetLocalControllerPosition(OVRInput.Controller.LTouch);
		this.rightHandAnchor.localPosition = OVRInput.GetLocalControllerPosition(OVRInput.Controller.RTouch);
		if (this.UpdatedAnchors != null)
		{
			this.UpdatedAnchors(this);
		}
	}

	public void EnsureGameObjectIntegrity()
	{
		if (this.trackingSpace == null)
		{
			this.trackingSpace = this.ConfigureRootAnchor(this.trackingSpaceName);
		}
		if (this.leftEyeAnchor == null)
		{
			this.leftEyeAnchor = this.ConfigureEyeAnchor(this.trackingSpace, VRNode.LeftEye);
		}
		if (this.centerEyeAnchor == null)
		{
			this.centerEyeAnchor = this.ConfigureEyeAnchor(this.trackingSpace, VRNode.CenterEye);
		}
		if (this.rightEyeAnchor == null)
		{
			this.rightEyeAnchor = this.ConfigureEyeAnchor(this.trackingSpace, VRNode.RightEye);
		}
		if (this.leftHandAnchor == null)
		{
			this.leftHandAnchor = this.ConfigureHandAnchor(this.trackingSpace, OVRPlugin.Node.HandLeft);
		}
		if (this.rightHandAnchor == null)
		{
			this.rightHandAnchor = this.ConfigureHandAnchor(this.trackingSpace, OVRPlugin.Node.HandRight);
		}
		if (this.trackerAnchor == null)
		{
			this.trackerAnchor = this.ConfigureTrackerAnchor(this.trackingSpace);
		}
		if (this._centerEyeCamera == null || this._leftEyeCamera == null || this._rightEyeCamera == null)
		{
			this._centerEyeCamera = this.centerEyeAnchor.GetComponent<Camera>();
			this._leftEyeCamera = this.leftEyeAnchor.GetComponent<Camera>();
			this._rightEyeCamera = this.rightEyeAnchor.GetComponent<Camera>();
			if (this._centerEyeCamera == null)
			{
				this._centerEyeCamera = this.centerEyeAnchor.gameObject.AddComponent<Camera>();
				this._centerEyeCamera.tag = "MainCamera";
			}
			if (this._leftEyeCamera == null)
			{
				this._leftEyeCamera = this.leftEyeAnchor.gameObject.AddComponent<Camera>();
				this._leftEyeCamera.tag = "MainCamera";
			}
			if (this._rightEyeCamera == null)
			{
				this._rightEyeCamera = this.rightEyeAnchor.gameObject.AddComponent<Camera>();
				this._rightEyeCamera.tag = "MainCamera";
			}
			this._centerEyeCamera.stereoTargetEye = StereoTargetEyeMask.Both;
			this._leftEyeCamera.stereoTargetEye = StereoTargetEyeMask.Left;
			this._rightEyeCamera.stereoTargetEye = StereoTargetEyeMask.Right;
		}
		this._centerEyeCamera.enabled = !this.usePerEyeCameras;
		this._leftEyeCamera.enabled = this.usePerEyeCameras;
		this._rightEyeCamera.enabled = this.usePerEyeCameras;
	}

	private Transform ConfigureRootAnchor(string name)
	{
		Transform transform = base.transform.Find(name);
		if (transform == null)
		{
			transform = new GameObject(name).transform;
		}
		transform.parent = base.transform;
		transform.localScale = Vector3.one;
		transform.localPosition = Vector3.zero;
		transform.localRotation = Quaternion.identity;
		return transform;
	}

	private Transform ConfigureEyeAnchor(Transform root, VRNode eye)
	{
		string str = (eye != VRNode.CenterEye) ? ((eye != VRNode.LeftEye) ? "Right" : "Left") : "Center";
		string text = str + this.eyeAnchorName;
		Transform transform = base.transform.Find(root.name + "/" + text);
		if (transform == null)
		{
			transform = base.transform.Find(text);
		}
		if (transform == null)
		{
			string name = this.legacyEyeAnchorName + eye.ToString();
			transform = base.transform.Find(name);
		}
		if (transform == null)
		{
			transform = new GameObject(text).transform;
		}
		transform.name = text;
		transform.parent = root;
		transform.localScale = Vector3.one;
		transform.localPosition = Vector3.zero;
		transform.localRotation = Quaternion.identity;
		return transform;
	}

	private Transform ConfigureHandAnchor(Transform root, OVRPlugin.Node hand)
	{
		string str = (hand != OVRPlugin.Node.HandLeft) ? "Right" : "Left";
		string text = str + this.handAnchorName;
		Transform transform = base.transform.Find(root.name + "/" + text);
		if (transform == null)
		{
			transform = base.transform.Find(text);
		}
		if (transform == null)
		{
			transform = new GameObject(text).transform;
		}
		transform.name = text;
		transform.parent = root;
		transform.localScale = Vector3.one;
		transform.localPosition = Vector3.zero;
		transform.localRotation = Quaternion.identity;
		return transform;
	}

	private Transform ConfigureTrackerAnchor(Transform root)
	{
		string text = this.trackerAnchorName;
		Transform transform = base.transform.Find(root.name + "/" + text);
		if (transform == null)
		{
			transform = new GameObject(text).transform;
		}
		transform.parent = root;
		transform.localScale = Vector3.one;
		transform.localPosition = Vector3.zero;
		transform.localRotation = Quaternion.identity;
		return transform;
	}

	public bool usePerEyeCameras;

	private readonly string trackingSpaceName = "TrackingSpace";

	private readonly string trackerAnchorName = "TrackerAnchor";

	private readonly string eyeAnchorName = "EyeAnchor";

	private readonly string handAnchorName = "HandAnchor";

	private readonly string legacyEyeAnchorName = "Camera";

	private Camera _centerEyeCamera;

	private Camera _leftEyeCamera;

	private Camera _rightEyeCamera;
}
