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

namespace QCRPG.TSL.BT.Packets;

public class ProdTestListModeTx : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum ProdTestListModeTxEnum
		{
			TxMode1_BR_EDR_LE,
			TxMode2_BR_EDR,
			TxMode3_LE1M,
			TxMode4_LE2M,
			TxMode5_LELR8,
			TxMode6_LELR2,
			TxMode7_LELR8_LELR2,
			TxMode8_LE1M_LE2M_LELR8,
			TxMode9_LE1M_LE2M_LELR8_LELR2,
			TxMode10_BR_EDR_LE1M_LE2M,
			TxMode11_BR_EDR_LE1M_LE2M_LELR8,
			TxMode12_BR_EDR_LE1M_LE2M_LELR8_LELR2,
			InvalidProdTestListModeTxValue
		}

		public static class ProdTestListModeTxInfo
		{
			public static ProdTestListModeTxEnum ValueToProdTestListModeTx(uint value)
			{
				return value switch
				{
					0u => ProdTestListModeTxEnum.TxMode1_BR_EDR_LE, 
					1u => ProdTestListModeTxEnum.TxMode2_BR_EDR, 
					2u => ProdTestListModeTxEnum.TxMode3_LE1M, 
					3u => ProdTestListModeTxEnum.TxMode4_LE2M, 
					4u => ProdTestListModeTxEnum.TxMode5_LELR8, 
					5u => ProdTestListModeTxEnum.TxMode6_LELR2, 
					6u => ProdTestListModeTxEnum.TxMode7_LELR8_LELR2, 
					7u => ProdTestListModeTxEnum.TxMode8_LE1M_LE2M_LELR8, 
					8u => ProdTestListModeTxEnum.TxMode9_LE1M_LE2M_LELR8_LELR2, 
					9u => ProdTestListModeTxEnum.TxMode10_BR_EDR_LE1M_LE2M, 
					10u => ProdTestListModeTxEnum.TxMode11_BR_EDR_LE1M_LE2M_LELR8, 
					11u => ProdTestListModeTxEnum.TxMode12_BR_EDR_LE1M_LE2M_LELR8_LELR2, 
					_ => ProdTestListModeTxEnum.InvalidProdTestListModeTxValue, 
				};
			}

			public static uint ProdTestListModeTxToValue(ProdTestListModeTxEnum enumItem)
			{
				return enumItem switch
				{
					ProdTestListModeTxEnum.TxMode1_BR_EDR_LE => 0u, 
					ProdTestListModeTxEnum.TxMode2_BR_EDR => 1u, 
					ProdTestListModeTxEnum.TxMode3_LE1M => 2u, 
					ProdTestListModeTxEnum.TxMode4_LE2M => 3u, 
					ProdTestListModeTxEnum.TxMode5_LELR8 => 4u, 
					ProdTestListModeTxEnum.TxMode6_LELR2 => 5u, 
					ProdTestListModeTxEnum.TxMode7_LELR8_LELR2 => 6u, 
					ProdTestListModeTxEnum.TxMode8_LE1M_LE2M_LELR8 => 7u, 
					ProdTestListModeTxEnum.TxMode9_LE1M_LE2M_LELR8_LELR2 => 8u, 
					ProdTestListModeTxEnum.TxMode10_BR_EDR_LE1M_LE2M => 9u, 
					ProdTestListModeTxEnum.TxMode11_BR_EDR_LE1M_LE2M_LELR8 => 10u, 
					ProdTestListModeTxEnum.TxMode12_BR_EDR_LE1M_LE2M_LELR8_LELR2 => 11u, 
					_ => uint.MaxValue, 
				};
			}

			public static string ProdTestListModeTxToString(ProdTestListModeTxEnum enumItem)
			{
				return enumItem switch
				{
					ProdTestListModeTxEnum.TxMode1_BR_EDR_LE => "Tx Mode 1 BR-EDR-LE1M", 
					ProdTestListModeTxEnum.TxMode2_BR_EDR => "Tx Mode 2 BR-EDR", 
					ProdTestListModeTxEnum.TxMode3_LE1M => "Tx Mode 3 LE1M", 
					ProdTestListModeTxEnum.TxMode4_LE2M => "Tx Mode 4 LE2M", 
					ProdTestListModeTxEnum.TxMode5_LELR8 => "Tx Mode 5 LELR8", 
					ProdTestListModeTxEnum.TxMode6_LELR2 => "Tx Mode 6 LELR2", 
					ProdTestListModeTxEnum.TxMode7_LELR8_LELR2 => "Tx Mode 7 LELR8-LELR2", 
					ProdTestListModeTxEnum.TxMode8_LE1M_LE2M_LELR8 => "Tx Mode 8 LE1M-LE2M-LELR8", 
					ProdTestListModeTxEnum.TxMode9_LE1M_LE2M_LELR8_LELR2 => "Tx Mode 9 LE1M-LE2M-LELR8-LELR2", 
					ProdTestListModeTxEnum.TxMode10_BR_EDR_LE1M_LE2M => "Tx Mode 10 BR-EDR-LE1M-LE2M", 
					ProdTestListModeTxEnum.TxMode11_BR_EDR_LE1M_LE2M_LELR8 => "Tx Mode 11 BR-EDR-LE1M-LE2M-LELR8", 
					ProdTestListModeTxEnum.TxMode12_BR_EDR_LE1M_LE2M_LELR8_LELR2 => "Tx Mode 12 BR-EDR-LE1M-LE2M-LELR8-LELR2", 
					_ => "Invalid ProdTestListModeTx Value", 
				};
			}

			public static string ProdTestListModeTxToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Tx Mode 1 BR-EDR-LE1M");
				stringBuilder.AppendLine("0x1 = Tx Mode 2 BR-EDR");
				stringBuilder.AppendLine("0x2 = Tx Mode 3 LE1M");
				stringBuilder.AppendLine("0x3 = Tx Mode 4 LE2M");
				stringBuilder.AppendLine("0x4 = Tx Mode 5 LELR8");
				stringBuilder.AppendLine("0x5 = Tx Mode 6 LELR2");
				stringBuilder.AppendLine("0x6 = Tx Mode 7 LELR8-LELR2");
				stringBuilder.AppendLine("0x7 = Tx Mode 8 LE1M-LE2M-LELR8");
				stringBuilder.AppendLine("0x8 = Tx Mode 9 LE1M-LE2M-LELR8-LELR2");
				stringBuilder.AppendLine("0x9 = Tx Mode 10 BR-EDR-LE1M-LE2M");
				stringBuilder.AppendLine("0xA = Tx Mode 11 BR-EDR-LE1M-LE2M-LELR8");
				stringBuilder.AppendLine("0xB = Tx Mode 12 BR-EDR-LE1M-LE2M-LELR8-LELR2");
				return stringBuilder.ToString();
			}
		}

		private const uint _Command = 32u;

		private uint _ProdTestListModeTx;

		private uint _Channel;

		private ulong _BluetoothTargetDeviceAddress;

		private uint _LogicalTransportAddress;

		private uint _TransmitOutputPower;

		private uint _NumberOfPackets;

		private uint _NullBeforeSequence;

		public uint Command => 32u;

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

		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 TransmitOutputPower
		{
			get
			{
				return _TransmitOutputPower;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for TransmitOutputPower is 0xFF");
				}
				_TransmitOutputPower = value;
			}
		}

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

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

		public ProdTestListModeTxEnum GetProdTestListModeTxAsProdTestListModeTxEnum()
		{
			return ProdTestListModeTxInfo.ValueToProdTestListModeTx(_ProdTestListModeTx);
		}

		public void SetProdTestListModeTxAsProdTestListModeTxEnum(ProdTestListModeTxEnum enumItem)
		{
			ProdTestListModeTx = ProdTestListModeTxInfo.ProdTestListModeTxToValue(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 ProdTestListModeTxInfo(this);
			}
			return info;
		}
	}

	public uint Opcode => 64516u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.Channel = 0u;
		UserFields.TransmitOutputPower = 0u;
		UserFields.NullBeforeSequence = 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.ProdTestListModeTx = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Channel = bitBuilder.RemoveBitsAsUint(8);
		UserFields.BluetoothTargetDeviceAddress = bitBuilder.RemoveBitsAsUlongLittleEndian(48);
		UserFields.LogicalTransportAddress = bitBuilder.RemoveBitsAsUint(8);
		UserFields.TransmitOutputPower = bitBuilder.RemoveBitsAsUint(8);
		UserFields.NumberOfPackets = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.NullBeforeSequence = 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.Command, 8);
		bitBuilder.AddBitsAsUint(UserFields.ProdTestListModeTx, 8);
		bitBuilder.AddBitsAsUint(UserFields.Channel, 8);
		bitBuilder.AddBitsAsUlongLittleEndian(UserFields.BluetoothTargetDeviceAddress, 48);
		bitBuilder.AddBitsAsUint(UserFields.LogicalTransportAddress, 8);
		bitBuilder.AddBitsAsUint(UserFields.TransmitOutputPower, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.NumberOfPackets, 16);
		bitBuilder.AddBitsAsUint(UserFields.NullBeforeSequence, 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.ProdTestListModeTx > 11)
		{
			list.Add("Maximum allowed value for PROD_TEST_List_Mode_TX is 0xB");
		}
		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");
		}
		if (UserFields.TransmitOutputPower < 0)
		{
			list.Add("Minimum allowed value for Transmit Output Power is 0x0");
		}
		if (UserFields.TransmitOutputPower > 9)
		{
			list.Add("Maximum allowed value for Transmit Output Power is 0x9");
		}
		if (UserFields.NullBeforeSequence < 0)
		{
			list.Add("Minimum allowed value for NULL before sequence is 0x0");
		}
		if (UserFields.NullBeforeSequence > 1)
		{
			list.Add("Maximum allowed value for NULL before sequence is 0x1");
		}
		return list;
	}
}
