// 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 Gtk;

using Hiperscan.Unix;


namespace Hiperscan.Extensions.ApoIdent
{
    public class DataExchangeImportDialog
    {
        [Widget] Gtk.Dialog data_exchange_import_dialog     = null;
        [Widget] Gtk.TreeView substance_treeview            = null;

        public static ResponseType Run(Gtk.Window parent, DataExchange exchange_data)
        {
            DataExchangeImportDialog tmp = new DataExchangeImportDialog(parent, exchange_data);
            ResponseType answer = (ResponseType)tmp.data_exchange_import_dialog.Run();
            tmp.data_exchange_import_dialog.Destroy();
            return answer;
        }

        private DataExchangeImportDialog (Gtk.Window parent, DataExchange management)
        {
            this.Management = management;

            Glade.XML gui = new Glade.XML(null, "ApoIdent.Resources.Glade.FinderWindow.glade", "data_exchange_import_dialog", "Hiperscan");
            gui.Autoconnect(this);

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

            this.data_exchange_import_dialog.TransientFor = parent;

            this.Update();
            this.data_exchange_import_dialog.SetDefaultSize(800, 300);

            this.data_exchange_import_dialog.IconList = ApoIdentStock.FinderIcons;
        }

        private void Update()
        {
            this.Store = new ListStore (
                typeof(string),                                    //Substance name
                typeof(string),                                    //Manufacturer
                typeof(string),                                    //Batch number
                typeof(string),                                    //Expiry date
                typeof(string),                                 //PZN
                typeof(string));                                //Key

            this.substance_treeview.RulesHint = true;
            this.substance_treeview.AppendColumn(Catalog.GetString("Substance Name"),            new CellRendererText(), "text"        , 0);
            this.substance_treeview.AppendColumn(Catalog.GetString("Manufacturer"),             new CellRendererText(), "text"        , 1);
            this.substance_treeview.AppendColumn(Catalog.GetString("Batch Number"),              new CellRendererText(), "text"        , 2);
            this.substance_treeview.AppendColumn(Catalog.GetString("Use-by Date/Shelf Life"),    new CellRendererText(), "text"        , 3);
            this.substance_treeview.AppendColumn(Catalog.GetString("PZN"),                        new CellRendererText(), "text"        , 4);
            
            foreach (string file_key in this.Management.Keys)
            {
                    ExchangeDataSet item = this.Management[file_key];
                this.Store.AppendValues(
                    item.PrimarySubstanceName,
                    item.Supplier,
                    item.BatchNumber,
                    item.ExpiryDate.ToString("dd.MM.yyyy"),
                    item.PZN,
                    file_key);
            }

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

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

            foreach(string key in this.Management.Keys)
            {
                this.Management.CurrentDataSetKey = key;
                break;
            }

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

        private void OnSelectionChanged(object sender, EventArgs e)
        {
            TreeIter iter;
            this.substance_treeview.Selection.GetSelected(out iter);
            this.Management.CurrentDataSetKey = (string) this.Store.GetValue(iter, 5);
        }

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

