using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace NPTGUI;

internal class XMLHandler
{
	public const string file_path1 = "..\\Databases\\NPT_TechInfo.xml";

	public const string file_path2 = "C:\\Program Files (x86)\\Qualcomm\\QDART\\Databases\\NPT_TechInfo.xml";

	public const string databasepath1 = "..\\Databases\\";

	public const string databasepath2 = "C:\\Program Files (x86)\\Qualcomm\\QDART\\Databases\\";

	public static List<TechInfo> ReadInXML()
	{
		List<TechInfo> list = null;
		List<TechInfo> list2 = null;
		string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
		string path = "..\\Databases\\NPT_TechInfo.xml";
		string path2 = "C:\\Program Files (x86)\\Qualcomm\\QDART\\Databases\\NPT_TechInfo.xml";
		List<string> list3 = new List<string>();
		if (File.Exists(path))
		{
			try
			{
				string[] files = Directory.GetFiles(baseDirectory + "..\\Databases\\", "*.xml");
				string[] array = files;
				foreach (string text in array)
				{
					if (text.Contains("NPT_TechInfo_NR5G"))
					{
						list3.Add(text);
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show("Error : " + ex.Message);
			}
			try
			{
				XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<TechInfo>));
				using FileStream stream = File.OpenRead(path);
				list = xmlSerializer.Deserialize(stream) as List<TechInfo>;
			}
			catch (Exception ex2)
			{
				MessageBox.Show("Error, Could not read XML input file : " + ex2.Message);
			}
		}
		else if (File.Exists(path2))
		{
			try
			{
				string[] files = Directory.GetFiles("C:\\Program Files (x86)\\Qualcomm\\QDART\\Databases\\", "*.xml");
				string[] array2 = files;
				foreach (string text2 in array2)
				{
					if (text2.Contains("NPT_TechInfo_NR5G"))
					{
						list3.Add(text2);
					}
				}
			}
			catch (Exception ex3)
			{
				MessageBox.Show("Error : " + ex3.Message);
			}
			try
			{
				XmlSerializer xmlSerializer2 = new XmlSerializer(typeof(List<TechInfo>));
				using FileStream stream2 = File.OpenRead(path2);
				list = xmlSerializer2.Deserialize(stream2) as List<TechInfo>;
			}
			catch (Exception ex4)
			{
				MessageBox.Show("Error, Could not read XML input file : " + ex4.Message);
			}
		}
		else
		{
			MessageBox.Show("Error, XML File NPT_TechInfo.xml not found");
		}
		foreach (string item in list3)
		{
			try
			{
				XmlSerializer xmlSerializer3 = new XmlSerializer(typeof(List<TechInfo>));
				using FileStream stream3 = File.OpenRead(item);
				list2 = xmlSerializer3.Deserialize(stream3) as List<TechInfo>;
				if (list == null)
				{
					list = new List<TechInfo>();
				}
				list.AddRange(list2);
			}
			catch
			{
			}
		}
		return list;
	}
}
