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

namespace QCRPG.TSL.BT.Packets;

public class LeReceiverTestV3 : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum PhyEnum
		{
			LE_1M_PHY,
			LE_2M_PHY,
			LE_Coded_PHY_S8,
			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 && 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.Reserved => 4u, 
					_ => uint.MaxValue, 
				};
			}

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

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

		public enum ModulationIndexEnum
		{
			Standard_Modulation_Index,
			Stable_Modulation_Index,
			Reserved,
			InvalidModulationIndexValue
		}

		public static class ModulationIndexInfo
		{
			public static ModulationIndexEnum ValueToModulationIndex(uint value)
			{
				if (value == 0)
				{
					return ModulationIndexEnum.Standard_Modulation_Index;
				}
				if (value == 1)
				{
					return ModulationIndexEnum.Stable_Modulation_Index;
				}
				if (value >= 3 && value <= 255)
				{
					return ModulationIndexEnum.Reserved;
				}
				return ModulationIndexEnum.InvalidModulationIndexValue;
			}

			public static uint ModulationIndexToValue(ModulationIndexEnum enumItem)
			{
				return enumItem switch
				{
					ModulationIndexEnum.Standard_Modulation_Index => 0u, 
					ModulationIndexEnum.Stable_Modulation_Index => 1u, 
					ModulationIndexEnum.Reserved => 3u, 
					_ => uint.MaxValue, 
				};
			}

			public static string ModulationIndexToString(ModulationIndexEnum enumItem)
			{
				return enumItem switch
				{
					ModulationIndexEnum.Standard_Modulation_Index => "Assume transmitter will have a standard modulation index", 
					ModulationIndexEnum.Stable_Modulation_Index => "Assume transmitter will have a stable modulation index", 
					ModulationIndexEnum.Reserved => "Reserved for future use", 
					_ => "Invalid ModulationIndex Value", 
				};
			}

			public static string ModulationIndexToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Assume transmitter will have a standard modulation index");
				stringBuilder.AppendLine("0x1 = Assume transmitter will have a stable modulation index");
				stringBuilder.AppendLine("0x3-0xFF = Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		public enum ExpectedCteLengthEnum
		{
			No_CTE,
			CTE_8us,
			Reserved_For_Future_Use,
			InvalidExpectedCteLengthValue
		}

		public static class ExpectedCteLengthInfo
		{
			public static ExpectedCteLengthEnum ValueToExpectedCteLength(uint value)
			{
				switch (value)
				{
				case 0u:
					return ExpectedCteLengthEnum.No_CTE;
				case 1u:
					return ExpectedCteLengthEnum.CTE_8us;
				default:
					if (value >= 2 && value <= 255)
					{
						return ExpectedCteLengthEnum.Reserved_For_Future_Use;
					}
					return ExpectedCteLengthEnum.InvalidExpectedCteLengthValue;
				}
			}

			public static uint ExpectedCteLengthToValue(ExpectedCteLengthEnum enumItem)
			{
				return enumItem switch
				{
					ExpectedCteLengthEnum.No_CTE => 0u, 
					ExpectedCteLengthEnum.CTE_8us => 1u, 
					ExpectedCteLengthEnum.Reserved_For_Future_Use => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string ExpectedCteLengthToString(ExpectedCteLengthEnum enumItem)
			{
				return enumItem switch
				{
					ExpectedCteLengthEnum.No_CTE => "No Constant Tone Extension expected", 
					ExpectedCteLengthEnum.CTE_8us => "Expected length of the Constant Tone Extension in 8us units", 
					ExpectedCteLengthEnum.Reserved_For_Future_Use => "Reserved for Future Use", 
					_ => "Invalid ExpectedCteLength Value", 
				};
			}

			public static string ExpectedCteLengthToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = No Constant Tone Extension expected");
				stringBuilder.AppendLine("0x1 = Expected length of the Constant Tone Extension in 8us units");
				stringBuilder.AppendLine("0x2-0xFF = Reserved for Future Use");
				return stringBuilder.ToString();
			}
		}

		public enum ExpectedCteTypeEnum
		{
			AoA_CTE,
			AoD_CTE_1us,
			AoD_CTE_2us,
			Reserved_For_Future_Use,
			InvalidExpectedCteTypeValue
		}

		public static class ExpectedCteTypeInfo
		{
			public static ExpectedCteTypeEnum ValueToExpectedCteType(uint value)
			{
				switch (value)
				{
				case 0u:
					return ExpectedCteTypeEnum.AoA_CTE;
				case 1u:
					return ExpectedCteTypeEnum.AoD_CTE_1us;
				case 2u:
					return ExpectedCteTypeEnum.AoD_CTE_2us;
				default:
					if (value >= 3 && value <= 255)
					{
						return ExpectedCteTypeEnum.Reserved_For_Future_Use;
					}
					return ExpectedCteTypeEnum.InvalidExpectedCteTypeValue;
				}
			}

			public static uint ExpectedCteTypeToValue(ExpectedCteTypeEnum enumItem)
			{
				return enumItem switch
				{
					ExpectedCteTypeEnum.AoA_CTE => 0u, 
					ExpectedCteTypeEnum.AoD_CTE_1us => 1u, 
					ExpectedCteTypeEnum.AoD_CTE_2us => 2u, 
					ExpectedCteTypeEnum.Reserved_For_Future_Use => 3u, 
					_ => uint.MaxValue, 
				};
			}

			public static string ExpectedCteTypeToString(ExpectedCteTypeEnum enumItem)
			{
				return enumItem switch
				{
					ExpectedCteTypeEnum.AoA_CTE => "Expect AoA Contant Tone Extension", 
					ExpectedCteTypeEnum.AoD_CTE_1us => "Expect AoD Constant Tone Extension with 1us slots", 
					ExpectedCteTypeEnum.AoD_CTE_2us => "Expect AoD Constant Tone Extension with 2us slots", 
					ExpectedCteTypeEnum.Reserved_For_Future_Use => "Reserved for Future Use", 
					_ => "Invalid ExpectedCteType Value", 
				};
			}

			public static string ExpectedCteTypeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Expect AoA Contant Tone Extension");
				stringBuilder.AppendLine("0x1 = Expect AoD Constant Tone Extension with 1us slots");
				stringBuilder.AppendLine("0x2 = Expect AoD Constant Tone Extension with 2us slots");
				stringBuilder.AppendLine("0x3-0xFF = Reserved for Future Use");
				return stringBuilder.ToString();
			}
		}

		public enum SlotDurationsEnum
		{
			CTE_1us,
			CTE_2us,
			Reserved_For_Future_Use,
			InvalidSlotDurationsValue
		}

		public static class SlotDurationsInfo
		{
			public static SlotDurationsEnum ValueToSlotDurations(uint value)
			{
				if (value == 1)
				{
					return SlotDurationsEnum.CTE_1us;
				}
				if (value == 2)
				{
					return SlotDurationsEnum.CTE_2us;
				}
				if (value >= 3 && value <= 255)
				{
					return SlotDurationsEnum.Reserved_For_Future_Use;
				}
				return SlotDurationsEnum.InvalidSlotDurationsValue;
			}

			public static uint SlotDurationsToValue(SlotDurationsEnum enumItem)
			{
				return enumItem switch
				{
					SlotDurationsEnum.CTE_1us => 1u, 
					SlotDurationsEnum.CTE_2us => 2u, 
					SlotDurationsEnum.Reserved_For_Future_Use => 3u, 
					_ => uint.MaxValue, 
				};
			}

			public static string SlotDurationsToString(SlotDurationsEnum enumItem)
			{
				return enumItem switch
				{
					SlotDurationsEnum.CTE_1us => "Switching and sampling slots are 1 us each", 
					SlotDurationsEnum.CTE_2us => "Switching and sampling slots are 2 us each", 
					SlotDurationsEnum.Reserved_For_Future_Use => "Reserved for Future Use", 
					_ => "Invalid SlotDurations Value", 
				};
			}

			public static string SlotDurationsToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x1 = Switching and sampling slots are 1 us each");
				stringBuilder.AppendLine("0x2 = Switching and sampling slots are 2 us each");
				stringBuilder.AppendLine("0x3-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 _RxChannel;

		private uint _Phy;

		private uint _ModulationIndex;

		private uint _ExpectedCteLength;

		private uint _ExpectedCteType;

		private uint _SlotDurations;

		private uint _LengthOfSwitchingPattern;

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

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

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

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

		public uint SlotDurations
		{
			get
			{
				return _SlotDurations;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for SlotDurations is 0xFF");
				}
				_SlotDurations = 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 PhyEnum GetPhyAsPhyEnum()
		{
			return PhyInfo.ValueToPhy(_Phy);
		}

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

		public ModulationIndexEnum GetModulationIndexAsModulationIndexEnum()
		{
			return ModulationIndexInfo.ValueToModulationIndex(_ModulationIndex);
		}

		public void SetModulationIndexAsModulationIndexEnum(ModulationIndexEnum enumItem)
		{
			ModulationIndex = ModulationIndexInfo.ModulationIndexToValue(enumItem);
		}

		public ExpectedCteLengthEnum GetExpectedCteLengthAsExpectedCteLengthEnum()
		{
			return ExpectedCteLengthInfo.ValueToExpectedCteLength(_ExpectedCteLength);
		}

		public void SetExpectedCteLengthAsExpectedCteLengthEnum(ExpectedCteLengthEnum enumItem)
		{
			ExpectedCteLength = ExpectedCteLengthInfo.ExpectedCteLengthToValue(enumItem);
		}

		public ExpectedCteTypeEnum GetExpectedCteTypeAsExpectedCteTypeEnum()
		{
			return ExpectedCteTypeInfo.ValueToExpectedCteType(_ExpectedCteType);
		}

		public void SetExpectedCteTypeAsExpectedCteTypeEnum(ExpectedCteTypeEnum enumItem)
		{
			ExpectedCteType = ExpectedCteTypeInfo.ExpectedCteTypeToValue(enumItem);
		}

		public SlotDurationsEnum GetSlotDurationsAsSlotDurationsEnum()
		{
			return SlotDurationsInfo.ValueToSlotDurations(_SlotDurations);
		}

		public void SetSlotDurationsAsSlotDurationsEnum(SlotDurationsEnum enumItem)
		{
			SlotDurations = SlotDurationsInfo.SlotDurationsToValue(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 = 8271u;

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

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

	public uint Opcode => 8271u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.Phy = 1u;
		UserFields.ExpectedCteLength = 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;
		}
		UserFields.RxChannel = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Phy = bitBuilder.RemoveBitsAsUint(8);
		UserFields.ModulationIndex = bitBuilder.RemoveBitsAsUint(8);
		UserFields.ExpectedCteLength = bitBuilder.RemoveBitsAsUint(8);
		UserFields.ExpectedCteType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.SlotDurations = 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.RxChannel, 8);
		bitBuilder.AddBitsAsUint(UserFields.Phy, 8);
		bitBuilder.AddBitsAsUint(UserFields.ModulationIndex, 8);
		bitBuilder.AddBitsAsUint(UserFields.ExpectedCteLength, 8);
		bitBuilder.AddBitsAsUint(UserFields.ExpectedCteType, 8);
		bitBuilder.AddBitsAsUint(UserFields.SlotDurations, 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.RxChannel > 39)
		{
			list.Add("Maximum allowed value for RX_Channel is 0x27");
		}
		return list;
	}
}
