using System;
using QCRPG.Framework;

namespace QCRPG.TSL.FMHCI.Packets;

public class CommandCompleteForPeekByteInfo : PacketInfoBase
{
	public override string Name => "Command Complete For Peek Byte";

	public override string DisplayGroup => "Diagnostic";

	public CommandCompleteForPeekByteInfo(CommandCompleteForPeekByte packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		CommandCompleteForPeekByte commandCompleteForPeekByte = (CommandCompleteForPeekByte)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("Num HCI Command Packets", "", 8, FormatValue(commandCompleteForPeekByte.UserFields.NumHciCommandPackets, 8), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Command OpCode", CommandOpcodeInfo.CommandOpcodeToDescription(), 16, FormatValue(commandCompleteForPeekByte.UserFields.CommandOpcode, 16), CommandOpcodeInfo.CommandOpcodeToString(CommandOpcodeInfo.ValueToCommandOpcode(commandCompleteForPeekByte.UserFields.CommandOpcode)), "", "", settable: false, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Status", ErrorCodeInfo.ErrorCodeToDescription(), 8, FormatValue(commandCompleteForPeekByte.UserFields.Status, 8), ErrorCodeInfo.ErrorCodeToString(ErrorCodeInfo.ValueToErrorCode(commandCompleteForPeekByte.UserFields.Status)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Sub OpCode", "Peek byte data", 8, FormatValue(commandCompleteForPeekByte.UserFields.SubOpcode, 8), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Address", "Memory address", 32, FormatValue(commandCompleteForPeekByte.UserFields.Address, 32), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Return Data Length", "Length of data", 8, FormatValue(commandCompleteForPeekByte.UserFields.ReturnDataLength, 8), "", "", "F4", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Data", "Data read from hardware", commandCompleteForPeekByte.UserFields.DataList.Count * 8, FormatValue(commandCompleteForPeekByte.UserFields.DataList, commandCompleteForPeekByte.UserFields.DataList.Count * 8, includeSpaces: false), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
	}

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