using System;
using QCRPG.Framework;

namespace QCRPG.TSL.FM.Packets;

public class RdsConfigInfo : PacketInfoBase
{
	public override string Name => "Rds Config";

	public override string DisplayGroup => "RDS Control";

	public RdsConfigInfo(RdsConfig packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		RdsConfig rdsConfig = (RdsConfig)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("Register Address", RegisterMapInfo.RegisterMapToDescription(), 8, FormatValue(rdsConfig.UserFields.RegisterAddress, 8), RegisterMapInfo.RegisterMapToString(RegisterMapInfo.ValueToRegisterMap(rdsConfig.UserFields.RegisterAddress)), "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("XFR Mode", XfrModeMapInfo.XfrModeMapToDescription(), 8, FormatValue(rdsConfig.UserFields.XfrMode, 8), XfrModeMapInfo.XfrModeMapToString(XfrModeMapInfo.ValueToXfrModeMap(rdsConfig.UserFields.XfrMode)), "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("RDS Group Flush Timer", "RDS group buffer flush timer (sec) to prevent data from sitting in buffer; 0 = disable flush timer", 8, FormatValue(rdsConfig.UserFields.RdsGroupFlushTimer, 8), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("RDS Group Buffer Depth", "Number of RDS data groups to buffer before setting the RDSDAT and interrupting the Host", 8, FormatValue(rdsConfig.UserFields.RdsGroupBufferDepth, 8), "", "01", "16", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Block-B Match", "Block-B matching condition; used in conjunction with Block-B mask bits; if match occurs, BLOCKB interrupt bit is set", 16, FormatValue(rdsConfig.UserFields.BlockBMatch, 16), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Block-B Mask", "Set bits for parts of Block-B for which match is required", 16, FormatValue(rdsConfig.UserFields.BlockBMask, 16), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Fast PI Check", "Program Identification (PI) code for fast PI check; if PI matches, the PROGID interrupt bit is set", 16, FormatValue(rdsConfig.UserFields.FastPiCheck, 16), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("RDS Group Filter", "Bit 0=1: discard group 0A; bit 1=1: discard group 0B, etc.", 32, FormatValue(rdsConfig.UserFields.RdsGroupFilter, 32), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Program Service Config", "Bit 0=1: pass all PS strings to host; bit 1=1: enable PI match checking", 8, FormatValue(rdsConfig.UserFields.ProgramServiceConfig, 8), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
	}

	public override void UpdatePacketFromFieldInfo(int index, string value)
	{
		RdsConfig rdsConfig = (RdsConfig)packet;
		int num = -1;
		if (index == ++num)
		{
			throw new Exception("Tried to set non-settable field RegisterAddress");
		}
		if (index == ++num)
		{
			throw new Exception("Tried to set non-settable field XfrMode");
		}
		if (index == ++num)
		{
			rdsConfig.UserFields.RdsGroupFlushTimer = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			rdsConfig.UserFields.RdsGroupBufferDepth = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			rdsConfig.UserFields.BlockBMatch = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			rdsConfig.UserFields.BlockBMask = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			rdsConfig.UserFields.FastPiCheck = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			rdsConfig.UserFields.RdsGroupFilter = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			rdsConfig.UserFields.ProgramServiceConfig = Convert.ToUInt32(value, 16);
		}
	}
}
