/* 

   Copyright (C) Arnaud Houdelette		2012-2014
   Copyright (C) Emmanuel Langlois		2012-2014

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using Gtk;

namespace DMX2
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			bool fullscreen = false;
			WebServer ws = null; bool webserv = false;
			System.IO.FileInfo openfile=null;

			// Traitement des options en ligne de commande :

			foreach (string arg in args) {
				switch (arg) {
				case "fullscreen":
				case "fs":
					fullscreen = true;
					break;
				case "ws":
					webserv = true;
					break;					
				default:
					if(System.IO.File.Exists(arg))
						openfile = new System.IO.FileInfo(arg);
					break;
				}
			}

			if(webserv) ws = new WebServer();

			// Initialisation GTK#
			Application.Init ();

			// Repertoire courrant est celui de l'executable
			System.IO.Directory.SetCurrentDirectory (
			     (new System.IO.FileInfo (
					System.Reflection.Assembly.GetExecutingAssembly ().Location
			)).DirectoryName
			);

			// Chargement du style GTK
			if (System.IO.File.Exists ("style.gtkrc"))
				Gtk.Rc.Parse ("style.gtkrc");
			else 
				using (System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("style.gtkrc"))
				using (System.IO.TextReader reader = new System.IO.StreamReader(stream))
					Gtk.Rc.ParseString (reader.ReadToEnd ()); 

			// Creation de la fenetre principale
			MainWindow win = new MainWindow ();
			// application des options
			if(fullscreen) win.ToggleFullscreen();

			if (openfile!=null) win.OpenFile(openfile);

			// Gestion des erreurs non traitées dans l'interface
			GLib.ExceptionManager.UnhandledException += HandleUnhandledException;

			// Lancement
			win.Show ();
			Application.Run ();

			// Nettoyage
			if (Conduite.Courante != null) {
				Conduite.Courante.Dispose ();
			}

			if(ws!=null) ws.Dispose();
		}

		static void HandleUnhandledException (GLib.UnhandledExceptionArgs args)
		{

			// En cas d'erreur non traitée : on affiche et on demande ...
			var dlg = new Dialog ("Erreur !", null, DialogFlags.Modal); 

			dlg.AddButton ("Continuer ..." , ResponseType.Cancel ).GrabDefault(); 
			dlg.AddButton ("Fermer l'application" , ResponseType.Close );

			dlg.VBox.Add (new Label (args.ExceptionObject.ToString()));
			dlg.VBox.ShowAll ();

			// Si l'utilisateur a clique sur le bon bouton, on continue comme si de rien n'etait...
			args.ExitApplication= (ResponseType)dlg.Run() == ResponseType.Close ;
			dlg.Destroy();

		}
	}
}