using System;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class GenericCommandInfo : PacketInfoBase
{
	public override string Name => "Generic Command";

	public override string DisplayGroup => "Generic";

	public GenericCommandInfo(GenericCommand packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		GenericCommand genericCommand = (GenericCommand)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("OpCode", "", 16, FormatValue(genericCommand.UserFields.Opcode, 16), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Payload Length", "Payload length", 8, FormatValue(genericCommand.UserFields.PayloadLength, 8), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Payload", "Payload", genericCommand.UserFields.PayloadList.Count * 8, FormatValue(genericCommand.UserFields.PayloadList, genericCommand.UserFields.PayloadList.Count * 8, includeSpaces: false), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
	}

	public override void UpdatePacketFromFieldInfo(int index, string value)
	{
		GenericCommand genericCommand = (GenericCommand)packet;
		int num = -1;
		if (index == ++num)
		{
			genericCommand.UserFields.Opcode = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			genericCommand.UserFields.PayloadLength = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			value = AddLeadingZerosAndCheckValueStringLength(value, genericCommand.UserFields.PayloadList.Count);
			int num2 = 0;
			while (value.Length > 0)
			{
				string value2 = value.Substring(0, 2);
				value = value.Remove(0, 2);
				genericCommand.UserFields.PayloadList[num2] = Convert.ToUInt32(value2, 16);
				num2++;
			}
		}
	}
}
