using System;
using QCRPG.Framework;

namespace QCRPG.TSL.NFC.Packets;

public class MemoryAccessPokeCmdInfo : PacketInfoBase
{
	public override string Name => "Memory Access Poke Cmd";

	public override string DisplayGroup => "Proprietary/Test";

	public MemoryAccessPokeCmdInfo(MemoryAccessPokeCmd packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		MemoryAccessPokeCmd memoryAccessPokeCmd = (MemoryAccessPokeCmd)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("Access Type", "", 1, FormatValue(memoryAccessPokeCmd.UserFields.AccessType, 1), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("RFU2", "", 5, FormatValue(memoryAccessPokeCmd.UserFields.Rfu2, 5), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Item Size", MemoryAccessItemSizeInfo.MemoryAccessItemSizeToDescription(), 2, FormatValue(memoryAccessPokeCmd.UserFields.ItemSize, 2), MemoryAccessItemSizeInfo.MemoryAccessItemSizeToString(MemoryAccessItemSizeInfo.ValueToMemoryAccessItemSize(memoryAccessPokeCmd.UserFields.ItemSize)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Start Address", "The physical address of the start of the first item to be accessed. This must be correctly aligned for the Item Size.", 32, FormatValue(memoryAccessPokeCmd.UserFields.StartAddress, 32), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Number Of Items", "The total number of items (not octets) to access. The size of each item, in octets, is defined in the Access Flags field.", 8, FormatValue(memoryAccessPokeCmd.UserFields.NumberOfItems, 8), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Address Delta", "The change to be applied to the Physical Address between successive item accesses, expressed as items (not octets). The size of each item, in octets, is defined in the Access Flags field.", 8, FormatValue(memoryAccessPokeCmd.UserFields.AddressDelta, 8), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Access Delay", "The delay between successive item accesses. Nominally milliseconds, but accuracy and granularity depends on underlying operating system and hardware.", 16, FormatValue(memoryAccessPokeCmd.UserFields.AccessDelay, 16), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		for (int i = 0; i < memoryAccessPokeCmd.UserFields.DataListOfLists.Count; i++)
		{
			fieldInfoList.Add(new PacketFieldInfo("Data[" + i + "]", "", memoryAccessPokeCmd.UserFields.DataListOfLists[i].Count * 8, FormatValue(memoryAccessPokeCmd.UserFields.DataListOfLists[i], memoryAccessPokeCmd.UserFields.DataListOfLists[i].Count * 8, includeSpaces: false), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		}
	}

	public override void UpdatePacketFromFieldInfo(int index, string value)
	{
		MemoryAccessPokeCmd memoryAccessPokeCmd = (MemoryAccessPokeCmd)packet;
		int num = -1;
		if (index == ++num)
		{
			throw new Exception("Tried to set non-settable field AccessType");
		}
		if (index == ++num)
		{
			throw new Exception("Tried to set non-settable field Rfu2");
		}
		if (index == ++num)
		{
			memoryAccessPokeCmd.UserFields.ItemSize = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			memoryAccessPokeCmd.UserFields.StartAddress = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			memoryAccessPokeCmd.UserFields.NumberOfItems = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			memoryAccessPokeCmd.UserFields.AddressDelta = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			memoryAccessPokeCmd.UserFields.AccessDelay = Convert.ToUInt32(value, 16);
			return;
		}
		for (int i = 0; i < memoryAccessPokeCmd.UserFields.DataListOfLists.Count; i++)
		{
			if (index == ++num)
			{
				value = AddLeadingZerosAndCheckValueStringLength(value, memoryAccessPokeCmd.UserFields.DataListOfLists[i].Count);
				int num2 = 0;
				while (value.Length > 0)
				{
					string value2 = value.Substring(0, 2);
					value = value.Remove(0, 2);
					memoryAccessPokeCmd.UserFields.DataListOfLists[i][num2] = Convert.ToUInt32(value2, 16);
					num2++;
				}
			}
		}
	}
}
