// 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 static void Run(Gtk.Window parent, DataExchange exchange_data)
        {
            FuzzyNameDialog tmp = new FuzzyNameDialog(exchange_data);
            tmp.Run(parent);
        }

        private FuzzyNameDialog(DataExchange exchange_data)
        {
            this.DataExchange = exchange_data;
        }

        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.DataExchange.DeterminedFuzzySet = null;
        }


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

            string exchange_key = this.DataExchange.CurrentDataSetKey;
            ExchangeDataSet exchange_data_set = this.DataExchange[exchange_key];
            string substance_name = exchange_data_set.PrimarySubstanceName;

            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."),
                                                  substance_name);

            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.DataExchange.FuzyList)
            {
                this.Store.AppendValues(
                    item.ModelClassName,
                    item.SubstanceName,
                    item);
            }

            this.fuzzy_treeview.Model = this.Store;
            this.SetSelectFirstRow();
            this.fuzzy_treeview.Selection.Changed += this.OnSelectionChanged;
        }

        private void SetSelectFirstRow()
        {
            TreeIter iter;
            this.Store.GetIterFirst(out iter);
            this.fuzzy_treeview.Selection.SelectIter(iter);
            this.DataExchange.DeterminedFuzzySet = (FuzzyNameSet)this.Store.GetValue(iter, 2);
        }

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

        private DataExchange DataExchange { get; set; }
        private ListStore Store { get; set; }

    }
}

