// 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 Hiperscan.Spectroscopy.IdentModule;


namespace Hiperscan.Extensions.ApoIdent
{
    public class AddSubstanceDialog
    {
        [Widget] Gtk.Dialog add_substance_dialog = null;
        [Widget] Gtk.Entry substance_entry = null;
        [Widget] Gtk.Entry latin_entry = null;
        [Widget] Gtk.Button add_usbstance_button = null;

        public AddSubstanceDialog(SubstanceExtensionDialog extensions_dialog)
        {
            Glade.XML gui = new Glade.XML(null, "substance_extension_dialog.glade", "add_substance_dialog", "Hiperscan");
            gui.Autoconnect(this);

            this.substance_entry.Activated += (sender, e) =>
            {
                if (String.IsNullOrEmpty(this.substance_entry.Text.Trim()) == false)
                    this.latin_entry.GrabFocus();
            };
            this.latin_entry.Activated += (sender, e) =>
            {
                if (String.IsNullOrEmpty(this.substance_entry.Text.Trim()) == false)
                    this.add_usbstance_button.Activate();
            };

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

            this.add_substance_dialog.TransientFor = extensions_dialog.Window;
            this.substance_entry.Text = extensions_dialog.SearchText;

            this.add_substance_dialog.IconList = ApoIdentStock.FinderIcons;                         
            if (this.add_substance_dialog.Run() == (int) Gtk.ResponseType.Accept)
            {
                this.ClassificationInfo = new ClassificationInfo(Guid.NewGuid().ToString());
                this.ClassificationInfo.ClassificationName = this.substance_entry.Text.Trim();
                this.ClassificationInfo.ClassificationType = ClassificationType.Single;
                this.ClassificationInfo.IsNirMeasurable = false;
                this.ClassificationInfo.IsUserDefined = true;

                SubstanceInfo substance_info = new SubstanceInfo(this.ClassificationInfo.Id);
                substance_info.CanInsert = false;
                substance_info.IsVisible = true;
                substance_info.PrimaryDefaultName = this.substance_entry.Text.Trim();
                substance_info.PrimaryLatinName = this.latin_entry.Text.Trim();

                this.ClassificationInfo.Substances.Add(substance_info);
            }

            this.add_substance_dialog.Destroy();
        }

        private void OnSubstanceNameEntryChanged(object sender, EventArgs e)
        {
            this.add_usbstance_button.Sensitive = String.IsNullOrEmpty(this.substance_entry.Text.Trim()) == false;
        }

        public ClassificationInfo ClassificationInfo { get; private set; }
    }
}
