using System;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class RemoteNameRequestCompleteInfo : PacketInfoBase
{
	public override string Name => "Remote Name Request Complete";

	public override string DisplayGroup => "Link Control";

	public RemoteNameRequestCompleteInfo(RemoteNameRequestComplete packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		RemoteNameRequestComplete remoteNameRequestComplete = (RemoteNameRequestComplete)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("Status", ErrorCodeInfo.ErrorCodeToDescription(), 8, FormatValue(remoteNameRequestComplete.UserFields.Status, 8), ErrorCodeInfo.ErrorCodeToString(ErrorCodeInfo.ValueToErrorCode(remoteNameRequestComplete.UserFields.Status)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("BD_ADDR", "BD_ADDR for the device whose name was requested.", 48, FormatValue(remoteNameRequestComplete.UserFields.BdAddr, 48), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("REMOTE_NAME", "A UTF-8 encoded user-friendly descriptive name for the remote device. If the name contained in the parameter is shorter than 248 octets, the end of the name is indicated by a NULL octet (0x00), and the following octets (to fill up 248 octets, which is the length of the parameter) do not have valid values", remoteNameRequestComplete.UserFields.RemoteNameList.Count * 8, FormatValue(remoteNameRequestComplete.UserFields.RemoteNameList, remoteNameRequestComplete.UserFields.RemoteNameList.Count * 8, includeSpaces: false), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
	}

	public override void UpdatePacketFromFieldInfo(int index, string value)
	{
		RemoteNameRequestComplete remoteNameRequestComplete = (RemoteNameRequestComplete)packet;
		int num = -1;
		if (index == ++num)
		{
			remoteNameRequestComplete.UserFields.Status = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			remoteNameRequestComplete.UserFields.BdAddr = Convert.ToUInt64(value, 16);
		}
		else if (index == ++num)
		{
			value = AddLeadingZerosAndCheckValueStringLength(value, remoteNameRequestComplete.UserFields.RemoteNameList.Count);
			int num2 = 0;
			while (value.Length > 0)
			{
				string value2 = value.Substring(0, 2);
				value = value.Remove(0, 2);
				remoteNameRequestComplete.UserFields.RemoteNameList[num2] = Convert.ToUInt32(value2, 16);
				num2++;
			}
		}
	}
}
