using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using skyscraper8.Ietf.FLUTE;

namespace skyscraper8.DvbNip
{
	public class DvbNipUtilities
	{
		private DvbNipUtilities() { }

		private static XmlSerializer multicastGatewayConfigurationXmlSerializer;

		public static MulticastGatewayConfigurationType UnpackMulticastGatewayConfiguration(Stream stream)
		{
			if (multicastGatewayConfigurationXmlSerializer == null)
			{
				multicastGatewayConfigurationXmlSerializer = new XmlSerializer(typeof(MulticastGatewayConfigurationType));
			}

			object v = multicastGatewayConfigurationXmlSerializer.Deserialize(stream);
			MulticastGatewayConfigurationType result = (MulticastGatewayConfigurationType)v;
			return result;
		}

		public static string MakeFilename(string originalLocation)
		{
			string newFilename = originalLocation.Replace("://", "/");
			newFilename = originalLocation.Replace(":", "/");

			while (newFilename.Contains("//"))
				newFilename = newFilename.Replace("//", "/");

			if (string.IsNullOrEmpty(Path.GetExtension(newFilename)))
				newFilename += ".dat";

			newFilename = newFilename.Replace("%3D", "-3D-");

			return newFilename;
		}

		public static bool IsContinuousFileType(string extension)
		{
			switch(extension)
			{
				case ".mpd":
				case ".m4s":
				case ".m3u8":
				case ".ts":
					return true;
				default:
					return false;
			}
		}

		public static bool IsContinuousFileType(FileType filteType)
		{
			return IsContinuousFileType(Path.GetExtension(filteType.ContentLocation));
		}

		private static XmlSerializer privateDataSignallingManifestXmlSerializer;
		public static PrivateDataSignallingManifestType UnpackPrivateDataSignallingManifest(Stream toStream)
		{
			if (privateDataSignallingManifestXmlSerializer == null)
			{
				privateDataSignallingManifestXmlSerializer = new XmlSerializer(typeof(PrivateDataSignallingManifestType));
			}

			byte[] buffer = new byte[toStream.Length];
			toStream.Read(buffer, 0, (int)toStream.Length);
			string bufferString = Encoding.UTF8.GetString(buffer, 0, (int)toStream.Length);
			if (bufferString.Contains("urn:dvb:metadata:nativeip:2023"))
			{
				bufferString = bufferString.Replace("urn:dvb:metadata:nativeip:2023", "urn:dvb:metadata:nativeip:2024");
			}

			object v = privateDataSignallingManifestXmlSerializer.Deserialize(new StringReader(bufferString));
			PrivateDataSignallingManifestType result = (PrivateDataSignallingManifestType)v;
			return result;
		}

		private static XmlSerializer timeOffsetFileXmlSerializer;

		public static TimeOffsetFileType UnpackTimeOffsetFile(Stream toStream)
		{
			if (timeOffsetFileXmlSerializer == null)
			{
				timeOffsetFileXmlSerializer = new XmlSerializer(typeof(TimeOffsetFileType));
			}

			byte[] buffer = new byte[toStream.Length];
			toStream.Read(buffer, 0, (int)toStream.Length);
			string bufferString = Encoding.UTF8.GetString(buffer, 0, (int)toStream.Length);
			if (bufferString.Contains("urn:dvb:metadata:nativeip:2023"))
			{
				bufferString = bufferString.Replace("urn:dvb:metadata:nativeip:2023", "urn:dvb:metadata:nativeip:2024");
			}


			object v = timeOffsetFileXmlSerializer.Deserialize(new StringReader(bufferString));
			TimeOffsetFileType result = (TimeOffsetFileType)v;
			return result;
		}

		private static XmlSerializer networkInformationFileXmlSerializer;
		public static NetworkInformationFileType UnpackNetworkInformationFile(Stream toStream)
		{
			if (networkInformationFileXmlSerializer == null)
			{
				networkInformationFileXmlSerializer = new XmlSerializer(typeof(NetworkInformationFileType));
			}

			byte[] buffer = new byte[toStream.Length];
			toStream.Read(buffer, 0, (int)toStream.Length);
			string bufferString = Encoding.UTF8.GetString(buffer, 0, (int)toStream.Length);
			if (bufferString.Contains("urn:dvb:metadata:nativeip:2023"))
			{
				bufferString = bufferString.Replace("urn:dvb:metadata:nativeip:2023", "urn:dvb:metadata:nativeip:2024");
			}

			object v = networkInformationFileXmlSerializer.Deserialize(new StringReader(bufferString));
			NetworkInformationFileType result = (NetworkInformationFileType)v;
			return result;
		}

		private static XmlSerializer serviceInformationFileXmlSerializer;
		public static ServiceInformationFileType UnpackServiceInformationFile(Stream toStream)
		{
			if (serviceInformationFileXmlSerializer == null)
			{
				serviceInformationFileXmlSerializer = new XmlSerializer(typeof(ServiceInformationFileType));
			}
			
			byte[] buffer = new byte[toStream.Length];
			toStream.Read(buffer, 0, (int)toStream.Length);
			string bufferString = Encoding.UTF8.GetString(buffer, 0, (int)toStream.Length);
			if (bufferString.Contains("urn:dvb:metadata:nativeip:2023"))
			{
				bufferString = bufferString.Replace("urn:dvb:metadata:nativeip:2023", "urn:dvb:metadata:nativeip:2024");
			}

			object v = serviceInformationFileXmlSerializer.Deserialize(new StringReader(bufferString));
			ServiceInformationFileType result = (ServiceInformationFileType)v;
			return result;
		}
	}
}
