using System;
using QCRPG.Framework;

namespace QCRPG.TSL.NFC.Packets;

public class RfDiscoverSelectCmd : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _RfDiscoveryId;

		private uint _RfProtocol;

		private uint _RfInterface;

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

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

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

		public RfDiscoveryIdEnum GetRfDiscoveryIdAsRfDiscoveryIdEnum()
		{
			return RfDiscoveryIdInfo.ValueToRfDiscoveryId(_RfDiscoveryId);
		}

		public void SetRfDiscoveryIdAsRfDiscoveryIdEnum(RfDiscoveryIdEnum enumItem)
		{
			RfDiscoveryId = RfDiscoveryIdInfo.RfDiscoveryIdToValue(enumItem);
		}

		public RfProtocolsEnum GetRfProtocolAsRfProtocolsEnum()
		{
			return RfProtocolsInfo.ValueToRfProtocols(_RfProtocol);
		}

		public void SetRfProtocolAsRfProtocolsEnum(RfProtocolsEnum enumItem)
		{
			RfProtocol = RfProtocolsInfo.RfProtocolsToValue(enumItem);
		}

		public RfInterfacesEnum GetRfInterfaceAsRfInterfacesEnum()
		{
			return RfInterfacesInfo.ValueToRfInterfaces(_RfInterface);
		}

		public void SetRfInterfaceAsRfInterfacesEnum(RfInterfacesEnum enumItem)
		{
			RfInterface = RfInterfacesInfo.RfInterfacesToValue(enumItem);
		}
	}

	private const uint _Mt = 1u;

	private const uint _Gid = 1u;

	private const uint _Oid = 4u;

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

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

	public uint Mt => 1u;

	public uint Pbf => _Pbf;

	public uint Gid => 1u;

	public uint Rfu => _Rfu;

	public uint Oid => 4u;

	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.RfDiscoveryId = bitBuilder.RemoveBitsAsUint(8);
		UserFields.RfProtocol = bitBuilder.RemoveBitsAsUint(8);
		UserFields.RfInterface = 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.RfDiscoveryId, 8);
		bitBuilder.AddBitsAsUint(UserFields.RfProtocol, 8);
		bitBuilder.AddBitsAsUint(UserFields.RfInterface, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
