// 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 Glade;
using GLib;
using Gtk;
using Hiperscan.AnalysisModule.Identification;
using Hiperscan.ApoIdentCore;
using Hiperscan.ApoIdentCore.SectionManager.Substance.SubstanceSelection;
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, AppControl control)
        {
            FuzzyNameDialog tmp = new FuzzyNameDialog(control.SubstanceManager.SubstancesCatalog);
            tmp.Run(parent);
        }

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

            ApoIdent.ApoIdentApplication.SetWmcClassName(this.fuzzy_dialog);

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

            this.fuzzy_treeview.ButtonPressEvent += this.OnButtonPressEvent;

            this.fuzzy_dialog.IconList = ApoIdentStock.FinderIcons;

            GuiDesigner.SetSchema(this.fuzzy_dialog);
            this.answer = (ResponseType) this.fuzzy_dialog.Run();

            if (this.fuzzy_dialog != null)
                this.fuzzy_dialog.Destroy();

            if (this.Answer != ResponseType.Ok)
                this.SubstancesCatalog.ResetUserSelections();
        }


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

            string substance_name = this.SubstancesCatalog.FuzzyName;

            this.fuzzy_label.Text = String.Format(Catalog.GetString("The substance name '{0}' results in several matches. Please choose one and click 'OK' for continuing data import. 'Cancel' aborts the data import."),
                                                  substance_name);

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

            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 (IdentSubstance item in this.SubstancesCatalog.PreselectedSubstances)
            {
                this.Store.AppendValues(
                    item.ModelClass.Name,
                    item.BestReferenceName,
                    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);
            ((IdentSubstance)this.Store.GetValue(iter, 2)).FromUserSelected = true;
        }

        private void OnSelectionChanged(object sender, EventArgs e)
        {
            this.SubstancesCatalog.ResetUserSelections();

            TreeIter iter;
            this.fuzzy_treeview.Selection.GetSelected(out iter);
            ((IdentSubstance)this.Store.GetValue(iter, 2)).FromUserSelected = true;
        }

        [ConnectBefore]
        private void OnButtonPressEvent(object sender, ButtonPressEventArgs e)
        {
            if (e.Event.Button == 1 && e.Event.Type == Gdk.EventType.TwoButtonPress)
            {
                this.fuzzy_dialog.Destroy();
                this.fuzzy_dialog = null;
            }
        }

        private ResponseType answer = ResponseType.None;
        public ResponseType Answer
        {
            get
            {
                if (this.answer == ResponseType.None)
                    return ResponseType.Ok;

                return this.answer;
            }
        }

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

