// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2012 Thomas Klose & Torsten Schache, Hiperscan GmbH Dresden, Germany
//
//    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 3 of the License, or
//    (at your option) any later version.
//
//    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;
using Hiperscan.Common;
using Hiperscan.GtkExtensions.Widgets;
using Hiperscan.Unix;


namespace Hiperscan.Extensions.ApoIdent
{
	public class GreetingWindow : Gtk.Window
	{
		private volatile bool quit_request = false;
		private volatile bool snooper_installed = false;
		private FinderWindow finder_window = null;
		private FinderSplashScreen splash_screen = null;
		private bool is_showing_warming_up = false;


		public GreetingWindow(FinderWindow finder_window) : base(WindowType.Toplevel)
		{
			this.Decorated = false;
			this.Title = Catalog.GetString("Starting Apo-Ident ...");
			this.Modal = true;
			this.KeepAbove = true;
			this.WindowPosition = WindowPosition.CenterAlways;

			this.DeleteEvent += (o, args) => { args.RetVal = true; };

			this.splash_screen = new FinderSplashScreen();
			
			this.WidthRequest = this.splash_screen.Width;
			this.HeightRequest = this.splash_screen.Height;
			this.splash_screen.Update(FinderSplashScreenStates.Hiperscan);
			this.Add(this.splash_screen);
			
			this.BorderWidth = 0;
			this.finder_window = finder_window;

			this.SetWmclass(Plugin.name.ToLower(), 
			                Plugin.name.ToLower());
			
			GLib.Timeout.Add(1000, delegate 
			{
				if (this.is_showing_warming_up == false)
					this.splash_screen.Update(FinderSplashScreenStates.ApoIdent);
				return false;
			});
			
			this.finder_window.host.InfoMessageShown += this.OnInfoMessageShown;
			this.ShowAll();
			this.finder_window.host.ProcessEvents();
		}
		
		private void OnInfoMessageShown(object sender, EventArgs e)
		{
			this.finder_window.host.InfoMessageShown -= this.OnInfoMessageShown;
			this.Destroy();
			this.finder_window.GreetingWin = null;
		}
		
		public void SetWarmupMode()
		{
			if (!this.snooper_installed)
			{
				Key.SnooperInstall(delegate(Widget w, Gdk.EventKey e) 
					{
						if (e.Type != Gdk.EventType.KeyPress)
						return 0;
						if (e.Key == Gdk.Key.Escape && this.Visible && !this.quit_request)
						{
							this.HideAll();
							Plugin.host.ProcessEvents();
							MessageDialog dialog = new MessageDialog(this, DialogFlags.Modal, MessageType.Question, ButtonsType.YesNo,
								Catalog.GetString("Do you want to cancel start-up and quit application?"));
							dialog.WindowPosition = WindowPosition.CenterAlways;
							GLib.Timeout.Add(3000, delegate 
								{
									dialog.Destroy();
									return false;
								});

                            dialog.IconList = ApoIdentStock.FinderIcons;
							ResponseType choice = (ResponseType)dialog.Run();
							dialog.Destroy();
							this.ShowAll();
							if (choice == ResponseType.Yes)
							{
								this.quit_request = true;
								this.finder_window.RequestQuit();
								this.splash_screen.Update(FinderSplashScreenStates.WarmUpCancelled);
							}
						}
						return 0;
					});
			}
			this.snooper_installed = true;

			this.ShowAll();
		}
		
		public void SetTemperature(double temperature)
		{
			this.splash_screen.Update(FinderSplashScreenStates.WarmUp, temperature);
		}
	}
}

