using System;
using QCRPG.Framework;

namespace QCRPG.TSL.FM.Packets;

public class BlockPokeInfo : PacketInfoBase
{
	public override string Name => "Block Poke";

	public override string DisplayGroup => "Test";

	public BlockPokeInfo(BlockPoke packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		BlockPoke blockPoke = (BlockPoke)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("Register Address", RegisterMapInfo.RegisterMapToDescription(), 8, FormatValue(blockPoke.UserFields.RegisterAddress, 8), RegisterMapInfo.RegisterMapToString(RegisterMapInfo.ValueToRegisterMap(blockPoke.UserFields.RegisterAddress)), "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Memory Control", "Write to memory", 1, FormatValue(blockPoke.UserFields.MemoryControl, 1), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Memory Access", "Memory access XFR mode", 1, FormatValue(blockPoke.UserFields.MemoryAccess, 1), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Read Status", "Read only field", 1, FormatValue(blockPoke.UserFields.ReadStatus, 1), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Length", "Length", 4, FormatValue(blockPoke.UserFields.Length, 4), "", "", "E", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Memory Mode", "Block memory access", 1, FormatValue(blockPoke.UserFields.MemoryMode, 1), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Address", "", 16, FormatValue(blockPoke.UserFields.Address, 16), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Data", "", blockPoke.UserFields.DataList.Count * 8, FormatValue(blockPoke.UserFields.DataList, blockPoke.UserFields.DataList.Count * 8, includeSpaces: false), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
	}

	public override void UpdatePacketFromFieldInfo(int index, string value)
	{
		BlockPoke blockPoke = (BlockPoke)packet;
		int num = -1;
		if (index == ++num)
		{
			throw new Exception("Tried to set non-settable field RegisterAddress");
		}
		if (index == ++num)
		{
			throw new Exception("Tried to set non-settable field MemoryControl");
		}
		if (index == ++num)
		{
			throw new Exception("Tried to set non-settable field MemoryAccess");
		}
		if (index == ++num)
		{
			throw new Exception("Tried to set non-settable field ReadStatus");
		}
		if (index == ++num)
		{
			blockPoke.UserFields.Length = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			throw new Exception("Tried to set non-settable field MemoryMode");
		}
		if (index == ++num)
		{
			blockPoke.UserFields.Address = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			value = AddLeadingZerosAndCheckValueStringLength(value, blockPoke.UserFields.DataList.Count);
			int num2 = 0;
			while (value.Length > 0)
			{
				string value2 = value.Substring(0, 2);
				value = value.Remove(0, 2);
				blockPoke.UserFields.DataList[num2] = Convert.ToUInt32(value2, 16);
				num2++;
			}
		}
	}
}
