using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Navigation;

namespace QCLVR;

public partial class MainWindow : Window, IComponentConnector
{
	private Uri HTMLFile;

	private string XmlFilePath;

	private string XmlcopyFile;

	private string CxmlcopyFile;

	private string tempDataFilePath;

	private COMclass COMinterf;

	public MainWindow(string XMLpath, string filePath, FileInfo inputFile, bool compressed)
	{
		InitializeComponent();
		base.Left = 0.0;
		base.Top = 0.0;
		base.Width = SystemParameters.PrimaryScreenWidth / 2.0;
		base.Height = SystemParameters.PrimaryScreenHeight - 45.0;
		string uriString = filePath + ".html";
		HTMLFile = new Uri(uriString);
		base.Title = Path.GetFileNameWithoutExtension(XMLpath);
		tempDataFilePath = filePath;
		XmlcopyFile = Path.GetDirectoryName(inputFile.FullName) + "\\" + Path.GetFileNameWithoutExtension(inputFile.FullName) + ".xml";
		CxmlcopyFile = Path.GetDirectoryName(inputFile.FullName) + "\\" + Path.GetFileNameWithoutExtension(inputFile.FullName) + ".cxml";
		if (compressed)
		{
			compressBtn.Visibility = Visibility.Collapsed;
		}
		else
		{
			xmlBtn.Visibility = Visibility.Collapsed;
		}
		FilePathBox.Text = inputFile.FullName;
		XmlFilePath = XMLpath;
	}

	public void Navigate()
	{
		WebB.Navigate(HTMLFile);
		COMinterf = new COMclass(this, tempDataFilePath + "DATA.xml");
		WebB.ObjectForScripting = COMinterf;
	}

	private void NavigationService_Navigating(object sender, NavigatingCancelEventArgs e)
	{
		if (!e.Uri.OriginalString.Contains("file://") && !e.Uri.OriginalString.Contains("\\\\127.0.0.1") && e.NavigationMode == NavigationMode.New)
		{
			e.Cancel = true;
			Process.Start(AppDomain.CurrentDomain.BaseDirectory + "QCLVR.exe", " " + e.Uri.OriginalString);
		}
	}

	private void Button_Click_Home(object sender, RoutedEventArgs e)
	{
		WebBrowser webB = WebB;
		object[] args = new string[1] { "#TOC" };
		webB.InvokeScript("JumpTo", args);
	}

	private void Button_Click_XML(object sender, RoutedEventArgs e)
	{
		MessageBoxButton button = MessageBoxButton.YesNo;
		MessageBoxImage icon = MessageBoxImage.Question;
		MessageBoxResult messageBoxResult = MessageBox.Show("Would you like to create an xml copy in the current directory?", "", button, icon);
		if (messageBoxResult != MessageBoxResult.Yes)
		{
			_ = 7;
			return;
		}
		try
		{
			File.Copy(tempDataFilePath + ".xml", XmlcopyFile);
		}
		catch (IOException ex)
		{
			Console.WriteLine("{0}: The write operation could not be performed. File already exists", ex.GetType().Name);
			MessageBox.Show("File already exists");
		}
		Process.Start(Path.GetDirectoryName(XmlcopyFile));
	}

	private void Button_Click_Compress(object sender, RoutedEventArgs e)
	{
		MessageBoxButton button = MessageBoxButton.YesNo;
		MessageBoxImage icon = MessageBoxImage.Question;
		MessageBoxResult messageBoxResult = MessageBox.Show("Would you like to compressed this xml into a cxml file?", "", button, icon);
		if (messageBoxResult != MessageBoxResult.Yes)
		{
			_ = 7;
			return;
		}
		try
		{
			Compression.CompressXML(new FileInfo(XmlcopyFile), CxmlcopyFile);
			Process.Start(Path.GetDirectoryName(CxmlcopyFile));
		}
		catch (Exception)
		{
			MessageBox.Show("Something went wrong while decompressing");
		}
	}

	private void SearchBtn_Click(object sender, RoutedEventArgs e)
	{
		string text = main.SelectFile();
		if (text != "")
		{
			StartApp(text);
		}
	}

	private void Back_Click(object sender, RoutedEventArgs e)
	{
		if (WebB.CanGoBack)
		{
			WebB.GoBack();
		}
	}

	private void Forward_Click(object sender, RoutedEventArgs e)
	{
		if (WebB.CanGoForward)
		{
			WebB.GoForward();
		}
	}

	private void FilePathBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
	{
		FilePathBox.SelectAll();
		FilePathBox.Focus();
	}

	private void FilePathBox_KeyDown(object sender, KeyEventArgs e)
	{
		if (e.Key != Key.Return)
		{
			return;
		}
		string text = FilePathBox.Text;
		if (Directory.Exists(text))
		{
			string[] files = Directory.GetFiles(text, "*.xml");
			string[] files2 = Directory.GetFiles(text, "*.cxml");
			string[] array = files;
			foreach (string text2 in array)
			{
				if (!isEqual(text2, files2))
				{
					StartApp(text2);
				}
			}
			array = files2;
			foreach (string inputFile in array)
			{
				StartApp(inputFile);
			}
		}
		else if (!File.Exists(text))
		{
			MessageBox.Show("File does not exist");
		}
		else
		{
			StartApp(text);
		}
	}

	private bool isEqual(string file, string[] files)
	{
		for (int i = 0; i < files.Length; i++)
		{
			if (Path.GetFileNameWithoutExtension(files[i]) == Path.GetFileNameWithoutExtension(file))
			{
				return true;
			}
		}
		return false;
	}

	private void FilePathBox_PreviewDrop(object sender, DragEventArgs e)
	{
		string[] array = (string[])e.Data.GetData(DataFormats.FileDrop);
		foreach (string inputFile in array)
		{
			StartApp(inputFile);
		}
		e.Handled = true;
	}

	private void FilePathBox_PreviewDragOver(object sender, DragEventArgs e)
	{
		e.Effects = DragDropEffects.All;
		e.Handled = true;
	}

	private void StartApp(string inputFile)
	{
		Process.Start(AppDomain.CurrentDomain.BaseDirectory + "QCLVR.exe", " \"" + inputFile + "\"");
	}

	private void DMBtn_Click(object sender, RoutedEventArgs e)
	{
		DataminerWin dataminerWin = new DataminerWin(XmlFilePath);
		dataminerWin.Owner = this;
		dataminerWin.Show();
		base.IsEnabled = false;
	}

	private void Window_Closing(object sender, CancelEventArgs e)
	{
		deleteTempFiles();
	}

	private void deleteTempFiles()
	{
		try
		{
			File.Delete(tempDataFilePath + ".html");
		}
		catch
		{
			MessageBox.Show(string.Format("Could not delete {0}", tempDataFilePath + ".html"));
		}
		try
		{
			File.Delete(tempDataFilePath + "DATA.xml");
		}
		catch
		{
			MessageBox.Show(string.Format("Could not delete {0}", tempDataFilePath + "DATA.xml"));
		}
	}
}
