using System;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

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

	public override string DisplayGroup => "LE Controller";

	public LeAdvertisingReportInfo(LeAdvertisingReport packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		LeAdvertisingReport leAdvertisingReport = (LeAdvertisingReport)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("Subevent_Code", "Subevent code for LE Advertising Report event", 8, FormatValue(leAdvertisingReport.UserFields.SubeventCode, 8), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Num_Reports", NumberOfReportsInfo.NumberOfReportsToDescription(), 8, FormatValue(leAdvertisingReport.UserFields.NumReports, 8), NumberOfReportsInfo.NumberOfReportsToString(NumberOfReportsInfo.ValueToNumberOfReports(leAdvertisingReport.UserFields.NumReports)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		for (int i = 0; i < leAdvertisingReport.UserFields.ReportsList.Count; i++)
		{
			fieldInfoList.Add(new PacketFieldInfo("Event_Type[" + i + "]", LeAdvertisingReport.ReportsSubField.EventTypeInfo.EventTypeToDescription(), 8, FormatValue(leAdvertisingReport.UserFields.ReportsList[i].EventType, 8), LeAdvertisingReport.ReportsSubField.EventTypeInfo.EventTypeToString(LeAdvertisingReport.ReportsSubField.EventTypeInfo.ValueToEventType(leAdvertisingReport.UserFields.ReportsList[i].EventType)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("Address_Type[" + i + "]", PeerResolvableAddressTypeInfo.PeerResolvableAddressTypeToDescription(), 8, FormatValue(leAdvertisingReport.UserFields.ReportsList[i].AddressType, 8), PeerResolvableAddressTypeInfo.PeerResolvableAddressTypeToString(PeerResolvableAddressTypeInfo.ValueToPeerResolvableAddressType(leAdvertisingReport.UserFields.ReportsList[i].AddressType)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("Address[" + i + "]", "Public Device Address, Random Device Address, Public Identity Address or Random (static) Identity Address of the advertising device", 48, FormatValue(leAdvertisingReport.UserFields.ReportsList[i].Address, 48), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("Length_Data[" + i + "]", "Length of the Data[i] field for each device which responded.", 8, FormatValue(leAdvertisingReport.UserFields.ReportsList[i].LengthData, 8), "", "00", "1F", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("Data[" + i + "]", "Length_Data[i] octets of advertising or scan response data formatted as defined in [Vol 3] Part C, Section 11. Note: Each element of this array has a variable length.", leAdvertisingReport.UserFields.ReportsList[i].DataList.Count * 8, FormatValue(leAdvertisingReport.UserFields.ReportsList[i].DataList, leAdvertisingReport.UserFields.ReportsList[i].DataList.Count * 8, includeSpaces: false), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("RSSI[" + i + "]", "Size: 1 Octet (signed integer). Range: -127 ≤ N ≤ +20, Units: dBm. RSSI is not available (127). Reserved for future use (21 to 126)\r\n\r\n" + AdvertisingRssiInfo.AdvertisingRssiToDescription(), 8, FormatValue(leAdvertisingReport.UserFields.ReportsList[i].Rssi, 8), AdvertisingRssiInfo.AdvertisingRssiToString(AdvertisingRssiInfo.ValueToAdvertisingRssi(leAdvertisingReport.UserFields.ReportsList[i].Rssi)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		}
	}

	public override void UpdatePacketFromFieldInfo(int index, string value)
	{
		LeAdvertisingReport leAdvertisingReport = (LeAdvertisingReport)packet;
		int num = -1;
		if (index == ++num)
		{
			throw new Exception("Tried to set non-settable field SubeventCode");
		}
		if (index == ++num)
		{
			leAdvertisingReport.UserFields.NumReports = Convert.ToUInt32(value, 16);
			return;
		}
		for (int i = 0; i < leAdvertisingReport.UserFields.ReportsList.Count; i++)
		{
			if (index == ++num)
			{
				leAdvertisingReport.UserFields.ReportsList[i].EventType = Convert.ToUInt32(value, 16);
				break;
			}
			if (index == ++num)
			{
				leAdvertisingReport.UserFields.ReportsList[i].AddressType = Convert.ToUInt32(value, 16);
				break;
			}
			if (index == ++num)
			{
				leAdvertisingReport.UserFields.ReportsList[i].Address = Convert.ToUInt64(value, 16);
				break;
			}
			if (index == ++num)
			{
				leAdvertisingReport.UserFields.ReportsList[i].LengthData = Convert.ToUInt32(value, 16);
				break;
			}
			if (index == ++num)
			{
				value = AddLeadingZerosAndCheckValueStringLength(value, leAdvertisingReport.UserFields.ReportsList[i].DataList.Count);
				int num2 = 0;
				while (value.Length > 0)
				{
					string value2 = value.Substring(0, 2);
					value = value.Remove(0, 2);
					leAdvertisingReport.UserFields.ReportsList[i].DataList[num2] = Convert.ToUInt32(value2, 16);
					num2++;
				}
			}
			if (index == ++num)
			{
				leAdvertisingReport.UserFields.ReportsList[i].Rssi = Convert.ToUInt32(value, 16);
				break;
			}
		}
	}
}
