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

namespace QCRPG.TSL.NFCCustom.Packets;

public class NfcBPollModeRfSpecificParameters : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _SensbResResponseLength;

		private List<uint> _SensbResResponseList = new List<uint>();

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

		public List<uint> SensbResResponseList => _SensbResResponseList;
	}

	private Type[] expectedResponseTypes = new Type[0];

	private PacketInfoBase info;

	public UserFieldsWrapper _UserFields = new UserFieldsWrapper();

	public override PacketType Type => PacketType.Data;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

	public override PacketInfoBase Info
	{
		get
		{
			if (info == null)
			{
				info = new NfcBPollModeRfSpecificParametersInfo(this);
			}
			return info;
		}
	}

	public UserFieldsWrapper UserFields => _UserFields;

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		UserFields.SensbResResponseLength = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.SensbResResponseList.Count; i++)
		{
			UserFields.SensbResResponseList[i] = bitBuilder.RemoveBitsAsUint(8);
		}
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUint(UserFields.SensbResResponseLength, 8);
		for (int i = 0; i < UserFields.SensbResResponseList.Count; i++)
		{
			if (UserFields.SensbResResponseList[i] > 255)
			{
				throw new Exception("UserFields.SensbResResponseList[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.SensbResResponseList[i], 8);
		}
		return bitBuilder.ToString();
	}
}
