using System;
using QCRPG.Framework;

namespace QCRPG.TSL.NFC.Packets;

public class CoreSetConfigCmdInfo : PacketInfoBase
{
	public override string Name => "Core Set Config Cmd";

	public override string DisplayGroup => "NCI Core";

	public CoreSetConfigCmdInfo(CoreSetConfigCmd packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		CoreSetConfigCmd coreSetConfigCmd = (CoreSetConfigCmd)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("Number Of Parameters", "The number of Parameter fields to follow (n).", 8, FormatValue(coreSetConfigCmd.UserFields.NumberOfParameters, 8), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		for (int i = 0; i < coreSetConfigCmd.UserFields.ParameterList.Count; i++)
		{
			fieldInfoList.Add(new PacketFieldInfo("ID[" + i + "]", ConfigurationParameterTagsInfo.ConfigurationParameterTagsToDescription(), 8, FormatValue(coreSetConfigCmd.UserFields.ParameterList[i].Id, 8), ConfigurationParameterTagsInfo.ConfigurationParameterTagsToString(ConfigurationParameterTagsInfo.ValueToConfigurationParameterTags(coreSetConfigCmd.UserFields.ParameterList[i].Id)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("Len[" + i + "]", "The length of Val (m). If Len is set to 0x00, then the Val field is omitted, and the NFCC SHALL set the configuration parameter to its default value.", 8, FormatValue(coreSetConfigCmd.UserFields.ParameterList[i].Len, 8), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("Val[" + i + "]", "The value of the configuration parameter.", coreSetConfigCmd.UserFields.ParameterList[i].ValList.Count * 8, FormatValue(coreSetConfigCmd.UserFields.ParameterList[i].ValList, coreSetConfigCmd.UserFields.ParameterList[i].ValList.Count * 8, includeSpaces: false), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		}
	}

	public override void UpdatePacketFromFieldInfo(int index, string value)
	{
		CoreSetConfigCmd coreSetConfigCmd = (CoreSetConfigCmd)packet;
		int num = -1;
		if (index == ++num)
		{
			coreSetConfigCmd.UserFields.NumberOfParameters = Convert.ToUInt32(value, 16);
			return;
		}
		for (int i = 0; i < coreSetConfigCmd.UserFields.ParameterList.Count; i++)
		{
			if (index == ++num)
			{
				coreSetConfigCmd.UserFields.ParameterList[i].Id = Convert.ToUInt32(value, 16);
				break;
			}
			if (index == ++num)
			{
				coreSetConfigCmd.UserFields.ParameterList[i].Len = Convert.ToUInt32(value, 16);
				break;
			}
			if (index == ++num)
			{
				value = AddLeadingZerosAndCheckValueStringLength(value, coreSetConfigCmd.UserFields.ParameterList[i].ValList.Count);
				int num2 = 0;
				while (value.Length > 0)
				{
					string value2 = value.Substring(0, 2);
					value = value.Remove(0, 2);
					coreSetConfigCmd.UserFields.ParameterList[i].ValList[num2] = Convert.ToUInt32(value2, 16);
					num2++;
				}
			}
		}
	}
}
