using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace skyscraper8.SatIp
{
	internal class SessionDescriptionProtocol
	{
		public SessionDescriptionProtocol(StreamReader sr)
		{
			while (!sr.EndOfStream)
			{
				string line = sr.ReadLine();
				int indexOf = line.IndexOf('=');
				string key = line.Substring(0, indexOf);
				string value = line.Substring(indexOf + 1);
				switch (key)
				{
					case "v":
						SdpVersion = int.Parse(value);
						break;
					case "o":
						string[] oArgs = value.Split(' ');
						Username = oArgs[0];
						SessionId = ulong.Parse(oArgs[1]);
						SessionVersion = uint.Parse(oArgs[2]);
						OwnerNetworkType = Enum.Parse<OwnerNetworkTypeEnum>(oArgs[3]);
						OwnerAddressType = Enum.Parse<OwnerAddressTypeEnum>(oArgs[4]);
						OwnerAddress = IPAddress.Parse(oArgs[5]);
						break;
					case "s":
						SessionName = value;
						break;
					case "t":
						string[] tArgs = value.Split(' ');
						SessionStartTime = uint.Parse(tArgs[0]);
						SessionStopType = uint.Parse(tArgs[1]);
						break;
					case "m":
						string[] mArgs = value.Split(' ');
						MediaType = Enum.Parse<MediaTypeEnum>(mArgs[0], true);
						MediaPort = uint.Parse(mArgs[1]);
						MediaProtocol = mArgs[2];
						MediaFormat = (MediaFormatEnum)int.Parse(mArgs[3]);
						break;
					case "c":
						string[] cArgs = value.Split(' ');
						ConnectionNetworkType = Enum.Parse<OwnerNetworkTypeEnum>(cArgs[0]);
						ConnectionAddressType = Enum.Parse<OwnerAddressTypeEnum>(cArgs[1]);
						ConnectionAddress = IPAddress.Parse(cArgs[2]);
						break;
					case "a":
						int aIndex = value.IndexOf(':');
						string aKey = value.Substring(0, aIndex);
						string aValue = value.Substring(aIndex + 1);
						if (MediaAttribute == null)
							MediaAttribute = new Dictionary<string, string>();
						MediaAttribute[aKey] = aValue;
						break;
					default:
						throw new NotImplementedException(key);
				}
			}
		}

		public Dictionary<string, string> MediaAttribute { get; set; }

		public IPAddress ConnectionAddress { get; set; }

		public OwnerAddressTypeEnum ConnectionAddressType { get; set; }

		public OwnerNetworkTypeEnum ConnectionNetworkType { get; set; }

		public MediaFormatEnum MediaFormat { get; set; }

		public string MediaProtocol { get; set; }

		public uint MediaPort { get; set; }

		public MediaTypeEnum MediaType { get; set; }

		public uint SessionStopType { get; set; }

		public uint SessionStartTime { get; set; }

		public string SessionName { get; set; }

		public IPAddress OwnerAddress { get; set; }

		public OwnerAddressTypeEnum OwnerAddressType { get; set; }

		public OwnerNetworkTypeEnum OwnerNetworkType { get; set; }

		public uint SessionVersion { get; set; }

		public ulong SessionId { get; set; }

		public string Username { get; set; }

		public int SdpVersion { get; private set; }
	}

	public enum OwnerNetworkTypeEnum
	{
		IN
	}

	public enum OwnerAddressTypeEnum
	{
		IP4
	}

	public enum MediaTypeEnum
	{
		Video,
	}

	public enum MediaFormatEnum
	{
		PCMU = 0,
		GSM = 3,
		G723 = 4,
		DVI4_32kbit = 5,
		DVI4_64kbit = 6,
		LPC = 7,
		PCMA = 8,
		G722 = 9,
		L16Stereo = 10,
		L16Mono = 11,
		QCELP = 12,
		ComfortNoise = 13,
		MPA = 14,
		G728 = 15,
		DVI4_44kbit = 16,
		DVI4_88kbit = 17,
		G729 = 18,
		CellB = 25,
		Jpeg = 26,
		NV = 28,
		H261 = 31,
		MPV = 32,
		M2T = 33,
		H263 = 34

	}
}
