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

namespace RenderHeads.Media.AVProVideo
{
	public class StreamParser : MonoBehaviour
	{
		public StreamParserEvent Events
		{
			get
			{
				if (this._events == null)
				{
					this._events = new StreamParserEvent();
				}
				return this._events;
			}
		}

		private void LoadFile()
		{
			try
			{
				StreamParser.StreamType streamType = this._streamType;
				if (streamType != StreamParser.StreamType.HLS)
				{
					this._parser = new HLSStream(this._url, 0, 0, 0);
				}
				else
				{
					this._parser = new HLSStream(this._url, 0, 0, 0);
				}
				this._substreams = this._parser.GetAllStreams();
				this._chunks = this._parser.GetAllChunks();
				this._loaded = true;
				Debug.Log("[AVProVideo] Stream parser completed parsing stream file " + this._url);
				this._events.Invoke(this, StreamParserEvent.EventType.Success);
			}
			catch (Exception ex)
			{
				this._loaded = false;
				Debug.LogError("[AVProVideo] Parser unable to read stream " + ex.Message);
				this._events.Invoke(this, StreamParserEvent.EventType.Failed);
			}
		}

		public bool Loaded
		{
			get
			{
				return this._loaded;
			}
		}

		public Stream Root
		{
			get
			{
				return (!this._loaded) ? null : this._parser;
			}
		}

		public List<Stream> SubStreams
		{
			get
			{
				return (!this._loaded) ? null : this._substreams;
			}
		}

		public List<Stream.Chunk> Chunks
		{
			get
			{
				return (!this._loaded) ? null : this._chunks;
			}
		}

		public void ParseStream()
		{
			Thread thread = new Thread(new ThreadStart(this.LoadFile));
			thread.Start();
		}

		private void Start()
		{
			if (this._autoLoad)
			{
				this.ParseStream();
			}
		}

		public string _url;

		public StreamParser.StreamType _streamType;

		public bool _autoLoad = true;

		private Stream _parser;

		private bool _loaded;

		private List<Stream> _substreams;

		private List<Stream.Chunk> _chunks;

		private StreamParserEvent _events;

		public enum StreamType
		{
			HLS
		}
	}
}
