// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2018 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.Collections.Generic;
using Glade;
using Gtk;

using Hiperscan.Unix;


namespace Hiperscan.Extensions.ApoIdent
{
	public class FuzzyNameDialog
	{
		[Widget] Gtk.Dialog fuzzy_dialog		= null;
		[Widget] Gtk.Label	fuzzy_label			= null;
		[Widget] Gtk.TreeView fuzzy_treeview	= null;

        public FuzzyNameDialog (List<FuzzyNameSet> name_sets, string substance_name, Gtk.Window parent)
		{
			this.NameSets 			= name_sets;
			this.SelectedNameSet 	= name_sets[0];
			this.SubstanceName		= substance_name;

            this.Run(parent);
		}

        private void Run(Gtk.Window parent)
		{
			Glade.XML gui = new Glade.XML(null, "ApoIdent.Resources.Glade.FinderWindow.glade", "fuzzy_dialog", "Hiperscan");
			gui.Autoconnect(this);

			this.fuzzy_dialog.SetWmclass(Plugin.name.ToLower(), 
			                             Plugin.name.ToLower());

            this.fuzzy_dialog.TransientFor = parent;
			this.Update();
			this.fuzzy_dialog.SetDefaultSize(800, 300);

            this.fuzzy_dialog.IconList = ApoIdentStock.FinderIcons;
			ResponseType answer = (ResponseType) this.fuzzy_dialog.Run();
			this.fuzzy_dialog.Destroy();

            if (answer != ResponseType.Ok)
				this.SelectedNameSet = null;
		}


		private void Update()
		{
			this.fuzzy_label.WidthRequest = 800;

			this.fuzzy_label.Text = String.Format(Catalog.GetString("The substance name '{0}' resulted to several solutions. " +
				"Please select one of them and click the button 'OK' for contenuing importing data. " +
				"In case of selection 'Cancel' the impport of data will be arborted."),
				this.SubstanceName);

			this.Store = new ListStore (
				typeof(string),				//Substance class name	
				typeof(string),				//Substance name
				typeof(FuzzyNameSet));							
			

			this.fuzzy_treeview.RulesHint = true;
			this.fuzzy_treeview.AppendColumn(Catalog.GetString("Substance Class"), 	new CellRendererText(), "text"		, 0);
			this.fuzzy_treeview.AppendColumn(Catalog.GetString("Substance Name"),   new CellRendererText(), "text"		, 1);


			foreach (FuzzyNameSet item in this.NameSets)
			{
				this.Store.AppendValues(
					item.ModelClassName,
					item.SubstanceName,
					item);
			}

			this.fuzzy_treeview.Model = this.Store;
			this.SetSelectFirstRow();
		}

		private void SetSelectFirstRow()
		{
			TreeIter iter;
			this.Store.GetIterFirst(out iter);
			this.fuzzy_treeview.Selection.SelectIter(iter);

			this.fuzzy_treeview.Selection.Changed += this.OnSelectionChanged;
		}

		private void OnSelectionChanged(object sender, EventArgs e)
		{
			TreeIter iter;
			this.fuzzy_treeview.Selection.GetSelected(out iter);
			this.SelectedNameSet = (FuzzyNameSet) this.Store.GetValue(iter, 2);
		}


		private List<FuzzyNameSet> NameSets {get; set;}
		public FuzzyNameSet SelectedNameSet	{get; set;}
		private string SubstanceName		{get; set;}
		private ListStore Store				{get; set;}
	}
}

