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

namespace QCRPG.TSL.BT.Packets;

public class LeEnhancedTransmitterTest : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum PacketPayloadEnum
		{
			Pseudo_random_bit_sequence_9,
			Repeated_11110000_sequence,
			Repeated_10101010_sequence,
			Pseudo_random_bit_sequence_15,
			Repeated_11111111_sequence,
			Repeated_00000000_sequence,
			Repeated_00001111_sequence,
			Repeated_01010101_sequence,
			Reserved_for_future_use,
			InvalidPacketPayloadValue
		}

		public static class PacketPayloadInfo
		{
			public static PacketPayloadEnum ValueToPacketPayload(uint value)
			{
				switch (value)
				{
				case 0u:
					return PacketPayloadEnum.Pseudo_random_bit_sequence_9;
				case 1u:
					return PacketPayloadEnum.Repeated_11110000_sequence;
				case 2u:
					return PacketPayloadEnum.Repeated_10101010_sequence;
				case 3u:
					return PacketPayloadEnum.Pseudo_random_bit_sequence_15;
				case 4u:
					return PacketPayloadEnum.Repeated_11111111_sequence;
				case 5u:
					return PacketPayloadEnum.Repeated_00000000_sequence;
				case 6u:
					return PacketPayloadEnum.Repeated_00001111_sequence;
				case 7u:
					return PacketPayloadEnum.Repeated_01010101_sequence;
				default:
					if (value >= 8 && value <= 255)
					{
						return PacketPayloadEnum.Reserved_for_future_use;
					}
					return PacketPayloadEnum.InvalidPacketPayloadValue;
				}
			}

			public static uint PacketPayloadToValue(PacketPayloadEnum enumItem)
			{
				return enumItem switch
				{
					PacketPayloadEnum.Pseudo_random_bit_sequence_9 => 0u, 
					PacketPayloadEnum.Repeated_11110000_sequence => 1u, 
					PacketPayloadEnum.Repeated_10101010_sequence => 2u, 
					PacketPayloadEnum.Pseudo_random_bit_sequence_15 => 3u, 
					PacketPayloadEnum.Repeated_11111111_sequence => 4u, 
					PacketPayloadEnum.Repeated_00000000_sequence => 5u, 
					PacketPayloadEnum.Repeated_00001111_sequence => 6u, 
					PacketPayloadEnum.Repeated_01010101_sequence => 7u, 
					PacketPayloadEnum.Reserved_for_future_use => 8u, 
					_ => uint.MaxValue, 
				};
			}

			public static string PacketPayloadToString(PacketPayloadEnum enumItem)
			{
				return enumItem switch
				{
					PacketPayloadEnum.Pseudo_random_bit_sequence_9 => "PRBS9 sequence '11111111100000111101...'", 
					PacketPayloadEnum.Repeated_11110000_sequence => "Repeated '11110000'", 
					PacketPayloadEnum.Repeated_10101010_sequence => "Repeated '10101010'", 
					PacketPayloadEnum.Pseudo_random_bit_sequence_15 => "PRBS15 sequence", 
					PacketPayloadEnum.Repeated_11111111_sequence => "Repeated '11111111'", 
					PacketPayloadEnum.Repeated_00000000_sequence => "Repeated '00000000'", 
					PacketPayloadEnum.Repeated_00001111_sequence => "Repeated '00001111'", 
					PacketPayloadEnum.Repeated_01010101_sequence => "Repeated '01010101'", 
					PacketPayloadEnum.Reserved_for_future_use => "Reserved for future use", 
					_ => "Invalid PacketPayload Value", 
				};
			}

			public static string PacketPayloadToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = PRBS9 sequence '11111111100000111101...'");
				stringBuilder.AppendLine("0x1 = Repeated '11110000'");
				stringBuilder.AppendLine("0x2 = Repeated '10101010'");
				stringBuilder.AppendLine("0x3 = PRBS15 sequence");
				stringBuilder.AppendLine("0x4 = Repeated '11111111'");
				stringBuilder.AppendLine("0x5 = Repeated '00000000'");
				stringBuilder.AppendLine("0x6 = Repeated '00001111'");
				stringBuilder.AppendLine("0x7 = Repeated '01010101'");
				stringBuilder.AppendLine("0x8-0xFF = Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		public enum PhyEnum
		{
			Reserved_for_future_use,
			Transmitter_set_to_use_the_LE_1M_PHY,
			Transmitter_set_to_use_the_LE_2M_PHY,
			Transmitter_set_to_use_the_LE_Coded_PHY_with_S_equals_8_data_coding,
			Transmitter_set_to_use_the_LE_Coded_PHY_with_S_equals_2_data_coding,
			Transmitter_set_to_use_the_LE_1M_Coded_PHY_Simultaneous,
			Transmitter_set_to_use_the_LE_HS_2M_data_coding,
			Transmitter_set_to_use_the_LE_HS_3M_data_coding,
			Transmitter_set_to_use_the_LE_HS_4M_data_coding,
			Transmitter_set_to_use_the_LE_HS_5M_data_coding,
			Transmitter_set_to_use_the_LE_HS_6M_data_coding,
			InvalidPhyValue
		}

		public static class PhyInfo
		{
			public static PhyEnum ValueToPhy(uint value)
			{
				switch (value)
				{
				case 0u:
					return PhyEnum.Reserved_for_future_use;
				case 1u:
					return PhyEnum.Transmitter_set_to_use_the_LE_1M_PHY;
				case 2u:
					return PhyEnum.Transmitter_set_to_use_the_LE_2M_PHY;
				case 3u:
					return PhyEnum.Transmitter_set_to_use_the_LE_Coded_PHY_with_S_equals_8_data_coding;
				case 4u:
					return PhyEnum.Transmitter_set_to_use_the_LE_Coded_PHY_with_S_equals_2_data_coding;
				case 5u:
					return PhyEnum.Transmitter_set_to_use_the_LE_1M_Coded_PHY_Simultaneous;
				case 6u:
					return PhyEnum.Transmitter_set_to_use_the_LE_HS_2M_data_coding;
				case 7u:
					return PhyEnum.Transmitter_set_to_use_the_LE_HS_3M_data_coding;
				case 8u:
					return PhyEnum.Transmitter_set_to_use_the_LE_HS_4M_data_coding;
				case 9u:
					return PhyEnum.Transmitter_set_to_use_the_LE_HS_5M_data_coding;
				case 10u:
					return PhyEnum.Transmitter_set_to_use_the_LE_HS_6M_data_coding;
				default:
					if (value >= 11 && value <= 255)
					{
						return PhyEnum.Reserved_for_future_use;
					}
					return PhyEnum.InvalidPhyValue;
				}
			}

			public static uint PhyToValue(PhyEnum enumItem)
			{
				switch (enumItem)
				{
				case PhyEnum.Reserved_for_future_use:
					return 0u;
				case PhyEnum.Transmitter_set_to_use_the_LE_1M_PHY:
					return 1u;
				case PhyEnum.Transmitter_set_to_use_the_LE_2M_PHY:
					return 2u;
				case PhyEnum.Transmitter_set_to_use_the_LE_Coded_PHY_with_S_equals_8_data_coding:
					return 3u;
				case PhyEnum.Transmitter_set_to_use_the_LE_Coded_PHY_with_S_equals_2_data_coding:
					return 4u;
				case PhyEnum.Transmitter_set_to_use_the_LE_1M_Coded_PHY_Simultaneous:
					return 5u;
				case PhyEnum.Transmitter_set_to_use_the_LE_HS_2M_data_coding:
					return 6u;
				case PhyEnum.Transmitter_set_to_use_the_LE_HS_3M_data_coding:
					return 7u;
				case PhyEnum.Transmitter_set_to_use_the_LE_HS_4M_data_coding:
					return 8u;
				case PhyEnum.Transmitter_set_to_use_the_LE_HS_5M_data_coding:
					return 9u;
				case PhyEnum.Transmitter_set_to_use_the_LE_HS_6M_data_coding:
					return 10u;
				default:
					if (enumItem == PhyEnum.Reserved_for_future_use)
					{
						return 11u;
					}
					return uint.MaxValue;
				}
			}

			public static string PhyToString(PhyEnum enumItem)
			{
				switch (enumItem)
				{
				case PhyEnum.Reserved_for_future_use:
					return "Reserved for future use";
				case PhyEnum.Transmitter_set_to_use_the_LE_1M_PHY:
					return "Use LE 1M PHY";
				case PhyEnum.Transmitter_set_to_use_the_LE_2M_PHY:
					return "Use LE 2M PHY";
				case PhyEnum.Transmitter_set_to_use_the_LE_Coded_PHY_with_S_equals_8_data_coding:
					return "Use  LE Coded PHY with S=8";
				case PhyEnum.Transmitter_set_to_use_the_LE_Coded_PHY_with_S_equals_2_data_coding:
					return "Use LE Coded PHY with S=2";
				case PhyEnum.Transmitter_set_to_use_the_LE_1M_Coded_PHY_Simultaneous:
					return "Use LE 1M and Coded PHY simultaneously";
				case PhyEnum.Transmitter_set_to_use_the_LE_HS_2M_data_coding:
					return "Use QHS 2M";
				case PhyEnum.Transmitter_set_to_use_the_LE_HS_3M_data_coding:
					return "Use QHS 3M";
				case PhyEnum.Transmitter_set_to_use_the_LE_HS_4M_data_coding:
					return "Use QHS 4M";
				case PhyEnum.Transmitter_set_to_use_the_LE_HS_5M_data_coding:
					return "Use QHS 5M";
				case PhyEnum.Transmitter_set_to_use_the_LE_HS_6M_data_coding:
					return "Use QHS 6M";
				default:
					if (enumItem == PhyEnum.Reserved_for_future_use)
					{
						return "Reserved for future use";
					}
					return "Invalid Phy Value";
				}
			}

			public static string PhyToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Reserved for future use");
				stringBuilder.AppendLine("0x1 = Use LE 1M PHY");
				stringBuilder.AppendLine("0x2 = Use LE 2M PHY");
				stringBuilder.AppendLine("0x3 = Use  LE Coded PHY with S=8");
				stringBuilder.AppendLine("0x4 = Use LE Coded PHY with S=2");
				stringBuilder.AppendLine("0x5 = Use LE 1M and Coded PHY simultaneously");
				stringBuilder.AppendLine("0x6 = Use QHS 2M");
				stringBuilder.AppendLine("0x7 = Use QHS 3M");
				stringBuilder.AppendLine("0x8 = Use QHS 4M");
				stringBuilder.AppendLine("0x9 = Use QHS 5M");
				stringBuilder.AppendLine("0xA = Use QHS 6M");
				stringBuilder.AppendLine("0xB-0xFF = Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		private uint _TxChannel;

		private uint _LengthOfTestData;

		private uint _PacketPayload;

		private uint _Phy;

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

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

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

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

		public PacketPayloadEnum GetPacketPayloadAsPacketPayloadEnum()
		{
			return PacketPayloadInfo.ValueToPacketPayload(_PacketPayload);
		}

		public void SetPacketPayloadAsPacketPayloadEnum(PacketPayloadEnum enumItem)
		{
			PacketPayload = PacketPayloadInfo.PacketPayloadToValue(enumItem);
		}

		public PhyEnum GetPhyAsPhyEnum()
		{
			return PhyInfo.ValueToPhy(_Phy);
		}

		public void SetPhyAsPhyEnum(PhyEnum enumItem)
		{
			Phy = PhyInfo.PhyToValue(enumItem);
		}
	}

	private const uint _Opcode = 8244u;

	private Type[] expectedResponseTypes = new Type[1] { typeof(CommandCompleteForLeEnhancedTransmitterTest) };

	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 LeEnhancedTransmitterTestInfo(this);
			}
			return info;
		}
	}

	public uint Opcode => 8244u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.TxChannel = 1u;
		UserFields.Phy = 1u;
	}

	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;
		}
		UserFields.TxChannel = bitBuilder.RemoveBitsAsUint(8);
		UserFields.LengthOfTestData = bitBuilder.RemoveBitsAsUint(8);
		UserFields.PacketPayload = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Phy = 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.TxChannel, 8);
		bitBuilder.AddBitsAsUint(UserFields.LengthOfTestData, 8);
		bitBuilder.AddBitsAsUint(UserFields.PacketPayload, 8);
		bitBuilder.AddBitsAsUint(UserFields.Phy, 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.TxChannel > 39)
		{
			list.Add("Maximum allowed value for TX_Channel is 0x27");
		}
		return list;
	}
}
