using System;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class ReturnLinkKeysEventInfo : PacketInfoBase
{
	public override string Name => "Return Link Keys Event";

	public override string DisplayGroup => "Host Control and Baseband";

	public ReturnLinkKeysEventInfo(ReturnLinkKeysEvent packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		ReturnLinkKeysEvent returnLinkKeysEvent = (ReturnLinkKeysEvent)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("Num Keys", "Number of Link Keys contained in this event.", 8, FormatValue(returnLinkKeysEvent.UserFields.NumKeys, 8), "", "0x01", "0x0B", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		for (int i = 0; i < returnLinkKeysEvent.UserFields.ParametersList.Count; i++)
		{
			fieldInfoList.Add(new PacketFieldInfo("BD Address[" + i + "]", "", 48, FormatValue(returnLinkKeysEvent.UserFields.ParametersList[i].BdAddress, 48), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("Link Key[" + i + "]", "", returnLinkKeysEvent.UserFields.ParametersList[i].LinkKeyList.Count * 8, FormatValue(returnLinkKeysEvent.UserFields.ParametersList[i].LinkKeyList, returnLinkKeysEvent.UserFields.ParametersList[i].LinkKeyList.Count * 8, includeSpaces: false), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		}
	}

	public override void UpdatePacketFromFieldInfo(int index, string value)
	{
		ReturnLinkKeysEvent returnLinkKeysEvent = (ReturnLinkKeysEvent)packet;
		int num = -1;
		if (index == ++num)
		{
			returnLinkKeysEvent.UserFields.NumKeys = Convert.ToUInt32(value, 16);
			return;
		}
		for (int i = 0; i < returnLinkKeysEvent.UserFields.ParametersList.Count; i++)
		{
			if (index == ++num)
			{
				returnLinkKeysEvent.UserFields.ParametersList[i].BdAddress = Convert.ToUInt64(value, 16);
				break;
			}
			if (index == ++num)
			{
				value = AddLeadingZerosAndCheckValueStringLength(value, returnLinkKeysEvent.UserFields.ParametersList[i].LinkKeyList.Count);
				int num2 = 0;
				while (value.Length > 0)
				{
					string value2 = value.Substring(0, 2);
					value = value.Remove(0, 2);
					returnLinkKeysEvent.UserFields.ParametersList[i].LinkKeyList[num2] = Convert.ToUInt32(value2, 16);
					num2++;
				}
			}
		}
	}
}
