using skyscraper5.Skyscraper.IO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Channels;
using System.Threading.Tasks;
using static skyscraper5.src.InteractionChannel.Model2.Tbtp2;

namespace skyscraper5.Docsis.AnnexC
{
	public class RcpIdEncoding
	{
		public RcpIdEncoding(byte[] buffer)
		{
			MemoryStream ms = new MemoryStream(buffer, false);

			while (ms.GetAvailableBytes() > 2)
			{
				byte type = ms.ReadUInt8();
				byte length = ms.ReadUInt8();
				byte[] v = ms.ReadBytes(length);
				switch (type)
				{
					case 7:
						SimplifiedReceiveChannelConfiguration = new SimplifiedReceiveChannelAssignmentEncoding(v);
						break;
					default:
						throw new NotImplementedException(String.Format("{0} {1}", nameof(ModemCapabilitiesEncoding), type));
				}
			}
		}

		public SimplifiedReceiveChannelAssignmentEncoding SimplifiedReceiveChannelConfiguration { get; private set; }
	}

	public class SimplifiedReceiveChannelAssignmentEncoding
	{
		public SimplifiedReceiveChannelAssignmentEncoding(byte[] buffer)
		{
			MemoryStream ms = new MemoryStream(buffer, false);

			while (ms.GetAvailableBytes() > 2)
			{
				byte type = ms.ReadUInt8();
				byte length = ms.ReadUInt8();
				byte[] v = ms.ReadBytes(length);
				switch (type)
				{
					case 1:
						PrimaryDownstreamChannelAssignment = v;
						break;
					case 2:
						DownstreamChannelAssignment = v;
						break;
					case 3:
						DownstreamProfileAssignment = v;
						break;
					default:
						//page 728, CM-SP-MULPIv4.0-I01-190815.pdf
						throw new NotImplementedException(String.Format("{0} {1}", nameof(SimplifiedReceiveChannelAssignmentEncoding), type));
				}
			}
		}

		public byte[] PrimaryDownstreamChannelAssignment { get; private set; }
		public byte[] DownstreamChannelAssignment { get; private set; }
		public byte[] DownstreamProfileAssignment { get; }
	}
}
