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

namespace QCRPG.TSL.BT.Packets;

public class ProdTestMaster : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum TxPatternPayloadEnum
		{
			ALL_ZEROES,
			ALL_ONES,
			ALTERNATING_BIT,
			ALTERNATING_NIBBLE,
			PSEUDO_RANDOM,
			InvalidTxPatternPayloadValue
		}

		public static class TxPatternPayloadInfo
		{
			public static TxPatternPayloadEnum ValueToTxPatternPayload(uint value)
			{
				return value switch
				{
					0u => TxPatternPayloadEnum.ALL_ZEROES, 
					1u => TxPatternPayloadEnum.ALL_ONES, 
					2u => TxPatternPayloadEnum.ALTERNATING_BIT, 
					3u => TxPatternPayloadEnum.ALTERNATING_NIBBLE, 
					4u => TxPatternPayloadEnum.PSEUDO_RANDOM, 
					_ => TxPatternPayloadEnum.InvalidTxPatternPayloadValue, 
				};
			}

			public static uint TxPatternPayloadToValue(TxPatternPayloadEnum enumItem)
			{
				return enumItem switch
				{
					TxPatternPayloadEnum.ALL_ZEROES => 0u, 
					TxPatternPayloadEnum.ALL_ONES => 1u, 
					TxPatternPayloadEnum.ALTERNATING_BIT => 2u, 
					TxPatternPayloadEnum.ALTERNATING_NIBBLE => 3u, 
					TxPatternPayloadEnum.PSEUDO_RANDOM => 4u, 
					_ => uint.MaxValue, 
				};
			}

			public static string TxPatternPayloadToString(TxPatternPayloadEnum enumItem)
			{
				return enumItem switch
				{
					TxPatternPayloadEnum.ALL_ZEROES => "All zeroes", 
					TxPatternPayloadEnum.ALL_ONES => "All ones", 
					TxPatternPayloadEnum.ALTERNATING_BIT => "Alternating bit", 
					TxPatternPayloadEnum.ALTERNATING_NIBBLE => "Alternating nibble", 
					TxPatternPayloadEnum.PSEUDO_RANDOM => "Psuedo-random", 
					_ => "Invalid TxPatternPayload Value", 
				};
			}

			public static string TxPatternPayloadToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = All zeroes");
				stringBuilder.AppendLine("0x1 = All ones");
				stringBuilder.AppendLine("0x2 = Alternating bit");
				stringBuilder.AppendLine("0x3 = Alternating nibble");
				stringBuilder.AppendLine("0x4 = Psuedo-random");
				return stringBuilder.ToString();
			}
		}

		public enum PacketTypeEnum
		{
			NULL_Packets,
			POLL_Packets,
			FHS_Packets,
			DM1_Packets,
			DH1_Packets,
			DM3_Packets,
			DH3_Packets,
			DM5_Packets,
			DH5_Packets,
			AUX1_Packets,
			TWO_DH1_Packets,
			TWO_DH3_Packets,
			TWO_DH5_Packets,
			THREE_DH1_Packets,
			THREE_DH3_Packets,
			THREE_DH5_Packets,
			HV1_Packets,
			HV2_Packets,
			HV3_Packets,
			DV_Packets,
			EV3_Packets,
			EV4_Packets,
			EV5_Packets,
			TWO_EV3_Packets,
			TWO_EV5_Packets,
			THREE_EV3_Packets,
			THREE_EV5_Packets,
			InvalidPacketTypeValue
		}

		public static class PacketTypeInfo
		{
			public static PacketTypeEnum ValueToPacketType(uint value)
			{
				return value switch
				{
					0u => PacketTypeEnum.NULL_Packets, 
					1u => PacketTypeEnum.POLL_Packets, 
					2u => PacketTypeEnum.FHS_Packets, 
					3u => PacketTypeEnum.DM1_Packets, 
					4u => PacketTypeEnum.DH1_Packets, 
					10u => PacketTypeEnum.DM3_Packets, 
					11u => PacketTypeEnum.DH3_Packets, 
					14u => PacketTypeEnum.DM5_Packets, 
					15u => PacketTypeEnum.DH5_Packets, 
					9u => PacketTypeEnum.AUX1_Packets, 
					36u => PacketTypeEnum.TWO_DH1_Packets, 
					42u => PacketTypeEnum.TWO_DH3_Packets, 
					46u => PacketTypeEnum.TWO_DH5_Packets, 
					40u => PacketTypeEnum.THREE_DH1_Packets, 
					43u => PacketTypeEnum.THREE_DH3_Packets, 
					47u => PacketTypeEnum.THREE_DH5_Packets, 
					5u => PacketTypeEnum.HV1_Packets, 
					6u => PacketTypeEnum.HV2_Packets, 
					7u => PacketTypeEnum.HV3_Packets, 
					8u => PacketTypeEnum.DV_Packets, 
					23u => PacketTypeEnum.EV3_Packets, 
					28u => PacketTypeEnum.EV4_Packets, 
					29u => PacketTypeEnum.EV5_Packets, 
					54u => PacketTypeEnum.TWO_EV3_Packets, 
					60u => PacketTypeEnum.TWO_EV5_Packets, 
					55u => PacketTypeEnum.THREE_EV3_Packets, 
					61u => PacketTypeEnum.THREE_EV5_Packets, 
					_ => PacketTypeEnum.InvalidPacketTypeValue, 
				};
			}

			public static uint PacketTypeToValue(PacketTypeEnum enumItem)
			{
				return enumItem switch
				{
					PacketTypeEnum.NULL_Packets => 0u, 
					PacketTypeEnum.POLL_Packets => 1u, 
					PacketTypeEnum.FHS_Packets => 2u, 
					PacketTypeEnum.DM1_Packets => 3u, 
					PacketTypeEnum.DH1_Packets => 4u, 
					PacketTypeEnum.DM3_Packets => 10u, 
					PacketTypeEnum.DH3_Packets => 11u, 
					PacketTypeEnum.DM5_Packets => 14u, 
					PacketTypeEnum.DH5_Packets => 15u, 
					PacketTypeEnum.AUX1_Packets => 9u, 
					PacketTypeEnum.TWO_DH1_Packets => 36u, 
					PacketTypeEnum.TWO_DH3_Packets => 42u, 
					PacketTypeEnum.TWO_DH5_Packets => 46u, 
					PacketTypeEnum.THREE_DH1_Packets => 40u, 
					PacketTypeEnum.THREE_DH3_Packets => 43u, 
					PacketTypeEnum.THREE_DH5_Packets => 47u, 
					PacketTypeEnum.HV1_Packets => 5u, 
					PacketTypeEnum.HV2_Packets => 6u, 
					PacketTypeEnum.HV3_Packets => 7u, 
					PacketTypeEnum.DV_Packets => 8u, 
					PacketTypeEnum.EV3_Packets => 23u, 
					PacketTypeEnum.EV4_Packets => 28u, 
					PacketTypeEnum.EV5_Packets => 29u, 
					PacketTypeEnum.TWO_EV3_Packets => 54u, 
					PacketTypeEnum.TWO_EV5_Packets => 60u, 
					PacketTypeEnum.THREE_EV3_Packets => 55u, 
					PacketTypeEnum.THREE_EV5_Packets => 61u, 
					_ => uint.MaxValue, 
				};
			}

			public static string PacketTypeToString(PacketTypeEnum enumItem)
			{
				return enumItem switch
				{
					PacketTypeEnum.NULL_Packets => "NULL_Packets", 
					PacketTypeEnum.POLL_Packets => "POLL_Packets", 
					PacketTypeEnum.FHS_Packets => "FHS_Packets", 
					PacketTypeEnum.DM1_Packets => "DM1_Packets", 
					PacketTypeEnum.DH1_Packets => "DH1_Packets", 
					PacketTypeEnum.DM3_Packets => "DM3_Packets", 
					PacketTypeEnum.DH3_Packets => "DH3_Packets", 
					PacketTypeEnum.DM5_Packets => "DM5_Packets", 
					PacketTypeEnum.DH5_Packets => "DH5_Packets", 
					PacketTypeEnum.AUX1_Packets => "AUX1_Packets", 
					PacketTypeEnum.TWO_DH1_Packets => "TWO_DH1_Packets", 
					PacketTypeEnum.TWO_DH3_Packets => "TWO_DH3_Packets", 
					PacketTypeEnum.TWO_DH5_Packets => "TWO_DH5_Packets", 
					PacketTypeEnum.THREE_DH1_Packets => "THREE_DH1_Packets", 
					PacketTypeEnum.THREE_DH3_Packets => "THREE_DH3_Packets", 
					PacketTypeEnum.THREE_DH5_Packets => "THREE_DH5_Packets", 
					PacketTypeEnum.HV1_Packets => "HV1_Packets", 
					PacketTypeEnum.HV2_Packets => "HV2_Packets", 
					PacketTypeEnum.HV3_Packets => "HV3_Packets", 
					PacketTypeEnum.DV_Packets => "DV_Packets", 
					PacketTypeEnum.EV3_Packets => "EV3_Packets", 
					PacketTypeEnum.EV4_Packets => "EV4_Packets", 
					PacketTypeEnum.EV5_Packets => "EV5_Packets", 
					PacketTypeEnum.TWO_EV3_Packets => "TWO_EV3_Packets", 
					PacketTypeEnum.TWO_EV5_Packets => "TWO_EV5_Packets", 
					PacketTypeEnum.THREE_EV3_Packets => "THREE_EV3_Packets", 
					PacketTypeEnum.THREE_EV5_Packets => "THREE_EV5_Packets", 
					_ => "Invalid PacketType Value", 
				};
			}

			public static string PacketTypeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = NULL_Packets");
				stringBuilder.AppendLine("0x1 = POLL_Packets");
				stringBuilder.AppendLine("0x2 = FHS_Packets");
				stringBuilder.AppendLine("0x3 = DM1_Packets");
				stringBuilder.AppendLine("0x4 = DH1_Packets");
				stringBuilder.AppendLine("0xA = DM3_Packets");
				stringBuilder.AppendLine("0xB = DH3_Packets");
				stringBuilder.AppendLine("0xE = DM5_Packets");
				stringBuilder.AppendLine("0xF = DH5_Packets");
				stringBuilder.AppendLine("0x9 = AUX1_Packets");
				stringBuilder.AppendLine("0x24 = TWO_DH1_Packets");
				stringBuilder.AppendLine("0x2A = TWO_DH3_Packets");
				stringBuilder.AppendLine("0x2E = TWO_DH5_Packets");
				stringBuilder.AppendLine("0x28 = THREE_DH1_Packets");
				stringBuilder.AppendLine("0x2B = THREE_DH3_Packets");
				stringBuilder.AppendLine("0x2F = THREE_DH5_Packets");
				stringBuilder.AppendLine("0x5 = HV1_Packets");
				stringBuilder.AppendLine("0x6 = HV2_Packets");
				stringBuilder.AppendLine("0x7 = HV3_Packets");
				stringBuilder.AppendLine("0x8 = DV_Packets");
				stringBuilder.AppendLine("0x17 = EV3_Packets");
				stringBuilder.AppendLine("0x1C = EV4_Packets");
				stringBuilder.AppendLine("0x1D = EV5_Packets");
				stringBuilder.AppendLine("0x36 = TWO_EV3_Packets");
				stringBuilder.AppendLine("0x3C = TWO_EV5_Packets");
				stringBuilder.AppendLine("0x37 = THREE_EV3_Packets");
				stringBuilder.AppendLine("0x3D = THREE_EV5_Packets");
				return stringBuilder.ToString();
			}
		}

		private const uint _Subopcode = 1u;

		private uint _HopChannel1;

		private uint _HopChannel2;

		private uint _HopChannel3;

		private uint _HopChannel4;

		private uint _HopChannel5;

		private uint _TxPatternPayload;

		private uint _PacketType;

		private uint _DataWhitening;

		private uint _TransmitOutputPower;

		private uint _ReceiverGain;

		private ulong _BluetoothTargetDeviceAddress;

		private uint _HoppingFunctionality;

		private uint _PayloadLength;

		private uint _LogicalTransportAddress;

		public uint Subopcode => 1u;

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

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

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

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

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

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

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

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

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

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

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

		public uint HoppingFunctionality
		{
			get
			{
				return _HoppingFunctionality;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for HoppingFunctionality is 0xFF");
				}
				_HoppingFunctionality = 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 LogicalTransportAddress
		{
			get
			{
				return _LogicalTransportAddress;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for LogicalTransportAddress is 0xFF");
				}
				_LogicalTransportAddress = value;
			}
		}

		public TxPatternPayloadEnum GetTxPatternPayloadAsTxPatternPayloadEnum()
		{
			return TxPatternPayloadInfo.ValueToTxPatternPayload(_TxPatternPayload);
		}

		public void SetTxPatternPayloadAsTxPatternPayloadEnum(TxPatternPayloadEnum enumItem)
		{
			TxPatternPayload = TxPatternPayloadInfo.TxPatternPayloadToValue(enumItem);
		}

		public PacketTypeEnum GetPacketTypeAsPacketTypeEnum()
		{
			return PacketTypeInfo.ValueToPacketType(_PacketType);
		}

		public void SetPacketTypeAsPacketTypeEnum(PacketTypeEnum enumItem)
		{
			PacketType = PacketTypeInfo.PacketTypeToValue(enumItem);
		}
	}

	private const uint _Opcode = 64516u;

	private Type[] expectedResponseTypes = new Type[3]
	{
		typeof(CommandStatusForProdTest),
		typeof(ConnectionComplete),
		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 ProdTestMasterInfo(this);
			}
			return info;
		}
	}

	public uint Opcode => 64516u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	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.Subopcode)
		{
			return false;
		}
		UserFields.HopChannel1 = bitBuilder.RemoveBitsAsUint(8);
		UserFields.HopChannel2 = bitBuilder.RemoveBitsAsUint(8);
		UserFields.HopChannel3 = bitBuilder.RemoveBitsAsUint(8);
		UserFields.HopChannel4 = bitBuilder.RemoveBitsAsUint(8);
		UserFields.HopChannel5 = bitBuilder.RemoveBitsAsUint(8);
		UserFields.TxPatternPayload = bitBuilder.RemoveBitsAsUint(8);
		UserFields.PacketType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.DataWhitening = bitBuilder.RemoveBitsAsUint(8);
		UserFields.TransmitOutputPower = bitBuilder.RemoveBitsAsUint(8);
		UserFields.ReceiverGain = bitBuilder.RemoveBitsAsUint(8);
		UserFields.BluetoothTargetDeviceAddress = bitBuilder.RemoveBitsAsUlongLittleEndian(48);
		UserFields.HoppingFunctionality = bitBuilder.RemoveBitsAsUint(8);
		UserFields.PayloadLength = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.LogicalTransportAddress = bitBuilder.RemoveBitsAsUint(8);
		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.Subopcode, 8);
		bitBuilder.AddBitsAsUint(UserFields.HopChannel1, 8);
		bitBuilder.AddBitsAsUint(UserFields.HopChannel2, 8);
		bitBuilder.AddBitsAsUint(UserFields.HopChannel3, 8);
		bitBuilder.AddBitsAsUint(UserFields.HopChannel4, 8);
		bitBuilder.AddBitsAsUint(UserFields.HopChannel5, 8);
		bitBuilder.AddBitsAsUint(UserFields.TxPatternPayload, 8);
		bitBuilder.AddBitsAsUint(UserFields.PacketType, 8);
		bitBuilder.AddBitsAsUint(UserFields.DataWhitening, 8);
		bitBuilder.AddBitsAsUint(UserFields.TransmitOutputPower, 8);
		bitBuilder.AddBitsAsUint(UserFields.ReceiverGain, 8);
		bitBuilder.AddBitsAsUlongLittleEndian(UserFields.BluetoothTargetDeviceAddress, 48);
		bitBuilder.AddBitsAsUint(UserFields.HoppingFunctionality, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.PayloadLength, 16);
		bitBuilder.AddBitsAsUint(UserFields.LogicalTransportAddress, 8);
		_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.HopChannel1 > 78)
		{
			list.Add("Maximum allowed value for Hop_Channel_1 is 0x4E");
		}
		if (UserFields.HopChannel2 > 78)
		{
			list.Add("Maximum allowed value for Hop_Channel_2 is 0x4E");
		}
		if (UserFields.HopChannel3 > 78)
		{
			list.Add("Maximum allowed value for Hop_Channel_3 is 0x4E");
		}
		if (UserFields.HopChannel4 > 78)
		{
			list.Add("Maximum allowed value for Hop_Channel_4 is 0x4E");
		}
		if (UserFields.HopChannel5 > 78)
		{
			list.Add("Maximum allowed value for Hop_Channel_5 is 0x4E");
		}
		if (UserFields.TxPatternPayload > 4)
		{
			list.Add("Maximum allowed value for TX_Pattern_Payload is 0x4");
		}
		if (UserFields.PacketType > 61)
		{
			list.Add("Maximum allowed value for Packet_Type is 0x3D");
		}
		if (UserFields.DataWhitening > 1)
		{
			list.Add("Maximum allowed value for Data_Whitening is 0x1");
		}
		if (UserFields.TransmitOutputPower > 9)
		{
			list.Add("Maximum allowed value for Transmit_Output_Power is 0x9");
		}
		if (UserFields.ReceiverGain > 1)
		{
			list.Add("Maximum allowed value for Receiver_Gain is 0x1");
		}
		if (UserFields.HoppingFunctionality > 1)
		{
			list.Add("Maximum allowed value for Hopping_Functionality is 0x1");
		}
		return list;
	}
}
