using System;
using QCRPG.Framework;

namespace QCRPG.TSL.NFC.Packets;

public class RfT3tPollingCmd : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _SensfReqParamsSc1;

		private uint _SensfReqParamsSc2;

		private uint _SensfReqParamsRc;

		private uint _SensfReqParamsTsn;

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

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

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

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

	private const uint _Mt = 1u;

	private const uint _Gid = 1u;

	private const uint _Oid = 8u;

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

	private PacketInfoBase info;

	private uint _Pbf;

	private uint _Rfu;

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

	public uint Mt => 1u;

	public uint Pbf => _Pbf;

	public uint Gid => 1u;

	public uint Rfu => _Rfu;

	public uint Oid => 8u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUint(3) != Mt)
		{
			return false;
		}
		_Pbf = bitBuilder.RemoveBitsAsUint(1);
		if (bitBuilder.RemoveBitsAsUint(4) != Gid)
		{
			return false;
		}
		_Rfu = bitBuilder.RemoveBitsAsUint(2);
		if (bitBuilder.RemoveBitsAsUint(6) != Oid)
		{
			return false;
		}
		_PayloadLength = bitBuilder.RemoveBitsAsUint(8);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		if (numberOfBits > _PayloadLength * 8 + 7)
		{
			return false;
		}
		if (numberOfBits < _PayloadLength * 8)
		{
			return false;
		}
		UserFields.SensfReqParamsSc1 = bitBuilder.RemoveBitsAsUint(8);
		UserFields.SensfReqParamsSc2 = bitBuilder.RemoveBitsAsUint(8);
		UserFields.SensfReqParamsRc = bitBuilder.RemoveBitsAsUint(8);
		UserFields.SensfReqParamsTsn = bitBuilder.RemoveBitsAsUint(8);
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUint(Mt, 3);
		bitBuilder.AddBitsAsUint(Pbf, 1);
		bitBuilder.AddBitsAsUint(Gid, 4);
		bitBuilder.AddBitsAsUint(Rfu, 2);
		bitBuilder.AddBitsAsUint(Oid, 6);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		bitBuilder.AddBitsAsUint(0u, 8);
		bitBuilder.AddBitsAsUint(UserFields.SensfReqParamsSc1, 8);
		bitBuilder.AddBitsAsUint(UserFields.SensfReqParamsSc2, 8);
		bitBuilder.AddBitsAsUint(UserFields.SensfReqParamsRc, 8);
		bitBuilder.AddBitsAsUint(UserFields.SensfReqParamsTsn, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
