using System;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class HogpAddDeviceInfoInfo : PacketInfoBase
{
	public override string Name => "Hogp Add Device Info";

	public override string DisplayGroup => "Vendor Specific";

	public HogpAddDeviceInfoInfo(HogpAddDeviceInfo packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		HogpAddDeviceInfo hogpAddDeviceInfo = (HogpAddDeviceInfo)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("Sub OpCode", "HCI_VSC_HOGP_ADD_DEVICE_INFO", 8, FormatValue(hogpAddDeviceInfo.UserFields.SubOpcode, 8), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Address", "", 48, FormatValue(hogpAddDeviceInfo.UserFields.Address, 48), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("AddrType", "", 8, FormatValue(hogpAddDeviceInfo.UserFields.Addrtype, 8), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("LTK", "", hogpAddDeviceInfo.UserFields.LtkList.Count * 8, FormatValue(hogpAddDeviceInfo.UserFields.LtkList, hogpAddDeviceInfo.UserFields.LtkList.Count * 8, includeSpaces: false), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Rand", "", 64, FormatValue(hogpAddDeviceInfo.UserFields.Rand, 64), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("IRK", "", hogpAddDeviceInfo.UserFields.IrkList.Count * 8, FormatValue(hogpAddDeviceInfo.UserFields.IrkList, hogpAddDeviceInfo.UserFields.IrkList.Count * 8, includeSpaces: false), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Ediv", "", 16, FormatValue(hogpAddDeviceInfo.UserFields.Ediv, 16), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
	}

	public override void UpdatePacketFromFieldInfo(int index, string value)
	{
		HogpAddDeviceInfo hogpAddDeviceInfo = (HogpAddDeviceInfo)packet;
		int num = -1;
		if (index == ++num)
		{
			throw new Exception("Tried to set non-settable field SubOpcode");
		}
		if (index == ++num)
		{
			hogpAddDeviceInfo.UserFields.Address = Convert.ToUInt64(value, 16);
			return;
		}
		if (index == ++num)
		{
			hogpAddDeviceInfo.UserFields.Addrtype = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			value = AddLeadingZerosAndCheckValueStringLength(value, hogpAddDeviceInfo.UserFields.LtkList.Count);
			int num2 = 0;
			while (value.Length > 0)
			{
				string value2 = value.Substring(0, 2);
				value = value.Remove(0, 2);
				hogpAddDeviceInfo.UserFields.LtkList[num2] = Convert.ToUInt32(value2, 16);
				num2++;
			}
		}
		if (index == ++num)
		{
			hogpAddDeviceInfo.UserFields.Rand = Convert.ToUInt64(value, 16);
			return;
		}
		if (index == ++num)
		{
			value = AddLeadingZerosAndCheckValueStringLength(value, hogpAddDeviceInfo.UserFields.IrkList.Count);
			int num3 = 0;
			while (value.Length > 0)
			{
				string value3 = value.Substring(0, 2);
				value = value.Remove(0, 2);
				hogpAddDeviceInfo.UserFields.IrkList[num3] = Convert.ToUInt32(value3, 16);
				num3++;
			}
		}
		if (index == ++num)
		{
			hogpAddDeviceInfo.UserFields.Ediv = Convert.ToUInt32(value, 16);
		}
	}
}
