using System;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class RamDumpFragmentInfo : PacketInfoBase
{
	public override string Name => "Ram Dump Fragment";

	public override string DisplayGroup => "Vendor Specific";

	public RamDumpFragmentInfo(RamDumpFragment packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		RamDumpFragment ramDumpFragment = (RamDumpFragment)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("Sub Event Code", "", 8, FormatValue(ramDumpFragment.UserFields.SubEventCode, 8), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Message Type", "", 8, FormatValue(ramDumpFragment.UserFields.MessageType, 8), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		if (ramDumpFragment.UserFields.SequenceNumber == 65535)
		{
			fieldInfoList.Add(new PacketFieldInfo("Sequence Number", RamDumpFragment.UserFieldsWrapper.SequenceNumberInfo.SequenceNumberToDescription(), 16, FormatValue(ramDumpFragment.UserFields.SequenceNumber, 16), RamDumpFragment.UserFieldsWrapper.SequenceNumberInfo.SequenceNumberToString(RamDumpFragment.UserFieldsWrapper.SequenceNumberInfo.ValueToSequenceNumber(ramDumpFragment.UserFields.SequenceNumber)), "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		}
		else
		{
			fieldInfoList.Add(new PacketFieldInfo("Sequence Number", "", 16, FormatValue(ramDumpFragment.UserFields.SequenceNumber, 16), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		}
		fieldInfoList.Add(new PacketFieldInfo("Reserved", "", 8, FormatValue(ramDumpFragment.UserFields.Reserved, 8), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Total Length", "Total length of RAM Dump in bytes", ramDumpFragment.UserFields.TotalLengthList.Count * 8, FormatValue(ramDumpFragment.UserFields.TotalLengthList, ramDumpFragment.UserFields.TotalLengthList.Count * 8, includeSpaces: false), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Dump Fragment Length", "Length of RAM Dump fragment in bytes", 8, FormatValue(ramDumpFragment.UserFields.DumpFragmentLength, 8), "", "01", "F8", settable: true, littleEndianTransport: false, omitFromTransport: true, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Dump Fragment", "RAM Dump fragment", ramDumpFragment.UserFields.DumpFragmentList.Count * 8, FormatValue(ramDumpFragment.UserFields.DumpFragmentList, ramDumpFragment.UserFields.DumpFragmentList.Count * 8, includeSpaces: false), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
	}

	public override void UpdatePacketFromFieldInfo(int index, string value)
	{
		RamDumpFragment ramDumpFragment = (RamDumpFragment)packet;
		int num = -1;
		if (index == ++num)
		{
			throw new Exception("Tried to set non-settable field SubEventCode");
		}
		if (index == ++num)
		{
			throw new Exception("Tried to set non-settable field MessageType");
		}
		if (index == ++num)
		{
			ramDumpFragment.UserFields.SequenceNumber = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			throw new Exception("Tried to set non-settable field Reserved");
		}
		if (index == ++num)
		{
			value = AddLeadingZerosAndCheckValueStringLength(value, ramDumpFragment.UserFields.TotalLengthList.Count);
			int num2 = 0;
			while (value.Length > 0)
			{
				string value2 = value.Substring(0, 2);
				value = value.Remove(0, 2);
				ramDumpFragment.UserFields.TotalLengthList[num2] = Convert.ToUInt32(value2, 16);
				num2++;
			}
		}
		if (index == ++num)
		{
			ramDumpFragment.UserFields.DumpFragmentLength = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			value = AddLeadingZerosAndCheckValueStringLength(value, ramDumpFragment.UserFields.DumpFragmentList.Count);
			int num3 = 0;
			while (value.Length > 0)
			{
				string value3 = value.Substring(0, 2);
				value = value.Remove(0, 2);
				ramDumpFragment.UserFields.DumpFragmentList[num3] = Convert.ToUInt32(value3, 16);
				num3++;
			}
		}
	}
}
