using System;
using QCRPG.Framework;

namespace QCRPG.TSL.FM.Packets;

public class MultiplePokeInfo : PacketInfoBase
{
	public override string Name => "Multiple Poke";

	public override string DisplayGroup => "Test";

	public MultiplePokeInfo(MultiplePoke packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		MultiplePoke multiplePoke = (MultiplePoke)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("Register Address", RegisterMapInfo.RegisterMapToDescription(), 8, FormatValue(multiplePoke.UserFields.RegisterAddress, 8), RegisterMapInfo.RegisterMapToString(RegisterMapInfo.ValueToRegisterMap(multiplePoke.UserFields.RegisterAddress)), "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Memory Control", "Write to memory", 1, FormatValue(multiplePoke.UserFields.MemoryControl, 1), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Memory Access", "Memory access XFR mode", 1, FormatValue(multiplePoke.UserFields.MemoryAccess, 1), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Read Status", "Read only field", 1, FormatValue(multiplePoke.UserFields.ReadStatus, 1), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Length", "Length", 4, FormatValue(multiplePoke.UserFields.Length, 4), "", "", "5", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Memory Mode", "Multiple memory access", 1, FormatValue(multiplePoke.UserFields.MemoryMode, 1), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		for (int i = 0; i < multiplePoke.UserFields.PokeInfoList.Count; i++)
		{
			fieldInfoList.Add(new PacketFieldInfo("Address[" + i + "]", "", 16, FormatValue(multiplePoke.UserFields.PokeInfoList[i].Address, 16), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("Data[" + i + "]", "", 8, FormatValue(multiplePoke.UserFields.PokeInfoList[i].Data, 8), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		}
	}

	public override void UpdatePacketFromFieldInfo(int index, string value)
	{
		MultiplePoke multiplePoke = (MultiplePoke)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)
		{
			multiplePoke.UserFields.Length = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			throw new Exception("Tried to set non-settable field MemoryMode");
		}
		for (int i = 0; i < multiplePoke.UserFields.PokeInfoList.Count; i++)
		{
			if (index == ++num)
			{
				multiplePoke.UserFields.PokeInfoList[i].Address = Convert.ToUInt32(value, 16);
				break;
			}
			if (index == ++num)
			{
				multiplePoke.UserFields.PokeInfoList[i].Data = Convert.ToUInt32(value, 16);
				break;
			}
		}
	}
}
