using System;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class SetExternalFrameConfigurationInfo : PacketInfoBase
{
	public override string Name => "Set External Frame Configuration";

	public override string DisplayGroup => "Host Control and Baseband";

	public SetExternalFrameConfigurationInfo(SetExternalFrameConfiguration packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		SetExternalFrameConfiguration setExternalFrameConfiguration = (SetExternalFrameConfiguration)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("Ext_Frame_Duration", "External frame duration in microseconds (unsigned integer)", 16, FormatValue(setExternalFrameConfiguration.UserFields.ExtFrameDuration, 16), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Ext_Frame_Sync_Assert_Offset", "External frame offset in microseconds (signed integer)", 16, FormatValue(setExternalFrameConfiguration.UserFields.ExtFrameSyncAssertOffset, 16), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Ext_Frame_Sync_Assert_Jitter", "External frame sync jitter in microseconds (unsigned integer)", 16, FormatValue(setExternalFrameConfiguration.UserFields.ExtFrameSyncAssertJitter, 16), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Ext_Num_Periods", "Number of specified periods in an external frame. Valid range: 1 to 32 (unsigned integer)", 8, FormatValue(setExternalFrameConfiguration.UserFields.ExtNumPeriods, 8), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		for (int i = 0; i < setExternalFrameConfiguration.UserFields.NumPeriodList.Count; i++)
		{
			fieldInfoList.Add(new PacketFieldInfo("Period_Duration[" + i + "]", "External frame duration in microseconds (unsigned integer)", 16, FormatValue(setExternalFrameConfiguration.UserFields.NumPeriodList[i].PeriodDuration, 16), "", "", "", settable: true, littleEndianTransport: true, omitFromTransport: false, DataEntryMode.RightAlignedHex));
			fieldInfoList.Add(new PacketFieldInfo("Period_Type[" + i + "]", SetExternalFrameConfiguration.NumPeriodSubField.PeriodTypeInfo.PeriodTypeToDescription(), 8, FormatValue(setExternalFrameConfiguration.UserFields.NumPeriodList[i].PeriodType, 8), SetExternalFrameConfiguration.NumPeriodSubField.PeriodTypeInfo.PeriodTypeToString(SetExternalFrameConfiguration.NumPeriodSubField.PeriodTypeInfo.ValueToPeriodType(setExternalFrameConfiguration.UserFields.NumPeriodList[i].PeriodType)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		}
	}

	public override void UpdatePacketFromFieldInfo(int index, string value)
	{
		SetExternalFrameConfiguration setExternalFrameConfiguration = (SetExternalFrameConfiguration)packet;
		int num = -1;
		if (index == ++num)
		{
			setExternalFrameConfiguration.UserFields.ExtFrameDuration = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			setExternalFrameConfiguration.UserFields.ExtFrameSyncAssertOffset = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			setExternalFrameConfiguration.UserFields.ExtFrameSyncAssertJitter = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			setExternalFrameConfiguration.UserFields.ExtNumPeriods = Convert.ToUInt32(value, 16);
			return;
		}
		for (int i = 0; i < setExternalFrameConfiguration.UserFields.NumPeriodList.Count; i++)
		{
			if (index == ++num)
			{
				setExternalFrameConfiguration.UserFields.NumPeriodList[i].PeriodDuration = Convert.ToUInt32(value, 16);
				break;
			}
			if (index == ++num)
			{
				setExternalFrameConfiguration.UserFields.NumPeriodList[i].PeriodType = Convert.ToUInt32(value, 16);
				break;
			}
		}
	}
}
