using System;
using QCRPG.Framework;

namespace QCRPG.TSL.FMHCI.Packets;

public class MultiPokeWordsInfo : PacketInfoBase
{
	public override string Name => "Multi Poke Words";

	public override string DisplayGroup => "Diagnostic";

	public MultiPokeWordsInfo(MultiPokeWords packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		MultiPokeWords multiPokeWords = (MultiPokeWords)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("Sub OpCode", "Multi-poke word data", 8, FormatValue(multiPokeWords.UserFields.SubOpcode, 8), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Number Of Words", "Number of words to write", 8, FormatValue(multiPokeWords.UserFields.NumberOfWords, 8), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		for (int i = 0; i < multiPokeWords.UserFields.WordsList.Count; i++)
		{
			fieldInfoList.Add(new PacketFieldInfo("Address[" + i + "]", "Memory address", 32, FormatValue(multiPokeWords.UserFields.WordsList[i].Address, 32), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("Data Length[" + i + "]", "Length of data", 8, FormatValue(multiPokeWords.UserFields.WordsList[i].DataLength, 8), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("Data[" + i + "]", "Data to write to hardware", 32, FormatValue(multiPokeWords.UserFields.WordsList[i].Data, 32), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		}
	}

	public override void UpdatePacketFromFieldInfo(int index, string value)
	{
		MultiPokeWords multiPokeWords = (MultiPokeWords)packet;
		int num = -1;
		if (index == ++num)
		{
			throw new Exception("Tried to set non-settable field SubOpcode");
		}
		if (index == ++num)
		{
			multiPokeWords.UserFields.NumberOfWords = Convert.ToUInt32(value, 16);
			return;
		}
		for (int i = 0; i < multiPokeWords.UserFields.WordsList.Count; i++)
		{
			if (index == ++num)
			{
				multiPokeWords.UserFields.WordsList[i].Address = Convert.ToUInt32(value, 16);
				break;
			}
			if (index == ++num)
			{
				throw new Exception("Tried to set non-settable field DataLength");
			}
			if (index == ++num)
			{
				multiPokeWords.UserFields.WordsList[i].Data = Convert.ToUInt32(value, 16);
				break;
			}
		}
	}
}
