using System;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class PersistAccessOtpSetInfo : PacketInfoBase
{
	public override string Name => "Persist Access Otp Set";

	public override string DisplayGroup => "Vendor Specific";

	public PersistAccessOtpSetInfo(PersistAccessOtpSet packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		PersistAccessOtpSet persistAccessOtpSet = (PersistAccessOtpSet)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("Sub-Opcode", "", 8, FormatValue(persistAccessOtpSet.UserFields.SubOpcode, 8), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("CFG_TYPE", CfgTypeOtpAccessInfo.CfgTypeOtpAccessToDescription(), 8, FormatValue(persistAccessOtpSet.UserFields.CfgType, 8), CfgTypeOtpAccessInfo.CfgTypeOtpAccessToString(CfgTypeOtpAccessInfo.ValueToCfgTypeOtpAccess(persistAccessOtpSet.UserFields.CfgType)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("CFG_LEN", "", 8, FormatValue(persistAccessOtpSet.UserFields.CfgLen, 8), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("CFG_DATA", "", persistAccessOtpSet.UserFields.CfgDataList.Count * 8, FormatValue(persistAccessOtpSet.UserFields.CfgDataList, persistAccessOtpSet.UserFields.CfgDataList.Count * 8, includeSpaces: false), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
	}

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