using System;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class SynchronizationTrainRecievedInfo : PacketInfoBase
{
	public override string Name => "Synchronization Train Recieved";

	public override string DisplayGroup => "Link Control";

	public SynchronizationTrainRecievedInfo(SynchronizationTrainRecieved packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		SynchronizationTrainRecieved synchronizationTrainRecieved = (SynchronizationTrainRecieved)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("Status", ErrorCodeInfo.ErrorCodeToDescription(), 8, FormatValue(synchronizationTrainRecieved.UserFields.Status, 8), ErrorCodeInfo.ErrorCodeToString(ErrorCodeInfo.ValueToErrorCode(synchronizationTrainRecieved.UserFields.Status)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("BD Address", "BD_ADDR of the Connectionless Slave Broadcast transmitter.", 48, FormatValue(synchronizationTrainRecieved.UserFields.BdAddress, 48), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Clock_Offset", "(CLKNslave - CLK) modulo 2^28.", 32, FormatValue(synchronizationTrainRecieved.UserFields.ClockOffset, 32), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("AFH_Channel_Map", "This parameter contains 80 1-bit fields. The nth such field (in the range 0 to 78) contains the value for channel n: 0: channel n is unused, 1: channel n is used. The most significant bit (bit 79) is reserved and shall be set to 0 when transmitted and ignored when received", synchronizationTrainRecieved.UserFields.AfhChannelMapList.Count * 8, FormatValue(synchronizationTrainRecieved.UserFields.AfhChannelMapList, synchronizationTrainRecieved.UserFields.AfhChannelMapList.Count * 8, includeSpaces: false), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("LT_ADDR", SynchronizationTrainRecieved.UserFieldsWrapper.LtAddrInfo.LtAddrToDescription(), 8, FormatValue(synchronizationTrainRecieved.UserFields.LtAddr, 8), SynchronizationTrainRecieved.UserFieldsWrapper.LtAddrInfo.LtAddrToString(SynchronizationTrainRecieved.UserFieldsWrapper.LtAddrInfo.ValueToLtAddr(synchronizationTrainRecieved.UserFields.LtAddr)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Next_Broadcast_Instant", "CLK of a future broadcast on this channel.", 32, FormatValue(synchronizationTrainRecieved.UserFields.NextBroadcastInstant, 32), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Connectionless_Slave_Broadcast_Interval", "Interval between Connectionless Slave Broadcast instants in slots (only even values are valid).", 16, FormatValue(synchronizationTrainRecieved.UserFields.ConnectionlessSlaveBroadcastInterval, 16), "", "0002", "FFFE", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Service_Data", "Value from octet 27 of the Synchronization Train packet. See BT Specification document.", 8, FormatValue(synchronizationTrainRecieved.UserFields.ServiceData, 8), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
	}

	public override void UpdatePacketFromFieldInfo(int index, string value)
	{
		SynchronizationTrainRecieved synchronizationTrainRecieved = (SynchronizationTrainRecieved)packet;
		int num = -1;
		if (index == ++num)
		{
			synchronizationTrainRecieved.UserFields.Status = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			synchronizationTrainRecieved.UserFields.BdAddress = Convert.ToUInt64(value, 16);
			return;
		}
		if (index == ++num)
		{
			synchronizationTrainRecieved.UserFields.ClockOffset = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			value = AddLeadingZerosAndCheckValueStringLength(value, synchronizationTrainRecieved.UserFields.AfhChannelMapList.Count);
			int num2 = 0;
			while (value.Length > 0)
			{
				string value2 = value.Substring(0, 2);
				value = value.Remove(0, 2);
				synchronizationTrainRecieved.UserFields.AfhChannelMapList[num2] = Convert.ToUInt32(value2, 16);
				num2++;
			}
		}
		if (index == ++num)
		{
			synchronizationTrainRecieved.UserFields.LtAddr = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			synchronizationTrainRecieved.UserFields.NextBroadcastInstant = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			synchronizationTrainRecieved.UserFields.ConnectionlessSlaveBroadcastInterval = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			synchronizationTrainRecieved.UserFields.ServiceData = Convert.ToUInt32(value, 16);
		}
	}
}
