using System;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class LeExtendedCreateConnectionInfo : PacketInfoBase
{
	public override string Name => "Le Extended Create Connection";

	public override string DisplayGroup => "LE Controller";

	public LeExtendedCreateConnectionInfo(LeExtendedCreateConnection packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		LeExtendedCreateConnection leExtendedCreateConnection = (LeExtendedCreateConnection)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("Initiating_Filter_Policy", InitiatingFilterPolicyInfo.InitiatingFilterPolicyToDescription(), 8, FormatValue(leExtendedCreateConnection.UserFields.InitiatingFilterPolicy, 8), InitiatingFilterPolicyInfo.InitiatingFilterPolicyToString(InitiatingFilterPolicyInfo.ValueToInitiatingFilterPolicy(leExtendedCreateConnection.UserFields.InitiatingFilterPolicy)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Own_Address_Type", OwnAddressTypeInfo.OwnAddressTypeToDescription(), 8, FormatValue(leExtendedCreateConnection.UserFields.OwnAddressType, 8), OwnAddressTypeInfo.OwnAddressTypeToString(OwnAddressTypeInfo.ValueToOwnAddressType(leExtendedCreateConnection.UserFields.OwnAddressType)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Peer_Address_Type", PeerResolvableAddressTypeInfo.PeerResolvableAddressTypeToDescription(), 8, FormatValue(leExtendedCreateConnection.UserFields.PeerAddressType, 8), PeerResolvableAddressTypeInfo.PeerResolvableAddressTypeToString(PeerResolvableAddressTypeInfo.ValueToPeerResolvableAddressType(leExtendedCreateConnection.UserFields.PeerAddressType)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Peer_Address", "Public Device Address, Random Device Address, Public Identity Address, or Random (static) Identity Address of the device to be connected", 48, FormatValue(leExtendedCreateConnection.UserFields.PeerAddress, 48), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Initiating_PHYs", LeExtendedCreateConnection.UserFieldsWrapper.InitiatingPhysInfo.InitiatingPhysToDescription(), 8, FormatValue(leExtendedCreateConnection.UserFields.InitiatingPhys, 8), LeExtendedCreateConnection.UserFieldsWrapper.InitiatingPhysInfo.InitiatingPhysToString(LeExtendedCreateConnection.UserFieldsWrapper.InitiatingPhysInfo.ValueToInitiatingPhys(leExtendedCreateConnection.UserFields.InitiatingPhys)), "01", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		for (int i = 0; i < leExtendedCreateConnection.UserFields.ParametersList.Count; i++)
		{
			fieldInfoList.Add(new PacketFieldInfo("Scan_Interval[" + i + "]", "Time interval from when the Controller started its last scan until it begins the subsequent scan on the primary advertising channel. Range: 0x0004 to 0xFFFF, Time = N * 0.625 ms, Time Range: 2.5 ms to 40.959375 s", 16, FormatValue(leExtendedCreateConnection.UserFields.ParametersList[i].ScanInterval, 16), "", "0004", "FFFF", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("Scan_Window[" + i + "]", "Duration of the scan on the primary advertising channel. Range: 0x0004 to 0xFFFF, Time = N * 0.625 ms, Time Range: 2.5 ms to 40.959375 s", 16, FormatValue(leExtendedCreateConnection.UserFields.ParametersList[i].ScanWindow, 16), "", "0004", "FFFF", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("Conn_Interval_Min[" + i + "]", "Minimum value for the connection interval. This shall be less than or equal to Conn_Interval_Max[i]. Range: 0x0006 to 0x0C80, Time = N * 1.25 ms, Time Range: 7.5 ms to 4 s. Reserved for future use (0x0000 - 0x0005 and 0x0C81 - 0xFFFF)", 16, FormatValue(leExtendedCreateConnection.UserFields.ParametersList[i].ConnIntervalMin, 16), "", "0006", "0C80", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("Conn_Interval_Max[" + i + "]", "Maximum value for the connection interval. This shall be greater than or equal to Conn_Interval_Min[i]. Range: 0x0006 to 0x0C80, Time = N * 1.25 ms, Time Range: 7.5 ms to 4 s. Reserved for future use (0x0000 - 0x0005 and 0x0C81 - 0xFFFF)", 16, FormatValue(leExtendedCreateConnection.UserFields.ParametersList[i].ConnIntervalMax, 16), "", "0006", "0C80", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("Conn_Latency[" + i + "]", "Slave latency for the connection in number of connection events. Range: 0x0000 to 0x01F3", 16, FormatValue(leExtendedCreateConnection.UserFields.ParametersList[i].ConnLatency, 16), "", "0000", "01F3", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("Supervision_Timeout[" + i + "]", "Supervision timeout for the LE Link. (See [Vol 6] Part B, Section 4.5.2) Range: 0x000A to 0x0C80, Time = N * 10 ms, Time Range: 100 ms to 32 s. Reserved for future use (0x0000 - 0x0009 and 0x0C81 - 0xFFFF)", 16, FormatValue(leExtendedCreateConnection.UserFields.ParametersList[i].SupervisionTimeout, 16), "", "000A", "0C80", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("Minimum_CE_Length[" + i + "]", "Informative parameter recommending the minimum length of connection event needed for this LE connection. Range: 0x0000 0xFFFF, Time = N * 0.625 ms", 16, FormatValue(leExtendedCreateConnection.UserFields.ParametersList[i].MinimumCeLength, 16), "", "0000", "FFFF", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("Maximum_CE_Length[" + i + "]", "Informative parameter recommending the maximum length of connection event needed for this LE connection. Range: 0x0000 0xFFFF, Time = N * 0.625 ms", 16, FormatValue(leExtendedCreateConnection.UserFields.ParametersList[i].MaximumCeLength, 16), "", "0000", "FFFF", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		}
	}

	public override void UpdatePacketFromFieldInfo(int index, string value)
	{
		LeExtendedCreateConnection leExtendedCreateConnection = (LeExtendedCreateConnection)packet;
		int num = -1;
		if (index == ++num)
		{
			leExtendedCreateConnection.UserFields.InitiatingFilterPolicy = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			leExtendedCreateConnection.UserFields.OwnAddressType = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			leExtendedCreateConnection.UserFields.PeerAddressType = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			leExtendedCreateConnection.UserFields.PeerAddress = Convert.ToUInt64(value, 16);
			return;
		}
		if (index == ++num)
		{
			leExtendedCreateConnection.UserFields.InitiatingPhys = Convert.ToUInt32(value, 16);
			return;
		}
		for (int i = 0; i < leExtendedCreateConnection.UserFields.ParametersList.Count; i++)
		{
			if (index == ++num)
			{
				leExtendedCreateConnection.UserFields.ParametersList[i].ScanInterval = Convert.ToUInt32(value, 16);
				break;
			}
			if (index == ++num)
			{
				leExtendedCreateConnection.UserFields.ParametersList[i].ScanWindow = Convert.ToUInt32(value, 16);
				break;
			}
			if (index == ++num)
			{
				leExtendedCreateConnection.UserFields.ParametersList[i].ConnIntervalMin = Convert.ToUInt32(value, 16);
				break;
			}
			if (index == ++num)
			{
				leExtendedCreateConnection.UserFields.ParametersList[i].ConnIntervalMax = Convert.ToUInt32(value, 16);
				break;
			}
			if (index == ++num)
			{
				leExtendedCreateConnection.UserFields.ParametersList[i].ConnLatency = Convert.ToUInt32(value, 16);
				break;
			}
			if (index == ++num)
			{
				leExtendedCreateConnection.UserFields.ParametersList[i].SupervisionTimeout = Convert.ToUInt32(value, 16);
				break;
			}
			if (index == ++num)
			{
				leExtendedCreateConnection.UserFields.ParametersList[i].MinimumCeLength = Convert.ToUInt32(value, 16);
				break;
			}
			if (index == ++num)
			{
				leExtendedCreateConnection.UserFields.ParametersList[i].MaximumCeLength = Convert.ToUInt32(value, 16);
				break;
			}
		}
	}
}
