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

namespace QCRPG.TSL.NFCCustom.Packets;

public class NfcAPollModeRfSpecificParameters : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _SensResResponse;

		private uint _Nfcid1Length;

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

		private uint _SelResResponseLength;

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

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

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

		public List<uint> Nfcid1List => _Nfcid1List;

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

		public List<uint> SelResResponseList => _SelResResponseList;
	}

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

	public UserFieldsWrapper UserFields => _UserFields;

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		UserFields.SensResResponse = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.Nfcid1Length = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.Nfcid1List.Count; i++)
		{
			UserFields.Nfcid1List[i] = bitBuilder.RemoveBitsAsUint(8);
		}
		UserFields.SelResResponseLength = bitBuilder.RemoveBitsAsUint(8);
		for (int j = 0; j < UserFields.SelResResponseList.Count; j++)
		{
			UserFields.SelResResponseList[j] = bitBuilder.RemoveBitsAsUint(8);
		}
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.SensResResponse, 16);
		bitBuilder.AddBitsAsUint(UserFields.Nfcid1Length, 8);
		for (int i = 0; i < UserFields.Nfcid1List.Count; i++)
		{
			if (UserFields.Nfcid1List[i] > 255)
			{
				throw new Exception("UserFields.Nfcid1List[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.Nfcid1List[i], 8);
		}
		bitBuilder.AddBitsAsUint(UserFields.SelResResponseLength, 8);
		for (int j = 0; j < UserFields.SelResResponseList.Count; j++)
		{
			if (UserFields.SelResResponseList[j] > 255)
			{
				throw new Exception("UserFields.SelResResponseList[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.SelResResponseList[j], 8);
		}
		return bitBuilder.ToString();
	}
}
