#define DEBUG
using System;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace qk30ic;

internal class Logfile
{
	public static uint ReadLogFile()
	{
		string[,] array = new string[15, 12];
		int num = 0;
		Debug.WriteLine(Form1.LogfilePath, "★LogFileName");
		if (!Directory.Exists(Form1.LogfilePath))
		{
			return 1u;
		}
		DirectoryInfo directoryInfo = new DirectoryInfo(Form1.LogfilePath);
		if (!directoryInfo.Exists)
		{
			return 0u;
		}
		FileInfo[] files = directoryInfo.GetFiles();
		if (files.Length == 0)
		{
			return 0u;
		}
		Array.Sort(files, (FileInfo a, FileInfo b) => a.Name.CompareTo(b.Name));
		Array.Reverse(files);
		string path = Form1.LogfilePath + files[0];
		try
		{
			using StreamReader streamReader = new StreamReader(path, Encoding.GetEncoding("shift_jis"));
			int num2 = 0;
			while (!streamReader.EndOfStream)
			{
				num = num2;
				string text = streamReader.ReadLine();
				string[] array2 = text.Split(',');
				int num3 = 0;
				string[] array3 = array2;
				foreach (string text2 in array3)
				{
					array[num2, num3++] = text2;
				}
				num2++;
				if (num2 == 15)
				{
					num2 = 0;
				}
			}
			for (int num5 = 0; num5 < 15; num5++)
			{
				Form1.koumoku[num5, 0] = array[num, 0];
				Form1.koumoku[num5, 1] = array[num, 3];
				Form1.koumoku[num5, 2] = array[num, 4];
				Form1.koumoku[num5, 3] = array[num, 5];
				Form1.koumoku[num5, 4] = array[num, 6];
				Form1.koumoku[num5, 5] = array[num, 7];
				Form1.koumoku[num5, 6] = array[num, 8];
				Form1.koumoku[num5, 7] = array[num, 9];
				num--;
				if (num < 0)
				{
					num = 14;
				}
			}
		}
		catch (Exception ex)
		{
			Debug.WriteLine("★★_ERROR_28");
			Console.WriteLine(ex.Message);
			KEEPALIVE.AutoClosingMessageBox.Show("ログファイルのフォーマットエラーがありますが、そのまま起動します。", "起動ワーニング", 5000);
			return 0u;
		}
		return 0u;
	}
}
