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

namespace QCRPG.TSL.NFC.Packets;

public class RfNfceeActionNtf : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum TriggerEnum
		{
			SELECT_WITH_AID,
			RF_PROTOCOL_BASED_ROUTING_DECISION,
			RF_TECHNOLOGY_BASED_ROUTING_DECISION,
			RFU,
			APPLICATION_INITIATION,
			InvalidTriggerValue
		}

		public static class TriggerInfo
		{
			public static TriggerEnum ValueToTrigger(uint value)
			{
				switch (value)
				{
				case 0u:
					return TriggerEnum.SELECT_WITH_AID;
				case 1u:
					return TriggerEnum.RF_PROTOCOL_BASED_ROUTING_DECISION;
				case 2u:
					return TriggerEnum.RF_TECHNOLOGY_BASED_ROUTING_DECISION;
				default:
					if (value >= 3 && value <= 15)
					{
						return TriggerEnum.RFU;
					}
					if (value == 16)
					{
						return TriggerEnum.APPLICATION_INITIATION;
					}
					if (value >= 17 && value <= 255)
					{
						return TriggerEnum.RFU;
					}
					return TriggerEnum.InvalidTriggerValue;
				}
			}

			public static uint TriggerToValue(TriggerEnum enumItem)
			{
				switch (enumItem)
				{
				case TriggerEnum.SELECT_WITH_AID:
					return 0u;
				case TriggerEnum.RF_PROTOCOL_BASED_ROUTING_DECISION:
					return 1u;
				case TriggerEnum.RF_TECHNOLOGY_BASED_ROUTING_DECISION:
					return 2u;
				case TriggerEnum.RFU:
					return 3u;
				case TriggerEnum.APPLICATION_INITIATION:
					return 16u;
				default:
					if (enumItem == TriggerEnum.RFU)
					{
						return 17u;
					}
					return uint.MaxValue;
				}
			}

			public static string TriggerToString(TriggerEnum enumItem)
			{
				switch (enumItem)
				{
				case TriggerEnum.SELECT_WITH_AID:
					return "SELECT_WITH_AID";
				case TriggerEnum.RF_PROTOCOL_BASED_ROUTING_DECISION:
					return "RF_PROTOCOL_BASED_ROUTING_DECISION";
				case TriggerEnum.RF_TECHNOLOGY_BASED_ROUTING_DECISION:
					return "RF_TECHNOLOGY_BASED_ROUTING_DECISION";
				case TriggerEnum.RFU:
					return "RFU";
				case TriggerEnum.APPLICATION_INITIATION:
					return "APPLICATION_INITIATION";
				default:
					if (enumItem == TriggerEnum.RFU)
					{
						return "RFU";
					}
					return "Invalid Trigger Value";
				}
			}

			public static string TriggerToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = SELECT_WITH_AID");
				stringBuilder.AppendLine("0x1 = RF_PROTOCOL_BASED_ROUTING_DECISION");
				stringBuilder.AppendLine("0x2 = RF_TECHNOLOGY_BASED_ROUTING_DECISION");
				stringBuilder.AppendLine("0x3-0xF = RFU");
				stringBuilder.AppendLine("0x10 = APPLICATION_INITIATION");
				stringBuilder.AppendLine("0x11-0xFF = RFU");
				return stringBuilder.ToString();
			}
		}

		private uint _NfceeId;

		private uint _Trigger;

		private uint _SupportingDataLength;

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

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

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

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

		public List<uint> SupportingDataList => _SupportingDataList;

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

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

		public TriggerEnum GetTriggerAsTriggerEnum()
		{
			return TriggerInfo.ValueToTrigger(_Trigger);
		}

		public void SetTriggerAsTriggerEnum(TriggerEnum enumItem)
		{
			Trigger = TriggerInfo.TriggerToValue(enumItem);
		}
	}

	private const uint _Mt = 3u;

	private const uint _Gid = 1u;

	private const uint _Oid = 9u;

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

	public uint Mt => 3u;

	public uint Pbf => _Pbf;

	public uint Gid => 1u;

	public uint Rfu => _Rfu;

	public uint Oid => 9u;

	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.Trigger = bitBuilder.RemoveBitsAsUint(8);
		UserFields.SupportingDataLength = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.SupportingDataList.Count; i++)
		{
			UserFields.SupportingDataList[i] = 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.NfceeId, 8);
		bitBuilder.AddBitsAsUint(UserFields.Trigger, 8);
		bitBuilder.AddBitsAsUint(UserFields.SupportingDataLength, 8);
		for (int i = 0; i < UserFields.SupportingDataList.Count; i++)
		{
			if (UserFields.SupportingDataList[i] > 255)
			{
				throw new Exception("UserFields.SupportingDataList[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.SupportingDataList[i], 8);
		}
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
