using skyscraper5.Skyscraper;
using skyscraper5.Skyscraper.IO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace skyscraper5.src.InteractionChannel.Model2
{
	public class Tmst2 : Validatable
	{
		public Tmst2(MemoryStream ms, int transmissionStandard)
		{
			CommonSystemMargin = ms.ReadUInt8();
			byte transmissionModeCount = ms.ReadUInt8();
			TransmissionModes = new TransmissionMode[transmissionModeCount];
			for (int i = 0; i < transmissionModeCount; i++)
			{
				TransmissionModes[i] = new TransmissionMode();
				byte bytea = ms.ReadUInt8();
				TransmissionModes[i].FrameLength = (bytea & 0xc0) >> 6;
				TransmissionModes[i].PilotSymbols = (bytea & 0x20) != 0;
				TransmissionModes[i].Modcod = (bytea & 0x1f);
				TransmissionModes[i].ModcodSystemMargin = ms.ReadUInt8();
				TransmissionModes[i].ISI = ms.ReadUInt8();
			}
			if (transmissionStandard == 3)
			{
				throw new NotImplementedException("Transmisison standard 3 in DVB-RCS2 is not yet supported. If possible, please share a sample of this stream.");
			}
			Valid = true;
		}

		public byte CommonSystemMargin { get; }
		public TransmissionMode[] TransmissionModes { get; }

		public class TransmissionMode
		{
			public int FrameLength { get; internal set; }
			public bool PilotSymbols { get; internal set; }
			public int Modcod { get; internal set; }
			public byte ModcodSystemMargin { get; internal set; }
			public byte ISI { get; internal set; }
		}
	}
}
