using System;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class LeSetExtendedScanParametersInfo : PacketInfoBase
{
	public override string Name => "Le Set Extended Scan Parameters";

	public override string DisplayGroup => "LE Controller";

	public LeSetExtendedScanParametersInfo(LeSetExtendedScanParameters packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		LeSetExtendedScanParameters leSetExtendedScanParameters = (LeSetExtendedScanParameters)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("Own_Address_Type", OwnAddressTypeInfo.OwnAddressTypeToDescription(), 8, FormatValue(leSetExtendedScanParameters.UserFields.OwnAddressType, 8), OwnAddressTypeInfo.OwnAddressTypeToString(OwnAddressTypeInfo.ValueToOwnAddressType(leSetExtendedScanParameters.UserFields.OwnAddressType)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Scanning_Filter_Policy", ScanningFilterPolicyInfo.ScanningFilterPolicyToDescription(), 8, FormatValue(leSetExtendedScanParameters.UserFields.ScanningFilterPolicy, 8), ScanningFilterPolicyInfo.ScanningFilterPolicyToString(ScanningFilterPolicyInfo.ValueToScanningFilterPolicy(leSetExtendedScanParameters.UserFields.ScanningFilterPolicy)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Scanning_PHYs", LeSetExtendedScanParameters.UserFieldsWrapper.ScanningPhysInfo.ScanningPhysToDescription(), 8, FormatValue(leSetExtendedScanParameters.UserFields.ScanningPhys, 8), LeSetExtendedScanParameters.UserFieldsWrapper.ScanningPhysInfo.ScanningPhysToString(LeSetExtendedScanParameters.UserFieldsWrapper.ScanningPhysInfo.ValueToScanningPhys(leSetExtendedScanParameters.UserFields.ScanningPhys)), "01", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		for (int i = 0; i < leSetExtendedScanParameters.UserFields.ParametersList.Count; i++)
		{
			fieldInfoList.Add(new PacketFieldInfo("Scan_Type[" + i + "]", LeSetExtendedScanParameters.ParametersSubField.ScanTypeInfo.ScanTypeToDescription(), 8, FormatValue(leSetExtendedScanParameters.UserFields.ParametersList[i].ScanType, 8), LeSetExtendedScanParameters.ParametersSubField.ScanTypeInfo.ScanTypeToString(LeSetExtendedScanParameters.ParametersSubField.ScanTypeInfo.ValueToScanType(leSetExtendedScanParameters.UserFields.ParametersList[i].ScanType)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			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(leSetExtendedScanParameters.UserFields.ParametersList[i].ScanInterval, 16), ValueTimes0p625MsecExpression.ValueTimes0p625MsecToString(leSetExtendedScanParameters.UserFields.ParametersList[i].ScanInterval), "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(leSetExtendedScanParameters.UserFields.ParametersList[i].ScanWindow, 16), ValueTimes0p625MsecExpression.ValueTimes0p625MsecToString(leSetExtendedScanParameters.UserFields.ParametersList[i].ScanWindow), "0004", "FFFF", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		}
	}

	public override void UpdatePacketFromFieldInfo(int index, string value)
	{
		LeSetExtendedScanParameters leSetExtendedScanParameters = (LeSetExtendedScanParameters)packet;
		int num = -1;
		if (index == ++num)
		{
			leSetExtendedScanParameters.UserFields.OwnAddressType = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			leSetExtendedScanParameters.UserFields.ScanningFilterPolicy = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			leSetExtendedScanParameters.UserFields.ScanningPhys = Convert.ToUInt32(value, 16);
			return;
		}
		for (int i = 0; i < leSetExtendedScanParameters.UserFields.ParametersList.Count; i++)
		{
			if (index == ++num)
			{
				leSetExtendedScanParameters.UserFields.ParametersList[i].ScanType = Convert.ToUInt32(value, 16);
				break;
			}
			if (index == ++num)
			{
				leSetExtendedScanParameters.UserFields.ParametersList[i].ScanInterval = Convert.ToUInt32(value, 16);
				break;
			}
			if (index == ++num)
			{
				leSetExtendedScanParameters.UserFields.ParametersList[i].ScanWindow = Convert.ToUInt32(value, 16);
				break;
			}
		}
	}
}
