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

namespace QCRPG.TSL.BT.Packets;

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

		private uint _IntervalMin = 6u;

		private uint _IntervalMax = 6u;

		private uint _Latency;

		private uint _Timeout = 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 IntervalMin
		{
			get
			{
				return _IntervalMin;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for IntervalMin is 0xFFFF");
				}
				_IntervalMin = value;
			}
		}

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

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

		public uint Timeout
		{
			get
			{
				return _Timeout;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for Timeout is 0xFFFF");
				}
				_Timeout = 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 = 8224u;

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

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

	public uint Opcode => 8224u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.ConnectionHandle = 0u;
		UserFields.IntervalMin = 6u;
		UserFields.IntervalMax = 6u;
		UserFields.Latency = 0u;
		UserFields.Timeout = 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.IntervalMin = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.IntervalMax = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.Latency = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.Timeout = 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.IntervalMin, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.IntervalMax, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Latency, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Timeout, 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.IntervalMin < 6)
		{
			list.Add("Minimum allowed value for Interval_Min is 0x6");
		}
		if (UserFields.IntervalMin > 3200)
		{
			list.Add("Maximum allowed value for Interval_Min is 0xC80");
		}
		if (UserFields.IntervalMax < 6)
		{
			list.Add("Minimum allowed value for Interval_Max is 0x6");
		}
		if (UserFields.IntervalMax > 3200)
		{
			list.Add("Maximum allowed value for Interval_Max is 0xC80");
		}
		if (UserFields.Latency < 0)
		{
			list.Add("Minimum allowed value for Latency is 0x0");
		}
		if (UserFields.Latency > 499)
		{
			list.Add("Maximum allowed value for Latency is 0x1F3");
		}
		if (UserFields.Timeout < 10)
		{
			list.Add("Minimum allowed value for Timeout is 0xA");
		}
		if (UserFields.Timeout > 3200)
		{
			list.Add("Maximum allowed value for 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;
	}
}
