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

namespace QCRPG.TSL.BT.Packets;

public class LeTransmitterTestV3 : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum PacketPayloadEnum
		{
			PSEUDO_RANDOM_BIT_SEQUENCE_9,
			ALTERNATING_BITS_11110000,
			ALTERNATING_BITS_10101010,
			PSEUDO_RANDOM_BIT_SEQUENCE_15,
			ALL_ONE_BITS,
			ALL_ZERO_BITS,
			ALTERNATING_BITS_00001111,
			ALTERNATING_BITS_0101,
			RESERVED,
			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.ALTERNATING_BITS_11110000;
				case 2u:
					return PacketPayloadEnum.ALTERNATING_BITS_10101010;
				case 3u:
					return PacketPayloadEnum.PSEUDO_RANDOM_BIT_SEQUENCE_15;
				case 4u:
					return PacketPayloadEnum.ALL_ONE_BITS;
				case 5u:
					return PacketPayloadEnum.ALL_ZERO_BITS;
				case 6u:
					return PacketPayloadEnum.ALTERNATING_BITS_00001111;
				case 7u:
					return PacketPayloadEnum.ALTERNATING_BITS_0101;
				default:
					if (value >= 8 && value <= 255)
					{
						return PacketPayloadEnum.RESERVED;
					}
					return PacketPayloadEnum.InvalidPacketPayloadValue;
				}
			}

			public static uint PacketPayloadToValue(PacketPayloadEnum enumItem)
			{
				return enumItem switch
				{
					PacketPayloadEnum.PSEUDO_RANDOM_BIT_SEQUENCE_9 => 0u, 
					PacketPayloadEnum.ALTERNATING_BITS_11110000 => 1u, 
					PacketPayloadEnum.ALTERNATING_BITS_10101010 => 2u, 
					PacketPayloadEnum.PSEUDO_RANDOM_BIT_SEQUENCE_15 => 3u, 
					PacketPayloadEnum.ALL_ONE_BITS => 4u, 
					PacketPayloadEnum.ALL_ZERO_BITS => 5u, 
					PacketPayloadEnum.ALTERNATING_BITS_00001111 => 6u, 
					PacketPayloadEnum.ALTERNATING_BITS_0101 => 7u, 
					PacketPayloadEnum.RESERVED => 8u, 
					_ => uint.MaxValue, 
				};
			}

			public static string PacketPayloadToString(PacketPayloadEnum enumItem)
			{
				return enumItem switch
				{
					PacketPayloadEnum.PSEUDO_RANDOM_BIT_SEQUENCE_9 => "PRBS9 sequence '11111111100000111101...'", 
					PacketPayloadEnum.ALTERNATING_BITS_11110000 => "Repeated '11110000'", 
					PacketPayloadEnum.ALTERNATING_BITS_10101010 => "Repeated '10101010'", 
					PacketPayloadEnum.PSEUDO_RANDOM_BIT_SEQUENCE_15 => "PRBS15 sequence", 
					PacketPayloadEnum.ALL_ONE_BITS => "Repeated '11111111'", 
					PacketPayloadEnum.ALL_ZERO_BITS => "Repeated '00000000'", 
					PacketPayloadEnum.ALTERNATING_BITS_00001111 => "Repeated '00001111'", 
					PacketPayloadEnum.ALTERNATING_BITS_0101 => "Repeated '01010101'", 
					PacketPayloadEnum.RESERVED => "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
		{
			LE_1M_PHY,
			LE_2M_PHY,
			LE_Coded_PHY_S8,
			LE_Coded_PHY_S2,
			Reserved,
			InvalidPhyValue
		}

		public static class PhyInfo
		{
			public static PhyEnum ValueToPhy(uint value)
			{
				if (value == 1)
				{
					return PhyEnum.LE_1M_PHY;
				}
				if (value == 2)
				{
					return PhyEnum.LE_2M_PHY;
				}
				if (value == 3)
				{
					return PhyEnum.LE_Coded_PHY_S8;
				}
				if (value == 4)
				{
					return PhyEnum.LE_Coded_PHY_S2;
				}
				if (value >= 5 && value <= 255)
				{
					return PhyEnum.Reserved;
				}
				return PhyEnum.InvalidPhyValue;
			}

			public static uint PhyToValue(PhyEnum enumItem)
			{
				return enumItem switch
				{
					PhyEnum.LE_1M_PHY => 1u, 
					PhyEnum.LE_2M_PHY => 2u, 
					PhyEnum.LE_Coded_PHY_S8 => 3u, 
					PhyEnum.LE_Coded_PHY_S2 => 4u, 
					PhyEnum.Reserved => 5u, 
					_ => uint.MaxValue, 
				};
			}

			public static string PhyToString(PhyEnum enumItem)
			{
				return enumItem switch
				{
					PhyEnum.LE_1M_PHY => "Transmitter set to use LE 1M PHY", 
					PhyEnum.LE_2M_PHY => "Transmitter set to use LE 2M PHY", 
					PhyEnum.LE_Coded_PHY_S8 => "Transmitter set to use LE Coded PHY with S=8 data coding", 
					PhyEnum.LE_Coded_PHY_S2 => "Transmitter set to use LE Coded PHY with S=2 data coding", 
					PhyEnum.Reserved => "Reserved for future use", 
					_ => "Invalid Phy Value", 
				};
			}

			public static string PhyToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x1 = Transmitter set to use LE 1M PHY");
				stringBuilder.AppendLine("0x2 = Transmitter set to use LE 2M PHY");
				stringBuilder.AppendLine("0x3 = Transmitter set to use LE Coded PHY with S=8 data coding");
				stringBuilder.AppendLine("0x4 = Transmitter set to use LE Coded PHY with S=2 data coding");
				stringBuilder.AppendLine("0x5-0xFF = Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		public enum CteLengthEnum
		{
			DONOT_TRANSMIT_CTE,
			RESERVED,
			CTE_Length_In_8_us_Units,
			Reserved_For_Future_Use,
			InvalidCteLengthValue
		}

		public static class CteLengthInfo
		{
			public static CteLengthEnum ValueToCteLength(uint value)
			{
				switch (value)
				{
				case 0u:
					return CteLengthEnum.DONOT_TRANSMIT_CTE;
				case 1u:
					return CteLengthEnum.RESERVED;
				default:
					if (value >= 2 && value <= 20)
					{
						return CteLengthEnum.CTE_Length_In_8_us_Units;
					}
					if (value >= 21 && value <= 255)
					{
						return CteLengthEnum.Reserved_For_Future_Use;
					}
					return CteLengthEnum.InvalidCteLengthValue;
				}
			}

			public static uint CteLengthToValue(CteLengthEnum enumItem)
			{
				return enumItem switch
				{
					CteLengthEnum.DONOT_TRANSMIT_CTE => 0u, 
					CteLengthEnum.RESERVED => 1u, 
					CteLengthEnum.CTE_Length_In_8_us_Units => 2u, 
					CteLengthEnum.Reserved_For_Future_Use => 21u, 
					_ => uint.MaxValue, 
				};
			}

			public static string CteLengthToString(CteLengthEnum enumItem)
			{
				return enumItem switch
				{
					CteLengthEnum.DONOT_TRANSMIT_CTE => "DONOT_TRANSMIT_CTE", 
					CteLengthEnum.RESERVED => "RESERVED", 
					CteLengthEnum.CTE_Length_In_8_us_Units => "Length of Constant Tone Extension in 8us units", 
					CteLengthEnum.Reserved_For_Future_Use => "Reserved for Future Use", 
					_ => "Invalid CteLength Value", 
				};
			}

			public static string CteLengthToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = DONOT_TRANSMIT_CTE");
				stringBuilder.AppendLine("0x1 = RESERVED");
				stringBuilder.AppendLine("0x2-0x14 = Length of Constant Tone Extension in 8us units");
				stringBuilder.AppendLine("0x15-0xFF = Reserved for Future Use");
				return stringBuilder.ToString();
			}
		}

		public enum LengthOfSwitchingPatternEnum
		{
			Reserved_For_Future_Use,
			Number_Of_Antennae,
			InvalidLengthOfSwitchingPatternValue
		}

		public static class LengthOfSwitchingPatternInfo
		{
			public static LengthOfSwitchingPatternEnum ValueToLengthOfSwitchingPattern(uint value)
			{
				if (value >= 0 && value <= 1)
				{
					return LengthOfSwitchingPatternEnum.Reserved_For_Future_Use;
				}
				if (value >= 2 && value <= 75)
				{
					return LengthOfSwitchingPatternEnum.Number_Of_Antennae;
				}
				if (value >= 76 && value <= 255)
				{
					return LengthOfSwitchingPatternEnum.Reserved_For_Future_Use;
				}
				return LengthOfSwitchingPatternEnum.InvalidLengthOfSwitchingPatternValue;
			}

			public static uint LengthOfSwitchingPatternToValue(LengthOfSwitchingPatternEnum enumItem)
			{
				switch (enumItem)
				{
				case LengthOfSwitchingPatternEnum.Reserved_For_Future_Use:
					return 0u;
				case LengthOfSwitchingPatternEnum.Number_Of_Antennae:
					return 2u;
				default:
					if (enumItem == LengthOfSwitchingPatternEnum.Reserved_For_Future_Use)
					{
						return 76u;
					}
					return uint.MaxValue;
				}
			}

			public static string LengthOfSwitchingPatternToString(LengthOfSwitchingPatternEnum enumItem)
			{
				switch (enumItem)
				{
				case LengthOfSwitchingPatternEnum.Reserved_For_Future_Use:
					return "Reserved_For_Future_Use";
				case LengthOfSwitchingPatternEnum.Number_Of_Antennae:
					return "The number of Antenna IDs in the pattern";
				default:
					if (enumItem == LengthOfSwitchingPatternEnum.Reserved_For_Future_Use)
					{
						return "Reserved for Future Use";
					}
					return "Invalid LengthOfSwitchingPattern Value";
				}
			}

			public static string LengthOfSwitchingPatternToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0-0x1 = Reserved_For_Future_Use");
				stringBuilder.AppendLine("0x2-0x4B = The number of Antenna IDs in the pattern");
				stringBuilder.AppendLine("0x4C-0xFF = Reserved for Future Use");
				return stringBuilder.ToString();
			}
		}

		private uint _TxChannel;

		private uint _LengthOfTestData;

		private uint _PacketPayload;

		private uint _Phy;

		private uint _CteLength;

		private uint _SupplementalSlotType;

		private uint _LengthOfSwitchingPattern;

		private List<AntennaIdsSetSubField> _AntennaIdsSetList = new List<AntennaIdsSetSubField>();

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

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

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

		public List<AntennaIdsSetSubField> AntennaIdsSetList => _AntennaIdsSetList;

		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);
		}

		public CteLengthEnum GetCteLengthAsCteLengthEnum()
		{
			return CteLengthInfo.ValueToCteLength(_CteLength);
		}

		public void SetCteLengthAsCteLengthEnum(CteLengthEnum enumItem)
		{
			CteLength = CteLengthInfo.CteLengthToValue(enumItem);
		}

		public SlotTypeEnum GetSupplementalSlotTypeAsSlotTypeEnum()
		{
			return SlotTypeInfo.ValueToSlotType(_SupplementalSlotType);
		}

		public void SetSupplementalSlotTypeAsSlotTypeEnum(SlotTypeEnum enumItem)
		{
			SupplementalSlotType = SlotTypeInfo.SlotTypeToValue(enumItem);
		}

		public LengthOfSwitchingPatternEnum GetLengthOfSwitchingPatternAsLengthOfSwitchingPatternEnum()
		{
			return LengthOfSwitchingPatternInfo.ValueToLengthOfSwitchingPattern(_LengthOfSwitchingPattern);
		}

		public void SetLengthOfSwitchingPatternAsLengthOfSwitchingPatternEnum(LengthOfSwitchingPatternEnum enumItem)
		{
			LengthOfSwitchingPattern = LengthOfSwitchingPatternInfo.LengthOfSwitchingPatternToValue(enumItem);
		}
	}

	public class AntennaIdsSetSubField
	{
		private uint _AntennaIds;

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

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			AntennaIds = bitBuilder.RemoveBitsAsUint(8);
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUint(AntennaIds, 8);
			return bitBuilder.ToString();
		}
	}

	private const uint _Opcode = 8272u;

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

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

	public uint Opcode => 8272u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		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);
		UserFields.CteLength = bitBuilder.RemoveBitsAsUint(8);
		UserFields.SupplementalSlotType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.LengthOfSwitchingPattern = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.AntennaIdsSetList.Count; i++)
		{
			UserFields.AntennaIdsSetList[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.TxChannel, 8);
		bitBuilder.AddBitsAsUint(UserFields.LengthOfTestData, 8);
		bitBuilder.AddBitsAsUint(UserFields.PacketPayload, 8);
		bitBuilder.AddBitsAsUint(UserFields.Phy, 8);
		bitBuilder.AddBitsAsUint(UserFields.CteLength, 8);
		bitBuilder.AddBitsAsUint(UserFields.SupplementalSlotType, 8);
		bitBuilder.AddBitsAsUint(UserFields.LengthOfSwitchingPattern, 8);
		for (int i = 0; i < UserFields.AntennaIdsSetList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.AntennaIdsSetList[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.TxChannel > 39)
		{
			list.Add("Maximum allowed value for TX_Channel is 0x27");
		}
		return list;
	}
}
