using System;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class LeAdvtTrackingInfo : PacketInfoBase
{
	public override string Name => "Le Advt Tracking";

	public override string DisplayGroup => "Vendor Specific";

	public LeAdvtTrackingInfo(LeAdvtTracking packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		LeAdvtTracking leAdvtTracking = (LeAdvtTracking)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("sub_event_code", "LE Advt tracking", 8, FormatValue(leAdvtTracking.UserFields.SubEventCode, 8), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("APCF_Filter_Index", "Filter index (0, max_filter-1)", 8, FormatValue(leAdvtTracking.UserFields.ApcfFilterIndex, 8), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Advertiser_State", LeAdvtTracking.UserFieldsWrapper.AdvertiserStateInfo.AdvertiserStateToDescription(), 8, FormatValue(leAdvtTracking.UserFields.AdvertiserState, 8), LeAdvtTracking.UserFieldsWrapper.AdvertiserStateInfo.AdvertiserStateToString(LeAdvtTracking.UserFieldsWrapper.AdvertiserStateInfo.ValueToAdvertiserState(leAdvtTracking.UserFields.AdvertiserState)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Advt_Info_present", LeAdvtTracking.UserFieldsWrapper.AdvtInfoPresentInfo.AdvtInfoPresentToDescription(), 8, FormatValue(leAdvtTracking.UserFields.AdvtInfoPresent, 8), LeAdvtTracking.UserFieldsWrapper.AdvtInfoPresentInfo.AdvtInfoPresentToString(LeAdvtTracking.UserFieldsWrapper.AdvtInfoPresentInfo.ValueToAdvtInfoPresent(leAdvtTracking.UserFields.AdvtInfoPresent)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Advertiser_Address", "Public or Random address", 48, FormatValue(leAdvtTracking.UserFields.AdvertiserAddress, 48), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Advertiser_Address_Type", DeviceAddressTypeInfo.DeviceAddressTypeToDescription(), 8, FormatValue(leAdvtTracking.UserFields.AdvertiserAddressType, 8), DeviceAddressTypeInfo.DeviceAddressTypeToString(DeviceAddressTypeInfo.ValueToDeviceAddressType(leAdvtTracking.UserFields.AdvertiserAddressType)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Advt_Info_Count", "", 8, FormatValue(leAdvtTracking.UserFields.AdvtInfoCount, 8), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: true, DataEntryMode.RightAlignedHex));
		for (int i = 0; i < leAdvtTracking.UserFields.AdvtInfoList.Count; i++)
		{
			fieldInfoList.Add(new PacketFieldInfo("Tx_Pwr[" + i + "]", "", 8, FormatValue(leAdvtTracking.UserFields.AdvtInfoList[i].TxPwr, 8), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("RSSI[" + i + "]", "", 8, FormatValue(leAdvtTracking.UserFields.AdvtInfoList[i].Rssi, 8), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("Timestamp[" + i + "]", "The unit of timestamp is 50ms. The value of timestamp is based off when the Read_Batch_Scan_Results_Sub_cmd is given by host.", 16, FormatValue(leAdvtTracking.UserFields.AdvtInfoList[i].Timestamp, 16), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("Adv_packet_len[" + i + "]", LeAdvtTracking.AdvtInfoSubField.AdvPacketLenInfo.AdvPacketLenToDescription(), 8, FormatValue(leAdvtTracking.UserFields.AdvtInfoList[i].AdvPacketLen, 8), LeAdvtTracking.AdvtInfoSubField.AdvPacketLenInfo.AdvPacketLenToString(LeAdvtTracking.AdvtInfoSubField.AdvPacketLenInfo.ValueToAdvPacketLen(leAdvtTracking.UserFields.AdvtInfoList[i].AdvPacketLen)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("Adv_packet[" + i + "]", "Length_Data[i] octets of advertising or scan response data formatted as defined in BT Specification (ADVERTISING AND SCAN RESPONSE DATA FORMAT Section)", leAdvtTracking.UserFields.AdvtInfoList[i].AdvPacketList.Count * 8, FormatValue(leAdvtTracking.UserFields.AdvtInfoList[i].AdvPacketList, leAdvtTracking.UserFields.AdvtInfoList[i].AdvPacketList.Count * 8, includeSpaces: false), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("Scan_data_resp_len[" + i + "]", LeAdvtTracking.AdvtInfoSubField.ScanDataRespLenInfo.ScanDataRespLenToDescription(), 8, FormatValue(leAdvtTracking.UserFields.AdvtInfoList[i].ScanDataRespLen, 8), LeAdvtTracking.AdvtInfoSubField.ScanDataRespLenInfo.ScanDataRespLenToString(LeAdvtTracking.AdvtInfoSubField.ScanDataRespLenInfo.ValueToScanDataRespLen(leAdvtTracking.UserFields.AdvtInfoList[i].ScanDataRespLen)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("Scan_data_resp[" + i + "]", "Length_Data[i] octets of advertising or scan response data formatted as defined in BT Specification (ADVERTISING AND SCAN RESPONSE DATA FORMAT Section)", leAdvtTracking.UserFields.AdvtInfoList[i].ScanDataRespList.Count * 8, FormatValue(leAdvtTracking.UserFields.AdvtInfoList[i].ScanDataRespList, leAdvtTracking.UserFields.AdvtInfoList[i].ScanDataRespList.Count * 8, includeSpaces: false), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		}
	}

	public override void UpdatePacketFromFieldInfo(int index, string value)
	{
		LeAdvtTracking leAdvtTracking = (LeAdvtTracking)packet;
		int num = -1;
		if (index == ++num)
		{
			throw new Exception("Tried to set non-settable field SubEventCode");
		}
		if (index == ++num)
		{
			leAdvtTracking.UserFields.ApcfFilterIndex = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			leAdvtTracking.UserFields.AdvertiserState = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			leAdvtTracking.UserFields.AdvtInfoPresent = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			leAdvtTracking.UserFields.AdvertiserAddress = Convert.ToUInt64(value, 16);
			return;
		}
		if (index == ++num)
		{
			leAdvtTracking.UserFields.AdvertiserAddressType = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			leAdvtTracking.UserFields.AdvtInfoCount = Convert.ToUInt32(value, 16);
			return;
		}
		for (int i = 0; i < leAdvtTracking.UserFields.AdvtInfoList.Count; i++)
		{
			if (index == ++num)
			{
				leAdvtTracking.UserFields.AdvtInfoList[i].TxPwr = Convert.ToUInt32(value, 16);
				break;
			}
			if (index == ++num)
			{
				leAdvtTracking.UserFields.AdvtInfoList[i].Rssi = Convert.ToUInt32(value, 16);
				break;
			}
			if (index == ++num)
			{
				leAdvtTracking.UserFields.AdvtInfoList[i].Timestamp = Convert.ToUInt32(value, 16);
				break;
			}
			if (index == ++num)
			{
				leAdvtTracking.UserFields.AdvtInfoList[i].AdvPacketLen = Convert.ToUInt32(value, 16);
				break;
			}
			if (index == ++num)
			{
				value = AddLeadingZerosAndCheckValueStringLength(value, leAdvtTracking.UserFields.AdvtInfoList[i].AdvPacketList.Count);
				int num2 = 0;
				while (value.Length > 0)
				{
					string value2 = value.Substring(0, 2);
					value = value.Remove(0, 2);
					leAdvtTracking.UserFields.AdvtInfoList[i].AdvPacketList[num2] = Convert.ToUInt32(value2, 16);
					num2++;
				}
			}
			if (index == ++num)
			{
				leAdvtTracking.UserFields.AdvtInfoList[i].ScanDataRespLen = Convert.ToUInt32(value, 16);
				break;
			}
			if (index == ++num)
			{
				value = AddLeadingZerosAndCheckValueStringLength(value, leAdvtTracking.UserFields.AdvtInfoList[i].ScanDataRespList.Count);
				int num3 = 0;
				while (value.Length > 0)
				{
					string value3 = value.Substring(0, 2);
					value = value.Remove(0, 2);
					leAdvtTracking.UserFields.AdvtInfoList[i].ScanDataRespList[num3] = Convert.ToUInt32(value3, 16);
					num3++;
				}
			}
		}
	}
}
