// 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 System.Threading;
using Gtk;

using Hiperscan.Unix;
 

namespace Hiperscan.Extensions.ApoIdent
{
	public partial class FinderWindow
	{      
		private void OnReadExchangeDataButtonClicked(object sender, EventArgs e)
		{
			if (this.RemindSave() == false)
				return;

            this.Config.Profiles.Current.ExchangeData.DeterminedFuzzySet = null;
			Thread.Sleep(500);

			string message = null;
			ResponseType answer;

			try
			{
                this.Config.Profiles.Current.ExchangeData.ImportFiles();
                if (this.Config.Profiles.Current.ExchangeData.Count == 0)
				{
					message =  String.Format(Catalog.GetString("There are no exchange data available."));
					this.host.ShowInfoMessage(this.finder_window, message);
					return;
				}

                if (this.Config.Profiles.Current.ExchangeData.Count > 1)
				{
                    DataExchangeImportDialog ddlg = new DataExchangeImportDialog(this.Config.Profiles.Current.ExchangeData);
                    answer = (ResponseType) ddlg.Run(this.GtkWindow);
                    if (answer != ResponseType.Ok)
						return;
				}
				else
				{
                    foreach (string key in this.Config.Profiles.Current.ExchangeData.Keys)
                        this.Config.Profiles.Current.ExchangeData.CurrentDataSetKey = key;
				}

                string exchange_key = this.Config.Profiles.Current.ExchangeData.CurrentDataSetKey;
                ExchangeDataSet exchange_data_set = this.Config.Profiles.Current.ExchangeData[exchange_key];

                List<FuzzyNameSet> fuzzy_sets = this.SubstanceManagement.FindSubstanceForDataExchange(exchange_data_set);

                if (fuzzy_sets.Count == 1 && fuzzy_sets[0].ResolvedOverId == true)
                {
                    this.Config.Profiles.Current.ExchangeData.DeterminedFuzzySet = fuzzy_sets[0];
                }
                else if (fuzzy_sets.Count == 0)
                {
                    message =  String.Format(Catalog.GetString("Could not find or resolve substance name '{0}'. " +
                                                               "If you want to delete this import data, you can do this over the '{1}'-application."), 
                                             exchange_data_set.PrimarySubstanceName,
                                             this.Config.Profiles.Current.ExchangeData.ApplicationName);
                    
                    Gtk.MessageDialog mdlg = new MessageDialog(this.finder_window,
                                                               DialogFlags.Modal,
                                                               MessageType.Info,
                                                               ButtonsType.Ok,
                                                               message);

                    mdlg.IconList = ApoIdentStock.FinderIcons;
                    mdlg.Run();
                    mdlg.Destroy();
                }
                else
                {
                    FuzzyNameDialog fdlg = new FuzzyNameDialog(fuzzy_sets, exchange_data_set.PrimarySubstanceName, this.GtkWindow);
                    if (fdlg.SelectedNameSet != null)
                        this.Config.Profiles.Current.ExchangeData.DeterminedFuzzySet = fdlg.SelectedNameSet;
                }

                if (this.Config.Profiles.Current.ExchangeData.DeterminedFuzzySet != null)
                {
                    this.IsUpdating = true;
                    this.substance_search_entry.Text = this.Config.Profiles.Current.ExchangeData.DeterminedFuzzySet.SubstanceName;
                    this.SubstanceManagement.SelectNewSubstance(this.Config.Profiles.Current.ExchangeData.DeterminedFuzzySet);
                    this.IsUpdating = false;
                    this.Update(FinderUpdateState.SubstanceChangedFromExtern);
                }
			}
			catch(Exception ex)
			{
				message = this.Config.Profiles.Current.ExchangeData.GetReadingErrorMessage(ex);
				this.host.ShowInfoMessage(this.finder_window, message);
			}
		}
	}
}

