using System;
using System.IO;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace RenderHeads.Media.AVProVideo.Demos
{
	public class VCR : MonoBehaviour
	{
		public MediaPlayer PlayingPlayer
		{
			get
			{
				if (this.LoadingPlayer == this._mediaPlayer)
				{
					return this._mediaPlayerB;
				}
				return this._mediaPlayer;
			}
		}

		public MediaPlayer LoadingPlayer
		{
			get
			{
				return this._loadingPlayer;
			}
		}

		private void SwapPlayers()
		{
			this.PlayingPlayer.Control.Pause();
			if (this.LoadingPlayer == this._mediaPlayer)
			{
				this._loadingPlayer = this._mediaPlayerB;
			}
			else
			{
				this._loadingPlayer = this._mediaPlayer;
			}
			this._mediaDisplay.CurrentMediaPlayer = this.PlayingPlayer;
		}

		public void OnOpenVideoFile()
		{
			this.LoadingPlayer.m_VideoPath = Path.Combine(this._folder, this._videoFiles[this._VideoIndex]);
			this._VideoIndex = (this._VideoIndex + 1) % this._videoFiles.Length;
			if (string.IsNullOrEmpty(this.LoadingPlayer.m_VideoPath))
			{
				this.LoadingPlayer.CloseVideo();
				this._VideoIndex = 0;
			}
			else
			{
				this.LoadingPlayer.OpenVideoFromFile(this._location, this.LoadingPlayer.m_VideoPath, this._AutoStartToggle.isOn);
			}
		}

		public void OnAutoStartChange()
		{
			if (this.PlayingPlayer && this._AutoStartToggle && this._AutoStartToggle.enabled && this.PlayingPlayer.m_AutoStart != this._AutoStartToggle.isOn)
			{
				this.PlayingPlayer.m_AutoStart = this._AutoStartToggle.isOn;
			}
			if (this.LoadingPlayer && this._AutoStartToggle && this._AutoStartToggle.enabled && this.LoadingPlayer.m_AutoStart != this._AutoStartToggle.isOn)
			{
				this.LoadingPlayer.m_AutoStart = this._AutoStartToggle.isOn;
			}
		}

		public void OnMuteChange()
		{
			if (this.PlayingPlayer)
			{
				this.PlayingPlayer.Control.MuteAudio(this._MuteToggle.isOn);
			}
			if (this.LoadingPlayer)
			{
				this.LoadingPlayer.Control.MuteAudio(this._MuteToggle.isOn);
			}
		}

		public void OnPlayButton()
		{
			if (this.PlayingPlayer)
			{
				this.PlayingPlayer.Control.Play();
			}
		}

		public void OnPauseButton()
		{
			if (this.PlayingPlayer)
			{
				this.PlayingPlayer.Control.Pause();
			}
		}

		public void OnVideoSeekSlider()
		{
			if (this.PlayingPlayer && this._videoSeekSlider && this._videoSeekSlider.value != this._setVideoSeekSliderValue)
			{
				this.PlayingPlayer.Control.Seek(this._videoSeekSlider.value * this.PlayingPlayer.Info.GetDurationMs());
			}
		}

		public void OnVideoSliderDown()
		{
			if (this.PlayingPlayer)
			{
				this._wasPlayingOnScrub = this.PlayingPlayer.Control.IsPlaying();
				if (this._wasPlayingOnScrub)
				{
					this.PlayingPlayer.Control.Pause();
				}
				this.OnVideoSeekSlider();
			}
		}

		public void OnVideoSliderUp()
		{
			if (this.PlayingPlayer && this._wasPlayingOnScrub)
			{
				this.PlayingPlayer.Control.Play();
				this._wasPlayingOnScrub = false;
			}
		}

		public void OnAudioVolumeSlider()
		{
			if (this.PlayingPlayer && this._audioVolumeSlider && this._audioVolumeSlider.value != this._setAudioVolumeSliderValue)
			{
				this.PlayingPlayer.Control.SetVolume(this._audioVolumeSlider.value);
			}
			if (this.LoadingPlayer && this._audioVolumeSlider && this._audioVolumeSlider.value != this._setAudioVolumeSliderValue)
			{
				this.LoadingPlayer.Control.SetVolume(this._audioVolumeSlider.value);
			}
		}

		public void OnRewindButton()
		{
			if (this.PlayingPlayer)
			{
				this.PlayingPlayer.Control.Rewind();
			}
		}

		private void Awake()
		{
			this._loadingPlayer = this._mediaPlayerB;
		}

		private void Start()
		{
			if (this.PlayingPlayer)
			{
				this.PlayingPlayer.Events.AddListener(new UnityAction<MediaPlayer, MediaPlayerEvent.EventType, ErrorCode>(this.OnVideoEvent));
				if (this.LoadingPlayer)
				{
					this.LoadingPlayer.Events.AddListener(new UnityAction<MediaPlayer, MediaPlayerEvent.EventType, ErrorCode>(this.OnVideoEvent));
				}
				if (this._audioVolumeSlider && this.PlayingPlayer.Control != null)
				{
					float volume = this.PlayingPlayer.Control.GetVolume();
					this._setAudioVolumeSliderValue = volume;
					this._audioVolumeSlider.value = volume;
				}
				this._AutoStartToggle.isOn = this.PlayingPlayer.m_AutoStart;
				if (this.PlayingPlayer.m_AutoOpen)
				{
				}
				this.OnOpenVideoFile();
			}
		}

		private void Update()
		{
			if (this.PlayingPlayer && this.PlayingPlayer.Info != null && this.PlayingPlayer.Info.GetDurationMs() > 0f)
			{
				float currentTimeMs = this.PlayingPlayer.Control.GetCurrentTimeMs();
				float num = currentTimeMs / this.PlayingPlayer.Info.GetDurationMs();
				this._setVideoSeekSliderValue = num;
				this._videoSeekSlider.value = num;
			}
		}

		public void OnVideoEvent(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode)
		{
			switch (et)
			{
			case MediaPlayerEvent.EventType.FirstFrameReady:
				this.SwapPlayers();
				break;
			}
			Debug.Log("Event: " + et.ToString());
		}

		public MediaPlayer _mediaPlayer;

		public MediaPlayer _mediaPlayerB;

		public DisplayUGUI _mediaDisplay;

		public Slider _videoSeekSlider;

		private float _setVideoSeekSliderValue;

		private bool _wasPlayingOnScrub;

		public Slider _audioVolumeSlider;

		private float _setAudioVolumeSliderValue;

		public Toggle _AutoStartToggle;

		public Toggle _MuteToggle;

		public MediaPlayer.FileLocation _location = MediaPlayer.FileLocation.RelativeToStreamingAssetsFolder;

		public string _folder = "AVProVideoDemos/";

		public string[] _videoFiles = new string[]
		{
			"BigBuckBunny_720p30.mp4",
			"SampleSphere.mp4"
		};

		private int _VideoIndex;

		private MediaPlayer _loadingPlayer;
	}
}
