using System;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class LePeriodicAdvertisingReportInfo : PacketInfoBase
{
	public override string Name => "Le Periodic Advertising Report";

	public override string DisplayGroup => "LE Controller";

	public LePeriodicAdvertisingReportInfo(LePeriodicAdvertisingReport packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		LePeriodicAdvertisingReport lePeriodicAdvertisingReport = (LePeriodicAdvertisingReport)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("Subevent_Code", "Subevent code for LE Periodic Advertising Report event", 8, FormatValue(lePeriodicAdvertisingReport.UserFields.SubeventCode, 8), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Sync_Handle", "Sync_Handle to be used to identify the periodic advertiser from which data has been received. Range: 0x0000-0x0EFF (0x0F00 - 0x0FFF Reserved for future use)", 16, FormatValue(lePeriodicAdvertisingReport.UserFields.SyncHandle, 16), "", "0000", "0EFF", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Tx_Power", "Size: 1 Octet (signed integer). Range: -127 ≤ N ≤ +126, Units: dBm. Tx Power information not available (127).\r\n\r\n" + AdvertisingReportTxPowerInfo.AdvertisingReportTxPowerToDescription(), 8, FormatValue(lePeriodicAdvertisingReport.UserFields.TxPower, 8), AdvertisingReportTxPowerInfo.AdvertisingReportTxPowerToString(AdvertisingReportTxPowerInfo.ValueToAdvertisingReportTxPower(lePeriodicAdvertisingReport.UserFields.TxPower)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("RSSI", "Size: 1 Octet (signed integer). Range: -127 ≤ N ≤ +20, Units: dBm. RSSI is not available (127). Reserved for future use (All other values)\r\n\r\n" + AdvertisingRssiInfo.AdvertisingRssiToDescription(), 8, FormatValue(lePeriodicAdvertisingReport.UserFields.Rssi, 8), AdvertisingRssiInfo.AdvertisingRssiToString(AdvertisingRssiInfo.ValueToAdvertisingRssi(lePeriodicAdvertisingReport.UserFields.Rssi)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Supplemental_Type", AodSupplementalTypeInfo.AodSupplementalTypeToDescription(), 8, FormatValue(lePeriodicAdvertisingReport.UserFields.SupplementalType, 8), AodSupplementalTypeInfo.AodSupplementalTypeToString(AodSupplementalTypeInfo.ValueToAodSupplementalType(lePeriodicAdvertisingReport.UserFields.SupplementalType)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Data_Status", AdvertisingDataStatusInfo.AdvertisingDataStatusToDescription(), 8, FormatValue(lePeriodicAdvertisingReport.UserFields.DataStatus, 8), AdvertisingDataStatusInfo.AdvertisingDataStatusToString(AdvertisingDataStatusInfo.ValueToAdvertisingDataStatus(lePeriodicAdvertisingReport.UserFields.DataStatus)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Data_Length", "Length of the Data field", 8, FormatValue(lePeriodicAdvertisingReport.UserFields.DataLength, 8), "", "00", "F9", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Data", "Data received from a Periodic Advertising packet", lePeriodicAdvertisingReport.UserFields.DataList.Count * 8, FormatValue(lePeriodicAdvertisingReport.UserFields.DataList, lePeriodicAdvertisingReport.UserFields.DataList.Count * 8, includeSpaces: false), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
	}

	public override void UpdatePacketFromFieldInfo(int index, string value)
	{
		LePeriodicAdvertisingReport lePeriodicAdvertisingReport = (LePeriodicAdvertisingReport)packet;
		int num = -1;
		if (index == ++num)
		{
			throw new Exception("Tried to set non-settable field SubeventCode");
		}
		if (index == ++num)
		{
			lePeriodicAdvertisingReport.UserFields.SyncHandle = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			lePeriodicAdvertisingReport.UserFields.TxPower = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			lePeriodicAdvertisingReport.UserFields.Rssi = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			lePeriodicAdvertisingReport.UserFields.SupplementalType = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			lePeriodicAdvertisingReport.UserFields.DataStatus = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			lePeriodicAdvertisingReport.UserFields.DataLength = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			value = AddLeadingZerosAndCheckValueStringLength(value, lePeriodicAdvertisingReport.UserFields.DataList.Count);
			int num2 = 0;
			while (value.Length > 0)
			{
				string value2 = value.Substring(0, 2);
				value = value.Remove(0, 2);
				lePeriodicAdvertisingReport.UserFields.DataList[num2] = Convert.ToUInt32(value2, 16);
				num2++;
			}
		}
	}
}
