using System;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Xsl;

namespace QCLVR;

public class main
{
	private static Thread th1;

	private static Thread th2;

	private static FileInfo inputFile;

	public bool InputCompressed;

	public XmlReader XmlreaderTable;

	public XmlReader XmlreaderData;

	public bool ValidateFile(string FileName)
	{
		if (!File.Exists(FileName))
		{
			System.Windows.MessageBox.Show("File does not exist");
			return false;
		}
		string extension = Path.GetExtension(FileName);
		if (extension == ".xml")
		{
			InputCompressed = false;
			return true;
		}
		if (extension == ".cxml")
		{
			InputCompressed = true;
			return true;
		}
		System.Windows.MessageBox.Show($"This file is unrecognized. {FileName}");
		return false;
	}

	public static string SelectFile()
	{
		OpenFileDialog openFileDialog = new OpenFileDialog();
		openFileDialog.Multiselect = false;
		openFileDialog.Filter = "Data files (*.xml; *.cxml) | *.xml; *.cxml";
		switch (openFileDialog.ShowDialog())
		{
		case DialogResult.OK:
			return openFileDialog.FileName;
		case DialogResult.Cancel:
			return "";
		default:
			System.Windows.MessageBox.Show("Something went wrong here");
			return "";
		}
	}

	public void setFilePath(string InputFile)
	{
		inputFile = new FileInfo(InputFile);
	}

	public void run()
	{
		int num = new Random((int)DateTime.Now.Ticks).Next();
		string text = Path.GetTempPath() + Path.GetFileNameWithoutExtension(inputFile.FullName) + ".xml";
		string TempInputFilePath = Path.GetTempPath() + Path.GetFileNameWithoutExtension(inputFile.FullName) + "_" + num;
		string TempInputFilePathHtml = TempInputFilePath + ".html";
		LoadWindow loadWindow = new LoadWindow();
		loadWindow.Show();
		if (InputCompressed)
		{
			loadWindow.STATUS.Text = "Decompressing..";
			Compression.DecompressCXML(inputFile, text);
		}
		else
		{
			text = inputFile.FullName;
		}
		SetXmlReaders(text);
		loadWindow.STATUS.Text = "Transforming..";
		th1 = new Thread((ThreadStart)delegate
		{
			LoadXmlTable(TempInputFilePathHtml);
		});
		th2 = new Thread((ThreadStart)delegate
		{
			LoadXmlData(TempInputFilePath + "DATA.xml");
		});
		th1.Start();
		th2.Start();
		th1.Join();
		th2.Join();
		MainWindow mainWindow = new MainWindow(text, TempInputFilePath, inputFile, InputCompressed);
		mainWindow.Navigate();
		mainWindow.Show();
		loadWindow.Close();
	}

	public void LoadXmlData(string XmlDataFilePath)
	{
		XsltSettings settings = new XsltSettings(enableDocumentFunction: true, enableScript: true);
		XslCompiledTransform xslCompiledTransform = new XslCompiledTransform();
		XmlReader stylesheet = XmlReader.Create(new StringReader(Resource1.HTML_QDART_DATA));
		try
		{
			xslCompiledTransform.Load(stylesheet, settings, null);
		}
		catch (Exception ex)
		{
			System.Windows.MessageBox.Show(ex.Message);
		}
		try
		{
			using XmlWriter xmlWriter = XmlWriter.Create(XmlDataFilePath);
			xslCompiledTransform.Transform(XmlreaderData, xmlWriter);
			xmlWriter.Flush();
			xmlWriter.Close();
		}
		catch (Exception ex2)
		{
			System.Windows.MessageBox.Show(ex2.Message);
		}
		XmlreaderData.Close();
		XmlreaderData = null;
	}

	public void LoadXmlTable(string XmlTableFilePath)
	{
		XsltSettings settings = new XsltSettings(enableDocumentFunction: true, enableScript: true);
		XslCompiledTransform xslCompiledTransform = new XslCompiledTransform();
		XmlReader stylesheet = XmlReader.Create(new StringReader(Resource1.HTML_QDART_TABLE));
		try
		{
			xslCompiledTransform.Load(stylesheet, settings, null);
		}
		catch (Exception ex)
		{
			System.Windows.MessageBox.Show(ex.Message);
		}
		try
		{
			using FileStream fileStream = new FileStream(XmlTableFilePath, FileMode.Create);
			xslCompiledTransform.Transform(XmlreaderTable, null, fileStream);
			fileStream.Dispose();
			fileStream.Close();
		}
		catch (Exception ex2)
		{
			System.Windows.MessageBox.Show(ex2.Message);
		}
		XmlreaderTable.Close();
		XmlreaderTable = null;
	}

	public void SetXmlReaders(string FilePath)
	{
		Thread thread = new Thread((ThreadStart)delegate
		{
			XmlreaderTable = XmlReader.Create(FilePath);
		});
		Thread thread2 = new Thread((ThreadStart)delegate
		{
			XmlreaderData = XmlReader.Create(FilePath);
		});
		thread.Start();
		thread2.Start();
		thread.Join();
		thread2.Join();
	}
}
