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

namespace QCRPG.TSL.BT.Packets;

public class CommandCompleteForLeReadAdvertisingChannelTxPower : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum TransmitPowerLevelEnum
		{
			Tx_PWR_Level_0_dBm,
			Tx_PWR_Level_1_dBm,
			Tx_PWR_Level_2_dBm,
			Tx_PWR_Level_3_dBm,
			Tx_PWR_Level_4_dBm,
			Tx_PWR_Level_5_dBm,
			Tx_PWR_Level_6_dBm,
			Tx_PWR_Level_7_dBm,
			Tx_PWR_Level_8_dBm,
			Tx_PWR_Level_9_dBm,
			Tx_PWR_Level_10_dBm,
			Tx_PWR_Level_MINUS_20_dBm,
			Tx_PWR_Level_MINUS_19_dBm,
			Tx_PWR_Level_MINUS_18_dBm,
			Tx_PWR_Level_MINUS_17_dBm,
			Tx_PWR_Level_MINUS_16_dBm,
			Tx_PWR_Level_MINUS_15_dBm,
			Tx_PWR_Level_MINUS_14_dBm,
			Tx_PWR_Level_MINUS_13_dBm,
			Tx_PWR_Level_MINUS_12_dBm,
			Tx_PWR_Level_MINUS_11_dBm,
			Tx_PWR_Level_MINUS_10_dBm,
			Tx_PWR_Level_MINUS_9_dBm,
			Tx_PWR_Level_MINUS_8_dBm,
			Tx_PWR_Level_MINUS_7_dBm,
			Tx_PWR_Level_MINUS_6_dBm,
			Tx_PWR_Level_MINUS_5_dBm,
			Tx_PWR_Level_MINUS_4_dBm,
			Tx_PWR_Level_MINUS_3_dBm,
			Tx_PWR_Level_MINUS_2_dBm,
			Tx_PWR_Level_MINUS_1_dBm,
			InvalidTransmitPowerLevelValue
		}

		public static class TransmitPowerLevelInfo
		{
			public static TransmitPowerLevelEnum ValueToTransmitPowerLevel(uint value)
			{
				return value switch
				{
					0u => TransmitPowerLevelEnum.Tx_PWR_Level_0_dBm, 
					1u => TransmitPowerLevelEnum.Tx_PWR_Level_1_dBm, 
					2u => TransmitPowerLevelEnum.Tx_PWR_Level_2_dBm, 
					3u => TransmitPowerLevelEnum.Tx_PWR_Level_3_dBm, 
					4u => TransmitPowerLevelEnum.Tx_PWR_Level_4_dBm, 
					5u => TransmitPowerLevelEnum.Tx_PWR_Level_5_dBm, 
					6u => TransmitPowerLevelEnum.Tx_PWR_Level_6_dBm, 
					7u => TransmitPowerLevelEnum.Tx_PWR_Level_7_dBm, 
					8u => TransmitPowerLevelEnum.Tx_PWR_Level_8_dBm, 
					9u => TransmitPowerLevelEnum.Tx_PWR_Level_9_dBm, 
					10u => TransmitPowerLevelEnum.Tx_PWR_Level_10_dBm, 
					236u => TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_20_dBm, 
					237u => TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_19_dBm, 
					238u => TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_18_dBm, 
					239u => TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_17_dBm, 
					240u => TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_16_dBm, 
					241u => TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_15_dBm, 
					242u => TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_14_dBm, 
					243u => TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_13_dBm, 
					244u => TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_12_dBm, 
					245u => TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_11_dBm, 
					246u => TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_10_dBm, 
					247u => TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_9_dBm, 
					248u => TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_8_dBm, 
					249u => TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_7_dBm, 
					250u => TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_6_dBm, 
					251u => TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_5_dBm, 
					252u => TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_4_dBm, 
					253u => TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_3_dBm, 
					254u => TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_2_dBm, 
					255u => TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_1_dBm, 
					_ => TransmitPowerLevelEnum.InvalidTransmitPowerLevelValue, 
				};
			}

			public static uint TransmitPowerLevelToValue(TransmitPowerLevelEnum enumItem)
			{
				return enumItem switch
				{
					TransmitPowerLevelEnum.Tx_PWR_Level_0_dBm => 0u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_1_dBm => 1u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_2_dBm => 2u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_3_dBm => 3u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_4_dBm => 4u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_5_dBm => 5u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_6_dBm => 6u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_7_dBm => 7u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_8_dBm => 8u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_9_dBm => 9u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_10_dBm => 10u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_20_dBm => 236u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_19_dBm => 237u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_18_dBm => 238u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_17_dBm => 239u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_16_dBm => 240u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_15_dBm => 241u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_14_dBm => 242u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_13_dBm => 243u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_12_dBm => 244u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_11_dBm => 245u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_10_dBm => 246u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_9_dBm => 247u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_8_dBm => 248u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_7_dBm => 249u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_6_dBm => 250u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_5_dBm => 251u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_4_dBm => 252u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_3_dBm => 253u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_2_dBm => 254u, 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_1_dBm => 255u, 
					_ => uint.MaxValue, 
				};
			}

			public static string TransmitPowerLevelToString(TransmitPowerLevelEnum enumItem)
			{
				return enumItem switch
				{
					TransmitPowerLevelEnum.Tx_PWR_Level_0_dBm => "0dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_1_dBm => "1dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_2_dBm => "2dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_3_dBm => "3dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_4_dBm => "4dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_5_dBm => "5dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_6_dBm => "6dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_7_dBm => "7dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_8_dBm => "8dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_9_dBm => "9dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_10_dBm => "10dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_20_dBm => "-20dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_19_dBm => "-19dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_18_dBm => "-18dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_17_dBm => "-17dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_16_dBm => "-16dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_15_dBm => "-15dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_14_dBm => "-14dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_13_dBm => "-13dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_12_dBm => "-12dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_11_dBm => "-11dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_10_dBm => "-10dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_9_dBm => "-9dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_8_dBm => "-8dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_7_dBm => "-7dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_6_dBm => "-6dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_5_dBm => "-5dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_4_dBm => "-4dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_3_dBm => "-3dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_2_dBm => "-2dBm", 
					TransmitPowerLevelEnum.Tx_PWR_Level_MINUS_1_dBm => "-1dBm", 
					_ => "Invalid TransmitPowerLevel Value", 
				};
			}

			public static string TransmitPowerLevelToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = 0dBm");
				stringBuilder.AppendLine("0x1 = 1dBm");
				stringBuilder.AppendLine("0x2 = 2dBm");
				stringBuilder.AppendLine("0x3 = 3dBm");
				stringBuilder.AppendLine("0x4 = 4dBm");
				stringBuilder.AppendLine("0x5 = 5dBm");
				stringBuilder.AppendLine("0x6 = 6dBm");
				stringBuilder.AppendLine("0x7 = 7dBm");
				stringBuilder.AppendLine("0x8 = 8dBm");
				stringBuilder.AppendLine("0x9 = 9dBm");
				stringBuilder.AppendLine("0xA = 10dBm");
				stringBuilder.AppendLine("0xEC = -20dBm");
				stringBuilder.AppendLine("0xED = -19dBm");
				stringBuilder.AppendLine("0xEE = -18dBm");
				stringBuilder.AppendLine("0xEF = -17dBm");
				stringBuilder.AppendLine("0xF0 = -16dBm");
				stringBuilder.AppendLine("0xF1 = -15dBm");
				stringBuilder.AppendLine("0xF2 = -14dBm");
				stringBuilder.AppendLine("0xF3 = -13dBm");
				stringBuilder.AppendLine("0xF4 = -12dBm");
				stringBuilder.AppendLine("0xF5 = -11dBm");
				stringBuilder.AppendLine("0xF6 = -10dBm");
				stringBuilder.AppendLine("0xF7 = -9dBm");
				stringBuilder.AppendLine("0xF8 = -8dBm");
				stringBuilder.AppendLine("0xF9 = -7dBm");
				stringBuilder.AppendLine("0xFA = -6dBm");
				stringBuilder.AppendLine("0xFB = -5dBm");
				stringBuilder.AppendLine("0xFC = -4dBm");
				stringBuilder.AppendLine("0xFD = -3dBm");
				stringBuilder.AppendLine("0xFE = -2dBm");
				stringBuilder.AppendLine("0xFF = -1dBm");
				return stringBuilder.ToString();
			}
		}

		private const uint _CommandOpcode = 8199u;

		private uint _NumHciCommandPackets;

		private uint _Status;

		private uint _TransmitPowerLevel;

		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 => 8199u;

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

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

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

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

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

		public TransmitPowerLevelEnum GetTransmitPowerLevelAsTransmitPowerLevelEnum()
		{
			return TransmitPowerLevelInfo.ValueToTransmitPowerLevel(_TransmitPowerLevel);
		}

		public void SetTransmitPowerLevelAsTransmitPowerLevelEnum(TransmitPowerLevelEnum enumItem)
		{
			TransmitPowerLevel = TransmitPowerLevelInfo.TransmitPowerLevelToValue(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 CommandCompleteForLeReadAdvertisingChannelTxPowerInfo(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.TransmitPowerLevel = 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.TransmitPowerLevel, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
