using System;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class WriteDdbKeyEntryInfo : PacketInfoBase
{
	public override string Name => "Write Ddb Key Entry";

	public override string DisplayGroup => "Vendor Specific";

	public WriteDdbKeyEntryInfo(WriteDdbKeyEntry packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		WriteDdbKeyEntry writeDdbKeyEntry = (WriteDdbKeyEntry)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("Bd_Address", "Bluetooth Address of Device", 48, FormatValue(writeDdbKeyEntry.UserFields.BdAddress, 48), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Link_Key", "Link key of remote device. Value = Variable", writeDdbKeyEntry.UserFields.LinkKeyList.Count * 8, FormatValue(writeDdbKeyEntry.UserFields.LinkKeyList, writeDdbKeyEntry.UserFields.LinkKeyList.Count * 8, includeSpaces: false), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Key_Type", "Link key type of remote device", 8, FormatValue(writeDdbKeyEntry.UserFields.KeyType, 8), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("COD", "Class of Device", 32, FormatValue(writeDdbKeyEntry.UserFields.Cod, 32), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
	}

	public override void UpdatePacketFromFieldInfo(int index, string value)
	{
		WriteDdbKeyEntry writeDdbKeyEntry = (WriteDdbKeyEntry)packet;
		int num = -1;
		if (index == ++num)
		{
			writeDdbKeyEntry.UserFields.BdAddress = Convert.ToUInt64(value, 16);
			return;
		}
		if (index == ++num)
		{
			value = AddLeadingZerosAndCheckValueStringLength(value, writeDdbKeyEntry.UserFields.LinkKeyList.Count);
			int num2 = 0;
			while (value.Length > 0)
			{
				string value2 = value.Substring(0, 2);
				value = value.Remove(0, 2);
				writeDdbKeyEntry.UserFields.LinkKeyList[num2] = Convert.ToUInt32(value2, 16);
				num2++;
			}
		}
		if (index == ++num)
		{
			writeDdbKeyEntry.UserFields.KeyType = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			writeDdbKeyEntry.UserFields.Cod = Convert.ToUInt32(value, 16);
		}
	}
}
