using System;
using System.Collections.Generic;
using System.IO;

namespace QCRPG.TSL.FMHCI;

public class SpurTableFileReader
{
	private string filename;

	private List<SpurTableInvalidLine> invalidLines;

	public List<SpurTableEntry> ReadSpurTableEntries(string filename)
	{
		List<SpurTableEntry> list = new List<SpurTableEntry>();
		this.filename = filename;
		invalidLines = new List<SpurTableInvalidLine>();
		int num = 0;
		string[] array = File.ReadAllLines(filename);
		foreach (string text in array)
		{
			num++;
			string text2 = text.Trim();
			if (!(text2 == "") && !text2.StartsWith("#"))
			{
				try
				{
					list.Add(GetSpurTableEntryFromLine(text2));
				}
				catch (Exception)
				{
					invalidLines.Add(new SpurTableInvalidLine(num, text));
				}
			}
		}
		return list;
	}

	public int GetInvalidLineCount()
	{
		return invalidLines.Count;
	}

	public List<SpurTableInvalidLine> GetInvalidLines()
	{
		return invalidLines;
	}

	private SpurTableEntry GetSpurTableEntryFromLine(string line)
	{
		SpurTableEntry result = default(SpurTableEntry);
		string[] array = line.Split(new char[1] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
		if (array.Length != 5)
		{
			throw new Exception("Incorrect number of fields: " + array.Length);
		}
		result.frequency = Convert.ToUInt32(array[0]);
		result.spursToTrack = Convert.ToUInt32(array[1]);
		result.spur0 = Convert.ToUInt32(array[2], 16);
		result.spur1 = Convert.ToUInt32(array[3], 16);
		result.spur2 = Convert.ToUInt32(array[4], 16);
		return result;
	}
}
