using System;
using UnityEngine;

namespace RenderHeads.Media.AVProVideo
{
	[AddComponentMenu("AVPro Video/Display IMGUI", 200)]
	[HelpURL("http://renderheads.com/product/avpro-video/")]
	[ExecuteInEditMode]
	public class DisplayIMGUI : MonoBehaviour
	{
		private void Awake()
		{
			if (DisplayIMGUI._propAlphaPack == 0)
			{
				DisplayIMGUI._propAlphaPack = Shader.PropertyToID("AlphaPack");
				DisplayIMGUI._propVertScale = Shader.PropertyToID("_VertScale");
				DisplayIMGUI._propApplyGamma = Shader.PropertyToID("_ApplyGamma");
				DisplayIMGUI._propChromaTex = Shader.PropertyToID("_ChromaTex");
			}
		}

		private void Start()
		{
			if (!this._useDepth)
			{
				base.useGUILayout = false;
			}
			if (DisplayIMGUI._shaderAlphaPacking == null)
			{
				DisplayIMGUI._shaderAlphaPacking = Shader.Find("AVProVideo/IMGUI/Texture Transparent");
				if (DisplayIMGUI._shaderAlphaPacking == null)
				{
					Debug.LogWarning("[AVProVideo] Missing shader AVProVideo/IMGUI/Transparent Packed");
				}
			}
		}

		private void OnDestroy()
		{
			if (this._material != null)
			{
				UnityEngine.Object.Destroy(this._material);
				this._material = null;
			}
		}

		private Shader GetRequiredShader()
		{
			Shader shader = null;
			AlphaPacking alphaPacking = this._mediaPlayer.m_AlphaPacking;
			if (alphaPacking != AlphaPacking.None)
			{
				if (alphaPacking == AlphaPacking.LeftRight || alphaPacking == AlphaPacking.TopBottom)
				{
					shader = DisplayIMGUI._shaderAlphaPacking;
				}
			}
			if (shader == null && this._mediaPlayer.Info != null && QualitySettings.activeColorSpace == ColorSpace.Linear && this._mediaPlayer.Info.PlayerSupportsLinearColorSpace())
			{
				shader = DisplayIMGUI._shaderAlphaPacking;
			}
			if (shader == null && this._mediaPlayer.TextureProducer != null && this._mediaPlayer.TextureProducer.GetTextureCount() == 2)
			{
				shader = DisplayIMGUI._shaderAlphaPacking;
			}
			return shader;
		}

		private void Update()
		{
			if (this._mediaPlayer != null)
			{
				Shader x = null;
				if (this._material != null)
				{
					x = this._material.shader;
				}
				Shader requiredShader = this.GetRequiredShader();
				if (x != requiredShader)
				{
					if (this._material != null)
					{
						UnityEngine.Object.Destroy(this._material);
						this._material = null;
					}
					if (requiredShader != null)
					{
						this._material = new Material(requiredShader);
					}
				}
				if (this._material != null)
				{
					if (this._material.HasProperty(DisplayIMGUI._propAlphaPack))
					{
						Helper.SetupAlphaPackedMaterial(this._material, this._mediaPlayer.m_AlphaPacking);
					}
					if (this._material.HasProperty(DisplayIMGUI._propApplyGamma) && this._mediaPlayer.Info != null)
					{
						Helper.SetupGammaMaterial(this._material, !this._mediaPlayer.Info.PlayerSupportsLinearColorSpace());
					}
				}
			}
		}

		private void OnGUI()
		{
			if (this._mediaPlayer == null)
			{
				return;
			}
			bool flag = false;
			Texture texture = null;
			if (this._displayInEditor)
			{
			}
			if (this._mediaPlayer.Info != null && !this._mediaPlayer.Info.HasVideo())
			{
				texture = null;
			}
			if (this._mediaPlayer.TextureProducer != null)
			{
				if (((!this._mediaPlayer.m_Resample) ? this._mediaPlayer.TextureProducer.GetTexture(0) : this._mediaPlayer.FrameResampler.OutputTexture[0]) != null)
				{
					texture = ((!this._mediaPlayer.m_Resample) ? this._mediaPlayer.TextureProducer.GetTexture(0) : this._mediaPlayer.FrameResampler.OutputTexture[0]);
					flag = this._mediaPlayer.TextureProducer.RequiresVerticalFlip();
				}
				if (this._mediaPlayer.TextureProducer.GetTextureCount() == 2 && this._material != null)
				{
					Texture texture2 = (this._mediaPlayer.FrameResampler != null && this._mediaPlayer.FrameResampler.OutputTexture != null) ? this._mediaPlayer.FrameResampler.OutputTexture[1] : null;
					Texture value = (!this._mediaPlayer.m_Resample) ? this._mediaPlayer.TextureProducer.GetTexture(1) : texture2;
					this._material.SetTexture(DisplayIMGUI._propChromaTex, value);
					this._material.EnableKeyword("USE_YPCBCR");
				}
			}
			if (texture != null && (!this._alphaBlend || this._color.a > 0f))
			{
				GUI.depth = this._depth;
				GUI.color = this._color;
				Rect rect = this.GetRect();
				if (this._material != null)
				{
					if (flag)
					{
						this._material.SetFloat(DisplayIMGUI._propVertScale, -1f);
					}
					else
					{
						this._material.SetFloat(DisplayIMGUI._propVertScale, 1f);
					}
					Helper.DrawTexture(rect, texture, this._scaleMode, this._mediaPlayer.m_AlphaPacking, this._material);
				}
				else
				{
					if (flag)
					{
						GUIUtility.ScaleAroundPivot(new Vector2(1f, -1f), new Vector2(0f, rect.y + rect.height / 2f));
					}
					GUI.DrawTexture(rect, texture, this._scaleMode, this._alphaBlend);
				}
			}
		}

		public Rect GetRect()
		{
			Rect result;
			if (this._fullScreen)
			{
				result = new Rect(0f, 0f, (float)Screen.width, (float)Screen.height);
			}
			else
			{
				result = new Rect(this._x * (float)(Screen.width - 1), this._y * (float)(Screen.height - 1), this._width * (float)Screen.width, this._height * (float)Screen.height);
			}
			return result;
		}

		private const string PropChromaTexName = "_ChromaTex";

		public MediaPlayer _mediaPlayer;

		public bool _displayInEditor = true;

		public ScaleMode _scaleMode = ScaleMode.ScaleToFit;

		public Color _color = Color.white;

		public bool _alphaBlend;

		[SerializeField]
		private bool _useDepth;

		public int _depth;

		public bool _fullScreen = true;

		[Range(0f, 1f)]
		public float _x;

		[Range(0f, 1f)]
		public float _y;

		[Range(0f, 1f)]
		public float _width = 1f;

		[Range(0f, 1f)]
		public float _height = 1f;

		private static int _propAlphaPack;

		private static int _propVertScale;

		private static int _propApplyGamma;

		private static int _propChromaTex;

		private static Shader _shaderAlphaPacking;

		private Material _material;
	}
}
