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

namespace QCRPG.TSL.BT.Packets;

public class LeDataLengthChange : PacketBase
{
	public class UserFieldsWrapper
	{
		private const uint _SubeventCode = 7u;

		private uint _ConnectionHandle;

		private uint _MaxTxOctets = 27u;

		private uint _MaxTxTime = 328u;

		private uint _MaxRxOctets = 27u;

		private uint _MaxRxTime = 328u;

		public uint SubeventCode => 7u;

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

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

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

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

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

	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 LeDataLengthChangeInfo(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.MaxTxOctets = 27u;
		UserFields.MaxTxTime = 328u;
		UserFields.MaxRxOctets = 27u;
		UserFields.MaxRxTime = 328u;
	}

	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.ConnectionHandle = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.MaxTxOctets = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.MaxTxTime = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.MaxRxOctets = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.MaxRxTime = 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.AddBitsAsUintLittleEndian(UserFields.ConnectionHandle, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MaxTxOctets, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MaxTxTime, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MaxRxOctets, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MaxRxTime, 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.MaxTxOctets < 27)
		{
			list.Add("Minimum allowed value for Max_Tx_Octets is 0x1B");
		}
		if (UserFields.MaxTxOctets > 251)
		{
			list.Add("Maximum allowed value for Max_Tx_Octets is 0xFB");
		}
		if (UserFields.MaxTxTime < 328)
		{
			list.Add("Minimum allowed value for Max_Tx_Time is 0x148");
		}
		if (UserFields.MaxTxTime > 17040)
		{
			list.Add("Maximum allowed value for Max_Tx_Time is 0x4290");
		}
		if (UserFields.MaxRxOctets < 27)
		{
			list.Add("Minimum allowed value for Max_Rx_Octets is 0x1B");
		}
		if (UserFields.MaxRxOctets > 251)
		{
			list.Add("Maximum allowed value for Max_Rx_Octets is 0xFB");
		}
		if (UserFields.MaxRxTime < 328)
		{
			list.Add("Minimum allowed value for Max_Rx_Time is 0x148");
		}
		if (UserFields.MaxRxTime > 17040)
		{
			list.Add("Maximum allowed value for Max_Rx_Time is 0x4290");
		}
		return list;
	}
}
