using System;
using System.IO;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Diagnostics;
using System.Globalization;
using System.Windows.Controls;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

using agree;

namespace WpfClient
{
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	///
	///
	///
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public class MyApp : Application
	{
		[STAThread()]
		static void Main()
		{
			new MyApp().Run();
		}

		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		///
		///
		///
		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		protected override void OnStartup(StartupEventArgs e)
		{
			base.OnStartup(e);

			String[] args = e.Args;

			int arg_ix;
			if ((arg_ix = Array.IndexOf<String>(args, "-singlethreaded")) != -1)
			{
				Console.WriteLine("disabling multi-threading");
				glue.Debugging.Nop.single_threaded = true;
				args = args.Where((z, x) => x != arg_ix).ToArray();
			}

			if (args.Length == 0)
			{
				MessageBox.Show("Please specify grammar file on the command line", "Command line argument missing", MessageBoxButton.OK, MessageBoxImage.Stop);
				this.Shutdown();
				return;
			}

			String f = Path.Combine(Environment.CurrentDirectory, args[0]);

			if (!System.IO.File.Exists(f))
			{
				String msg = String.Format("File not found: {0}", f);
				MessageBox.Show(msg, "Command line argument missing", MessageBoxButton.OK, MessageBoxImage.Stop);
				this.Shutdown();
				return;
			}

			this.MainWindow = new MainWindow(f);
			this.MainWindow.Show();
		}
	};
}