using System;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class EdlReadPsResponseInfo : PacketInfoBase
{
	public override string Name => "Edl Read Ps Response";

	public override string DisplayGroup => "Vendor Specific";

	public EdlReadPsResponseInfo(EdlReadPsResponse packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		EdlReadPsResponse edlReadPsResponse = (EdlReadPsResponse)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("Vendor Download Response", "HCI_DOWNLOAD_EVENT_CODE", 8, FormatValue(edlReadPsResponse.UserFields.VendorDownloadResponse, 8), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Sub OpCode", "EDL_READ_PS_RESPONSE", 8, FormatValue(edlReadPsResponse.UserFields.SubOpcode, 8), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Status", "EDL Status Info\r\n\r\n" + EdlErrorCodeInfo.EdlErrorCodeToDescription(), 8, FormatValue(edlReadPsResponse.UserFields.Status, 8), EdlErrorCodeInfo.EdlErrorCodeToString(EdlErrorCodeInfo.ValueToEdlErrorCode(edlReadPsResponse.UserFields.Status)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("PS Type", "0x00 = OTP; 0x01 = flash/eeprom", 8, FormatValue(edlReadPsResponse.UserFields.PsType, 8), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Offset", "PS offset", 16, FormatValue(edlReadPsResponse.UserFields.Offset, 16), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Data Length", "Length of PS data", 8, FormatValue(edlReadPsResponse.UserFields.DataLength, 8), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Data", "PS data value", edlReadPsResponse.UserFields.DataList.Count * 8, FormatValue(edlReadPsResponse.UserFields.DataList, edlReadPsResponse.UserFields.DataList.Count * 8, includeSpaces: false), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
	}

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