using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using UnityEngine;

namespace RenderHeads.Media.AVProVideo
{
	public class HLSStream : Stream
	{
		public HLSStream(string filename, int width = 0, int height = 0, int bandwidth = 0)
		{
			this._streams = new List<Stream>();
			this._chunks = new List<Stream.Chunk>();
			this._width = width;
			this._height = height;
			this._bandwidth = bandwidth;
			this._streamURL = filename;
			try
			{
				string[] text2;
				if (filename.ToLower().StartsWith("http://") || filename.ToLower().StartsWith("https://"))
				{
					string text = new WebClient().DownloadString(filename);
					text2 = text.Split(new char[]
					{
						'\n'
					});
				}
				else
				{
					text2 = File.ReadAllLines(filename);
				}
				int num = filename.LastIndexOf('/');
				if (num < 0)
				{
					num = filename.LastIndexOf('\\');
				}
				string path = this._streamURL.Substring(0, num + 1);
				this.ParseFile(text2, path);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		public override int Width
		{
			get
			{
				return this._width;
			}
		}

		public override int Height
		{
			get
			{
				return this._height;
			}
		}

		public override int Bandwidth
		{
			get
			{
				return this._bandwidth;
			}
		}

		public override string URL
		{
			get
			{
				return this._streamURL;
			}
		}

		public override List<Stream.Chunk> GetAllChunks()
		{
			List<Stream.Chunk> list = new List<Stream.Chunk>();
			for (int i = 0; i < this._streams.Count; i++)
			{
				List<Stream.Chunk> allChunks = this._streams[i].GetAllChunks();
				list.AddRange(allChunks);
			}
			list.AddRange(this._chunks);
			return list;
		}

		public override List<Stream.Chunk> GetChunks()
		{
			return this._chunks;
		}

		public override List<Stream> GetAllStreams()
		{
			List<Stream> list = new List<Stream>();
			for (int i = 0; i < this._streams.Count; i++)
			{
				List<Stream> allStreams = this._streams[i].GetAllStreams();
				list.AddRange(allStreams);
			}
			list.AddRange(this._streams);
			return list;
		}

		public override List<Stream> GetStreams()
		{
			return this._streams;
		}

		private bool ExtractStreamInfo(string line, ref int width, ref int height, ref int bandwidth)
		{
			if (line.StartsWith("#EXT-X-STREAM-INF"))
			{
				int num = line.IndexOf("BANDWIDTH=");
				if (num >= 0)
				{
					int num2 = line.IndexOf(',', num + "BANDWIDTH=".Length);
					if (num2 < 0)
					{
						num2 = line.Length - 1;
					}
					if (num2 >= 0 && num2 - "BANDWIDTH=".Length > num)
					{
						int length = num2 - num - "BANDWIDTH=".Length;
						string s = line.Substring(num + "BANDWIDTH=".Length, length);
						if (!int.TryParse(s, out bandwidth))
						{
							bandwidth = 0;
						}
					}
				}
				else
				{
					bandwidth = 0;
				}
				int num3 = line.IndexOf("RESOLUTION=");
				if (num3 >= 0)
				{
					int num4 = line.IndexOf(',', num3 + "RESOLUTION=".Length);
					if (num4 < 0)
					{
						num4 = line.Length - 1;
					}
					if (num4 >= 0 && num4 - "RESOLUTION=".Length > num3)
					{
						int length2 = num4 - num3 - "RESOLUTION=".Length;
						string text = line.Substring(num3 + "RESOLUTION=".Length, length2);
						int num5 = text.IndexOf('x');
						if (num5 < 0 || !int.TryParse(text.Substring(0, num5), out width) || !int.TryParse(text.Substring(num5 + 1, text.Length - (num5 + 1)), out height))
						{
							width = (height = 0);
						}
					}
				}
				else
				{
					width = (height = 0);
				}
				return true;
			}
			return false;
		}

		private static bool IsChunk(string line)
		{
			return line.StartsWith("#EXTINF");
		}

		private void ParseFile(string[] text, string path)
		{
			bool flag = false;
			bool flag2 = false;
			int width = 0;
			int height = 0;
			int bandwidth = 0;
			for (int i = 0; i < text.Length; i++)
			{
				if (this.ExtractStreamInfo(text[i], ref width, ref height, ref bandwidth))
				{
					flag2 = true;
					flag = false;
				}
				else if (HLSStream.IsChunk(text[i]))
				{
					flag = true;
					flag2 = false;
				}
				else if (flag)
				{
					Stream.Chunk item;
					item.name = path + text[i];
					this._chunks.Add(item);
					flag = false;
					flag2 = false;
				}
				else if (flag2)
				{
					try
					{
						string filename = (text[i].IndexOf("://") >= 0) ? text[i] : (path + text[i]);
						HLSStream item2 = new HLSStream(filename, width, height, bandwidth);
						this._streams.Add(item2);
					}
					catch (Exception ex)
					{
						Debug.LogError(string.Concat(new string[]
						{
							"[AVProVideo]HLSParser cannot parse stream ",
							path,
							text[i],
							", ",
							ex.Message
						}));
					}
					flag = false;
					flag2 = false;
				}
				else
				{
					flag = false;
					flag2 = false;
				}
			}
		}

		private const string BANDWITH_NAME = "BANDWIDTH=";

		private const string RESOLUTION_NAME = "RESOLUTION=";

		private const string CHUNK_TAG = "#EXTINF";

		private const string STREAM_TAG = "#EXT-X-STREAM-INF";

		private List<Stream> _streams;

		private List<Stream.Chunk> _chunks;

		private string _streamURL;

		private int _width;

		private int _height;

		private int _bandwidth;
	}
}
