using System;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class LeStartEncryptionInfo : PacketInfoBase
{
	public override string Name => "Le Start Encryption";

	public override string DisplayGroup => "LE Controller";

	public LeStartEncryptionInfo(LeStartEncryption packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		LeStartEncryption leStartEncryption = (LeStartEncryption)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("Connection_Handle", "Connection_Handle for which the link layer encryption has been enabled/disabled for all Connection_Handles with the same BR/EDR Controller endpoint as the specified Connection_Handle. Range: 0x0000-0x0EFF. Reserved for future use (0x0F00 - 0x0FFF)", 16, FormatValue(leStartEncryption.UserFields.ConnectionHandle, 16), "", "0000", "0EFF", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Random_Number", "64 bit random number", 64, FormatValue(leStartEncryption.UserFields.RandomNumber, 64), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Encrypted_Diversifier", "16 bit encrypted diversifier", 16, FormatValue(leStartEncryption.UserFields.EncryptedDiversifier, 16), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Long_Term_Key", "128 bit long term key", leStartEncryption.UserFields.LongTermKeyList.Count * 8, FormatValue(leStartEncryption.UserFields.LongTermKeyList, leStartEncryption.UserFields.LongTermKeyList.Count * 8, includeSpaces: false), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
	}

	public override void UpdatePacketFromFieldInfo(int index, string value)
	{
		LeStartEncryption leStartEncryption = (LeStartEncryption)packet;
		int num = -1;
		if (index == ++num)
		{
			leStartEncryption.UserFields.ConnectionHandle = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			leStartEncryption.UserFields.RandomNumber = Convert.ToUInt64(value, 16);
		}
		else if (index == ++num)
		{
			leStartEncryption.UserFields.EncryptedDiversifier = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			value = AddLeadingZerosAndCheckValueStringLength(value, leStartEncryption.UserFields.LongTermKeyList.Count);
			int num2 = 0;
			while (value.Length > 0)
			{
				string value2 = value.Substring(0, 2);
				value = value.Remove(0, 2);
				leStartEncryption.UserFields.LongTermKeyList[num2] = Convert.ToUInt32(value2, 16);
				num2++;
			}
		}
	}
}
