using System;
using QCRPG.Framework;

namespace QCRPG.TSL.FMHCI.Packets;

public class LogInfo : PacketInfoBase
{
	public override string Name => "Log";

	public override string DisplayGroup => "Events";

	public LogInfo(Log packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		Log log = (Log)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("Log Type", Log.UserFieldsWrapper.LogTypeInfo.LogTypeToDescription(), 2, FormatValue(log.UserFields.LogType, 2), Log.UserFieldsWrapper.LogTypeInfo.LogTypeToString(Log.UserFieldsWrapper.LogTypeInfo.ValueToLogType(log.UserFields.LogType)), "", "3", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Audio Log Type", "Only used if Log Type is AUDIO (0x02)\r\n\r\n" + Log.UserFieldsWrapper.AudioLogTypeInfo.AudioLogTypeToDescription(), 2, FormatValue(log.UserFields.AudioLogType, 2), Log.UserFieldsWrapper.AudioLogTypeInfo.AudioLogTypeToString(Log.UserFieldsWrapper.AudioLogTypeInfo.ValueToAudioLogType(log.UserFields.AudioLogType)), "", "3", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Audio Log Sample Rate", "Only used if Log Type is AUDIO (0x02)\r\n\r\n" + Log.UserFieldsWrapper.AudioLogSampleRateInfo.AudioLogSampleRateToDescription(), 2, FormatValue(log.UserFields.AudioLogSampleRate, 2), Log.UserFieldsWrapper.AudioLogSampleRateInfo.AudioLogSampleRateToString(Log.UserFieldsWrapper.AudioLogSampleRateInfo.ValueToAudioLogSampleRate(log.UserFields.AudioLogSampleRate)), "", "3", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Audio Log Channel Count", "Only used if Log Type is AUDIO (0x02)\r\n\r\n" + Log.UserFieldsWrapper.AudioLogChannelCountInfo.AudioLogChannelCountToDescription(), 1, FormatValue(log.UserFields.AudioLogChannelCount, 1), Log.UserFieldsWrapper.AudioLogChannelCountInfo.AudioLogChannelCountToString(Log.UserFieldsWrapper.AudioLogChannelCountInfo.ValueToAudioLogChannelCount(log.UserFields.AudioLogChannelCount)), "", "1", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Reserved", "", 1, FormatValue(log.UserFields.Reserved, 1), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Log Sequence Number", "Sequence number is incremented by 1 and goes back to 0 after reaching 0xFF.  Two separate sequence numbers are maintained per Log Type.", 8, FormatValue(log.UserFields.LogSequenceNumber, 8), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Log Timestamp", "Timestamp in string format (mmssSSS) not including the terminating null character at the end", 56, FormatValue(log.UserFields.LogTimestamp, 56), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Log String Length", "Length of log string", 8, FormatValue(log.UserFields.LogStringLength, 8), "", "", "EB", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Log Data", "Either a log message string or raw audio bytes", log.UserFields.LogDataList.Count * 8, FormatValue(log.UserFields.LogDataList, log.UserFields.LogDataList.Count * 8, includeSpaces: false), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
	}

	public override void UpdatePacketFromFieldInfo(int index, string value)
	{
		Log log = (Log)packet;
		int num = -1;
		if (index == ++num)
		{
			log.UserFields.LogType = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			log.UserFields.AudioLogType = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			log.UserFields.AudioLogSampleRate = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			log.UserFields.AudioLogChannelCount = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			log.UserFields.Reserved = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			log.UserFields.LogSequenceNumber = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			log.UserFields.LogTimestamp = Convert.ToUInt64(value, 16);
		}
		else if (index == ++num)
		{
			log.UserFields.LogStringLength = Convert.ToUInt32(value, 16);
		}
		else if (index == ++num)
		{
			value = AddLeadingZerosAndCheckValueStringLength(value, log.UserFields.LogDataList.Count);
			int num2 = 0;
			while (value.Length > 0)
			{
				string value2 = value.Substring(0, 2);
				value = value.Remove(0, 2);
				log.UserFields.LogDataList[num2] = Convert.ToUInt32(value2, 16);
				num2++;
			}
		}
	}
}
