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

namespace QCRPG.TSL.BT.Packets;

public class CommandCompleteForLeReadBufferSizeV2 : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum LeAclDataPacketLengthEnum
		{
			No_dedicated_LE_Buffer,
			Reserved,
			Max_Length,
			InvalidLeAclDataPacketLengthValue
		}

		public static class LeAclDataPacketLengthInfo
		{
			public static LeAclDataPacketLengthEnum ValueToLeAclDataPacketLength(uint value)
			{
				if (value == 0)
				{
					return LeAclDataPacketLengthEnum.No_dedicated_LE_Buffer;
				}
				if (value >= 1 && value <= 26)
				{
					return LeAclDataPacketLengthEnum.Reserved;
				}
				if (value >= 27 && value <= 65535)
				{
					return LeAclDataPacketLengthEnum.Max_Length;
				}
				return LeAclDataPacketLengthEnum.InvalidLeAclDataPacketLengthValue;
			}

			public static uint LeAclDataPacketLengthToValue(LeAclDataPacketLengthEnum enumItem)
			{
				return enumItem switch
				{
					LeAclDataPacketLengthEnum.No_dedicated_LE_Buffer => 0u, 
					LeAclDataPacketLengthEnum.Reserved => 1u, 
					LeAclDataPacketLengthEnum.Max_Length => 27u, 
					_ => uint.MaxValue, 
				};
			}

			public static string LeAclDataPacketLengthToString(LeAclDataPacketLengthEnum enumItem)
			{
				return enumItem switch
				{
					LeAclDataPacketLengthEnum.No_dedicated_LE_Buffer => "No dedicated LE Buffer", 
					LeAclDataPacketLengthEnum.Reserved => "Reserved", 
					LeAclDataPacketLengthEnum.Max_Length => "Maximum length in octets of the data portion", 
					_ => "Invalid LeAclDataPacketLength Value", 
				};
			}

			public static string LeAclDataPacketLengthToDescription()
			{
				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");
				return stringBuilder.ToString();
			}
		}

		public enum TotalNumLeDataPacketsEnum
		{
			No_dedicated_LE_Buffer,
			Total_Number,
			InvalidTotalNumLeDataPacketsValue
		}

		public static class TotalNumLeDataPacketsInfo
		{
			public static TotalNumLeDataPacketsEnum ValueToTotalNumLeDataPackets(uint value)
			{
				if (value == 0)
				{
					return TotalNumLeDataPacketsEnum.No_dedicated_LE_Buffer;
				}
				if (value >= 1 && value <= 255)
				{
					return TotalNumLeDataPacketsEnum.Total_Number;
				}
				return TotalNumLeDataPacketsEnum.InvalidTotalNumLeDataPacketsValue;
			}

			public static uint TotalNumLeDataPacketsToValue(TotalNumLeDataPacketsEnum enumItem)
			{
				return enumItem switch
				{
					TotalNumLeDataPacketsEnum.No_dedicated_LE_Buffer => 0u, 
					TotalNumLeDataPacketsEnum.Total_Number => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string TotalNumLeDataPacketsToString(TotalNumLeDataPacketsEnum enumItem)
			{
				return enumItem switch
				{
					TotalNumLeDataPacketsEnum.No_dedicated_LE_Buffer => "No dedicated LE Buffer", 
					TotalNumLeDataPacketsEnum.Total_Number => "Total number of LE ACL Data Packets", 
					_ => "Invalid TotalNumLeDataPackets Value", 
				};
			}

			public static string TotalNumLeDataPacketsToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = No dedicated LE Buffer");
				stringBuilder.AppendLine("0x1-0xFF = Total number of LE ACL Data Packets");
				return stringBuilder.ToString();
			}
		}

		public enum IsoDataPacketLengthEnum
		{
			No_dedicated_ISO_Buffer,
			Max_Length,
			InvalidIsoDataPacketLengthValue
		}

		public static class IsoDataPacketLengthInfo
		{
			public static IsoDataPacketLengthEnum ValueToIsoDataPacketLength(uint value)
			{
				if (value == 0)
				{
					return IsoDataPacketLengthEnum.No_dedicated_ISO_Buffer;
				}
				if (value >= 1 && value <= 65535)
				{
					return IsoDataPacketLengthEnum.Max_Length;
				}
				return IsoDataPacketLengthEnum.InvalidIsoDataPacketLengthValue;
			}

			public static uint IsoDataPacketLengthToValue(IsoDataPacketLengthEnum enumItem)
			{
				return enumItem switch
				{
					IsoDataPacketLengthEnum.No_dedicated_ISO_Buffer => 0u, 
					IsoDataPacketLengthEnum.Max_Length => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string IsoDataPacketLengthToString(IsoDataPacketLengthEnum enumItem)
			{
				return enumItem switch
				{
					IsoDataPacketLengthEnum.No_dedicated_ISO_Buffer => "No dedicated ISO Buffer", 
					IsoDataPacketLengthEnum.Max_Length => "Maximum length in octets of the data portion", 
					_ => "Invalid IsoDataPacketLength Value", 
				};
			}

			public static string IsoDataPacketLengthToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = No dedicated ISO Buffer");
				stringBuilder.AppendLine("0x1-0xFFFF = Maximum length in octets of the data portion");
				return stringBuilder.ToString();
			}
		}

		public enum TotalNumIsoDataPacketsEnum
		{
			No_dedicated_ISO_Buffer,
			Total_Number,
			InvalidTotalNumIsoDataPacketsValue
		}

		public static class TotalNumIsoDataPacketsInfo
		{
			public static TotalNumIsoDataPacketsEnum ValueToTotalNumIsoDataPackets(uint value)
			{
				if (value == 0)
				{
					return TotalNumIsoDataPacketsEnum.No_dedicated_ISO_Buffer;
				}
				if (value >= 1 && value <= 255)
				{
					return TotalNumIsoDataPacketsEnum.Total_Number;
				}
				return TotalNumIsoDataPacketsEnum.InvalidTotalNumIsoDataPacketsValue;
			}

			public static uint TotalNumIsoDataPacketsToValue(TotalNumIsoDataPacketsEnum enumItem)
			{
				return enumItem switch
				{
					TotalNumIsoDataPacketsEnum.No_dedicated_ISO_Buffer => 0u, 
					TotalNumIsoDataPacketsEnum.Total_Number => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string TotalNumIsoDataPacketsToString(TotalNumIsoDataPacketsEnum enumItem)
			{
				return enumItem switch
				{
					TotalNumIsoDataPacketsEnum.No_dedicated_ISO_Buffer => "No dedicated ISO Buffer", 
					TotalNumIsoDataPacketsEnum.Total_Number => "Total number of ISO Data Packets", 
					_ => "Invalid TotalNumIsoDataPackets Value", 
				};
			}

			public static string TotalNumIsoDataPacketsToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = No dedicated ISO Buffer");
				stringBuilder.AppendLine("0x1-0xFF = Total number of ISO Data Packets");
				return stringBuilder.ToString();
			}
		}

		private const uint _CommandOpcode = 8288u;

		private uint _NumHciCommandPackets;

		private uint _Status;

		private uint _LeAclDataPacketLength;

		private uint _TotalNumLeDataPackets;

		private uint _IsoDataPacketLength;

		private uint _TotalNumIsoDataPackets;

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

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

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

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

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

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

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

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

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

		public LeAclDataPacketLengthEnum GetLeAclDataPacketLengthAsLeAclDataPacketLengthEnum()
		{
			return LeAclDataPacketLengthInfo.ValueToLeAclDataPacketLength(_LeAclDataPacketLength);
		}

		public void SetLeAclDataPacketLengthAsLeAclDataPacketLengthEnum(LeAclDataPacketLengthEnum enumItem)
		{
			LeAclDataPacketLength = LeAclDataPacketLengthInfo.LeAclDataPacketLengthToValue(enumItem);
		}

		public TotalNumLeDataPacketsEnum GetTotalNumLeDataPacketsAsTotalNumLeDataPacketsEnum()
		{
			return TotalNumLeDataPacketsInfo.ValueToTotalNumLeDataPackets(_TotalNumLeDataPackets);
		}

		public void SetTotalNumLeDataPacketsAsTotalNumLeDataPacketsEnum(TotalNumLeDataPacketsEnum enumItem)
		{
			TotalNumLeDataPackets = TotalNumLeDataPacketsInfo.TotalNumLeDataPacketsToValue(enumItem);
		}

		public IsoDataPacketLengthEnum GetIsoDataPacketLengthAsIsoDataPacketLengthEnum()
		{
			return IsoDataPacketLengthInfo.ValueToIsoDataPacketLength(_IsoDataPacketLength);
		}

		public void SetIsoDataPacketLengthAsIsoDataPacketLengthEnum(IsoDataPacketLengthEnum enumItem)
		{
			IsoDataPacketLength = IsoDataPacketLengthInfo.IsoDataPacketLengthToValue(enumItem);
		}

		public TotalNumIsoDataPacketsEnum GetTotalNumIsoDataPacketsAsTotalNumIsoDataPacketsEnum()
		{
			return TotalNumIsoDataPacketsInfo.ValueToTotalNumIsoDataPackets(_TotalNumIsoDataPackets);
		}

		public void SetTotalNumIsoDataPacketsAsTotalNumIsoDataPacketsEnum(TotalNumIsoDataPacketsEnum enumItem)
		{
			TotalNumIsoDataPackets = TotalNumIsoDataPacketsInfo.TotalNumIsoDataPacketsToValue(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 CommandCompleteForLeReadBufferSizeV2Info(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.LeAclDataPacketLength = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.TotalNumLeDataPackets = bitBuilder.RemoveBitsAsUint(8);
		UserFields.IsoDataPacketLength = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.TotalNumIsoDataPackets = 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.LeAclDataPacketLength, 16);
		bitBuilder.AddBitsAsUint(UserFields.TotalNumLeDataPackets, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.IsoDataPacketLength, 16);
		bitBuilder.AddBitsAsUint(UserFields.TotalNumIsoDataPackets, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
