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


namespace skyscraper5.src.InteractionChannel.Model.Descriptors
{
	[SkyscraperPlugin]
	[TsDescriptor(0xb5,"TIM")]
	internal class _0xb5_ImplementationTypeDescriptor : TsDescriptor
	{
		public _0xb5_ImplementationTypeDescriptor(byte[] buffer)
		{
			MemoryStream ms = new MemoryStream(buffer, false);
			NccProtocolVersion = ms.ReadUInt8();
			
			byte bytea = ms.ReadUInt8();
			bool reserved1 = (bytea & 0x80) != 0;
			bool reserved2 = (bytea & 0x40) != 0;
			LocationUpdateAllowedFlag = (bytea & 0x20) != 0;
			RbdcAcceptedFlag = (bytea & 0x10) != 0;
			VbdcAcceptedFlag = (bytea & 0x08) != 0;
			AvdbcAcceptedFlag = (bytea & 0x04) != 0;
			TimingOffsetFlag = (bytea & 0x02) != 0;
			TimingReferenceFlag = (bytea & 0x01) != 0;

			C2PProtocolVersion = ms.ReadUInt8() & 0x07;
			HubTypeId = ms.ReadUInt24BE();
			HubSwId = ms.ReadUInt24BE();
			byte userOptionsCount = ms.ReadUInt8();
			if (ms.GetAvailableBytes() < userOptionsCount)
			{
				Valid = false;
				return;
			}

			UserOptions = ms.ReadBytes(userOptionsCount);
			//skip reserved bytes
			Valid = true;
		}

		public byte NccProtocolVersion { get; }
		public bool LocationUpdateAllowedFlag { get; }
		public bool RbdcAcceptedFlag { get; }
		public bool VbdcAcceptedFlag { get; }
		public bool AvdbcAcceptedFlag { get; }
		public bool TimingOffsetFlag { get; }
		public bool TimingReferenceFlag { get; }
		public int C2PProtocolVersion { get; }
		public uint HubTypeId { get; }
		public uint HubSwId { get; }
		public byte[] UserOptions { get; }

		public override string ToString()
		{
			return String.Format("Implementation Type for Hub Type {0}", HubTypeId);
		}
	}
}
