using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace QC.MMI.MMI_Common;

public class IniFileUtils
{
	public const string EncodingIni = "utf-8";

	private static string filePath = "";

	public static string IniFilePath
	{
		get
		{
			return filePath;
		}
		set
		{
			filePath = value;
		}
	}

	public IniFileUtils()
	{
	}

	public IniFileUtils(string path)
	{
		IniFilePath = path;
	}

	[DllImport("kernel32")]
	public static extern bool WritePrivateProfileString(byte[] section, byte[] key, byte[] val, string filePath);

	[DllImport("kernel32")]
	public static extern int GetPrivateProfileString(byte[] section, byte[] key, byte[] def, byte[] retVal, int size, string filePath);

	[DllImport("kernel32")]
	public static extern bool WritePrivateProfileSection(byte[] section, byte[] val, string filePath);

	[DllImport("kernel32")]
	public static extern int GetPrivateProfileSection(byte[] section, byte[] retString, int nSize, string filePath);

	public static bool WriteSectionKeyValue(string SectionName, string Key, string Val, string FileName)
	{
		bool flag = false;
		return WriteString(SectionName, Key, Val, FileName);
	}

	public static bool WriteSectionKeyValue(string SectionName, string Key, string Val)
	{
		bool flag = false;
		return WriteString(SectionName, Key, Val, IniFilePath);
	}

	public static bool WriteSectionValue(string SectionName, string Val, string FileName)
	{
		bool flag = false;
		return WriteSectionString(SectionName, Val, FileName);
	}

	public static bool WriteSectionValue(string SectionName, string Val)
	{
		bool flag = false;
		return WriteSectionString(SectionName, Val, IniFilePath);
	}

	public static List<string> GetSectionNames(string FilePath)
	{
		List<string> list = new List<string>();
		string[] array = File.ReadAllLines(FilePath);
		string[] array2 = array;
		foreach (string text in array2)
		{
			text.TrimStart();
			if (text.Length > 0 && text[0] == '[' && text.Contains("]"))
			{
				list.Add(text.TrimStart().TrimEnd().Replace("[", "")
					.Replace("]", ""));
			}
		}
		return list;
	}

	public static List<string> GetSectionNames()
	{
		List<string> list = new List<string>();
		string[] array = File.ReadAllLines(IniFilePath);
		string[] array2 = array;
		foreach (string text in array2)
		{
			if (text.Contains("[") && text.Contains("]"))
			{
				list.Add(text.TrimStart().TrimEnd().Replace("[", "")
					.Replace("]", ""));
			}
		}
		return list;
	}

	public static string GetSectionKeyValue(string SectionName, string KeyName, string FilePath)
	{
		string text = "";
		return ReadString(SectionName, KeyName, "", FilePath);
	}

	public static string GetSectionKeyValue(string SectionName, string KeyName)
	{
		string text = "";
		return ReadString(SectionName, KeyName, "", IniFilePath);
	}

	public static string GetSectionValue(string SectionName, string FilePath)
	{
		string result = "";
		byte[] array = new byte[4096];
		int privateProfileSection = GetPrivateProfileSection(getBytes(SectionName, "utf-8"), array, array.GetUpperBound(0), FilePath);
		if (privateProfileSection > 0)
		{
			result = Encoding.GetEncoding("utf-8").GetString(array, 0, privateProfileSection).Trim()
				.Replace("\0", "\r\n");
		}
		return result;
	}

	public static string GetSectionValue(string SectionName)
	{
		string result = "";
		byte[] array = new byte[4096];
		int privateProfileSection = GetPrivateProfileSection(getBytes(SectionName, "utf-8"), array, array.GetUpperBound(0), IniFilePath);
		if (privateProfileSection > 0)
		{
			result = Encoding.GetEncoding("utf-8").GetString(array, 0, privateProfileSection).Trim()
				.Replace("\0", "\r\n");
		}
		return result;
	}

	public static void ClearAll(string FilePath)
	{
		File.WriteAllText(FilePath, string.Empty, Encoding.UTF8);
	}

	public static void ClearAll()
	{
		File.WriteAllText(IniFilePath, string.Empty, Encoding.UTF8);
	}

	public static bool ClearSection(string SectionName, string FilePath)
	{
		return WriteSectionKeyValue(SectionName, null, null, FilePath);
	}

	public static bool ClearSection(string SectionName)
	{
		return WriteSectionKeyValue(SectionName, null, null, IniFilePath);
	}

	public static bool ClearKey(string SectionName, string KeyName, string FilePath)
	{
		return WriteSectionKeyValue(SectionName, KeyName, null, FilePath);
	}

	public static bool ClearKey(string SectionName, string KeyName)
	{
		return WriteSectionKeyValue(SectionName, KeyName, null, IniFilePath);
	}

	public static void FormatAll(string FilePath)
	{
		string text = File.ReadAllText(FilePath, Encoding.UTF8);
		text = text.Replace("[", "\r\n[").TrimStart();
		File.WriteAllText(FilePath, text, Encoding.UTF8);
	}

	public static void FormatAll()
	{
		string text = File.ReadAllText(IniFilePath, Encoding.UTF8);
		text = text.Replace("[", "\r\n[").TrimStart();
		File.WriteAllText(IniFilePath, text, Encoding.UTF8);
	}

	private static byte[] getBytes(string s, string encodingName)
	{
		if (s != null)
		{
			return Encoding.GetEncoding(encodingName).GetBytes(s);
		}
		return null;
	}

	private static string ReadString(string section, string key, string def, string fileName, string encodingName = "utf-8", int size = 1024)
	{
		byte[] array = new byte[size];
		int privateProfileString = GetPrivateProfileString(getBytes(section, encodingName), getBytes(key, encodingName), getBytes(def, encodingName), array, size, fileName);
		return Encoding.GetEncoding(encodingName).GetString(array, 0, privateProfileString).Trim();
	}

	private static bool WriteString(string section, string key, string value, string fileName, string encodingName = "utf-8")
	{
		return WritePrivateProfileString(getBytes(section, encodingName), getBytes(key, encodingName), getBytes(value, encodingName), fileName);
	}

	private static bool WriteSectionString(string section, string value, string fileName, string encodingName = "utf-8")
	{
		return WritePrivateProfileSection(getBytes(section, encodingName), getBytes(value, encodingName), fileName);
	}
}
