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

namespace skyscraper5.src.InteractionChannel.Model.Descriptors
{
	[SkyscraperPlugin]
	[TsDescriptor(0xb3,"TIM")]
	internal class _0xb3_MeshLogonInitializeDescriptor : TsDescriptor
	{
		public _0xb3_MeshLogonInitializeDescriptor(byte[] buffer)
		{
			InBitStream ibs = new InBitStream(new MemoryStream(buffer, false));
			GroupId = ibs.ReadBitsAsByte(8);
			LogonId = ibs.ReadBitsAsUint(16);

			byte reserved = ibs.ReadBitsAsByte(2);
			ContinuousCarrier = ibs.ReadBitAsBoolean();
			SecurityHandshakeRequired = ibs.ReadBitAsBoolean();
			PrefixFlag = ibs.ReadBitAsBoolean();
			DataUnitLabelingFlag = ibs.ReadBitAsBoolean();
			MiniSlotFlag = ibs.ReadBitAsBoolean();
			ContentionBasedMiniSlotFlag = ibs.ReadBitAsBoolean();

			MeshConnectivityFlag = ibs.ReadBitAsBoolean();
			CapacityTypeFlag = ibs.ReadBitAsBoolean();
			TrafficBurstType = ibs.ReadBitAsBoolean();
			if (!TrafficBurstType)
			{
				Connectivity = ibs.ReadBitAsBoolean();
				if (!Connectivity.Value)
				{
					reserved = ibs.ReadBitsAsByte(4);
					ReturnVPI = ibs.ReadBitsAsByte(8);
					ReturnVCI = ibs.ReadBitsAsUInt16(16);
				}
				else
				{
					reserved = ibs.ReadBitsAsByte(4);
					ReturnSignallingVPI = ibs.ReadBitsAsByte(8);
					ReturnSignallingVCI = ibs.ReadBitsAsUInt16(16);
					reserved = ibs.ReadBitsAsByte(8);
					ForwardSignallingVPI = ibs.ReadBitsAsByte(8);
					ForwardSignallingVCI = ibs.ReadBitsAsUInt16(16);
				}
			}
			else
			{
				ReturnTrfPid = ibs.ReadBitsAsUInt16(13);
				reserved = ibs.ReadBitsAsByte(3);
				ReturnCtrlMngmPid = ibs.ReadBitsAsUInt16(13);
			}
			if (!CapacityTypeFlag)
			{
				if (ibs.GetBitsAvailable() < 80)
				{
					Valid = false;
					return;
				}
				CraLevel = ibs.ReadBitsAsUint(24);
				reserved = ibs.ReadBitsAsByte(5);
				VbdcMax = ibs.ReadBitsAsUint(11);
				RbdcMax = ibs.ReadBitsAsUint(24);
				RbdcTimeout = ibs.ReadBitsAsUint(16);
			}
			Valid = true;
		}

		public byte GroupId { get; }
		public uint LogonId { get; }
		public bool ContinuousCarrier { get; }
		public bool SecurityHandshakeRequired { get; }
		public bool PrefixFlag { get; }
		public bool DataUnitLabelingFlag { get; }
		public bool MiniSlotFlag { get; }
		public bool ContentionBasedMiniSlotFlag { get; }
		public bool MeshConnectivityFlag { get; }
		public bool CapacityTypeFlag { get; }
		public bool TrafficBurstType { get; }
		public bool? Connectivity { get; }
		public byte ReturnVPI { get; }
		public ushort ReturnVCI { get; }
		public byte ReturnSignallingVPI { get; }
		public ushort ReturnSignallingVCI { get; }
		public byte ForwardSignallingVPI { get; }
		public ushort ForwardSignallingVCI { get; }
		public ushort ReturnTrfPid { get; }
		public ushort ReturnCtrlMngmPid { get; }
		public uint CraLevel { get; }
		public uint VbdcMax { get; }
		public uint RbdcMax { get; }
		public uint RbdcTimeout { get; }

		public override string ToString()
		{
			return String.Format("Mesh Logon Initalization for Group {0}, Logon {1}", GroupId, LogonId);
		}
	}
}
