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

namespace QCRPG.Framework;

public class NVMFileReader
{
	private string filename;

	private List<string> lines;

	private int currentLine;

	private string currentSection = "";

	private int tagCount;

	public List<NVMTag> ReadTags(string filename)
	{
		List<NVMTag> list = new List<NVMTag>();
		this.filename = filename;
		ReadLinesFromFile();
		tagCount = GetTagCount();
		VerifyTagCount();
		for (int i = 0; i < tagCount; i++)
		{
			list.Add(GetTag(i));
		}
		return list;
	}

	private void ReadLinesFromFile()
	{
		lines = new List<string>();
		string[] array = File.ReadAllLines(filename);
		string[] array2 = array;
		foreach (string text in array2)
		{
			string text2 = text.Trim();
			if (text2 != "")
			{
				lines.Add(text2);
			}
		}
	}

	private int GetTagCount()
	{
		FindSection("Tag");
		return GetIntValue("Num");
	}

	private void VerifyTagCount()
	{
		int num = 0;
		for (int i = currentLine; i < lines.Count; i++)
		{
			if (lines[i].StartsWith("[Tag"))
			{
				num++;
			}
		}
		if (num != tagCount)
		{
			throw new Exception($"Tag count mismatch; expected {tagCount} but found {num}");
		}
	}

	private NVMTag GetTag(int tagNumber)
	{
		NVMTag result = default(NVMTag);
		FindSection("Tag" + tagNumber);
		result.tagNumber = GetIntValue("TagNum");
		result.tagLength = GetIntValue("TagLength");
		result.tagValue = GetByteArrayValues("TagValue", result.tagLength);
		return result;
	}

	private void FindSection(string section)
	{
		while (currentLine < lines.Count)
		{
			if (lines[currentLine] == "[" + section + "]")
			{
				currentLine++;
				currentSection = section;
				return;
			}
			currentLine++;
		}
		throw new Exception("Could't find section " + section);
	}

	private int GetIntValue(string expectedKey)
	{
		if (!lines[currentLine].Contains("="))
		{
			throw new Exception($"Couldn't find equals when looking for key {expectedKey} in section {currentSection} on line \"{lines[currentLine]}\"");
		}
		string[] array = lines[currentLine].Split(new char[1] { '=' }, StringSplitOptions.RemoveEmptyEntries);
		if (array.Length != 2)
		{
			throw new Exception($"Couldn't split line when looking for key {expectedKey} in section {currentSection} on line \"{lines[currentLine]}\"");
		}
		string text = array[0].Trim();
		string value = array[1].Trim();
		if (text != expectedKey)
		{
			throw new Exception($"Couldn't find expected key {expectedKey} in section {currentSection} on line \"{lines[currentLine]}\"");
		}
		int num = 0;
		try
		{
			num = Convert.ToInt32(value);
		}
		catch (Exception)
		{
			throw new Exception($"Couldn't convert value to int in section {currentSection} on line \"{lines[currentLine]}\"");
		}
		currentLine++;
		return num;
	}

	private byte[] GetByteArrayValues(string expectedKey, int expectedLength)
	{
		if (!lines[currentLine].Contains("="))
		{
			throw new Exception($"Couldn't find equals when looking for key {expectedKey} in section {currentSection} on line \"{lines[currentLine]}\"");
		}
		string[] array = lines[currentLine].Split(new char[1] { '=' }, StringSplitOptions.RemoveEmptyEntries);
		if (array.Length != 2)
		{
			throw new Exception($"Couldn't split line when looking for key {expectedKey} in section {currentSection} on line \"{lines[currentLine]}\"");
		}
		string text = array[0].Trim();
		string text2 = array[1].Trim();
		if (text != expectedKey)
		{
			throw new Exception($"Couldn't find expected key {expectedKey} in section {currentSection} on line \"{lines[currentLine]}\"");
		}
		byte[] array2 = null;
		try
		{
			array2 = Conversions.HexStringToByteArray(text2.Replace(" ", ""));
		}
		catch (Exception)
		{
			throw new Exception($"Couldn't convert value to byte array in section {currentSection} on line \"{lines[currentLine]}\"");
		}
		if (array2.Length != expectedLength)
		{
			throw new Exception($"Byte array length mismatch in section {currentSection} on line \"{lines[currentLine]}\"");
		}
		currentLine++;
		return array2;
	}
}
