using System.Collections.Generic;
using System.IO;
using skyscraper5.Mpeg2;
using skyscraper5.Skyscraper.IO;

namespace skyscraper5.Scorcher
{
	public class PmtGeneratorStream
	{
		public PmtGeneratorStream(byte type, ushort pid)
		{
			descriptors = new List<TsDescriptor>();
			this.StreamType = type;
			this.ElementaryPid = pid;
		}

		private List<TsDescriptor> descriptors;
		public byte StreamType { get; }
		public ushort ElementaryPid { get; }

		public byte[] SerializeDescriptors()
		{
			MemoryStream ms = new MemoryStream();
			foreach (TsDescriptor descriptor in descriptors)
			{
				ms.WriteUInt8(descriptor.GetDescriptorId());    //descriptor_tag

				byte[] descriptorBytes = descriptor.Serialize();
				ms.WriteUInt8((byte)descriptorBytes.Length);    //descriptor_length
				ms.Write(descriptorBytes, 0, descriptorBytes.Length);
			}
			return ms.ToArray();
		}

		public void AddDescriptor(TsDescriptor privateDataSpecifierDescriptor)
		{
			descriptors.Add(privateDataSpecifierDescriptor);
		}
	}
}
