using System;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

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

	public override string DisplayGroup => "LE Controller";

	public LeConnectionCompleteInfo(LeConnectionComplete packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		LeConnectionComplete leConnectionComplete = (LeConnectionComplete)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("Subevent_Code", "Subevent code for LE Connection Complete event", 8, FormatValue(leConnectionComplete.UserFields.SubeventCode, 8), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Status", ErrorCodeInfo.ErrorCodeToDescription(), 8, FormatValue(leConnectionComplete.UserFields.Status, 8), ErrorCodeInfo.ErrorCodeToString(ErrorCodeInfo.ValueToErrorCode(leConnectionComplete.UserFields.Status)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Connection_Handle", "Connection_Handle to be used to identify a connection between two Bluetooth devices. The Connection_Handle is used as an identifier for transmitting and receiving data. (0x0F00 - 0x0FFF Reserved for future use).", 16, FormatValue(leConnectionComplete.UserFields.ConnectionHandle, 16), "", "0000", "0EFF", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Role", RoleInfo.RoleToDescription(), 8, FormatValue(leConnectionComplete.UserFields.Role, 8), RoleInfo.RoleToString(RoleInfo.ValueToRole(leConnectionComplete.UserFields.Role)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Peer_Address_Type", LeConnectionComplete.UserFieldsWrapper.PeerAddressTypeInfo.PeerAddressTypeToDescription(), 8, FormatValue(leConnectionComplete.UserFields.PeerAddressType, 8), LeConnectionComplete.UserFieldsWrapper.PeerAddressTypeInfo.PeerAddressTypeToString(LeConnectionComplete.UserFieldsWrapper.PeerAddressTypeInfo.ValueToPeerAddressType(leConnectionComplete.UserFields.PeerAddressType)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Peer_Address", "Public Device Address or Random Device Address of the peer device", 48, FormatValue(leConnectionComplete.UserFields.PeerAddress, 48), "", "", "", 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(leConnectionComplete.UserFields.ConnInterval, 16), ValueTimes1p25MsecExpression.ValueTimes1p25MsecToString(leConnectionComplete.UserFields.ConnInterval), "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", 16, FormatValue(leConnectionComplete.UserFields.ConnLatency, 16), "", "0000", "01F3", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Supervision_Timeout", "Connection supervision timeout. Range: 0x000A to 0x0C80, Time = N * 10 msec, Time Range: 100 msec to 32 seconds", 16, FormatValue(leConnectionComplete.UserFields.SupervisionTimeout, 16), ValueTimes10MsecExpression.ValueTimes10MsecToString(leConnectionComplete.UserFields.SupervisionTimeout), "000A", "0C80", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Master_Clock_Accuracy", ClockAccuracyInfo.ClockAccuracyToDescription(), 8, FormatValue(leConnectionComplete.UserFields.MasterClockAccuracy, 8), ClockAccuracyInfo.ClockAccuracyToString(ClockAccuracyInfo.ValueToClockAccuracy(leConnectionComplete.UserFields.MasterClockAccuracy)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
	}

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