using System;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.VR;

public class OVRManager : MonoBehaviour
{
	public static OVRManager instance { get; private set; }

	public static OVRDisplay display { get; private set; }

	public static OVRTracker tracker { get; private set; }

	public static OVRBoundary boundary { get; private set; }

	public static OVRProfile profile
	{
		get
		{
			if (OVRManager._profile == null)
			{
				OVRManager._profile = new OVRProfile();
			}
			return OVRManager._profile;
		}
	}

	private bool paused
	{
		get
		{
			return this._isPaused;
		}
		set
		{
			if (value == this._isPaused)
			{
				return;
			}
			this._isPaused = value;
		}
	}

	[DebuggerBrowsable(DebuggerBrowsableState.Never)]
	public static event Action HMDAcquired;

	[DebuggerBrowsable(DebuggerBrowsableState.Never)]
	public static event Action HMDLost;

	[DebuggerBrowsable(DebuggerBrowsableState.Never)]
	public static event Action HMDMounted;

	[DebuggerBrowsable(DebuggerBrowsableState.Never)]
	public static event Action HMDUnmounted;

	[DebuggerBrowsable(DebuggerBrowsableState.Never)]
	public static event Action VrFocusAcquired;

	[DebuggerBrowsable(DebuggerBrowsableState.Never)]
	public static event Action VrFocusLost;

	[DebuggerBrowsable(DebuggerBrowsableState.Never)]
	public static event Action AudioOutChanged;

	[DebuggerBrowsable(DebuggerBrowsableState.Never)]
	public static event Action AudioInChanged;

	[DebuggerBrowsable(DebuggerBrowsableState.Never)]
	public static event Action TrackingAcquired;

	[DebuggerBrowsable(DebuggerBrowsableState.Never)]
	public static event Action TrackingLost;

	[Obsolete]
	[DebuggerBrowsable(DebuggerBrowsableState.Never)]
	public static event Action HSWDismissed;

	public static bool isHmdPresent
	{
		get
		{
			if (!OVRManager._isHmdPresentCached)
			{
				OVRManager._isHmdPresentCached = true;
				OVRManager._isHmdPresent = OVRPlugin.hmdPresent;
			}
			return OVRManager._isHmdPresent;
		}
		private set
		{
			OVRManager._isHmdPresentCached = true;
			OVRManager._isHmdPresent = value;
		}
	}

	public static string audioOutId
	{
		get
		{
			return OVRPlugin.audioOutId;
		}
	}

	public static string audioInId
	{
		get
		{
			return OVRPlugin.audioInId;
		}
	}

	public static bool hasVrFocus
	{
		get
		{
			if (!OVRManager._hasVrFocusCached)
			{
				OVRManager._hasVrFocusCached = true;
				OVRManager._hasVrFocus = OVRPlugin.hasVrFocus;
			}
			return OVRManager._hasVrFocus;
		}
		private set
		{
			OVRManager._hasVrFocusCached = true;
			OVRManager._hasVrFocus = value;
		}
	}

	[Obsolete]
	public static bool isHSWDisplayed
	{
		get
		{
			return false;
		}
	}

	[Obsolete]
	public static void DismissHSWDisplay()
	{
	}

	public bool chromatic
	{
		get
		{
			return OVRManager.isHmdPresent && OVRPlugin.chromatic;
		}
		set
		{
			if (!OVRManager.isHmdPresent)
			{
				return;
			}
			OVRPlugin.chromatic = value;
		}
	}

	public bool monoscopic
	{
		get
		{
			return !OVRManager.isHmdPresent || OVRPlugin.monoscopic;
		}
		set
		{
			if (!OVRManager.isHmdPresent)
			{
				return;
			}
			OVRPlugin.monoscopic = value;
		}
	}

	public int vsyncCount
	{
		get
		{
			if (!OVRManager.isHmdPresent)
			{
				return 1;
			}
			return OVRPlugin.vsyncCount;
		}
		set
		{
			if (!OVRManager.isHmdPresent)
			{
				return;
			}
			OVRPlugin.vsyncCount = value;
		}
	}

	public static float batteryLevel
	{
		get
		{
			if (!OVRManager.isHmdPresent)
			{
				return 1f;
			}
			return OVRPlugin.batteryLevel;
		}
	}

	public static float batteryTemperature
	{
		get
		{
			if (!OVRManager.isHmdPresent)
			{
				return 0f;
			}
			return OVRPlugin.batteryTemperature;
		}
	}

	public static int batteryStatus
	{
		get
		{
			if (!OVRManager.isHmdPresent)
			{
				return -1;
			}
			return (int)OVRPlugin.batteryStatus;
		}
	}

	public static float volumeLevel
	{
		get
		{
			if (!OVRManager.isHmdPresent)
			{
				return 0f;
			}
			return OVRPlugin.systemVolume;
		}
	}

	public static int cpuLevel
	{
		get
		{
			if (!OVRManager.isHmdPresent)
			{
				return 2;
			}
			return OVRPlugin.cpuLevel;
		}
		set
		{
			if (!OVRManager.isHmdPresent)
			{
				return;
			}
			OVRPlugin.cpuLevel = value;
		}
	}

	public static int gpuLevel
	{
		get
		{
			if (!OVRManager.isHmdPresent)
			{
				return 2;
			}
			return OVRPlugin.gpuLevel;
		}
		set
		{
			if (!OVRManager.isHmdPresent)
			{
				return;
			}
			OVRPlugin.gpuLevel = value;
		}
	}

	public static bool isPowerSavingActive
	{
		get
		{
			return OVRManager.isHmdPresent && OVRPlugin.powerSaving;
		}
	}

	public OVRManager.TrackingOrigin trackingOriginType
	{
		get
		{
			if (!OVRManager.isHmdPresent)
			{
				return this._trackingOriginType;
			}
			return (OVRManager.TrackingOrigin)OVRPlugin.GetTrackingOriginType();
		}
		set
		{
			if (!OVRManager.isHmdPresent)
			{
				return;
			}
			if (OVRPlugin.SetTrackingOriginType((OVRPlugin.TrackingOrigin)value))
			{
				this._trackingOriginType = value;
			}
		}
	}

	public bool isSupportedPlatform { get; private set; }

	public bool isUserPresent
	{
		get
		{
			if (!OVRManager._isUserPresentCached)
			{
				OVRManager._isUserPresentCached = true;
				OVRManager._isUserPresent = OVRPlugin.userPresent;
			}
			return OVRManager._isUserPresent;
		}
		private set
		{
			OVRManager._isUserPresentCached = true;
			OVRManager._isUserPresent = value;
		}
	}

	private void Awake()
	{
		if (OVRManager.instance != null)
		{
			base.enabled = false;
			UnityEngine.Object.DestroyImmediate(this);
			return;
		}
		OVRManager.instance = this;
		UnityEngine.Debug.Log(string.Concat(new object[]
		{
			"Unity v",
			Application.unityVersion,
			", Oculus Utilities v",
			OVRPlugin.wrapperVersion,
			", OVRPlugin v",
			OVRPlugin.version,
			", SDK v",
			OVRPlugin.nativeSDKVersion,
			"."
		}));
		if (SystemInfo.graphicsDeviceType != GraphicsDeviceType.Direct3D11)
		{
			UnityEngine.Debug.LogWarning("VR rendering requires Direct3D11. Your graphics device: " + SystemInfo.graphicsDeviceType);
		}
		RuntimePlatform platform = Application.platform;
		this.isSupportedPlatform |= (platform == RuntimePlatform.Android);
		this.isSupportedPlatform |= (platform == RuntimePlatform.OSXEditor);
		this.isSupportedPlatform |= (platform == RuntimePlatform.OSXPlayer);
		this.isSupportedPlatform |= (platform == RuntimePlatform.WindowsEditor);
		this.isSupportedPlatform |= (platform == RuntimePlatform.WindowsPlayer);
		if (!this.isSupportedPlatform)
		{
			UnityEngine.Debug.LogWarning("This platform is unsupported");
			return;
		}
		if (OVRManager.display == null)
		{
			OVRManager.display = new OVRDisplay();
		}
		if (OVRManager.tracker == null)
		{
			OVRManager.tracker = new OVRTracker();
		}
		if (OVRManager.boundary == null)
		{
			OVRManager.boundary = new OVRBoundary();
		}
		if (this.resetTrackerOnLoad)
		{
			OVRManager.display.RecenterPose();
		}
		OVRPlugin.occlusionMesh = false;
		OVRPlugin.ignoreVrFocus = OVRManager.runInBackground;
	}

	private void Update()
	{
		this.paused = !OVRPlugin.hasVrFocus;
		if (OVRPlugin.shouldQuit)
		{
			Application.Quit();
		}
		if (OVRPlugin.shouldRecenter)
		{
			OVRManager.display.RecenterPose();
		}
		if (this.trackingOriginType != this._trackingOriginType)
		{
			this.trackingOriginType = this._trackingOriginType;
		}
		OVRManager.tracker.isEnabled = this.usePositionTracking;
		OVRPlugin.useIPDInPositionTracking = this.useIPDInPositionTracking;
		OVRManager.isHmdPresent = OVRPlugin.hmdPresent;
		if (this.useRecommendedMSAALevel && QualitySettings.antiAliasing != OVRManager.display.recommendedMSAALevel)
		{
			UnityEngine.Debug.Log(string.Concat(new object[]
			{
				"The current MSAA level is ",
				QualitySettings.antiAliasing,
				", but the recommended MSAA level is ",
				OVRManager.display.recommendedMSAALevel,
				". Switching to the recommended level."
			}));
			QualitySettings.antiAliasing = OVRManager.display.recommendedMSAALevel;
		}
		if (OVRManager._wasHmdPresent && !OVRManager.isHmdPresent)
		{
			try
			{
				if (OVRManager.HMDLost != null)
				{
					OVRManager.HMDLost();
				}
			}
			catch (Exception arg)
			{
				UnityEngine.Debug.LogError("Caught Exception: " + arg);
			}
		}
		if (!OVRManager._wasHmdPresent && OVRManager.isHmdPresent)
		{
			try
			{
				if (OVRManager.HMDAcquired != null)
				{
					OVRManager.HMDAcquired();
				}
			}
			catch (Exception arg2)
			{
				UnityEngine.Debug.LogError("Caught Exception: " + arg2);
			}
		}
		OVRManager._wasHmdPresent = OVRManager.isHmdPresent;
		this.isUserPresent = OVRPlugin.userPresent;
		if (OVRManager._wasUserPresent && !this.isUserPresent)
		{
			try
			{
				if (OVRManager.HMDUnmounted != null)
				{
					OVRManager.HMDUnmounted();
				}
			}
			catch (Exception arg3)
			{
				UnityEngine.Debug.LogError("Caught Exception: " + arg3);
			}
		}
		if (!OVRManager._wasUserPresent && this.isUserPresent)
		{
			try
			{
				if (OVRManager.HMDMounted != null)
				{
					OVRManager.HMDMounted();
				}
			}
			catch (Exception arg4)
			{
				UnityEngine.Debug.LogError("Caught Exception: " + arg4);
			}
		}
		OVRManager._wasUserPresent = this.isUserPresent;
		OVRManager.hasVrFocus = OVRPlugin.hasVrFocus;
		if (OVRManager._hadVrFocus && !OVRManager.hasVrFocus)
		{
			try
			{
				if (OVRManager.VrFocusLost != null)
				{
					OVRManager.VrFocusLost();
				}
			}
			catch (Exception arg5)
			{
				UnityEngine.Debug.LogError("Caught Exception: " + arg5);
			}
		}
		if (!OVRManager._hadVrFocus && OVRManager.hasVrFocus)
		{
			try
			{
				if (OVRManager.VrFocusAcquired != null)
				{
					OVRManager.VrFocusAcquired();
				}
			}
			catch (Exception arg6)
			{
				UnityEngine.Debug.LogError("Caught Exception: " + arg6);
			}
		}
		OVRManager._hadVrFocus = OVRManager.hasVrFocus;
		if (this.enableAdaptiveResolution)
		{
			if (VRSettings.renderScale < this.maxRenderScale)
			{
				VRSettings.renderScale = this.maxRenderScale;
			}
			else
			{
				this.maxRenderScale = Mathf.Max(this.maxRenderScale, VRSettings.renderScale);
			}
			float min = this.minRenderScale / VRSettings.renderScale;
			float num = OVRPlugin.GetEyeRecommendedResolutionScale() / VRSettings.renderScale;
			num = Mathf.Clamp(num, min, 1f);
			VRSettings.renderViewportScale = num;
		}
		string audioOutId = OVRPlugin.audioOutId;
		if (!OVRManager.prevAudioOutIdIsCached)
		{
			OVRManager.prevAudioOutId = audioOutId;
			OVRManager.prevAudioOutIdIsCached = true;
		}
		else if (audioOutId != OVRManager.prevAudioOutId)
		{
			try
			{
				if (OVRManager.AudioOutChanged != null)
				{
					OVRManager.AudioOutChanged();
				}
			}
			catch (Exception arg7)
			{
				UnityEngine.Debug.LogError("Caught Exception: " + arg7);
			}
			OVRManager.prevAudioOutId = audioOutId;
		}
		string audioInId = OVRPlugin.audioInId;
		if (!OVRManager.prevAudioInIdIsCached)
		{
			OVRManager.prevAudioInId = audioInId;
			OVRManager.prevAudioInIdIsCached = true;
		}
		else if (audioInId != OVRManager.prevAudioInId)
		{
			try
			{
				if (OVRManager.AudioInChanged != null)
				{
					OVRManager.AudioInChanged();
				}
			}
			catch (Exception arg8)
			{
				UnityEngine.Debug.LogError("Caught Exception: " + arg8);
			}
			OVRManager.prevAudioInId = audioInId;
		}
		if (OVRManager.wasPositionTracked && !OVRManager.tracker.isPositionTracked)
		{
			try
			{
				if (OVRManager.TrackingLost != null)
				{
					OVRManager.TrackingLost();
				}
			}
			catch (Exception arg9)
			{
				UnityEngine.Debug.LogError("Caught Exception: " + arg9);
			}
		}
		if (!OVRManager.wasPositionTracked && OVRManager.tracker.isPositionTracked)
		{
			try
			{
				if (OVRManager.TrackingAcquired != null)
				{
					OVRManager.TrackingAcquired();
				}
			}
			catch (Exception arg10)
			{
				UnityEngine.Debug.LogError("Caught Exception: " + arg10);
			}
		}
		OVRManager.wasPositionTracked = OVRManager.tracker.isPositionTracked;
		OVRManager.display.Update();
		OVRInput.Update();
	}

	private void LateUpdate()
	{
		OVRHaptics.Process();
	}

	private void FixedUpdate()
	{
		OVRInput.FixedUpdate();
	}

	public void ReturnToLauncher()
	{
		OVRManager.PlatformUIConfirmQuit();
	}

	public static void PlatformUIConfirmQuit()
	{
		if (!OVRManager.isHmdPresent)
		{
			return;
		}
		OVRPlugin.ShowUI(OVRPlugin.PlatformUI.ConfirmQuit);
	}

	public static void PlatformUIGlobalMenu()
	{
		if (!OVRManager.isHmdPresent)
		{
			return;
		}
		OVRPlugin.ShowUI(OVRPlugin.PlatformUI.GlobalMenu);
	}

	private static OVRProfile _profile;

	private bool _isPaused;

	private IEnumerable<Camera> disabledCameras;

	private float prevTimeScale;

	private static bool _isHmdPresentCached = false;

	private static bool _isHmdPresent = false;

	private static bool _wasHmdPresent = false;

	private static bool _hasVrFocusCached = false;

	private static bool _hasVrFocus = false;

	private static bool _hadVrFocus = false;

	public bool queueAhead = true;

	public bool useRecommendedMSAALevel;

	public bool enableAdaptiveResolution;

	[Range(0.5f, 2f)]
	public float maxRenderScale = 1f;

	[Range(0.5f, 2f)]
	public float minRenderScale = 0.7f;

	[SerializeField]
	private OVRManager.TrackingOrigin _trackingOriginType;

	public bool usePositionTracking = true;

	public bool useIPDInPositionTracking = true;

	public bool resetTrackerOnLoad;

	private static bool _isUserPresentCached = false;

	private static bool _isUserPresent = false;

	private static bool _wasUserPresent = false;

	private static bool prevAudioOutIdIsCached = false;

	private static bool prevAudioInIdIsCached = false;

	private static string prevAudioOutId = string.Empty;

	private static string prevAudioInId = string.Empty;

	private static bool wasPositionTracked = false;

	[SerializeField]
	[HideInInspector]
	internal static bool runInBackground = false;

	public enum TrackingOrigin
	{
		EyeLevel,
		FloorLevel
	}
}
