using System;
using QCRPG.Framework;

namespace QCRPG.TSL.FM.Packets;

public class StartSearchInfo : PacketInfoBase
{
	public override string Name => "Start Search";

	public override string DisplayGroup => "Control";

	public StartSearchInfo(StartSearch packet)
		: base(packet)
	{
		UpdateFieldInfoFromPacket();
	}

	public override void UpdateFieldInfoFromPacket()
	{
		StartSearch startSearch = (StartSearch)packet;
		fieldInfoList.Clear();
		fieldInfoList.Add(new PacketFieldInfo("Register Address", RegisterMapInfo.RegisterMapToDescription(), 8, FormatValue(startSearch.UserFields.RegisterAddress, 8), RegisterMapInfo.RegisterMapToString(RegisterMapInfo.ValueToRegisterMap(startSearch.UserFields.RegisterAddress)), "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Unused 1", "", 12, FormatValue(startSearch.UserFields.Unused1, 12), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Scan Number", "Number of strong/weak stations to scan; use 0 to scan the entire band", 4, FormatValue(startSearch.UserFields.ScanNumber, 4), "", "", "C", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Search Control", StartSearch.UserFieldsWrapper.SearchControlInfo.SearchControlToDescription(), 1, FormatValue(startSearch.UserFields.SearchControl, 1), StartSearch.UserFieldsWrapper.SearchControlInfo.SearchControlToString(StartSearch.UserFieldsWrapper.SearchControlInfo.ValueToSearchControl(startSearch.UserFields.SearchControl)), "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Scan Time", "Amount of dwell time (sec) that the FM Controller will stay on a 'good' channel before tuning to the next channel.  Only valid for Scan modes.", 3, FormatValue(startSearch.UserFields.ScanTime, 3), "", "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Search Direction", StartSearch.UserFieldsWrapper.SearchDirectionInfo.SearchDirectionToDescription(), 1, FormatValue(startSearch.UserFields.SearchDirection, 1), StartSearch.UserFieldsWrapper.SearchDirectionInfo.SearchDirectionToString(StartSearch.UserFieldsWrapper.SearchDirectionInfo.ValueToSearchDirection(startSearch.UserFields.SearchDirection)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Unused 2", "", 1, FormatValue(startSearch.UserFields.Unused2, 1), "", "", "", settable: false, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
		fieldInfoList.Add(new PacketFieldInfo("Search Mode", StartSearch.UserFieldsWrapper.SearchModeInfo.SearchModeToDescription(), 2, FormatValue(startSearch.UserFields.SearchMode, 2), StartSearch.UserFieldsWrapper.SearchModeInfo.SearchModeToString(StartSearch.UserFieldsWrapper.SearchModeInfo.ValueToSearchMode(startSearch.UserFields.SearchMode)), "", "", settable: true, littleEndianTransport: false, omitFromTransport: false, DataEntryMode.RightAlignedHex));
	}

	public override void UpdatePacketFromFieldInfo(int index, string value)
	{
		StartSearch startSearch = (StartSearch)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 Unused1");
		}
		if (index == ++num)
		{
			startSearch.UserFields.ScanNumber = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			throw new Exception("Tried to set non-settable field SearchControl");
		}
		if (index == ++num)
		{
			startSearch.UserFields.ScanTime = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			startSearch.UserFields.SearchDirection = Convert.ToUInt32(value, 16);
			return;
		}
		if (index == ++num)
		{
			throw new Exception("Tried to set non-settable field Unused2");
		}
		if (index == ++num)
		{
			startSearch.UserFields.SearchMode = Convert.ToUInt32(value, 16);
		}
	}
}
