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

namespace QCRPG.TSL.BT.Packets;

public class LeConnectionUpdate : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _ConnectionHandle;

		private uint _ConnIntervalMin = 6u;

		private uint _ConnIntervalMax = 6u;

		private uint _ConnLatency;

		private uint _SupervisionTimeout = 10u;

		private uint _MinimumCeLength;

		private uint _MaximumCeLength;

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

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

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

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

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

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

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

	private const uint _Opcode = 8211u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(CommandStatusForLeConnectionUpdate),
		typeof(LeConnectionUpdateComplete)
	};

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

	public uint Opcode => 8211u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.ConnectionHandle = 0u;
		UserFields.ConnIntervalMin = 6u;
		UserFields.ConnIntervalMax = 6u;
		UserFields.ConnLatency = 0u;
		UserFields.SupervisionTimeout = 10u;
		UserFields.MinimumCeLength = 0u;
		UserFields.MaximumCeLength = 0u;
	}

	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.ConnectionHandle = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.ConnIntervalMin = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.ConnIntervalMax = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.ConnLatency = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.SupervisionTimeout = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.MinimumCeLength = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.MaximumCeLength = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		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.AddBitsAsUintLittleEndian(UserFields.ConnectionHandle, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.ConnIntervalMin, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.ConnIntervalMax, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.ConnLatency, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.SupervisionTimeout, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MinimumCeLength, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MaximumCeLength, 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.ConnIntervalMin < 6)
		{
			list.Add("Minimum allowed value for Conn_Interval_Min is 0x6");
		}
		if (UserFields.ConnIntervalMin > 3200)
		{
			list.Add("Maximum allowed value for Conn_Interval_Min is 0xC80");
		}
		if (UserFields.ConnIntervalMax < 6)
		{
			list.Add("Minimum allowed value for Conn_Interval_Max is 0x6");
		}
		if (UserFields.ConnIntervalMax > 3200)
		{
			list.Add("Maximum allowed value for Conn_Interval_Max is 0xC80");
		}
		if (UserFields.ConnLatency < 0)
		{
			list.Add("Minimum allowed value for Conn_Latency is 0x0");
		}
		if (UserFields.ConnLatency > 499)
		{
			list.Add("Maximum allowed value for Conn_Latency is 0x1F3");
		}
		if (UserFields.SupervisionTimeout < 10)
		{
			list.Add("Minimum allowed value for Supervision_Timeout is 0xA");
		}
		if (UserFields.SupervisionTimeout > 3200)
		{
			list.Add("Maximum allowed value for Supervision_Timeout is 0xC80");
		}
		if (UserFields.MinimumCeLength < 0)
		{
			list.Add("Minimum allowed value for Minimum_CE_Length is 0x0");
		}
		if (UserFields.MinimumCeLength > 65535)
		{
			list.Add("Maximum allowed value for Minimum_CE_Length is 0xFFFF");
		}
		if (UserFields.MaximumCeLength < 0)
		{
			list.Add("Minimum allowed value for Maximum_CE_Length is 0x0");
		}
		if (UserFields.MaximumCeLength > 65535)
		{
			list.Add("Maximum allowed value for Maximum_CE_Length is 0xFFFF");
		}
		return list;
	}
}
