using System;
using System.Text;
using QCRPG.Framework;

namespace QCRPG.TSL.NFC.Packets;

public class RfFieldInfoNtf : PacketBase
{
	public class UserFieldsWrapper
	{
		public class RfFieldStatusEnumCollection
		{
			private AssertBitEnum _OperatingFieldGeneratedByRemoveNfcEndpoint;

			public AssertBitEnum OperatingFieldGeneratedByRemoveNfcEndpoint
			{
				get
				{
					return _OperatingFieldGeneratedByRemoveNfcEndpoint;
				}
				set
				{
					_OperatingFieldGeneratedByRemoveNfcEndpoint = value;
				}
			}
		}

		public static class RfFieldStatusInfo
		{
			public static RfFieldStatusEnumCollection ValueToRfFieldStatus(uint value)
			{
				RfFieldStatusEnumCollection rfFieldStatusEnumCollection = new RfFieldStatusEnumCollection();
				rfFieldStatusEnumCollection.OperatingFieldGeneratedByRemoveNfcEndpoint = AssertBitInfo.ValueToAssertBit(value & 1u);
				return rfFieldStatusEnumCollection;
			}

			public static uint RfFieldStatusToValue(RfFieldStatusEnumCollection enumCollection)
			{
				uint num = 0u;
				return num | AssertBitInfo.AssertBitToValue(enumCollection.OperatingFieldGeneratedByRemoveNfcEndpoint);
			}

			public static string RfFieldStatusToString(RfFieldStatusEnumCollection enumCollection)
			{
				string text = "";
				text = text + "Operating field generated by Remove NFC Endpoint: " + AssertBitInfo.AssertBitToString(enumCollection.OperatingFieldGeneratedByRemoveNfcEndpoint) + "\r\n";
				return text.TrimEnd('\r', '\n');
			}

			public static string RfFieldStatusToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("Bit 0: Operating field generated by Remove NFC Endpoint");
				stringBuilder.AppendLine(AssertBitInfo.AssertBitToDescription());
				stringBuilder.AppendLine("Bits 7-1: RFU");
				return stringBuilder.ToString();
			}
		}

		private uint _RfFieldStatus;

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

		public RfFieldStatusEnumCollection GetRfFieldStatusAsRfFieldStatusEnumCollection()
		{
			return RfFieldStatusInfo.ValueToRfFieldStatus(_RfFieldStatus);
		}

		public void SetRfFieldStatusAsRfFieldStatusEnumCollection(RfFieldStatusEnumCollection enumCollection)
		{
			RfFieldStatus = RfFieldStatusInfo.RfFieldStatusToValue(enumCollection);
		}
	}

	private const uint _Mt = 3u;

	private const uint _Gid = 1u;

	private const uint _Oid = 7u;

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

	private PacketInfoBase info;

	private uint _Pbf;

	private uint _Rfu;

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

	public uint Mt => 3u;

	public uint Pbf => _Pbf;

	public uint Gid => 1u;

	public uint Rfu => _Rfu;

	public uint Oid => 7u;

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