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.Model
{
	internal class InteractionChannelSiSectionHeader : Validatable
	{
		public InteractionChannelSiSectionHeader(MemoryStream ms)
		{
			TableId = ms.ReadUInt8();

			byte byte2 = ms.ReadUInt8();
			SectionSyntaxIndicator = (byte2 & 0x80) != 0;
			if (!SectionSyntaxIndicator)
			{
				Valid = false;
				return;
			}

			bool reserved = (byte2 & 0x40) != 0;
			int reserved2 = (byte2 & 0x30) >> 4;

			SectionLength = (byte2 & 0x0f);
			SectionLength <<= 4;
			SectionLength += ms.ReadUInt8();
			long AvailableBytes = ms.GetAvailableBytes();
			if (AvailableBytes < SectionLength)
			{
				Valid = false;
				return;
			}
			if (SectionLength > 1021)
			{
				Valid = false;
				return;
			}
			if (AvailableBytes > 1021)
			{
				Valid = false;
				return;
			}

			InteractiveNetworkId = ms.ReadUInt16BE();

			byte byte6 = ms.ReadUInt8();
			int reserved3 = (byte6 & 0xc0) >> 6;
			VersionNumber = (byte6 & 0x3e) >> 1;
			CurrentNextIndicator = (byte6 & 0x01) != 0;

			SectionNumber = ms.ReadUInt8();
			LastSectionNumber = ms.ReadUInt8();

			Valid = true;
		}

		public byte TableId { get; private set; }
		public bool SectionSyntaxIndicator { get; private set; }
		public int SectionLength { get; }
		public ushort InteractiveNetworkId { get; }
		public int VersionNumber { get; }
		public bool CurrentNextIndicator { get; }
		public byte SectionNumber { get; }
		public byte LastSectionNumber { get; }
	}
}
