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

namespace QCRPG.TSL.NFC.Packets;

public class RfParameterUpdateCmd : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _NumberOfParameters;

		private List<RfCommunicationParameterSubField> _RfCommunicationParameterList = new List<RfCommunicationParameterSubField>();

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

		public List<RfCommunicationParameterSubField> RfCommunicationParameterList => _RfCommunicationParameterList;
	}

	public class RfCommunicationParameterSubField
	{
		public enum IdEnum
		{
			RF_TECHNOLOGY_AND_MODE,
			TRANSMIT_BIT_RATE,
			RECEIVE_BIT_RATE,
			NFCB_DATA_EXCHANGE_CONFIGURATION,
			RFU,
			PROPRIETARY,
			InvalidIdValue
		}

		public static class IdInfo
		{
			public static IdEnum ValueToId(uint value)
			{
				switch (value)
				{
				case 0u:
					return IdEnum.RF_TECHNOLOGY_AND_MODE;
				case 1u:
					return IdEnum.TRANSMIT_BIT_RATE;
				case 2u:
					return IdEnum.RECEIVE_BIT_RATE;
				case 3u:
					return IdEnum.NFCB_DATA_EXCHANGE_CONFIGURATION;
				default:
					if (value >= 4 && value <= 127)
					{
						return IdEnum.RFU;
					}
					if (value >= 128 && value <= 255)
					{
						return IdEnum.PROPRIETARY;
					}
					return IdEnum.InvalidIdValue;
				}
			}

			public static uint IdToValue(IdEnum enumItem)
			{
				return enumItem switch
				{
					IdEnum.RF_TECHNOLOGY_AND_MODE => 0u, 
					IdEnum.TRANSMIT_BIT_RATE => 1u, 
					IdEnum.RECEIVE_BIT_RATE => 2u, 
					IdEnum.NFCB_DATA_EXCHANGE_CONFIGURATION => 3u, 
					IdEnum.RFU => 4u, 
					IdEnum.PROPRIETARY => 128u, 
					_ => uint.MaxValue, 
				};
			}

			public static string IdToString(IdEnum enumItem)
			{
				return enumItem switch
				{
					IdEnum.RF_TECHNOLOGY_AND_MODE => "RF Technology And Mode", 
					IdEnum.TRANSMIT_BIT_RATE => "Transmit Bit Rate", 
					IdEnum.RECEIVE_BIT_RATE => "Receive Bit Rate", 
					IdEnum.NFCB_DATA_EXCHANGE_CONFIGURATION => "NFC-B Data Exchange Configuration", 
					IdEnum.RFU => "RFU", 
					IdEnum.PROPRIETARY => "PROPRIETARY", 
					_ => "Invalid Id Value", 
				};
			}

			public static string IdToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = RF Technology And Mode");
				stringBuilder.AppendLine("0x1 = Transmit Bit Rate");
				stringBuilder.AppendLine("0x2 = Receive Bit Rate");
				stringBuilder.AppendLine("0x3 = NFC-B Data Exchange Configuration");
				stringBuilder.AppendLine("0x4-0x7F = RFU");
				stringBuilder.AppendLine("0x80-0xFF = PROPRIETARY");
				return stringBuilder.ToString();
			}
		}

		private uint _Id;

		private uint _Length;

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

		public uint Id
		{
			get
			{
				return _Id;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for Id is 0xFF");
				}
				_Id = 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 void SetDefaultValues()
		{
			Length = 1u;
		}

		public IdEnum GetIdAsIdEnum()
		{
			return IdInfo.ValueToId(_Id);
		}

		public void SetIdAsIdEnum(IdEnum enumItem)
		{
			Id = IdInfo.IdToValue(enumItem);
		}

		public RfTechnologyAndModeEnum GetValueAsRfTechnologyAndModeEnum(int index)
		{
			if (_Id != 0)
			{
				throw new Exception("ID is not 0x0, can not get Value as RfTechnologyAndModeEnum");
			}
			return RfTechnologyAndModeInfo.ValueToRfTechnologyAndMode(_ValueList[index]);
		}

		public void SetValueAsRfTechnologyAndModeEnum(int index, RfTechnologyAndModeEnum enumItem)
		{
			if (_Id != 0)
			{
				throw new Exception("ID is not 0x0, can not set Value as RfTechnologyAndModeEnum");
			}
			_ValueList[index] = RfTechnologyAndModeInfo.RfTechnologyAndModeToValue(enumItem);
		}

		public BitRatesEnum GetValueAsBitRatesEnum(int index)
		{
			if (_Id < 1 || _Id > 2)
			{
				throw new Exception("ID is not in the range 0x1-0x2, can not get Value as BitRatesEnum");
			}
			return BitRatesInfo.ValueToBitRates(_ValueList[index]);
		}

		public void SetValueAsBitRatesEnum(int index, BitRatesEnum enumItem)
		{
			if (_Id < 1 || _Id > 2)
			{
				throw new Exception("ID is not in the range 0x1-0x2, can not set Value as BitRatesEnum");
			}
			_ValueList[index] = BitRatesInfo.BitRatesToValue(enumItem);
		}

		public NfcbDataExchangeConfigurationEnumCollection GetValueAsNfcbDataExchangeConfigurationEnumCollection(int index)
		{
			if (_Id != 3)
			{
				throw new Exception("ID is not 0x3, can not get Value as NfcbDataExchangeConfigurationEnumCollection");
			}
			return NfcbDataExchangeConfigurationInfo.ValueToNfcbDataExchangeConfiguration(_ValueList[index]);
		}

		public void SetValueAsNfcbDataExchangeConfigurationEnumCollection(int index, NfcbDataExchangeConfigurationEnumCollection enumCollection)
		{
			if (_Id != 3)
			{
				throw new Exception("ID is not 0x3, can not set Value as NfcbDataExchangeConfigurationEnumCollection");
			}
			_ValueList[index] = NfcbDataExchangeConfigurationInfo.NfcbDataExchangeConfigurationToValue(enumCollection);
		}

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			Id = 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(Id, 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 = 1u;

	private const uint _Gid = 1u;

	private const uint _Oid = 11u;

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

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

	public uint Mt => 1u;

	public uint Pbf => _Pbf;

	public uint Gid => 1u;

	public uint Rfu => _Rfu;

	public uint Oid => 11u;

	public uint PayloadLength
	{
		get
		{
			ToBitString();
			return _PayloadLength;
		}
	}

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.NumberOfParameters = 1u;
		for (int i = 0; i < UserFields.RfCommunicationParameterList.Count; i++)
		{
			UserFields.RfCommunicationParameterList[i].SetDefaultValues();
		}
	}

	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.NumberOfParameters = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.RfCommunicationParameterList.Count; i++)
		{
			UserFields.RfCommunicationParameterList[i].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.NumberOfParameters, 8);
		for (int i = 0; i < UserFields.RfCommunicationParameterList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.RfCommunicationParameterList[i].ToBitString());
		}
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
