using System;
using QCRPG.Framework;

namespace QCRPG.TSL.FM.Packets;

public class RdsTransmitProgramServiceInfo : PacketInfoBase
{
	public override string Name => "Rds Transmit Program Service";

	public override string DisplayGroup => "RDS Control";

	public RdsTransmitProgramServiceInfo(RdsTransmitProgramService packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		RdsTransmitProgramService rdsTransmitProgramService = (RdsTransmitProgramService)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("Read Only", "", 4, FormatValue(rdsTransmitProgramService.UserFields.ReadOnly, 4), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Number Of Program Service Names", "", 4, FormatValue(rdsTransmitProgramService.UserFields.NumberOfProgramServiceNames, 4), "", "", "C", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Undefined 1", "", 4, FormatValue(rdsTransmitProgramService.UserFields.Undefined1, 4), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Program Type", "RDS program type\r\n\r\n" + ProgramTypeInfo.ProgramTypeToDescription(), 4, FormatValue(rdsTransmitProgramService.UserFields.ProgramType, 4), ProgramTypeInfo.ProgramTypeToString(ProgramTypeInfo.ValueToProgramType(rdsTransmitProgramService.UserFields.ProgramType)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Program Identification", "Program identification code", 16, FormatValue(rdsTransmitProgramService.UserFields.ProgramIdentification, 16), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("TX Control", RdsTransmitProgramService.UserFieldsWrapper.TxControlInfo.TxControlToDescription(), 1, FormatValue(rdsTransmitProgramService.UserFields.TxControl, 1), RdsTransmitProgramService.UserFieldsWrapper.TxControlInfo.TxControlToString(RdsTransmitProgramService.UserFieldsWrapper.TxControlInfo.ValueToTxControl(rdsTransmitProgramService.UserFields.TxControl)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Undefined 2", "", 3, FormatValue(rdsTransmitProgramService.UserFields.Undefined2, 3), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("TX Repeat", "Number of times to repeat each Program Service name", 4, FormatValue(rdsTransmitProgramService.UserFields.TxRepeat, 4), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		for (int i = 0; i < rdsTransmitProgramService.UserFields.ProgramServiceNameListList.Count; i++)
		{
			fieldInfoList.Add(new PacketFieldInfo("Program Service Name[" + i + "]", "Program service name", 64, FormatValue(rdsTransmitProgramService.UserFields.ProgramServiceNameListList[i].ProgramServiceName, 64), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.NullPaddedString));
		}
	}

	public override void UpdatePacketFromFieldInfo(int index, string value)
	{
		RdsTransmitProgramService rdsTransmitProgramService = (RdsTransmitProgramService)packet;
		int num = -1;
		if (index == ++num)
		{
			throw new Exception("Tried to set non-settable field ReadOnly");
		}
		if (index == ++num)
		{
			rdsTransmitProgramService.UserFields.NumberOfProgramServiceNames = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			throw new Exception("Tried to set non-settable field Undefined1");
		}
		if (index == ++num)
		{
			rdsTransmitProgramService.UserFields.ProgramType = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			rdsTransmitProgramService.UserFields.ProgramIdentification = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			rdsTransmitProgramService.UserFields.TxControl = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			throw new Exception("Tried to set non-settable field Undefined2");
		}
		if (index == ++num)
		{
			rdsTransmitProgramService.UserFields.TxRepeat = Convert.ToUInt32(value, 16);
			return;
		}
		for (int i = 0; i < rdsTransmitProgramService.UserFields.ProgramServiceNameListList.Count; i++)
		{
			if (index == ++num)
			{
				rdsTransmitProgramService.UserFields.ProgramServiceNameListList[i].ProgramServiceName = Convert.ToUInt64(value, 16);
				break;
			}
		}
	}
}
