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

namespace QCRPG.TSL.BT.Packets;

public class LeCisEstablished : PacketBase
{
	public class UserFieldsWrapper
	{
		private const uint _SubeventCode = 25u;

		private uint _Status;

		private uint _ConnectionHandle;

		private uint _CigSyncDelay = 234u;

		private uint _CisSyncDelay = 234u;

		private uint _TransportLatencyMToS = 234u;

		private uint _TransportLatencySToM = 234u;

		private uint _PhyMToS;

		private uint _PhySToM;

		private uint _Nse = 1u;

		private uint _BnMToS;

		private uint _BnSToM;

		private uint _FtMToS = 1u;

		private uint _FtSToM = 1u;

		private uint _MaxPduMToS;

		private uint _MaxPduSToM;

		private uint _IsoInterval = 4u;

		public uint SubeventCode => 25u;

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

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

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

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

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

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

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

		public uint PhySToM
		{
			get
			{
				return _PhySToM;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for PhySToM is 0xFF");
				}
				_PhySToM = 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 BnMToS
		{
			get
			{
				return _BnMToS;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for BnMToS is 0xFF");
				}
				_BnMToS = value;
			}
		}

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

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

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

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

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

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

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

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

		public LePhyEnum GetPhyMToSAsLePhyEnum()
		{
			return LePhyInfo.ValueToLePhy(_PhyMToS);
		}

		public void SetPhyMToSAsLePhyEnum(LePhyEnum enumItem)
		{
			PhyMToS = LePhyInfo.LePhyToValue(enumItem);
		}

		public LePhyEnum GetPhySToMAsLePhyEnum()
		{
			return LePhyInfo.ValueToLePhy(_PhySToM);
		}

		public void SetPhySToMAsLePhyEnum(LePhyEnum enumItem)
		{
			PhySToM = LePhyInfo.LePhyToValue(enumItem);
		}
	}

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

	public uint EventCode => 62u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.ConnectionHandle = 0u;
		UserFields.CigSyncDelay = 234u;
		UserFields.CisSyncDelay = 234u;
		UserFields.TransportLatencyMToS = 234u;
		UserFields.TransportLatencySToM = 234u;
		UserFields.Nse = 1u;
		UserFields.BnMToS = 0u;
		UserFields.BnSToM = 0u;
		UserFields.FtMToS = 1u;
		UserFields.FtSToM = 1u;
		UserFields.MaxPduMToS = 0u;
		UserFields.MaxPduSToM = 0u;
		UserFields.IsoInterval = 4u;
	}

	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.ConnectionHandle = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.CigSyncDelay = bitBuilder.RemoveBitsAsUintLittleEndian(24);
		UserFields.CisSyncDelay = bitBuilder.RemoveBitsAsUintLittleEndian(24);
		UserFields.TransportLatencyMToS = bitBuilder.RemoveBitsAsUintLittleEndian(24);
		UserFields.TransportLatencySToM = bitBuilder.RemoveBitsAsUintLittleEndian(24);
		UserFields.PhyMToS = bitBuilder.RemoveBitsAsUint(8);
		UserFields.PhySToM = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Nse = bitBuilder.RemoveBitsAsUint(8);
		UserFields.BnMToS = bitBuilder.RemoveBitsAsUint(8);
		UserFields.BnSToM = bitBuilder.RemoveBitsAsUint(8);
		UserFields.FtMToS = bitBuilder.RemoveBitsAsUint(8);
		UserFields.FtSToM = bitBuilder.RemoveBitsAsUint(8);
		UserFields.MaxPduMToS = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.MaxPduSToM = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.IsoInterval = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		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.AddBitsAsUintLittleEndian(UserFields.ConnectionHandle, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.CigSyncDelay, 24);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.CisSyncDelay, 24);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.TransportLatencyMToS, 24);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.TransportLatencySToM, 24);
		bitBuilder.AddBitsAsUint(UserFields.PhyMToS, 8);
		bitBuilder.AddBitsAsUint(UserFields.PhySToM, 8);
		bitBuilder.AddBitsAsUint(UserFields.Nse, 8);
		bitBuilder.AddBitsAsUint(UserFields.BnMToS, 8);
		bitBuilder.AddBitsAsUint(UserFields.BnSToM, 8);
		bitBuilder.AddBitsAsUint(UserFields.FtMToS, 8);
		bitBuilder.AddBitsAsUint(UserFields.FtSToM, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MaxPduMToS, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MaxPduSToM, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.IsoInterval, 16);
		_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.ConnectionHandle < 0)
		{
			list.Add("Minimum allowed value for Connection_Handle is 0x0");
		}
		if (UserFields.ConnectionHandle > 3839)
		{
			list.Add("Maximum allowed value for Connection_Handle is 0xEFF");
		}
		if (UserFields.CigSyncDelay < 234)
		{
			list.Add("Minimum allowed value for CIG_Sync_Delay is 0xEA");
		}
		if (UserFields.CigSyncDelay > 8388607)
		{
			list.Add("Maximum allowed value for CIG_Sync_Delay is 0x7FFFFF");
		}
		if (UserFields.CisSyncDelay < 234)
		{
			list.Add("Minimum allowed value for CIS_Sync_Delay is 0xEA");
		}
		if (UserFields.CisSyncDelay > 8388607)
		{
			list.Add("Maximum allowed value for CIS_Sync_Delay is 0x7FFFFF");
		}
		if (UserFields.TransportLatencyMToS < 234)
		{
			list.Add("Minimum allowed value for Transport_Latency_M_To_S is 0xEA");
		}
		if (UserFields.TransportLatencyMToS > 8388607)
		{
			list.Add("Maximum allowed value for Transport_Latency_M_To_S is 0x7FFFFF");
		}
		if (UserFields.TransportLatencySToM < 234)
		{
			list.Add("Minimum allowed value for Transport_Latency_S_To_M is 0xEA");
		}
		if (UserFields.TransportLatencySToM > 8388607)
		{
			list.Add("Maximum allowed value for Transport_Latency_S_To_M 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.BnMToS < 0)
		{
			list.Add("Minimum allowed value for BN_M_TO_S is 0x0");
		}
		if (UserFields.BnMToS > 15)
		{
			list.Add("Maximum allowed value for BN_M_TO_S is 0xF");
		}
		if (UserFields.BnSToM < 0)
		{
			list.Add("Minimum allowed value for BN_S_TO_M is 0x0");
		}
		if (UserFields.BnSToM > 15)
		{
			list.Add("Maximum allowed value for BN_S_TO_M is 0xF");
		}
		if (UserFields.FtMToS < 1)
		{
			list.Add("Minimum allowed value for FT_M_TO_S is 0x1");
		}
		if (UserFields.FtMToS > 255)
		{
			list.Add("Maximum allowed value for FT_M_TO_S is 0xFF");
		}
		if (UserFields.FtSToM < 1)
		{
			list.Add("Minimum allowed value for FT_S_TO_M is 0x1");
		}
		if (UserFields.FtSToM > 255)
		{
			list.Add("Maximum allowed value for FT_S_TO_M is 0xFF");
		}
		if (UserFields.MaxPduMToS < 0)
		{
			list.Add("Minimum allowed value for Max_PDU_M_TO_S is 0x0");
		}
		if (UserFields.MaxPduMToS > 251)
		{
			list.Add("Maximum allowed value for Max_PDU_M_TO_S is 0xFB");
		}
		if (UserFields.MaxPduSToM < 0)
		{
			list.Add("Minimum allowed value for Max_PDU_S_TO_M is 0x0");
		}
		if (UserFields.MaxPduSToM > 251)
		{
			list.Add("Maximum allowed value for Max_PDU_S_TO_M 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");
		}
		return list;
	}
}
