using System;
using UnityEngine;
using UnityEngine.VR;

namespace RenderHeads.Media.AVProVideo.Demos
{
	public class SphereDemo : MonoBehaviour
	{
		private void Start()
		{
			if (VRDevice.isPresent)
			{
				return;
			}
			if (SystemInfo.supportsGyroscope)
			{
				Input.gyro.enabled = true;
				base.transform.parent.Rotate(new Vector3(90f, 0f, 0f));
			}
		}

		private void OnDestroy()
		{
			if (SystemInfo.supportsGyroscope)
			{
				Input.gyro.enabled = false;
			}
		}

		private void Update()
		{
			if (VRDevice.isPresent)
			{
				if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space))
				{
					InputTracking.Recenter();
				}
				if (Input.GetKeyDown(KeyCode.V))
				{
					VRSettings.enabled = !VRSettings.enabled;
				}
			}
			else
			{
				if (SystemInfo.supportsGyroscope)
				{
					base.transform.localRotation = new Quaternion(Input.gyro.attitude.x, Input.gyro.attitude.y, -Input.gyro.attitude.z, -Input.gyro.attitude.w);
				}
				else if (Input.GetMouseButton(0))
				{
					float num = 40f * -Input.GetAxis("Mouse X") * Time.deltaTime;
					float num2 = 40f * Input.GetAxis("Mouse Y") * Time.deltaTime;
					num = Mathf.Clamp(num, -0.5f, 0.5f);
					num2 = Mathf.Clamp(num2, -0.5f, 0.5f);
					this._spinX += num;
					this._spinY += num2;
				}
				if (!Mathf.Approximately(this._spinX, 0f) || !Mathf.Approximately(this._spinY, 0f))
				{
					base.transform.Rotate(Vector3.up, this._spinX);
					base.transform.Rotate(Vector3.right, this._spinY);
					this._spinX = Mathf.MoveTowards(this._spinX, 0f, 5f * Time.deltaTime);
					this._spinY = Mathf.MoveTowards(this._spinY, 0f, 5f * Time.deltaTime);
				}
			}
		}

		private float _spinX;

		private float _spinY;
	}
}
