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

namespace QCRPG.TSL.NFC.Packets;

public class RfDiscoverNtf : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum NotificationTypeEnum
		{
			LAST_NOTIFICATION,
			LAST_NOTIFICATION_DUE_TO_RESOURCE_LIMIT,
			MORE_NOTIFICATIONS_TO_FOLLOW,
			RFU,
			InvalidNotificationTypeValue
		}

		public static class NotificationTypeInfo
		{
			public static NotificationTypeEnum ValueToNotificationType(uint value)
			{
				switch (value)
				{
				case 0u:
					return NotificationTypeEnum.LAST_NOTIFICATION;
				case 1u:
					return NotificationTypeEnum.LAST_NOTIFICATION_DUE_TO_RESOURCE_LIMIT;
				case 2u:
					return NotificationTypeEnum.MORE_NOTIFICATIONS_TO_FOLLOW;
				default:
					if (value >= 3 && value <= 255)
					{
						return NotificationTypeEnum.RFU;
					}
					return NotificationTypeEnum.InvalidNotificationTypeValue;
				}
			}

			public static uint NotificationTypeToValue(NotificationTypeEnum enumItem)
			{
				return enumItem switch
				{
					NotificationTypeEnum.LAST_NOTIFICATION => 0u, 
					NotificationTypeEnum.LAST_NOTIFICATION_DUE_TO_RESOURCE_LIMIT => 1u, 
					NotificationTypeEnum.MORE_NOTIFICATIONS_TO_FOLLOW => 2u, 
					NotificationTypeEnum.RFU => 3u, 
					_ => uint.MaxValue, 
				};
			}

			public static string NotificationTypeToString(NotificationTypeEnum enumItem)
			{
				return enumItem switch
				{
					NotificationTypeEnum.LAST_NOTIFICATION => "LAST_NOTIFICATION", 
					NotificationTypeEnum.LAST_NOTIFICATION_DUE_TO_RESOURCE_LIMIT => "LAST_NOTIFICATION_DUE_TO_RESOURCE_LIMIT", 
					NotificationTypeEnum.MORE_NOTIFICATIONS_TO_FOLLOW => "MORE_NOTIFICATIONS_TO_FOLLOW", 
					NotificationTypeEnum.RFU => "RFU", 
					_ => "Invalid NotificationType Value", 
				};
			}

			public static string NotificationTypeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = LAST_NOTIFICATION");
				stringBuilder.AppendLine("0x1 = LAST_NOTIFICATION_DUE_TO_RESOURCE_LIMIT");
				stringBuilder.AppendLine("0x2 = MORE_NOTIFICATIONS_TO_FOLLOW");
				stringBuilder.AppendLine("0x3-0xFF = RFU");
				return stringBuilder.ToString();
			}
		}

		private uint _RfDiscoveryId;

		private uint _RfProtocol;

		private uint _RfTechnologyAndMode;

		private uint _LengthOfRfTechnologySpecificParameters;

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

		private uint _NotificationType;

		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 RfTechnologyAndMode
		{
			get
			{
				return _RfTechnologyAndMode;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for RfTechnologyAndMode is 0xFF");
				}
				_RfTechnologyAndMode = value;
			}
		}

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

		public List<uint> RfTechnologySpecificParametersList => _RfTechnologySpecificParametersList;

		public uint NotificationType
		{
			get
			{
				return _NotificationType;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for NotificationType is 0xFF");
				}
				_NotificationType = 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 RfTechnologyAndModeEnum GetRfTechnologyAndModeAsRfTechnologyAndModeEnum()
		{
			return RfTechnologyAndModeInfo.ValueToRfTechnologyAndMode(_RfTechnologyAndMode);
		}

		public void SetRfTechnologyAndModeAsRfTechnologyAndModeEnum(RfTechnologyAndModeEnum enumItem)
		{
			RfTechnologyAndMode = RfTechnologyAndModeInfo.RfTechnologyAndModeToValue(enumItem);
		}

		public NotificationTypeEnum GetNotificationTypeAsNotificationTypeEnum()
		{
			return NotificationTypeInfo.ValueToNotificationType(_NotificationType);
		}

		public void SetNotificationTypeAsNotificationTypeEnum(NotificationTypeEnum enumItem)
		{
			NotificationType = NotificationTypeInfo.NotificationTypeToValue(enumItem);
		}
	}

	private const uint _Mt = 3u;

	private const uint _Gid = 1u;

	private const uint _Oid = 3u;

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

	public uint Mt => 3u;

	public uint Pbf => _Pbf;

	public uint Gid => 1u;

	public uint Rfu => _Rfu;

	public uint Oid => 3u;

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