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

namespace QCRPG.TSL.BT.Packets;

public class LeCreateBigCompleteEvent : PacketBase
{
	public class UserFieldsWrapper
	{
		private const uint _SubeventCode = 27u;

		private uint _Status;

		private uint _BigHandle;

		private uint _BigSyncDelay = 234u;

		private uint _TransportLatencyBig = 234u;

		private uint _Phy;

		private uint _Nse = 1u;

		private uint _Bn;

		private uint _Pto;

		private uint _Irc = 1u;

		private uint _MaxPdu;

		private uint _IsoInterval = 4u;

		private uint _NumBis = 1u;

		private List<ConnectionHandlesSubField> _ConnectionHandlesList = new List<ConnectionHandlesSubField>();

		public uint SubeventCode => 27u;

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

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

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

		public uint TransportLatencyBig
		{
			get
			{
				return _TransportLatencyBig;
			}
			set
			{
				if (value > 16777215)
				{
					throw new Exception("Maximum possible value for TransportLatencyBig is 0xFFFFFF");
				}
				_TransportLatencyBig = 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 Nse
		{
			get
			{
				return _Nse;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for Nse is 0xFF");
				}
				_Nse = 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 Pto
		{
			get
			{
				return _Pto;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for Pto is 0xFF");
				}
				_Pto = 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 MaxPdu
		{
			get
			{
				return _MaxPdu;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for MaxPdu is 0xFFFF");
				}
				_MaxPdu = 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 NumBis
		{
			get
			{
				return _NumBis;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for NumBis is 0xFF");
				}
				_NumBis = value;
				while (_ConnectionHandlesList.Count < _NumBis)
				{
					_ConnectionHandlesList.Add(new ConnectionHandlesSubField());
				}
				while (_ConnectionHandlesList.Count > _NumBis)
				{
					_ConnectionHandlesList.RemoveAt(_ConnectionHandlesList.Count - 1);
				}
			}
		}

		public List<ConnectionHandlesSubField> ConnectionHandlesList => _ConnectionHandlesList;

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

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

		public LePhyEnum GetPhyAsLePhyEnum()
		{
			return LePhyInfo.ValueToLePhy(_Phy);
		}

		public void SetPhyAsLePhyEnum(LePhyEnum enumItem)
		{
			Phy = LePhyInfo.LePhyToValue(enumItem);
		}
	}

	public class ConnectionHandlesSubField
	{
		private uint _ConnectionHandle;

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

		public void SetDefaultValues()
		{
			ConnectionHandle = 0u;
		}

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			ConnectionHandle = bitBuilder.RemoveBitsAsUintLittleEndian(16);
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUintLittleEndian(ConnectionHandle, 16);
			return bitBuilder.ToString();
		}

		public List<string> GetWarnings()
		{
			List<string> list = new List<string>();
			if (ConnectionHandle < 0)
			{
				list.Add("Minimum allowed value for Connection_Handle is 0x0");
			}
			if (ConnectionHandle > 3839)
			{
				list.Add("Maximum allowed value for Connection_Handle is 0xEFF");
			}
			return list;
		}
	}

	private const uint _EventCode = 62u;

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

	public uint EventCode => 62u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.BigHandle = 0u;
		UserFields.BigSyncDelay = 234u;
		UserFields.TransportLatencyBig = 234u;
		UserFields.Nse = 1u;
		UserFields.Bn = 0u;
		UserFields.Pto = 0u;
		UserFields.Irc = 1u;
		UserFields.MaxPdu = 0u;
		UserFields.IsoInterval = 4u;
		UserFields.NumBis = 1u;
		for (int i = 0; i < UserFields.ConnectionHandlesList.Count; i++)
		{
			UserFields.ConnectionHandlesList[i].SetDefaultValues();
		}
	}

	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;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.SubeventCode)
		{
			return false;
		}
		UserFields.Status = bitBuilder.RemoveBitsAsUint(8);
		UserFields.BigHandle = bitBuilder.RemoveBitsAsUint(8);
		UserFields.BigSyncDelay = bitBuilder.RemoveBitsAsUintLittleEndian(24);
		UserFields.TransportLatencyBig = bitBuilder.RemoveBitsAsUintLittleEndian(24);
		UserFields.Phy = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Nse = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Bn = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Pto = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Irc = bitBuilder.RemoveBitsAsUint(8);
		UserFields.MaxPdu = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.IsoInterval = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.NumBis = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.ConnectionHandlesList.Count; i++)
		{
			UserFields.ConnectionHandlesList[i].FromBitBuilder(ref bitBuilder);
		}
		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.SubeventCode, 8);
		bitBuilder.AddBitsAsUint(UserFields.Status, 8);
		bitBuilder.AddBitsAsUint(UserFields.BigHandle, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.BigSyncDelay, 24);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.TransportLatencyBig, 24);
		bitBuilder.AddBitsAsUint(UserFields.Phy, 8);
		bitBuilder.AddBitsAsUint(UserFields.Nse, 8);
		bitBuilder.AddBitsAsUint(UserFields.Bn, 8);
		bitBuilder.AddBitsAsUint(UserFields.Pto, 8);
		bitBuilder.AddBitsAsUint(UserFields.Irc, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MaxPdu, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.IsoInterval, 16);
		bitBuilder.AddBitsAsUint(UserFields.NumBis, 8);
		for (int i = 0; i < UserFields.ConnectionHandlesList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.ConnectionHandlesList[i].ToBitString());
		}
		_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.BigSyncDelay < 234)
		{
			list.Add("Minimum allowed value for BIG_Sync_Delay is 0xEA");
		}
		if (UserFields.BigSyncDelay > 8388607)
		{
			list.Add("Maximum allowed value for BIG_Sync_Delay is 0x7FFFFF");
		}
		if (UserFields.TransportLatencyBig < 234)
		{
			list.Add("Minimum allowed value for Transport_Latency_BIG is 0xEA");
		}
		if (UserFields.TransportLatencyBig > 8388607)
		{
			list.Add("Maximum allowed value for Transport_Latency_BIG is 0x7FFFFF");
		}
		if (UserFields.Nse < 1)
		{
			list.Add("Minimum allowed value for NSE is 0x1");
		}
		if (UserFields.Nse > 30)
		{
			list.Add("Maximum allowed value for NSE is 0x1E");
		}
		if (UserFields.Bn < 0)
		{
			list.Add("Minimum allowed value for BN is 0x0");
		}
		if (UserFields.Bn > 7)
		{
			list.Add("Maximum allowed value for BN is 0x7");
		}
		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");
		}
		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.MaxPdu < 0)
		{
			list.Add("Minimum allowed value for Max_PDU is 0x0");
		}
		if (UserFields.MaxPdu > 251)
		{
			list.Add("Maximum allowed value for Max_PDU is 0xFB");
		}
		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.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");
		}
		for (int i = 0; i < UserFields.ConnectionHandlesList.Count; i++)
		{
			list.AddRange(UserFields.ConnectionHandlesList[i].GetWarnings());
		}
		return list;
	}
}
