using System;
using UnityEngine;

namespace RenderHeads.Media.AVProVideo.Demos
{
	public class StartEndPoint : MonoBehaviour
	{
		private void OnEnable()
		{
			this._isStartQueued = true;
		}

		private void Update()
		{
			if (StartEndPoint.IsVideoLoaded(this._mediaPlayer))
			{
				if (this._isStartQueued)
				{
					StartEndPoint.DoStart(this._mediaPlayer, this._startPointSeconds);
					this._isStartQueued = false;
				}
				else if (!this._loop)
				{
					StartEndPoint.DoCheckEnd(this._mediaPlayer, this._endPointSeconds);
				}
				else
				{
					StartEndPoint.DoCheckLoop(this._mediaPlayer, this._endPointSeconds, this._startLoopSeconds);
				}
			}
		}

		private static bool IsVideoLoaded(MediaPlayer mp)
		{
			return mp != null && mp.Control != null && mp.Control.HasMetaData();
		}

		private static void DoStart(MediaPlayer mp, float positionSeconds)
		{
			mp.Control.Seek(positionSeconds * 1000f);
			mp.Play();
		}

		private static void DoCheckEnd(MediaPlayer mp, float positionSeconds)
		{
			if (mp.Control.IsPlaying() && mp.Control.GetCurrentTimeMs() >= positionSeconds * 1000f)
			{
				mp.Pause();
			}
		}

		private static void DoCheckLoop(MediaPlayer mp, float positionSeconds, float positionLoop)
		{
			if (mp.Control.IsPlaying() && mp.Control.GetCurrentTimeMs() >= positionSeconds * 1000f)
			{
				mp.Control.Seek(positionLoop * 1000f);
			}
		}

		public MediaPlayer _mediaPlayer;

		public float _startPointSeconds;

		public float _endPointSeconds;

		public bool _loop;

		[Tooltip("If looping is enabled, this is the time the video rewinds to when it reaches the end point")]
		public float _startLoopSeconds;

		private bool _isStartQueued;
	}
}
