using System;
using QCRPG.Framework;

namespace QCRPG.TSL.FMHCI.Packets;

public class PokeWordInfo : PacketInfoBase
{
	public override string Name => "Poke Word";

	public override string DisplayGroup => "Diagnostic";

	public PokeWordInfo(PokeWord packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		PokeWord pokeWord = (PokeWord)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("Sub OpCode", "Poke word data", 8, FormatValue(pokeWord.UserFields.SubOpcode, 8), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Address", "Memory address", 32, FormatValue(pokeWord.UserFields.Address, 32), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Data Length", "Length of data", 8, FormatValue(pokeWord.UserFields.DataLength, 8), "", "", "F4", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Data", "Data to write to hardware", pokeWord.UserFields.DataList.Count * 8, FormatValue(pokeWord.UserFields.DataList, pokeWord.UserFields.DataList.Count * 8, includeSpaces: false), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
	}

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