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

namespace QCRPG.TSL.BT.Packets;

public class InquiryResult : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _NumResponses;

		private List<ResponseSubField> _ResponseList = new List<ResponseSubField>();

		public uint NumResponses
		{
			get
			{
				return _NumResponses;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for NumResponses is 0xFF");
				}
				_NumResponses = value;
				while (_ResponseList.Count < _NumResponses)
				{
					_ResponseList.Add(new ResponseSubField());
				}
				while (_ResponseList.Count > _NumResponses)
				{
					_ResponseList.RemoveAt(_ResponseList.Count - 1);
				}
			}
		}

		public List<ResponseSubField> ResponseList => _ResponseList;
	}

	public class ResponseSubField
	{
		private ulong _BdAddr;

		private uint _PageScanRepetitionMode;

		private uint _Reserved0;

		private uint _Reserved1;

		private uint _ClassOfDevice;

		private uint _ClockOffset;

		public ulong BdAddr
		{
			get
			{
				return _BdAddr;
			}
			set
			{
				_BdAddr = value;
			}
		}

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

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

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

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

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

		public PageScanRepetitionModeEnum GetPageScanRepetitionModeAsPageScanRepetitionModeEnum()
		{
			return PageScanRepetitionModeInfo.ValueToPageScanRepetitionMode(_PageScanRepetitionMode);
		}

		public void SetPageScanRepetitionModeAsPageScanRepetitionModeEnum(PageScanRepetitionModeEnum enumItem)
		{
			PageScanRepetitionMode = PageScanRepetitionModeInfo.PageScanRepetitionModeToValue(enumItem);
		}

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			BdAddr = bitBuilder.RemoveBitsAsUlongLittleEndian(48);
			PageScanRepetitionMode = bitBuilder.RemoveBitsAsUint(8);
			Reserved0 = bitBuilder.RemoveBitsAsUint(8);
			Reserved1 = bitBuilder.RemoveBitsAsUint(8);
			ClassOfDevice = bitBuilder.RemoveBitsAsUintLittleEndian(24);
			ClockOffset = bitBuilder.RemoveBitsAsUintLittleEndian(16);
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUlongLittleEndian(BdAddr, 48);
			bitBuilder.AddBitsAsUint(PageScanRepetitionMode, 8);
			bitBuilder.AddBitsAsUint(Reserved0, 8);
			bitBuilder.AddBitsAsUint(Reserved1, 8);
			bitBuilder.AddBitsAsUintLittleEndian(ClassOfDevice, 24);
			bitBuilder.AddBitsAsUintLittleEndian(ClockOffset, 16);
			return bitBuilder.ToString();
		}
	}

	private const uint _EventCode = 2u;

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

	public uint EventCode => 2u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	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;
		}
		UserFields.NumResponses = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.ResponseList.Count; i++)
		{
			UserFields.ResponseList[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.NumResponses, 8);
		for (int i = 0; i < UserFields.ResponseList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.ResponseList[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();
	}
}
