using System;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class LinkKeyNotificationInfo : PacketInfoBase
{
	public override string Name => "Link Key Notification";

	public override string DisplayGroup => "Link Control";

	public LinkKeyNotificationInfo(LinkKeyNotification packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		LinkKeyNotification linkKeyNotification = (LinkKeyNotification)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("BD_ADDR", "BD_ADDR of the Device for which the new link key has been generated.", 48, FormatValue(linkKeyNotification.UserFields.BdAddr, 48), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Link_Key", "Link Key for the associated BD_ADDR.", linkKeyNotification.UserFields.LinkKeyList.Count * 8, FormatValue(linkKeyNotification.UserFields.LinkKeyList, linkKeyNotification.UserFields.LinkKeyList.Count * 8, includeSpaces: false), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Key_Type", LinkKeyNotification.UserFieldsWrapper.KeyTypeInfo.KeyTypeToDescription(), 8, FormatValue(linkKeyNotification.UserFields.KeyType, 8), LinkKeyNotification.UserFieldsWrapper.KeyTypeInfo.KeyTypeToString(LinkKeyNotification.UserFieldsWrapper.KeyTypeInfo.ValueToKeyType(linkKeyNotification.UserFields.KeyType)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
	}

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