using System;
using System.Text;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class CommandCompleteForLeReadAntennaInformation : PacketBase
{
	public class UserFieldsWrapper
	{
		public class SupportedSwitchingSamplingRatesEnumCollection
		{
			private EnableBitEnum _Switching1UsSupportedForAodTransmission;

			private EnableBitEnum _Sampling1UsSupportedForAodTransmission;

			private EnableBitEnum _SwitchingAndSampling1UsSupportedForAodTransmission;

			private EnableBitEnum _ReservedForFutureUse;

			public EnableBitEnum Switching1UsSupportedForAodTransmission
			{
				get
				{
					return _Switching1UsSupportedForAodTransmission;
				}
				set
				{
					_Switching1UsSupportedForAodTransmission = value;
				}
			}

			public EnableBitEnum Sampling1UsSupportedForAodTransmission
			{
				get
				{
					return _Sampling1UsSupportedForAodTransmission;
				}
				set
				{
					_Sampling1UsSupportedForAodTransmission = value;
				}
			}

			public EnableBitEnum SwitchingAndSampling1UsSupportedForAodTransmission
			{
				get
				{
					return _SwitchingAndSampling1UsSupportedForAodTransmission;
				}
				set
				{
					_SwitchingAndSampling1UsSupportedForAodTransmission = value;
				}
			}

			public EnableBitEnum ReservedForFutureUse
			{
				get
				{
					return _ReservedForFutureUse;
				}
				set
				{
					_ReservedForFutureUse = value;
				}
			}
		}

		public static class SupportedSwitchingSamplingRatesInfo
		{
			public static SupportedSwitchingSamplingRatesEnumCollection ValueToSupportedSwitchingSamplingRates(uint value)
			{
				SupportedSwitchingSamplingRatesEnumCollection supportedSwitchingSamplingRatesEnumCollection = new SupportedSwitchingSamplingRatesEnumCollection();
				supportedSwitchingSamplingRatesEnumCollection.Switching1UsSupportedForAodTransmission = EnableBitInfo.ValueToEnableBit(value & 1u);
				supportedSwitchingSamplingRatesEnumCollection.Sampling1UsSupportedForAodTransmission = EnableBitInfo.ValueToEnableBit((value & 2) >> 1);
				supportedSwitchingSamplingRatesEnumCollection.SwitchingAndSampling1UsSupportedForAodTransmission = EnableBitInfo.ValueToEnableBit((value & 4) >> 2);
				supportedSwitchingSamplingRatesEnumCollection.ReservedForFutureUse = EnableBitInfo.ValueToEnableBit((value & 0xF8) >> 3);
				return supportedSwitchingSamplingRatesEnumCollection;
			}

			public static uint SupportedSwitchingSamplingRatesToValue(SupportedSwitchingSamplingRatesEnumCollection enumCollection)
			{
				uint num = 0u;
				num |= EnableBitInfo.EnableBitToValue(enumCollection.Switching1UsSupportedForAodTransmission);
				num |= EnableBitInfo.EnableBitToValue(enumCollection.Sampling1UsSupportedForAodTransmission) << 1;
				num |= EnableBitInfo.EnableBitToValue(enumCollection.SwitchingAndSampling1UsSupportedForAodTransmission) << 2;
				return num | (EnableBitInfo.EnableBitToValue(enumCollection.ReservedForFutureUse) << 3);
			}

			public static string SupportedSwitchingSamplingRatesToString(SupportedSwitchingSamplingRatesEnumCollection enumCollection)
			{
				string text = "";
				text = text + "Switching 1 us supported for AoD transmission: " + EnableBitInfo.EnableBitToString(enumCollection.Switching1UsSupportedForAodTransmission) + "\r\n";
				text = text + "Sampling 1 us supported for AoD transmission: " + EnableBitInfo.EnableBitToString(enumCollection.Sampling1UsSupportedForAodTransmission) + "\r\n";
				text = text + "Switching and sampling 1 us supported for AoD transmission: " + EnableBitInfo.EnableBitToString(enumCollection.SwitchingAndSampling1UsSupportedForAodTransmission) + "\r\n";
				text = text + "RESERVED_FOR_FUTURE_USE: " + EnableBitInfo.EnableBitToString(enumCollection.ReservedForFutureUse) + "\r\n";
				return text.TrimEnd('\r', '\n');
			}

			public static string SupportedSwitchingSamplingRatesToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("Bit 0: Switching 1 us supported for AoD transmission");
				stringBuilder.AppendLine("Bit 1: Sampling 1 us supported for AoD transmission");
				stringBuilder.AppendLine("Bit 2: Switching and sampling 1 us supported for AoD transmission");
				stringBuilder.AppendLine("Bits 7-3: RESERVED_FOR_FUTURE_USE");
				return stringBuilder.ToString();
			}
		}

		public enum NumberOfAntennaeEnum
		{
			NOT_SPECIFIED,
			Number_Of_Antennae,
			Reserved_For_Future_Use,
			InvalidNumberOfAntennaeValue
		}

		public static class NumberOfAntennaeInfo
		{
			public static NumberOfAntennaeEnum ValueToNumberOfAntennae(uint value)
			{
				if (value == 0)
				{
					return NumberOfAntennaeEnum.NOT_SPECIFIED;
				}
				if (value >= 1 && value <= 75)
				{
					return NumberOfAntennaeEnum.Number_Of_Antennae;
				}
				if (value >= 76 && value <= 255)
				{
					return NumberOfAntennaeEnum.Reserved_For_Future_Use;
				}
				return NumberOfAntennaeEnum.InvalidNumberOfAntennaeValue;
			}

			public static uint NumberOfAntennaeToValue(NumberOfAntennaeEnum enumItem)
			{
				return enumItem switch
				{
					NumberOfAntennaeEnum.NOT_SPECIFIED => 0u, 
					NumberOfAntennaeEnum.Number_Of_Antennae => 1u, 
					NumberOfAntennaeEnum.Reserved_For_Future_Use => 76u, 
					_ => uint.MaxValue, 
				};
			}

			public static string NumberOfAntennaeToString(NumberOfAntennaeEnum enumItem)
			{
				return enumItem switch
				{
					NumberOfAntennaeEnum.NOT_SPECIFIED => "NOT_SPECIFIED", 
					NumberOfAntennaeEnum.Number_Of_Antennae => "Number_Of_Antennae", 
					NumberOfAntennaeEnum.Reserved_For_Future_Use => "Reserved for Future Use", 
					_ => "Invalid NumberOfAntennae Value", 
				};
			}

			public static string NumberOfAntennaeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = NOT_SPECIFIED");
				stringBuilder.AppendLine("0x1-0x4B = Number_Of_Antennae");
				stringBuilder.AppendLine("0x4C-0xFF = Reserved for Future Use");
				return stringBuilder.ToString();
			}
		}

		public enum MaxLengthOfSwitchingPatternEnum
		{
			Max_Antenna_Switching_Pattern,
			Reserved_For_Future_Use,
			InvalidMaxLengthOfSwitchingPatternValue
		}

		public static class MaxLengthOfSwitchingPatternInfo
		{
			public static MaxLengthOfSwitchingPatternEnum ValueToMaxLengthOfSwitchingPattern(uint value)
			{
				if (value >= 2 && value <= 75)
				{
					return MaxLengthOfSwitchingPatternEnum.Max_Antenna_Switching_Pattern;
				}
				if (value >= 76 && value <= 255)
				{
					return MaxLengthOfSwitchingPatternEnum.Reserved_For_Future_Use;
				}
				return MaxLengthOfSwitchingPatternEnum.InvalidMaxLengthOfSwitchingPatternValue;
			}

			public static uint MaxLengthOfSwitchingPatternToValue(MaxLengthOfSwitchingPatternEnum enumItem)
			{
				return enumItem switch
				{
					MaxLengthOfSwitchingPatternEnum.Max_Antenna_Switching_Pattern => 2u, 
					MaxLengthOfSwitchingPatternEnum.Reserved_For_Future_Use => 76u, 
					_ => uint.MaxValue, 
				};
			}

			public static string MaxLengthOfSwitchingPatternToString(MaxLengthOfSwitchingPatternEnum enumItem)
			{
				return enumItem switch
				{
					MaxLengthOfSwitchingPatternEnum.Max_Antenna_Switching_Pattern => "Maximum length supported by the controller", 
					MaxLengthOfSwitchingPatternEnum.Reserved_For_Future_Use => "Reserved for Future Use", 
					_ => "Invalid MaxLengthOfSwitchingPattern Value", 
				};
			}

			public static string MaxLengthOfSwitchingPatternToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x2-0x4B = Maximum length supported by the controller");
				stringBuilder.AppendLine("0x4C-0xFF = Reserved for Future Use");
				return stringBuilder.ToString();
			}
		}

		public enum MaxCteLengthEnum
		{
			Maximum_CTE_Supported_in_8us_units,
			Reserved_For_Future_Use,
			InvalidMaxCteLengthValue
		}

		public static class MaxCteLengthInfo
		{
			public static MaxCteLengthEnum ValueToMaxCteLength(uint value)
			{
				if (value >= 2 && value <= 20)
				{
					return MaxCteLengthEnum.Maximum_CTE_Supported_in_8us_units;
				}
				if (value >= 21 && value <= 255)
				{
					return MaxCteLengthEnum.Reserved_For_Future_Use;
				}
				return MaxCteLengthEnum.InvalidMaxCteLengthValue;
			}

			public static uint MaxCteLengthToValue(MaxCteLengthEnum enumItem)
			{
				return enumItem switch
				{
					MaxCteLengthEnum.Maximum_CTE_Supported_in_8us_units => 2u, 
					MaxCteLengthEnum.Reserved_For_Future_Use => 21u, 
					_ => uint.MaxValue, 
				};
			}

			public static string MaxCteLengthToString(MaxCteLengthEnum enumItem)
			{
				return enumItem switch
				{
					MaxCteLengthEnum.Maximum_CTE_Supported_in_8us_units => "Maximum length of Supplemental supported in 8us units", 
					MaxCteLengthEnum.Reserved_For_Future_Use => "Reserved for Future Use", 
					_ => "Invalid MaxCteLength Value", 
				};
			}

			public static string MaxCteLengthToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x2-0x14 = Maximum length of Supplemental supported in 8us units");
				stringBuilder.AppendLine("0x15-0xFF = Reserved for Future Use");
				return stringBuilder.ToString();
			}
		}

		private const uint _CommandOpcode = 8280u;

		private uint _NumHciCommandPackets;

		private uint _Status;

		private uint _SupportedSwitchingSamplingRates;

		private uint _NumberOfAntennae;

		private uint _MaxLengthOfSwitchingPattern;

		private uint _MaxCteLength;

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

		public uint CommandOpcode => 8280u;

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

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

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

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

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

		public CommandOpcodeEnum GetCommandOpcodeAsCommandOpcodeEnum()
		{
			return CommandOpcodeInfo.ValueToCommandOpcode(8280u);
		}

		public ErrorCodeEnum GetStatusAsErrorCodeEnum()
		{
			return ErrorCodeInfo.ValueToErrorCode(_Status);
		}

		public void SetStatusAsErrorCodeEnum(ErrorCodeEnum enumItem)
		{
			Status = ErrorCodeInfo.ErrorCodeToValue(enumItem);
		}

		public SupportedSwitchingSamplingRatesEnumCollection GetSupportedSwitchingSamplingRatesAsSupportedSwitchingSamplingRatesEnumCollection()
		{
			return SupportedSwitchingSamplingRatesInfo.ValueToSupportedSwitchingSamplingRates(_SupportedSwitchingSamplingRates);
		}

		public void SetSupportedSwitchingSamplingRatesAsSupportedSwitchingSamplingRatesEnumCollection(SupportedSwitchingSamplingRatesEnumCollection enumCollection)
		{
			SupportedSwitchingSamplingRates = SupportedSwitchingSamplingRatesInfo.SupportedSwitchingSamplingRatesToValue(enumCollection);
		}

		public NumberOfAntennaeEnum GetNumberOfAntennaeAsNumberOfAntennaeEnum()
		{
			return NumberOfAntennaeInfo.ValueToNumberOfAntennae(_NumberOfAntennae);
		}

		public void SetNumberOfAntennaeAsNumberOfAntennaeEnum(NumberOfAntennaeEnum enumItem)
		{
			NumberOfAntennae = NumberOfAntennaeInfo.NumberOfAntennaeToValue(enumItem);
		}

		public MaxLengthOfSwitchingPatternEnum GetMaxLengthOfSwitchingPatternAsMaxLengthOfSwitchingPatternEnum()
		{
			return MaxLengthOfSwitchingPatternInfo.ValueToMaxLengthOfSwitchingPattern(_MaxLengthOfSwitchingPattern);
		}

		public void SetMaxLengthOfSwitchingPatternAsMaxLengthOfSwitchingPatternEnum(MaxLengthOfSwitchingPatternEnum enumItem)
		{
			MaxLengthOfSwitchingPattern = MaxLengthOfSwitchingPatternInfo.MaxLengthOfSwitchingPatternToValue(enumItem);
		}

		public MaxCteLengthEnum GetMaxCteLengthAsMaxCteLengthEnum()
		{
			return MaxCteLengthInfo.ValueToMaxCteLength(_MaxCteLength);
		}

		public void SetMaxCteLengthAsMaxCteLengthEnum(MaxCteLengthEnum enumItem)
		{
			MaxCteLength = MaxCteLengthInfo.MaxCteLengthToValue(enumItem);
		}
	}

	private const uint _EventCode = 14u;

	private Type[] expectedResponseTypes = new Type[0];

	private PacketInfoBase info;

	private uint _PayloadLength;

	public UserFieldsWrapper _UserFields = new UserFieldsWrapper();

	public override PacketType Type => PacketType.Notification;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

	public override PacketInfoBase Info
	{
		get
		{
			if (info == null)
			{
				info = new CommandCompleteForLeReadAntennaInformationInfo(this);
			}
			return info;
		}
	}

	public uint EventCode => 14u;

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

	public UserFieldsWrapper UserFields => _UserFields;

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

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUint(8) != EventCode)
		{
			return false;
		}
		_PayloadLength = bitBuilder.RemoveBitsAsUint(8);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		if (numberOfBits > _PayloadLength * 8 + 7)
		{
			return false;
		}
		if (numberOfBits < _PayloadLength * 8)
		{
			return false;
		}
		UserFields.NumHciCommandPackets = bitBuilder.RemoveBitsAsUint(8);
		if (bitBuilder.RemoveBitsAsUintLittleEndian(16) != UserFields.CommandOpcode)
		{
			return false;
		}
		UserFields.Status = bitBuilder.RemoveBitsAsUint(8);
		UserFields.SupportedSwitchingSamplingRates = bitBuilder.RemoveBitsAsUint(8);
		UserFields.NumberOfAntennae = bitBuilder.RemoveBitsAsUint(8);
		UserFields.MaxLengthOfSwitchingPattern = bitBuilder.RemoveBitsAsUint(8);
		UserFields.MaxCteLength = bitBuilder.RemoveBitsAsUint(8);
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUint(EventCode, 8);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		bitBuilder.AddBitsAsUint(0u, 8);
		bitBuilder.AddBitsAsUint(UserFields.NumHciCommandPackets, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.CommandOpcode, 16);
		bitBuilder.AddBitsAsUint(UserFields.Status, 8);
		bitBuilder.AddBitsAsUint(UserFields.SupportedSwitchingSamplingRates, 8);
		bitBuilder.AddBitsAsUint(UserFields.NumberOfAntennae, 8);
		bitBuilder.AddBitsAsUint(UserFields.MaxLengthOfSwitchingPattern, 8);
		bitBuilder.AddBitsAsUint(UserFields.MaxCteLength, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
