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.Threading.Tasks;

namespace skyscraper5.src.InteractionChannel.Model.Descriptors
{
	[SkyscraperPlugin]
	[TsDescriptor(0xa5,"TIM")]
	internal class _0xa5_EncryptedLoginIdDescriptor : TsDescriptor
	{
		public _0xa5_EncryptedLoginIdDescriptor(byte[] buffer)
		{
			MemoryStream ms = new MemoryStream(buffer, false);
			IdStartTime = ms.ReadUInt16BE();
			IdUpdatePeriod = ms.ReadUInt16BE();
			byte idLoopCount = ms.ReadUInt8(); idLoopCount++;

			int requiredLength = idLoopCount * 2;
			if (ms.GetAvailableBytes() < requiredLength)
			{
				Valid = false;
				return;
			}
			EncryptedLoginId = new ushort[idLoopCount];
			for (int i = 0; i < idLoopCount; i++)
			{
				EncryptedLoginId[i] = ms.ReadUInt16BE();
			}
			Valid = true;
		}

		public ushort IdStartTime { get; }
		public ushort IdUpdatePeriod { get; }
		public ushort[] EncryptedLoginId { get; }

		public override string ToString()
		{
			StringBuilder sb = new StringBuilder();
			sb.Append("Encrypted Login Ids: ");
			foreach(ushort u in EncryptedLoginId)
			{
				sb.Append(u);
				sb.Append(',');
			}
			return sb.ToString();
		}
	}
}
