using System;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class LeConnectionUpdateCompleteInfo : PacketInfoBase
{
	public override string Name => "Le Connection Update Complete";

	public override string DisplayGroup => "LE Controller";

	public LeConnectionUpdateCompleteInfo(LeConnectionUpdateComplete packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		LeConnectionUpdateComplete leConnectionUpdateComplete = (LeConnectionUpdateComplete)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("Subevent_Code", "Subevent code for LE Connection Update Complete event", 8, FormatValue(leConnectionUpdateComplete.UserFields.SubeventCode, 8), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Status", ErrorCodeInfo.ErrorCodeToDescription(), 8, FormatValue(leConnectionUpdateComplete.UserFields.Status, 8), ErrorCodeInfo.ErrorCodeToString(ErrorCodeInfo.ValueToErrorCode(leConnectionUpdateComplete.UserFields.Status)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Connection_Handle", "Public Device Address or Random Device Address of the peer device", 16, FormatValue(leConnectionUpdateComplete.UserFields.ConnectionHandle, 16), "", "0000", "0EFF", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Conn_Interval", "Connection interval used on this connection. Range: 0x0006 to 0x0C80, Time = N * 1.25 msec, Time Range: 7.5 msec to 4000 msec", 16, FormatValue(leConnectionUpdateComplete.UserFields.ConnInterval, 16), "", "0006", "0C80", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Conn_Latency", "Slave latency for the connection in number of connection events. Range: 0x0000 to 0x01F3", 16, FormatValue(leConnectionUpdateComplete.UserFields.ConnLatency, 16), "", "0000", "01F3", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Supervision_Timeout", "Supervision timeout for this connection. Range: 0x000A to 0x0C80, Time = N * 10 msec, Time Range: 100 msec to 32 seconds", 16, FormatValue(leConnectionUpdateComplete.UserFields.SupervisionTimeout, 16), "", "000A", "0C80", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
	}

	public override void UpdatePacketFromFieldInfo(int index, string value)
	{
		LeConnectionUpdateComplete leConnectionUpdateComplete = (LeConnectionUpdateComplete)packet;
		int num = -1;
		if (index == ++num)
		{
			throw new Exception("Tried to set non-settable field SubeventCode");
		}
		if (index == ++num)
		{
			leConnectionUpdateComplete.UserFields.Status = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			leConnectionUpdateComplete.UserFields.ConnectionHandle = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			leConnectionUpdateComplete.UserFields.ConnInterval = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			leConnectionUpdateComplete.UserFields.ConnLatency = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			leConnectionUpdateComplete.UserFields.SupervisionTimeout = Convert.ToUInt32(value, 16);
		}
	}
}
