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

namespace QCRPG.TSL.NFC.Packets;

public class NfceeDiscoverNtf : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum NfceeStatusEnum
		{
			NFCEE_CONNECTED_AND_ENABLED,
			NFCEE_CONNECTED_AND_DISABLED,
			NFCEE_REMOVED,
			RFU,
			InvalidNfceeStatusValue
		}

		public static class NfceeStatusInfo
		{
			public static NfceeStatusEnum ValueToNfceeStatus(uint value)
			{
				switch (value)
				{
				case 0u:
					return NfceeStatusEnum.NFCEE_CONNECTED_AND_ENABLED;
				case 1u:
					return NfceeStatusEnum.NFCEE_CONNECTED_AND_DISABLED;
				case 2u:
					return NfceeStatusEnum.NFCEE_REMOVED;
				default:
					if (value >= 3 && value <= 255)
					{
						return NfceeStatusEnum.RFU;
					}
					return NfceeStatusEnum.InvalidNfceeStatusValue;
				}
			}

			public static uint NfceeStatusToValue(NfceeStatusEnum enumItem)
			{
				return enumItem switch
				{
					NfceeStatusEnum.NFCEE_CONNECTED_AND_ENABLED => 0u, 
					NfceeStatusEnum.NFCEE_CONNECTED_AND_DISABLED => 1u, 
					NfceeStatusEnum.NFCEE_REMOVED => 2u, 
					NfceeStatusEnum.RFU => 3u, 
					_ => uint.MaxValue, 
				};
			}

			public static string NfceeStatusToString(NfceeStatusEnum enumItem)
			{
				return enumItem switch
				{
					NfceeStatusEnum.NFCEE_CONNECTED_AND_ENABLED => "NFCEE connected and enabled.", 
					NfceeStatusEnum.NFCEE_CONNECTED_AND_DISABLED => "NFCEE connected and disabled.", 
					NfceeStatusEnum.NFCEE_REMOVED => "NFCEE removed.", 
					NfceeStatusEnum.RFU => "RFU", 
					_ => "Invalid NfceeStatus Value", 
				};
			}

			public static string NfceeStatusToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = NFCEE connected and enabled.");
				stringBuilder.AppendLine("0x1 = NFCEE connected and disabled.");
				stringBuilder.AppendLine("0x2 = NFCEE removed.");
				stringBuilder.AppendLine("0x3-0xFF = RFU");
				return stringBuilder.ToString();
			}
		}

		private uint _NfceeId;

		private uint _NfceeStatus;

		private uint _NumberOfProtocolInformationEntries;

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

		private uint _NumberOfNfceeInformationTlvs;

		private List<NfceeInformationTlvSubField> _NfceeInformationTlvList = new List<NfceeInformationTlvSubField>();

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

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

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

		public List<uint> SupportedNfceeProtocolsList => _SupportedNfceeProtocolsList;

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

		public List<NfceeInformationTlvSubField> NfceeInformationTlvList => _NfceeInformationTlvList;

		public NfceeIdsEnum GetNfceeIdAsNfceeIdsEnum()
		{
			return NfceeIdsInfo.ValueToNfceeIds(_NfceeId);
		}

		public void SetNfceeIdAsNfceeIdsEnum(NfceeIdsEnum enumItem)
		{
			NfceeId = NfceeIdsInfo.NfceeIdsToValue(enumItem);
		}

		public NfceeStatusEnum GetNfceeStatusAsNfceeStatusEnum()
		{
			return NfceeStatusInfo.ValueToNfceeStatus(_NfceeStatus);
		}

		public void SetNfceeStatusAsNfceeStatusEnum(NfceeStatusEnum enumItem)
		{
			NfceeStatus = NfceeStatusInfo.NfceeStatusToValue(enumItem);
		}

		public NfceeProtocolsAndInterfacesEnum GetSupportedNfceeProtocolsAsNfceeProtocolsAndInterfacesEnum(int index)
		{
			return NfceeProtocolsAndInterfacesInfo.ValueToNfceeProtocolsAndInterfaces(_SupportedNfceeProtocolsList[index]);
		}

		public void SetSupportedNfceeProtocolsAsNfceeProtocolsAndInterfacesEnum(int index, NfceeProtocolsAndInterfacesEnum enumItem)
		{
			_SupportedNfceeProtocolsList[index] = NfceeProtocolsAndInterfacesInfo.NfceeProtocolsAndInterfacesToValue(enumItem);
		}
	}

	public class NfceeInformationTlvSubField
	{
		public enum TypeEnum
		{
			HW_REGISTRATION_IDENTIFICATION,
			ATR_BYTES,
			T3T_COMMAND_SET_INTERFACE_SUPPLEMENTARY_INFORMATION,
			RFU,
			PROPRIETARY,
			InvalidTypeValue
		}

		public static class TypeInfo
		{
			public static TypeEnum ValueToType(uint value)
			{
				switch (value)
				{
				case 0u:
					return TypeEnum.HW_REGISTRATION_IDENTIFICATION;
				case 1u:
					return TypeEnum.ATR_BYTES;
				case 2u:
					return TypeEnum.T3T_COMMAND_SET_INTERFACE_SUPPLEMENTARY_INFORMATION;
				default:
					if (value >= 3 && value <= 159)
					{
						return TypeEnum.RFU;
					}
					if (value >= 160 && value <= 255)
					{
						return TypeEnum.PROPRIETARY;
					}
					return TypeEnum.InvalidTypeValue;
				}
			}

			public static uint TypeToValue(TypeEnum enumItem)
			{
				return enumItem switch
				{
					TypeEnum.HW_REGISTRATION_IDENTIFICATION => 0u, 
					TypeEnum.ATR_BYTES => 1u, 
					TypeEnum.T3T_COMMAND_SET_INTERFACE_SUPPLEMENTARY_INFORMATION => 2u, 
					TypeEnum.RFU => 3u, 
					TypeEnum.PROPRIETARY => 160u, 
					_ => uint.MaxValue, 
				};
			}

			public static string TypeToString(TypeEnum enumItem)
			{
				return enumItem switch
				{
					TypeEnum.HW_REGISTRATION_IDENTIFICATION => "Hardware / Registration Identification", 
					TypeEnum.ATR_BYTES => "ATR Bytes", 
					TypeEnum.T3T_COMMAND_SET_INTERFACE_SUPPLEMENTARY_INFORMATION => "T3T Command Set Supplementary Information", 
					TypeEnum.RFU => "RFU", 
					TypeEnum.PROPRIETARY => "Proprietary", 
					_ => "Invalid Type Value", 
				};
			}

			public static string TypeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Hardware / Registration Identification");
				stringBuilder.AppendLine("0x1 = ATR Bytes");
				stringBuilder.AppendLine("0x2 = T3T Command Set Supplementary Information");
				stringBuilder.AppendLine("0x3-0x9F = RFU");
				stringBuilder.AppendLine("0xA0-0xFF = Proprietary");
				return stringBuilder.ToString();
			}
		}

		private uint _Type;

		private uint _Length;

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

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

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

		public List<uint> ValueList => _ValueList;

		public TypeEnum GetTypeAsTypeEnum()
		{
			return TypeInfo.ValueToType(_Type);
		}

		public void SetTypeAsTypeEnum(TypeEnum enumItem)
		{
			Type = TypeInfo.TypeToValue(enumItem);
		}

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			Type = bitBuilder.RemoveBitsAsUint(8);
			Length = bitBuilder.RemoveBitsAsUint(8);
			for (int num = ValueList.Count - 1; num >= 0; num--)
			{
				ValueList[num] = bitBuilder.RemoveBitsAsUint(8);
			}
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUint(Type, 8);
			bitBuilder.AddBitsAsUint(Length, 8);
			for (int num = ValueList.Count - 1; num >= 0; num--)
			{
				if (ValueList[num] > 255)
				{
					throw new Exception("ValueList[i] is > 0xFF");
				}
				bitBuilder.AddBitsAsUint(ValueList[num], 8);
			}
			return bitBuilder.ToString();
		}
	}

	private const uint _Mt = 3u;

	private const uint _Gid = 2u;

	private const uint _Oid = 0u;

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

	public uint Mt => 3u;

	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.NfceeId = bitBuilder.RemoveBitsAsUint(8);
		UserFields.NfceeStatus = bitBuilder.RemoveBitsAsUint(8);
		UserFields.NumberOfProtocolInformationEntries = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.SupportedNfceeProtocolsList.Count; i++)
		{
			UserFields.SupportedNfceeProtocolsList[i] = bitBuilder.RemoveBitsAsUint(8);
		}
		UserFields.NumberOfNfceeInformationTlvs = bitBuilder.RemoveBitsAsUint(8);
		for (int j = 0; j < UserFields.NfceeInformationTlvList.Count; j++)
		{
			UserFields.NfceeInformationTlvList[j].FromBitBuilder(ref bitBuilder);
		}
		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.NfceeId, 8);
		bitBuilder.AddBitsAsUint(UserFields.NfceeStatus, 8);
		bitBuilder.AddBitsAsUint(UserFields.NumberOfProtocolInformationEntries, 8);
		for (int i = 0; i < UserFields.SupportedNfceeProtocolsList.Count; i++)
		{
			if (UserFields.SupportedNfceeProtocolsList[i] > 255)
			{
				throw new Exception("Maximum possible value for SupportedNfceeProtocols is 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.SupportedNfceeProtocolsList[i], 8);
		}
		bitBuilder.AddBitsAsUint(UserFields.NumberOfNfceeInformationTlvs, 8);
		for (int j = 0; j < UserFields.NfceeInformationTlvList.Count; j++)
		{
			bitBuilder.AddBitsAsString(UserFields.NfceeInformationTlvList[j].ToBitString());
		}
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
