using System;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class ExtendedInquiryResultInfo : PacketInfoBase
{
	public override string Name => "Extended Inquiry Result";

	public override string DisplayGroup => "Link Control";

	public ExtendedInquiryResultInfo(ExtendedInquiryResult packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		ExtendedInquiryResult extendedInquiryResult = (ExtendedInquiryResult)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("Num Responses", "Number of responses from the inquiry.", 8, FormatValue(extendedInquiryResult.UserFields.NumResponses, 8), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		for (int i = 0; i < extendedInquiryResult.UserFields.ResponseList.Count; i++)
		{
			fieldInfoList.Add(new PacketFieldInfo("BD_ADDR[" + i + "]", "", 48, FormatValue(extendedInquiryResult.UserFields.ResponseList[i].BdAddr, 48), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("Page Scan Repetition Mode[" + i + "]", PageScanRepetitionModeInfo.PageScanRepetitionModeToDescription(), 8, FormatValue(extendedInquiryResult.UserFields.ResponseList[i].PageScanRepetitionMode, 8), PageScanRepetitionModeInfo.PageScanRepetitionModeToString(PageScanRepetitionModeInfo.ValueToPageScanRepetitionMode(extendedInquiryResult.UserFields.ResponseList[i].PageScanRepetitionMode)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("Reserved[" + i + "]", "", 8, FormatValue(extendedInquiryResult.UserFields.ResponseList[i].Reserved, 8), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("Class of Device[" + i + "]", "", 24, FormatValue(extendedInquiryResult.UserFields.ResponseList[i].ClassOfDevice, 24), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("Clock Offset[" + i + "]", "Bit 14-0 = Bit 16-2 of CLKNslave-CLK, Bit 15 = Reserved", 16, FormatValue(extendedInquiryResult.UserFields.ResponseList[i].ClockOffset, 16), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("RSSI[" + i + "]", "Range is -127dBm to +20dBm\r\n\r\n" + ExtendedInquiryResult.ResponseSubField.RssiInfo.RssiToDescription(), 8, FormatValue(extendedInquiryResult.UserFields.ResponseList[i].Rssi, 8), ExtendedInquiryResult.ResponseSubField.RssiInfo.RssiToString(ExtendedInquiryResult.ResponseSubField.RssiInfo.ValueToRssi(extendedInquiryResult.UserFields.ResponseList[i].Rssi)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		}
		fieldInfoList.Add(new PacketFieldInfo("Extended Inquiry Response", "", extendedInquiryResult.UserFields.ExtendedInquiryResponseList.Count * 8, FormatValue(extendedInquiryResult.UserFields.ExtendedInquiryResponseList, extendedInquiryResult.UserFields.ExtendedInquiryResponseList.Count * 8, includeSpaces: false), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
	}

	public override void UpdatePacketFromFieldInfo(int index, string value)
	{
		ExtendedInquiryResult extendedInquiryResult = (ExtendedInquiryResult)packet;
		int num = -1;
		if (index == ++num)
		{
			extendedInquiryResult.UserFields.NumResponses = Convert.ToUInt32(value, 16);
			return;
		}
		for (int i = 0; i < extendedInquiryResult.UserFields.ResponseList.Count; i++)
		{
			if (index == ++num)
			{
				extendedInquiryResult.UserFields.ResponseList[i].BdAddr = Convert.ToUInt64(value, 16);
				return;
			}
			if (index == ++num)
			{
				extendedInquiryResult.UserFields.ResponseList[i].PageScanRepetitionMode = Convert.ToUInt32(value, 16);
				return;
			}
			if (index == ++num)
			{
				extendedInquiryResult.UserFields.ResponseList[i].Reserved = Convert.ToUInt32(value, 16);
				return;
			}
			if (index == ++num)
			{
				extendedInquiryResult.UserFields.ResponseList[i].ClassOfDevice = Convert.ToUInt32(value, 16);
				return;
			}
			if (index == ++num)
			{
				extendedInquiryResult.UserFields.ResponseList[i].ClockOffset = Convert.ToUInt32(value, 16);
				return;
			}
			if (index == ++num)
			{
				extendedInquiryResult.UserFields.ResponseList[i].Rssi = Convert.ToUInt32(value, 16);
				return;
			}
		}
		if (index == ++num)
		{
			value = AddLeadingZerosAndCheckValueStringLength(value, extendedInquiryResult.UserFields.ExtendedInquiryResponseList.Count);
			int num2 = 0;
			while (value.Length > 0)
			{
				string value2 = value.Substring(0, 2);
				value = value.Remove(0, 2);
				extendedInquiryResult.UserFields.ExtendedInquiryResponseList[num2] = Convert.ToUInt32(value2, 16);
				num2++;
			}
		}
	}
}
