using System;
using UnityEngine;
using UnityEngine.VR;

public class OVROverlay : MonoBehaviour
{
	public void OverrideOverlayTextureInfo(Texture srcTexture, IntPtr nativePtr, VRNode node)
	{
		int num = (node != VRNode.RightEye) ? 0 : 1;
		this.textures[num] = srcTexture;
		this.cachedTextures[num] = srcTexture;
		this.texNativePtrs[num] = nativePtr;
	}

	private void Awake()
	{
		Debug.Log("Overlay Awake");
		this.rend = base.GetComponent<Renderer>();
		for (int i = 0; i < 2; i++)
		{
			if (this.rend != null && this.textures[i] == null)
			{
				this.textures[i] = this.rend.material.mainTexture;
			}
			if (this.textures[i] != null)
			{
				this.cachedTextures[i] = this.textures[i];
				this.texNativePtrs[i] = this.textures[i].GetNativeTexturePtr();
			}
		}
	}

	private void OnEnable()
	{
		if (!OVRManager.isHmdPresent)
		{
			base.enabled = false;
			return;
		}
		this.OnDisable();
		for (int i = 0; i < 15; i++)
		{
			if (OVROverlay.instances[i] == null || OVROverlay.instances[i] == this)
			{
				this.layerIndex = i;
				OVROverlay.instances[i] = this;
				break;
			}
		}
	}

	private void OnDisable()
	{
		if (this.layerIndex != -1)
		{
			OVRPlugin.SetOverlayQuad(true, false, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, OVRPose.identity.ToPosef(), Vector3.one.ToVector3f(), this.layerIndex, OVRPlugin.OverlayShape.Quad);
			OVROverlay.instances[this.layerIndex] = null;
		}
		this.layerIndex = -1;
	}

	private void OnRenderObject()
	{
		if (!Camera.current.CompareTag("MainCamera") || Camera.current.cameraType != CameraType.Game || this.layerIndex == -1 || this.currentOverlayType == OVROverlay.OverlayType.None)
		{
			return;
		}
		if (this.currentOverlayShape == OVROverlay.OverlayShape.Cylinder)
		{
			Debug.LogWarning("Overlay shape " + this.currentOverlayShape + " is not supported on current platform");
		}
		for (int i = 0; i < 2; i++)
		{
			if (i < this.textures.Length)
			{
				if (this.textures[i] != this.cachedTextures[i])
				{
					this.cachedTextures[i] = this.textures[i];
					if (this.cachedTextures[i] != null)
					{
						this.texNativePtrs[i] = this.cachedTextures[i].GetNativeTexturePtr();
					}
				}
				if (this.currentOverlayShape == OVROverlay.OverlayShape.Cubemap && this.textures[i] != null && this.textures[i].GetType() != typeof(Cubemap))
				{
					Debug.LogError("Need Cubemap texture for cube map overlay");
					return;
				}
			}
		}
		if (this.cachedTextures[0] == null || this.texNativePtrs[0] == IntPtr.Zero)
		{
			return;
		}
		bool onTop = this.currentOverlayType == OVROverlay.OverlayType.Overlay;
		bool flag = false;
		Transform transform = base.transform;
		while (transform != null && !flag)
		{
			flag |= (transform == Camera.current.transform);
			transform = transform.parent;
		}
		OVRPose ovrpose = (!flag) ? base.transform.ToTrackingSpacePose() : base.transform.ToHeadSpacePose();
		Vector3 lossyScale = base.transform.lossyScale;
		for (int j = 0; j < 3; j++)
		{
			ref Vector3 ptr = ref lossyScale;
			int index;
			lossyScale[index = j] = ptr[index] / Camera.current.transform.lossyScale[j];
		}
		if (this.currentOverlayShape == OVROverlay.OverlayShape.Cubemap)
		{
			ovrpose.position = Camera.current.transform.position;
		}
		if (this.currentOverlayShape == OVROverlay.OverlayShape.Cylinder)
		{
			float num = lossyScale.x / lossyScale.z / 3.14159274f * 180f;
			if (num > 180f)
			{
				Debug.LogError("Cylinder overlay's arc angle has to be below 180 degree, current arc angle is " + num + " degree.");
				return;
			}
		}
		bool flag2 = OVRPlugin.SetOverlayQuad(onTop, flag, this.texNativePtrs[0], this.texNativePtrs[1], IntPtr.Zero, ovrpose.flipZ().ToPosef(), lossyScale.ToVector3f(), this.layerIndex, (OVRPlugin.OverlayShape)this.currentOverlayShape);
		if (this.rend)
		{
			this.rend.enabled = !flag2;
		}
	}

	private const int maxInstances = 15;

	private static OVROverlay[] instances = new OVROverlay[15];

	public OVROverlay.OverlayType currentOverlayType = OVROverlay.OverlayType.Overlay;

	public OVROverlay.OverlayShape currentOverlayShape;

	public Texture[] textures = new Texture[2];

	private Texture[] cachedTextures = new Texture[2];

	private IntPtr[] texNativePtrs = new IntPtr[]
	{
		IntPtr.Zero,
		IntPtr.Zero
	};

	private int layerIndex = -1;

	private Renderer rend;

	public enum OverlayShape
	{
		Quad,
		Cylinder,
		Cubemap
	}

	public enum OverlayType
	{
		None,
		Underlay,
		Overlay,
		OverlayShowLod
	}
}
