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

namespace QCRPG.TSL.NFC.Packets;

public class NfceeDiscoverCmd : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum DiscoveryActionEnum
		{
			DISABLE_DISCOVERY_OF_NFCEE,
			ENABLE_DISCOVERY_OF_NFCEE,
			RFU,
			InvalidDiscoveryActionValue
		}

		public static class DiscoveryActionInfo
		{
			public static DiscoveryActionEnum ValueToDiscoveryAction(uint value)
			{
				switch (value)
				{
				case 0u:
					return DiscoveryActionEnum.DISABLE_DISCOVERY_OF_NFCEE;
				case 1u:
					return DiscoveryActionEnum.ENABLE_DISCOVERY_OF_NFCEE;
				default:
					if (value >= 2 && value <= 255)
					{
						return DiscoveryActionEnum.RFU;
					}
					return DiscoveryActionEnum.InvalidDiscoveryActionValue;
				}
			}

			public static uint DiscoveryActionToValue(DiscoveryActionEnum enumItem)
			{
				return enumItem switch
				{
					DiscoveryActionEnum.DISABLE_DISCOVERY_OF_NFCEE => 0u, 
					DiscoveryActionEnum.ENABLE_DISCOVERY_OF_NFCEE => 1u, 
					DiscoveryActionEnum.RFU => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string DiscoveryActionToString(DiscoveryActionEnum enumItem)
			{
				return enumItem switch
				{
					DiscoveryActionEnum.DISABLE_DISCOVERY_OF_NFCEE => "Disable discovery of NFCEE.", 
					DiscoveryActionEnum.ENABLE_DISCOVERY_OF_NFCEE => "Enable discovery of NFCEE.", 
					DiscoveryActionEnum.RFU => "RFU", 
					_ => "Invalid DiscoveryAction Value", 
				};
			}

			public static string DiscoveryActionToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Disable discovery of NFCEE.");
				stringBuilder.AppendLine("0x1 = Enable discovery of NFCEE.");
				stringBuilder.AppendLine("0x2-0xFF = RFU");
				return stringBuilder.ToString();
			}
		}

		private uint _DiscoveryAction;

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

		public DiscoveryActionEnum GetDiscoveryActionAsDiscoveryActionEnum()
		{
			return DiscoveryActionInfo.ValueToDiscoveryAction(_DiscoveryAction);
		}

		public void SetDiscoveryActionAsDiscoveryActionEnum(DiscoveryActionEnum enumItem)
		{
			DiscoveryAction = DiscoveryActionInfo.DiscoveryActionToValue(enumItem);
		}
	}

	private const uint _Mt = 1u;

	private const uint _Gid = 2u;

	private const uint _Oid = 0u;

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

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

	public uint Mt => 1u;

	public uint Pbf => _Pbf;

	public uint Gid => 2u;

	public uint Rfu => _Rfu;

	public uint Oid => 0u;

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