// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2017 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 System.Reflection;
using System.IO;
using Gtk;

using Hiperscan.Unix;
using Hiperscan.I18n;


namespace Hiperscan.GtkExtensions.Widgets
{
	
	public enum FinderReferenceType
	{
		ExternalBlack,
		ExternalWhiteZenith,	
		ExternalInsert
	}
		
	public class FinderReferenceDialog
	{
		public FinderReferenceDialog(Window parent, FinderReferenceType reference_type) : this(parent, reference_type, -1)
		{
		}
		
		public FinderReferenceDialog(Window parent, FinderReferenceType reference_type, int button_height)
		{
			string picure_file 	= String.Empty;
			string message 		= String.Empty;		

			switch(reference_type)
			{

			case FinderReferenceType.ExternalBlack:
				message = Catalog.GetString("\n\n\nPlease apply the black reference.");
				picure_file = "black_reference.png";
				break;
				
			case FinderReferenceType.ExternalWhiteZenith:
				message = Catalog.GetString("\n\n\nPlease apply the Zenith white reference.");
				picure_file = "white_zenith_reference.png";
				break;

			case FinderReferenceType.ExternalInsert:
				message = Catalog.GetString("\n\n\nPlease apply the insert white reference.");
				picure_file = "white_insert_reference.png";
				break;
				
			default:
				throw new Exception(Catalog.GetString("FinderMessageDialog: Invalid reference type."));

			}
			
			this.Dialog = new MessageDialog(parent,
			                                DialogFlags.Modal,
			                                Gtk.MessageType.Other,
			                                ButtonsType.None,
			                                message);
			
			string path = Path.Combine("Widgets", "FinderReferenceDialog", "Resources");
            Gdk.Pixbuf buf = new Gdk.Pixbuf(Locale.ResourceFileRelativeToAssembly(Assembly.GetExecutingAssembly(), path, picure_file, false));
			this.Dialog.Image = new Gtk.Image(buf);
			this.Dialog.Image.Show();
			this.Dialog.WindowPosition = Gtk.WindowPosition.CenterOnParent;
			this.Dialog.AddButton(Gtk.Stock.Cancel, Gtk.ResponseType.Cancel);
			this.Dialog.AddButton(Gtk.Stock.Ok, Gtk.ResponseType.Ok).HasFocus = true;

            if (parent != null && parent.IconList != null && parent.IconList.Length != 0)
                this.Dialog.IconList = parent.IconList;

			if (button_height > 0)
			{
				foreach (Gtk.Widget w in this.Dialog.ActionArea.Children)
				{
					if (w is Gtk.Button)
						w.HeightRequest = button_height;
				}
			}
		}
		
		public void Run()
		{
			ResponseType response = (ResponseType)this.Dialog.Run();
			this.Dialog.Destroy();

			if (response != ResponseType.Ok)
				throw new Exception(Catalog.GetString("Recalibration procedure was not finished. " + 
                                                      "Please repeat measurement."));
		}
		
		private MessageDialog Dialog {get;set;}
	}
}

