using skyscraper5.Skyscraper.IO;
using System.Net;

namespace skyscraper5.Docsis.AnnexC
{
	public class Ipv6PacketClassificationEncoding
	{
		public Ipv6PacketClassificationEncoding(byte[] buffer)
		{
			MemoryStream ms = new MemoryStream(buffer, false);
			while (ms.GetAvailableBytes() > 3)
			{
				byte type = ms.ReadUInt8();
				ushort length = ms.ReadUInt8();
				byte[] v = ms.ReadBytes(length);
				switch (type)
				{
					case 3:
						(v[0], v[1]) = (v[1], v[0]);
						NextHeaderType = BitConverter.ToUInt16(v, 0);
						break;
					case 4:
						Ipv6SourceAddress = new IPAddress(v);
						break;
					case 6:
						Ipv6DestinationAddress = new IPAddress(v);
						break;
					default:
						//CM-SP-MULPIv4.0-I01-190815.pdf, page 749
						throw new NotImplementedException(string.Format("Ipv6PacketClassificationEncoding [22/23/60].12.{0}", type));
				}
			}
		}

		public ushort NextHeaderType { get; }
		public IPAddress Ipv6DestinationAddress { get; }
		public IPAddress Ipv6SourceAddress { get; }
	}
}