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

namespace skyscraper8.Docsis.DQoS
{
	public class PacketCableAuthorizationBlockEncoding : Validatable
	{
		public PacketCableAuthorizationBlockEncoding(byte[] buffer)
		{
			MemoryStream ms = new MemoryStream(buffer, false);
			while (ms.GetAvailableBytes() > 3)
			{
				byte type = ms.ReadUInt8();
				ushort length = ms.ReadUInt8();
				byte[] value = ms.ReadBytes(length);

				switch (type)
				{
					case 1:
						(value[0], value[1], value[2], value[3]) = (value[3], value[2], value[1], value[0]);
						GateId = BitConverter.ToUInt32(value);
						break;
					case 2:
						(value[0], value[1], value[2], value[3]) = (value[3], value[2], value[1], value[0]);
						ResourceId = BitConverter.ToUInt32(value);
						break;
					default:
						//According to https://de.scribd.com/document/482615299/PKT-SP-DQOS-C01-071129 page 43,
						//this is complete.
						Valid = false;
						return;
				}
			}
			Valid = true;
		}

		public uint GateId { get; }
		public uint ResourceId { get; }
	}
}
