using System;
using System.Collections.Generic;
using UnityEngine;

namespace RenderHeads.Media.AVProVideo.Demos
{
	public class SampleApp_Multiple : MonoBehaviour
	{
		private void Update()
		{
			foreach (DisplayUGUI displayUGUI in this.m_aAddedVideos)
			{
				if (displayUGUI.gameObject != null && !displayUGUI.gameObject.activeSelf && displayUGUI._mediaPlayer != null && displayUGUI._mediaPlayer.Control != null && displayUGUI._mediaPlayer.Control.IsPlaying())
				{
					displayUGUI.gameObject.SetActive(true);
				}
			}
		}

		private void UpdateVideosLayout()
		{
			GameObject gameObject = GameObject.Find("Canvas/Panel");
			RectTransform rectTransform = (!gameObject) ? null : gameObject.GetComponent<RectTransform>();
			if (rectTransform)
			{
				Vector2 sizeDelta = rectTransform.sizeDelta;
				Vector2 vector = new Vector2(sizeDelta.x * 0.5f, sizeDelta.y * 0.5f);
				int count = this.m_aAddedVideos.Count;
				int num = Mathf.CeilToInt(Mathf.Sqrt((float)count));
				float num2 = 1f / (float)num * sizeDelta.x;
				float num3 = 1f / (float)num * sizeDelta.y;
				for (int i = 0; i < count; i++)
				{
					DisplayUGUI displayUGUI = this.m_aAddedVideos[i];
					int num4 = i % num;
					int num5 = i / num;
					displayUGUI.rectTransform.anchoredPosition = new Vector2(num2 * (float)num4 - vector.x, num3 * (float)(-(float)num5) + vector.y);
					displayUGUI.rectTransform.sizeDelta = new Vector2(num2, num3);
				}
			}
		}

		public void AddVideoClicked()
		{
			this.m_NumVideosAdded++;
			GameObject gameObject = new GameObject("AVPro MediaPlayer " + this.m_NumVideosAdded.ToString());
			MediaPlayer mediaPlayer = gameObject.AddComponent<MediaPlayer>();
			mediaPlayer.m_VideoPath = this.m_videoPath;
			mediaPlayer.m_AutoStart = true;
			mediaPlayer.m_Loop = true;
			mediaPlayer.SetGuiPositionFromVideoIndex(this.m_NumVideosAdded - 1);
			mediaPlayer.SetDebugGuiEnabled(this.m_NumVideosAdded < 5);
			GameObject gameObject2 = GameObject.Find("Canvas/Panel");
			if (gameObject2 != null)
			{
				GameObject gameObject3 = new GameObject("AVPro Video uGUI " + this.m_NumVideosAdded.ToString());
				gameObject3.transform.parent = gameObject2.transform;
				gameObject3.SetActive(false);
				gameObject3.AddComponent<RectTransform>();
				gameObject3.AddComponent<CanvasRenderer>();
				DisplayUGUI displayUGUI = gameObject3.AddComponent<DisplayUGUI>();
				displayUGUI._mediaPlayer = mediaPlayer;
				displayUGUI._scaleMode = ScaleMode.StretchToFill;
				displayUGUI.rectTransform.localScale = Vector3.one;
				displayUGUI.rectTransform.pivot = new Vector2(0f, 1f);
				this.m_aAddedVideos.Add(displayUGUI);
				this.UpdateVideosLayout();
			}
		}

		public void RemoveVideoClicked()
		{
			if (this.m_aAddedVideos.Count > 0)
			{
				int index = UnityEngine.Random.Range(0, this.m_aAddedVideos.Count);
				DisplayUGUI displayUGUI = this.m_aAddedVideos[index];
				if (displayUGUI._mediaPlayer != null)
				{
					displayUGUI._mediaPlayer.CloseVideo();
					UnityEngine.Object.Destroy(displayUGUI._mediaPlayer.gameObject);
					displayUGUI._mediaPlayer = null;
				}
				UnityEngine.Object.Destroy(displayUGUI.gameObject);
				this.m_aAddedVideos.RemoveAt(index);
				this.m_NumVideosAdded--;
			}
		}

		private void OnDestroy()
		{
			foreach (DisplayUGUI displayUGUI in this.m_aAddedVideos)
			{
				if (displayUGUI._mediaPlayer)
				{
					displayUGUI._mediaPlayer = null;
				}
			}
			this.m_aAddedVideos.Clear();
		}

		public string m_videoPath = "BigBuckBunny_360p30.mp4";

		private int m_NumVideosAdded;

		private List<DisplayUGUI> m_aAddedVideos = new List<DisplayUGUI>();
	}
}
