using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Markup;
using System.Windows.Media;
using System.Xml.Serialization;

namespace QFDT_Components;

public partial class HLOS : UserControl, IComponentConnector
{
	public delegate void PrintMessage(MessageType type, string s);

	public delegate void ClearOutput();

	public enum MessageType
	{
		COMMAND,
		OUTPUT
	}

	private HLOS_XML HLOS_Commands;

	private Thread worker;

	private string[] commandsToBeExecuted;

	public HLOS()
	{
		InitializeComponent();
	}

	private void MyClearOutput()
	{
		if (output.Dispatcher.CheckAccess())
		{
			output.Document.Blocks.Clear();
			return;
		}
		ClearOutput method = MyClearOutput;
		base.Dispatcher.BeginInvoke(method);
	}

	private void MyPrintMessage(MessageType type, string s)
	{
		if (output.Dispatcher.CheckAccess())
		{
			Run run = new Run(s + "\n", output.CaretPosition.DocumentEnd);
			switch (type)
			{
			case MessageType.COMMAND:
				run.Foreground = Brushes.Red;
				break;
			case MessageType.OUTPUT:
				run.Foreground = Brushes.Purple;
				break;
			}
			output.ScrollToEnd();
		}
		else
		{
			PrintMessage method = MyPrintMessage;
			base.Dispatcher.BeginInvoke(method, type, s);
		}
	}

	public void Load_HLOS_XML(string path)
	{
		XmlSerializer xmlSerializer = new XmlSerializer(typeof(HLOS_XML));
		HLOS_Commands = new HLOS_XML();
		if (File.Exists(path))
		{
			try
			{
				FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
				HLOS_Commands = (HLOS_XML)xmlSerializer.Deserialize(fileStream);
				fileStream.Close();
				if (HLOS_Commands != null)
				{
					comboCommands.Items.Clear();
					for (int i = 0; i < HLOS_Commands.Action.Length; i++)
					{
						comboCommands.Items.Add(HLOS_Commands.Action[i].Name);
					}
				}
				return;
			}
			catch
			{
				MyPrintMessage(MessageType.OUTPUT, base.Resources["FailedtoParse"].ToString());
				return;
			}
		}
		MyPrintMessage(MessageType.OUTPUT, base.Resources["FailedtoParse"].ToString());
	}

	private void RunTask()
	{
		if (commandsToBeExecuted == null || commandsToBeExecuted.Length <= 0)
		{
			return;
		}
		MyClearOutput();
		for (int i = 0; i < commandsToBeExecuted.Length; i++)
		{
			Process process = new Process();
			char[] separator = new char[1] { ' ' };
			process.StartInfo.FileName = commandsToBeExecuted[i].Split(separator)[0];
			string text = "";
			for (int j = 1; j < commandsToBeExecuted[i].Split(separator).Length; j++)
			{
				text = text + commandsToBeExecuted[i].Split(separator)[j] + " ";
			}
			process.StartInfo.RedirectStandardOutput = true;
			process.StartInfo.UseShellExecute = false;
			process.StartInfo.Arguments = text;
			process.StartInfo.CreateNoWindow = true;
			MyPrintMessage(MessageType.COMMAND, commandsToBeExecuted[i]);
			process.Start();
			string s = process.StandardOutput.ReadToEnd();
			MyPrintMessage(MessageType.OUTPUT, s);
			process.WaitForExit();
		}
	}

	private void button1_Click(object sender, RoutedEventArgs e)
	{
		if (comboCommands.SelectedIndex != -1)
		{
			worker = new Thread(RunTask);
			worker.Name = "Run Command";
			worker.Start();
		}
	}

	private void comboCommands_SelectionChanged(object sender, SelectionChangedEventArgs e)
	{
		if (comboCommands.SelectedIndex != -1)
		{
			commandsToBeExecuted = HLOS_Commands.Action[comboCommands.SelectedIndex].Commands.Command;
		}
	}

	public void SetLanguage(string culture)
	{
		ResourceDictionary resourceDictionary = new ResourceDictionary();
		try
		{
			resourceDictionary.Source = new Uri(Thread.GetDomain().BaseDirectory + "\\Resources\\UI_" + culture + ".xaml", UriKind.Absolute);
		}
		catch
		{
			resourceDictionary.Source = new Uri(Thread.GetDomain().BaseDirectory + "..\\Resources\\UI_en-US.xaml", UriKind.Absolute);
		}
		base.Resources.MergedDictionaries.Clear();
		base.Resources.MergedDictionaries.Add(resourceDictionary);
	}
}
