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

namespace QCRPG.TSL.BT.Packets;

public class CommandCompleteForLeReadBufferSize : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum HcLeDataPacketLengthEnum
		{
			No_dedicated_LE_Buffer,
			Reserved,
			Maximum_length_in_octets_of_the_data_portion_of_each_HCI_ACL_Data_Packet_that_the_Controller_is_able_to_accept,
			InvalidHcLeDataPacketLengthValue
		}

		public static class HcLeDataPacketLengthInfo
		{
			public static HcLeDataPacketLengthEnum ValueToHcLeDataPacketLength(uint value)
			{
				if (value == 0)
				{
					return HcLeDataPacketLengthEnum.No_dedicated_LE_Buffer;
				}
				if (value >= 1 && value <= 26)
				{
					return HcLeDataPacketLengthEnum.Reserved;
				}
				if (value >= 27 && value <= 65535)
				{
					return HcLeDataPacketLengthEnum.Maximum_length_in_octets_of_the_data_portion_of_each_HCI_ACL_Data_Packet_that_the_Controller_is_able_to_accept;
				}
				return HcLeDataPacketLengthEnum.InvalidHcLeDataPacketLengthValue;
			}

			public static uint HcLeDataPacketLengthToValue(HcLeDataPacketLengthEnum enumItem)
			{
				return enumItem switch
				{
					HcLeDataPacketLengthEnum.No_dedicated_LE_Buffer => 0u, 
					HcLeDataPacketLengthEnum.Reserved => 1u, 
					HcLeDataPacketLengthEnum.Maximum_length_in_octets_of_the_data_portion_of_each_HCI_ACL_Data_Packet_that_the_Controller_is_able_to_accept => 27u, 
					_ => uint.MaxValue, 
				};
			}

			public static string HcLeDataPacketLengthToString(HcLeDataPacketLengthEnum enumItem)
			{
				return enumItem switch
				{
					HcLeDataPacketLengthEnum.No_dedicated_LE_Buffer => "No dedicated LE Buffer", 
					HcLeDataPacketLengthEnum.Reserved => "Reserved", 
					HcLeDataPacketLengthEnum.Maximum_length_in_octets_of_the_data_portion_of_each_HCI_ACL_Data_Packet_that_the_Controller_is_able_to_accept => "Maximum length in octets of the data portion of each HCI ACL Data Packet that the Controller is_able to accept", 
					_ => "Invalid HcLeDataPacketLength Value", 
				};
			}

			public static string HcLeDataPacketLengthToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = No dedicated LE Buffer");
				stringBuilder.AppendLine("0x1-0x1A = Reserved");
				stringBuilder.AppendLine("0x1B-0xFFFF = Maximum length in octets of the data portion of each HCI ACL Data Packet that the Controller is_able to accept");
				return stringBuilder.ToString();
			}
		}

		public enum HcTotalNumLeDataPacketsEnum
		{
			No_dedicated_LE_Buffer,
			Total_number_of_HCI_ACL_Data_Packets_that_can_be_stored_in_the_data_buffers_of_the_Controller,
			InvalidHcTotalNumLeDataPacketsValue
		}

		public static class HcTotalNumLeDataPacketsInfo
		{
			public static HcTotalNumLeDataPacketsEnum ValueToHcTotalNumLeDataPackets(uint value)
			{
				if (value == 0)
				{
					return HcTotalNumLeDataPacketsEnum.No_dedicated_LE_Buffer;
				}
				if (value >= 1 && value <= 255)
				{
					return HcTotalNumLeDataPacketsEnum.Total_number_of_HCI_ACL_Data_Packets_that_can_be_stored_in_the_data_buffers_of_the_Controller;
				}
				return HcTotalNumLeDataPacketsEnum.InvalidHcTotalNumLeDataPacketsValue;
			}

			public static uint HcTotalNumLeDataPacketsToValue(HcTotalNumLeDataPacketsEnum enumItem)
			{
				return enumItem switch
				{
					HcTotalNumLeDataPacketsEnum.No_dedicated_LE_Buffer => 0u, 
					HcTotalNumLeDataPacketsEnum.Total_number_of_HCI_ACL_Data_Packets_that_can_be_stored_in_the_data_buffers_of_the_Controller => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string HcTotalNumLeDataPacketsToString(HcTotalNumLeDataPacketsEnum enumItem)
			{
				return enumItem switch
				{
					HcTotalNumLeDataPacketsEnum.No_dedicated_LE_Buffer => "No dedicated LE Buffer", 
					HcTotalNumLeDataPacketsEnum.Total_number_of_HCI_ACL_Data_Packets_that_can_be_stored_in_the_data_buffers_of_the_Controller => "Total number of HCI ACL Data Packets that can be stored in the data buffers of the Controller", 
					_ => "Invalid HcTotalNumLeDataPackets Value", 
				};
			}

			public static string HcTotalNumLeDataPacketsToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = No dedicated LE Buffer");
				stringBuilder.AppendLine("0x1-0xFF = Total number of HCI ACL Data Packets that can be stored in the data buffers of the Controller");
				return stringBuilder.ToString();
			}
		}

		private const uint _CommandOpcode = 8194u;

		private uint _NumHciCommandPackets;

		private uint _Status;

		private uint _HcLeDataPacketLength;

		private uint _HcTotalNumLeDataPackets;

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

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

		public uint HcLeDataPacketLength
		{
			get
			{
				return _HcLeDataPacketLength;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for HcLeDataPacketLength is 0xFFFF");
				}
				_HcLeDataPacketLength = value;
			}
		}

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

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

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

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

		public HcLeDataPacketLengthEnum GetHcLeDataPacketLengthAsHcLeDataPacketLengthEnum()
		{
			return HcLeDataPacketLengthInfo.ValueToHcLeDataPacketLength(_HcLeDataPacketLength);
		}

		public void SetHcLeDataPacketLengthAsHcLeDataPacketLengthEnum(HcLeDataPacketLengthEnum enumItem)
		{
			HcLeDataPacketLength = HcLeDataPacketLengthInfo.HcLeDataPacketLengthToValue(enumItem);
		}

		public HcTotalNumLeDataPacketsEnum GetHcTotalNumLeDataPacketsAsHcTotalNumLeDataPacketsEnum()
		{
			return HcTotalNumLeDataPacketsInfo.ValueToHcTotalNumLeDataPackets(_HcTotalNumLeDataPackets);
		}

		public void SetHcTotalNumLeDataPacketsAsHcTotalNumLeDataPacketsEnum(HcTotalNumLeDataPacketsEnum enumItem)
		{
			HcTotalNumLeDataPackets = HcTotalNumLeDataPacketsInfo.HcTotalNumLeDataPacketsToValue(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 CommandCompleteForLeReadBufferSizeInfo(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.HcLeDataPacketLength = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.HcTotalNumLeDataPackets = 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.AddBitsAsUintLittleEndian(UserFields.HcLeDataPacketLength, 16);
		bitBuilder.AddBitsAsUint(UserFields.HcTotalNumLeDataPackets, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
