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

namespace QCRPG.TSL.NFCCustom.Packets;

public class NfcFPollModeRfSpecificParameters : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum BitRateEnum
		{
			RFU,
			BIT_RATE_212KBPS,
			BIT_RATE_424KBPS,
			InvalidBitRateValue
		}

		public static class BitRateInfo
		{
			public static BitRateEnum ValueToBitRate(uint value)
			{
				switch (value)
				{
				case 0u:
					return BitRateEnum.RFU;
				case 1u:
					return BitRateEnum.BIT_RATE_212KBPS;
				case 2u:
					return BitRateEnum.BIT_RATE_424KBPS;
				default:
					if (value >= 3 && value <= 255)
					{
						return BitRateEnum.RFU;
					}
					return BitRateEnum.InvalidBitRateValue;
				}
			}

			public static uint BitRateToValue(BitRateEnum enumItem)
			{
				switch (enumItem)
				{
				case BitRateEnum.RFU:
					return 0u;
				case BitRateEnum.BIT_RATE_212KBPS:
					return 1u;
				case BitRateEnum.BIT_RATE_424KBPS:
					return 2u;
				default:
					if (enumItem == BitRateEnum.RFU)
					{
						return 3u;
					}
					return uint.MaxValue;
				}
			}

			public static string BitRateToString(BitRateEnum enumItem)
			{
				switch (enumItem)
				{
				case BitRateEnum.RFU:
					return "RFU";
				case BitRateEnum.BIT_RATE_212KBPS:
					return "212 kbps";
				case BitRateEnum.BIT_RATE_424KBPS:
					return "424 kbps";
				default:
					if (enumItem == BitRateEnum.RFU)
					{
						return "RFU";
					}
					return "Invalid BitRate Value";
				}
			}

			public static string BitRateToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = RFU");
				stringBuilder.AppendLine("0x1 = 212 kbps");
				stringBuilder.AppendLine("0x2 = 424 kbps");
				stringBuilder.AppendLine("0x3-0xFF = RFU");
				return stringBuilder.ToString();
			}
		}

		private uint _BitRate;

		private uint _SensfResResponseLength;

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

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

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

		public List<uint> SensfResResponseList => _SensfResResponseList;

		public BitRateEnum GetBitRateAsBitRateEnum()
		{
			return BitRateInfo.ValueToBitRate(_BitRate);
		}

		public void SetBitRateAsBitRateEnum(BitRateEnum enumItem)
		{
			BitRate = BitRateInfo.BitRateToValue(enumItem);
		}
	}

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

	private PacketInfoBase info;

	public UserFieldsWrapper _UserFields = new UserFieldsWrapper();

	public override PacketType Type => PacketType.Data;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

	public override PacketInfoBase Info
	{
		get
		{
			if (info == null)
			{
				info = new NfcFPollModeRfSpecificParametersInfo(this);
			}
			return info;
		}
	}

	public UserFieldsWrapper UserFields => _UserFields;

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		UserFields.BitRate = bitBuilder.RemoveBitsAsUint(8);
		UserFields.SensfResResponseLength = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.SensfResResponseList.Count; i++)
		{
			UserFields.SensfResResponseList[i] = bitBuilder.RemoveBitsAsUint(8);
		}
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUint(UserFields.BitRate, 8);
		bitBuilder.AddBitsAsUint(UserFields.SensfResResponseLength, 8);
		for (int i = 0; i < UserFields.SensfResResponseList.Count; i++)
		{
			if (UserFields.SensfResResponseList[i] > 255)
			{
				throw new Exception("UserFields.SensfResResponseList[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.SensfResResponseList[i], 8);
		}
		return bitBuilder.ToString();
	}
}
