using System;
using System.Collections.Generic;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class ProdTestListModeRx : PacketBase
{
	public class UserFieldsWrapper
	{
		private const uint _Command = 33u;

		private uint _NumberOfSequencesInList;

		private uint _Channel;

		private ulong _BluetoothTargetDeviceAddress;

		private uint _LogicalTransportAddress;

		private uint _NumberOfRotations;

		private List<ParameterSubField> _ParameterList = new List<ParameterSubField>();

		public uint Command => 33u;

		public uint NumberOfSequencesInList
		{
			get
			{
				return _NumberOfSequencesInList;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for NumberOfSequencesInList is 0xFF");
				}
				_NumberOfSequencesInList = value;
				while (_ParameterList.Count < _NumberOfSequencesInList)
				{
					_ParameterList.Add(new ParameterSubField());
				}
				while (_ParameterList.Count > _NumberOfSequencesInList)
				{
					_ParameterList.RemoveAt(_ParameterList.Count - 1);
				}
			}
		}

		public uint Channel
		{
			get
			{
				return _Channel;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for Channel is 0xFF");
				}
				_Channel = value;
			}
		}

		public ulong BluetoothTargetDeviceAddress
		{
			get
			{
				return _BluetoothTargetDeviceAddress;
			}
			set
			{
				_BluetoothTargetDeviceAddress = value;
			}
		}

		public uint LogicalTransportAddress
		{
			get
			{
				return _LogicalTransportAddress;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for LogicalTransportAddress is 0xFF");
				}
				_LogicalTransportAddress = value;
			}
		}

		public uint NumberOfRotations
		{
			get
			{
				return _NumberOfRotations;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for NumberOfRotations is 0xFFFF");
				}
				_NumberOfRotations = value;
			}
		}

		public List<ParameterSubField> ParameterList => _ParameterList;
	}

	public class ParameterSubField
	{
		private uint _PacketType;

		private uint _PayloadType;

		private uint _PayloadLength;

		private uint _NumberOfPackets;

		public uint PacketType
		{
			get
			{
				return _PacketType;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for PacketType is 0xFF");
				}
				_PacketType = value;
			}
		}

		public uint PayloadType
		{
			get
			{
				return _PayloadType;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for PayloadType is 0xFF");
				}
				_PayloadType = value;
			}
		}

		public uint PayloadLength
		{
			get
			{
				return _PayloadLength;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for PayloadLength is 0xFFFF");
				}
				_PayloadLength = value;
			}
		}

		public uint NumberOfPackets
		{
			get
			{
				return _NumberOfPackets;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for NumberOfPackets is 0xFFFF");
				}
				_NumberOfPackets = value;
			}
		}

		public ListModeRxPacketTypeEnum GetPacketTypeAsListModeRxPacketTypeEnum()
		{
			return ListModeRxPacketTypeInfo.ValueToListModeRxPacketType(_PacketType);
		}

		public void SetPacketTypeAsListModeRxPacketTypeEnum(ListModeRxPacketTypeEnum enumItem)
		{
			PacketType = ListModeRxPacketTypeInfo.ListModeRxPacketTypeToValue(enumItem);
		}

		public ListModeRxPayloadTypeEnum GetPayloadTypeAsListModeRxPayloadTypeEnum()
		{
			return ListModeRxPayloadTypeInfo.ValueToListModeRxPayloadType(_PayloadType);
		}

		public void SetPayloadTypeAsListModeRxPayloadTypeEnum(ListModeRxPayloadTypeEnum enumItem)
		{
			PayloadType = ListModeRxPayloadTypeInfo.ListModeRxPayloadTypeToValue(enumItem);
		}

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			PacketType = bitBuilder.RemoveBitsAsUintLittleEndian(8);
			PayloadType = bitBuilder.RemoveBitsAsUintLittleEndian(8);
			PayloadLength = bitBuilder.RemoveBitsAsUintLittleEndian(16);
			NumberOfPackets = bitBuilder.RemoveBitsAsUintLittleEndian(16);
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUintLittleEndian(PacketType, 8);
			bitBuilder.AddBitsAsUintLittleEndian(PayloadType, 8);
			bitBuilder.AddBitsAsUintLittleEndian(PayloadLength, 16);
			bitBuilder.AddBitsAsUintLittleEndian(NumberOfPackets, 16);
			return bitBuilder.ToString();
		}
	}

	private const uint _Opcode = 64516u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(CommandStatusForProdTest),
		typeof(CommandCompleteForNop)
	};

	private PacketInfoBase info;

	private uint _PayloadLength;

	public UserFieldsWrapper _UserFields = new UserFieldsWrapper();

	public override PacketType Type => PacketType.Command;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

	public override PacketInfoBase Info
	{
		get
		{
			if (info == null)
			{
				info = new ProdTestListModeRxInfo(this);
			}
			return info;
		}
	}

	public uint Opcode => 64516u;

	public uint PayloadLength
	{
		get
		{
			ToBitString();
			return _PayloadLength;
		}
	}

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.NumberOfSequencesInList = 1u;
		UserFields.Channel = 0u;
	}

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUintLittleEndian(16) != Opcode)
		{
			return false;
		}
		_PayloadLength = bitBuilder.RemoveBitsAsUint(8);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		if (numberOfBits > _PayloadLength * 8 + 7)
		{
			return false;
		}
		if (numberOfBits < _PayloadLength * 8)
		{
			return false;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.Command)
		{
			return false;
		}
		UserFields.NumberOfSequencesInList = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Channel = bitBuilder.RemoveBitsAsUint(8);
		UserFields.BluetoothTargetDeviceAddress = bitBuilder.RemoveBitsAsUlongLittleEndian(48);
		UserFields.LogicalTransportAddress = bitBuilder.RemoveBitsAsUint(8);
		UserFields.NumberOfRotations = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		for (int i = 0; i < UserFields.ParameterList.Count; i++)
		{
			UserFields.ParameterList[i].FromBitBuilder(ref bitBuilder);
		}
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUintLittleEndian(Opcode, 16);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		bitBuilder.AddBitsAsUint(0u, 8);
		bitBuilder.AddBitsAsUint(UserFields.Command, 8);
		bitBuilder.AddBitsAsUint(UserFields.NumberOfSequencesInList, 8);
		bitBuilder.AddBitsAsUint(UserFields.Channel, 8);
		bitBuilder.AddBitsAsUlongLittleEndian(UserFields.BluetoothTargetDeviceAddress, 48);
		bitBuilder.AddBitsAsUint(UserFields.LogicalTransportAddress, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.NumberOfRotations, 16);
		for (int i = 0; i < UserFields.ParameterList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.ParameterList[i].ToBitString());
		}
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}

	public override List<string> GetWarnings()
	{
		List<string> list = new List<string>();
		if (UserFields.Channel < 0)
		{
			list.Add("Minimum allowed value for Channel is 0x0");
		}
		if (UserFields.Channel > 78)
		{
			list.Add("Maximum allowed value for Channel is 0x4E");
		}
		return list;
	}
}
