using N2_Patcher.Control;
using N2_Patcher.Model;
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Windows;

namespace N2_Patcher
{
	internal class Program
	{
		public Program()
		{
		}

		public static void Config_EXE()
		{
			if (!File.Exists(string.Concat(Functions.GetCurrentFolder(), Config.GetConfigFile())))
			{
				MessageBox.Show(string.Concat(Config.GetConfigFile(), " konnte nicht gefunden werden"), "Error", MessageBoxButton.OK, MessageBoxImage.Hand);
			}
			else
			{
				try
				{
					Process process = new Process();
					process.StartInfo.FileName = string.Concat(Functions.GetCurrentFolder(), Config.GetConfigFile());
					process.Start();
				}
				catch
				{
					return;
				}
			}
		}

		public static void Files_Thread()
		{
			(new Thread(new ThreadStart(DownloadFiles.WC_DownloadFiles))).Start();
		}

		public static void newsThread()
		{
			(new Thread(new ThreadStart(NewsSystem.loadNews))).Start();
		}

		public static void Patchlist_Thread()
		{
			(new Thread(new ThreadStart(CreatePatchlist.WC_DownloadPatchlist))).Start();
		}

		public static void Self()
		{
			Process.Start(Assembly.GetExecutingAssembly().Location);
		}

		public static void SelfUpgrade()
		{
			if (!File.Exists(string.Concat(Functions.GetCurrentFolder(), Config.GetStartFile())))
			{
				MessageBox.Show(string.Concat(Config.GetStartFile(), " konnte nicht gefunden werden"), "Error", MessageBoxButton.OK, MessageBoxImage.Hand);
			}
			else
			{
				string str = "The patcher now closes and restarts automatically for a self-update.";
				string str1 = "Patcher-Message";
				MessageBox.Show(str, str1, MessageBoxButton.OK);
				try
				{
					FileInfo fileInfo = new FileInfo(Assembly.GetExecutingAssembly().Location);
					int id = Process.GetCurrentProcess().Id;
					string str2 = string.Concat(Functions.GetCurrentFolder(), Config.GetUpdateFile());
					ProcessStartInfo processStartInfo = new ProcessStartInfo(str2)
					{
						Arguments = string.Concat(new string[] { "\"\"\"", fileInfo.Name, "\"\"\" \"", id.ToString(), "\"" })
					};
					Process.Start(processStartInfo);
					Environment.Exit(0);
				}
				catch
				{
					MessageBox.Show("The self-upgrade was brocken. Can not start the application.", str1, MessageBoxButton.OK);
				}
			}
		}

		public static void Start_EXE()
		{
			if (!File.Exists(string.Concat(Functions.GetCurrentFolder(), Config.GetStartFile())))
			{
				MessageBox.Show(string.Concat(Config.GetStartFile(), " konnte nicht gefunden werden"), "Error", MessageBoxButton.OK, MessageBoxImage.Hand);
			}
			else
			{
				try
				{
					DateTime now = DateTime.Now;
					string privateKey = Config.getPrivateKey();
					string[] str = new string[] { privateKey, null, null, null, null, null };
					int year = now.Year;
					str[1] = year.ToString();
					str[2] = "-";
					year = now.Day;
					str[3] = year.ToString();
					str[4] = "-";
					year = now.Hour;
					str[5] = year.ToString();
					string str1 = string.Concat(str);
					string lower = Functions.CreateMD5Hash(str1).ToLower();
					Process process = new Process();
					process.StartInfo.FileName = string.Concat(Functions.GetCurrentFolder(), Config.GetStartFile());
					process.StartInfo.Arguments = lower;
					process.Start();
					if (Config.isCLoseOnStart())
					{
						Environment.Exit(0);
					}
				}
				catch
				{
					return;
				}
			}
		}

		public static void URL(string Link, string type = null)
		{
			if ((Link == null ? false : Link != ""))
			{
				Process.Start(Link);
			}
			else if (type != null)
			{
				MessageBox.Show(string.Concat("Config Eintrag \"", type, "\" konnte nicht gefunden werden"), "Error", MessageBoxButton.OK, MessageBoxImage.Hand);
			}
		}
	}
}