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

namespace QCRPG.TSL.BT.Packets;

public class LeCreateBigTest : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _BigHandle;

		private uint _AdvertisingHandle;

		private uint _NumBis = 1u;

		private uint _SduInterval = 256u;

		private uint _IsoInterval = 4u;

		private uint _Nse = 1u;

		private uint _MaxSdu = 1u;

		private uint _MaxPdu = 1u;

		private uint _Phy;

		private uint _Packing;

		private uint _Framing;

		private uint _Bn = 1u;

		private uint _Irc = 1u;

		private uint _Pto;

		private uint _Encryption;

		private List<uint> _BroadcastcodeList = new List<uint>(new uint[16]);

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

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

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

		public uint SduInterval
		{
			get
			{
				return _SduInterval;
			}
			set
			{
				if (value > 16777215)
				{
					throw new Exception("Maximum possible value for SduInterval is 0xFFFFFF");
				}
				_SduInterval = value;
			}
		}

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

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

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

		public uint MaxPdu
		{
			get
			{
				return _MaxPdu;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for MaxPdu is 0xFFFF");
				}
				_MaxPdu = 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 Packing
		{
			get
			{
				return _Packing;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for Packing is 0xFF");
				}
				_Packing = value;
			}
		}

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

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

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

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

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

		public List<uint> BroadcastcodeList => _BroadcastcodeList;

		public TransmitterPhyPreferenceEnumCollection GetPhyAsTransmitterPhyPreferenceEnumCollection()
		{
			return TransmitterPhyPreferenceInfo.ValueToTransmitterPhyPreference(_Phy);
		}

		public void SetPhyAsTransmitterPhyPreferenceEnumCollection(TransmitterPhyPreferenceEnumCollection enumCollection)
		{
			Phy = TransmitterPhyPreferenceInfo.TransmitterPhyPreferenceToValue(enumCollection);
		}

		public IsoPackingEnum GetPackingAsIsoPackingEnum()
		{
			return IsoPackingInfo.ValueToIsoPacking(_Packing);
		}

		public void SetPackingAsIsoPackingEnum(IsoPackingEnum enumItem)
		{
			Packing = IsoPackingInfo.IsoPackingToValue(enumItem);
		}

		public IsoFramingEnum GetFramingAsIsoFramingEnum()
		{
			return IsoFramingInfo.ValueToIsoFraming(_Framing);
		}

		public void SetFramingAsIsoFramingEnum(IsoFramingEnum enumItem)
		{
			Framing = IsoFramingInfo.IsoFramingToValue(enumItem);
		}

		public IsoEncryptionEnum GetEncryptionAsIsoEncryptionEnum()
		{
			return IsoEncryptionInfo.ValueToIsoEncryption(_Encryption);
		}

		public void SetEncryptionAsIsoEncryptionEnum(IsoEncryptionEnum enumItem)
		{
			Encryption = IsoEncryptionInfo.IsoEncryptionToValue(enumItem);
		}
	}

	private const uint _Opcode = 8297u;

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

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

	public uint Opcode => 8297u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.BigHandle = 0u;
		UserFields.AdvertisingHandle = 0u;
		UserFields.NumBis = 1u;
		UserFields.SduInterval = 256u;
		UserFields.IsoInterval = 4u;
		UserFields.Nse = 1u;
		UserFields.MaxSdu = 1u;
		UserFields.MaxPdu = 1u;
		UserFields.Phy = 1u;
		UserFields.Bn = 1u;
		UserFields.Irc = 1u;
		UserFields.Pto = 0u;
		for (int num = UserFields.BroadcastcodeList.Count - 1; num >= 0; num--)
		{
		}
	}

	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.BigHandle = bitBuilder.RemoveBitsAsUint(8);
		UserFields.AdvertisingHandle = bitBuilder.RemoveBitsAsUint(8);
		UserFields.NumBis = bitBuilder.RemoveBitsAsUint(8);
		UserFields.SduInterval = bitBuilder.RemoveBitsAsUintLittleEndian(24);
		UserFields.IsoInterval = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.Nse = bitBuilder.RemoveBitsAsUint(8);
		UserFields.MaxSdu = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.MaxPdu = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.Phy = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Packing = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Framing = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Bn = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Irc = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Pto = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Encryption = bitBuilder.RemoveBitsAsUint(8);
		for (int num = UserFields.BroadcastcodeList.Count - 1; num >= 0; num--)
		{
			UserFields.BroadcastcodeList[num] = bitBuilder.RemoveBitsAsUint(8);
		}
		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.BigHandle, 8);
		bitBuilder.AddBitsAsUint(UserFields.AdvertisingHandle, 8);
		bitBuilder.AddBitsAsUint(UserFields.NumBis, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.SduInterval, 24);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.IsoInterval, 16);
		bitBuilder.AddBitsAsUint(UserFields.Nse, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MaxSdu, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MaxPdu, 16);
		bitBuilder.AddBitsAsUint(UserFields.Phy, 8);
		bitBuilder.AddBitsAsUint(UserFields.Packing, 8);
		bitBuilder.AddBitsAsUint(UserFields.Framing, 8);
		bitBuilder.AddBitsAsUint(UserFields.Bn, 8);
		bitBuilder.AddBitsAsUint(UserFields.Irc, 8);
		bitBuilder.AddBitsAsUint(UserFields.Pto, 8);
		bitBuilder.AddBitsAsUint(UserFields.Encryption, 8);
		for (int num = UserFields.BroadcastcodeList.Count - 1; num >= 0; num--)
		{
			if (UserFields.BroadcastcodeList[num] > 255)
			{
				throw new Exception("UserFields.BroadcastcodeList[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.BroadcastcodeList[num], 8);
		}
		_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.BigHandle < 0)
		{
			list.Add("Minimum allowed value for BIG_Handle is 0x0");
		}
		if (UserFields.BigHandle > 239)
		{
			list.Add("Maximum allowed value for BIG_Handle is 0xEF");
		}
		if (UserFields.AdvertisingHandle < 0)
		{
			list.Add("Minimum allowed value for Advertising_Handle is 0x0");
		}
		if (UserFields.AdvertisingHandle > 239)
		{
			list.Add("Maximum allowed value for Advertising_Handle is 0xEF");
		}
		if (UserFields.NumBis < 1)
		{
			list.Add("Minimum allowed value for Num_BIS is 0x1");
		}
		if (UserFields.NumBis > 31)
		{
			list.Add("Maximum allowed value for Num_BIS is 0x1F");
		}
		if (UserFields.SduInterval < 256)
		{
			list.Add("Minimum allowed value for SDU_Interval is 0x100");
		}
		if (UserFields.SduInterval > 1048575)
		{
			list.Add("Maximum allowed value for SDU_Interval is 0xFFFFF");
		}
		if (UserFields.IsoInterval < 4)
		{
			list.Add("Minimum allowed value for ISO_Interval is 0x4");
		}
		if (UserFields.IsoInterval > 3200)
		{
			list.Add("Maximum allowed value for ISO_Interval is 0xC80");
		}
		if (UserFields.Nse < 1)
		{
			list.Add("Minimum allowed value for NSE is 0x1");
		}
		if (UserFields.Nse > 31)
		{
			list.Add("Maximum allowed value for NSE is 0x1F");
		}
		if (UserFields.MaxSdu < 1)
		{
			list.Add("Minimum allowed value for Max_SDU is 0x1");
		}
		if (UserFields.MaxSdu > 4095)
		{
			list.Add("Maximum allowed value for Max_SDU is 0xFFF");
		}
		if (UserFields.MaxPdu < 1)
		{
			list.Add("Minimum allowed value for Max_PDU is 0x1");
		}
		if (UserFields.MaxPdu > 251)
		{
			list.Add("Maximum allowed value for Max_PDU is 0xFB");
		}
		if (UserFields.Bn < 1)
		{
			list.Add("Minimum allowed value for BN is 0x1");
		}
		if (UserFields.Bn > 7)
		{
			list.Add("Maximum allowed value for BN is 0x7");
		}
		if (UserFields.Irc < 1)
		{
			list.Add("Minimum allowed value for IRC is 0x1");
		}
		if (UserFields.Irc > 15)
		{
			list.Add("Maximum allowed value for IRC is 0xF");
		}
		if (UserFields.Pto < 0)
		{
			list.Add("Minimum allowed value for PTO is 0x0");
		}
		if (UserFields.Pto > 15)
		{
			list.Add("Maximum allowed value for PTO is 0xF");
		}
		return list;
	}
}
