using System;
using QCRPG.Framework;

namespace QCRPG.TSL.NFC.Packets;

public class MemoryAccessRspInfo : PacketInfoBase
{
	public override string Name => "Memory Access Rsp";

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

	public MemoryAccessRspInfo(MemoryAccessRsp packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		MemoryAccessRsp memoryAccessRsp = (MemoryAccessRsp)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("Status", StatusCodesInfo.StatusCodesToDescription(), 8, FormatValue(memoryAccessRsp.UserFields.Status, 8), StatusCodesInfo.StatusCodesToString(StatusCodesInfo.ValueToStatusCodes(memoryAccessRsp.UserFields.Status)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Item Size", "", 8, FormatValue(memoryAccessRsp.UserFields.ItemSize, 8), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Number of Items", "The total number of items (not octets) returned. Will always be zero for a POKE command.", 8, FormatValue(memoryAccessRsp.UserFields.NumberOfItems, 8), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		for (int i = 0; i < memoryAccessRsp.UserFields.DataListOfLists.Count; i++)
		{
			fieldInfoList.Add(new PacketFieldInfo("Data[" + i + "]", "The data read for a PEEK command. Total data size, in octets, must be equal to Number of Items * Item Size. Items larger than a single octet are in Little Endian format.", memoryAccessRsp.UserFields.DataListOfLists[i].Count * 8, FormatValue(memoryAccessRsp.UserFields.DataListOfLists[i], memoryAccessRsp.UserFields.DataListOfLists[i].Count * 8, includeSpaces: false), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		}
	}

	public override void UpdatePacketFromFieldInfo(int index, string value)
	{
		MemoryAccessRsp memoryAccessRsp = (MemoryAccessRsp)packet;
		int num = -1;
		if (index == ++num)
		{
			memoryAccessRsp.UserFields.Status = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			memoryAccessRsp.UserFields.ItemSize = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			memoryAccessRsp.UserFields.NumberOfItems = Convert.ToUInt32(value, 16);
			return;
		}
		for (int i = 0; i < memoryAccessRsp.UserFields.DataListOfLists.Count; i++)
		{
			if (index == ++num)
			{
				value = AddLeadingZerosAndCheckValueStringLength(value, memoryAccessRsp.UserFields.DataListOfLists[i].Count);
				int num2 = 0;
				while (value.Length > 0)
				{
					string value2 = value.Substring(0, 2);
					value = value.Remove(0, 2);
					memoryAccessRsp.UserFields.DataListOfLists[i][num2] = Convert.ToUInt32(value2, 16);
					num2++;
				}
			}
		}
	}
}
