// 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;
using Hiperscan.ApoIdentCore;
using GLib;
using Hiperscan.ApoIdentCommon.ApoConfig.ThirdPartyDataExchange;

namespace Hiperscan.Extensions.ApoIdent
{
    public class DataExchangeImportDialog
    {
        private enum Columns
        {
            SubstanceName,
            Producer,
            BatchNumber,
            ExpiryDate,
            PZN,
            FileKey,
        }

        [Widget] Gtk.Dialog data_exchange_import_dialog     = null;
        [Widget] Gtk.TreeView substance_treeview            = null;

        public static ResponseType Run(Gtk.Window parent, AppControl control)
        {
            DataExchangeImportDialog tmp = new DataExchangeImportDialog(parent, control.ProfileManager.CurrentProfile.ExchangeData);
            return tmp.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);

            ApoIdent.ApoIdentApplication.SetWmcClassName(this.data_exchange_import_dialog);

            this.data_exchange_import_dialog.TransientFor = parent;

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

            this.substance_treeview.ButtonPressEvent += this.OnButtonPressEvent;

            this.data_exchange_import_dialog.IconList = ApoIdentStock.FinderIcons;

            GuiDesigner.SetSchema(this.data_exchange_import_dialog);

            this.answer = (ResponseType)this.data_exchange_import_dialog.Run();

            if (this.data_exchange_import_dialog != null)
                this.data_exchange_import_dialog.Destroy();
        }

        private void Update()
        {
            this.Store = new ListStore(
                typeof(string),                                    //Substance name
                typeof(string),                                    //Producer
                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", (int)Columns.SubstanceName);
            this.substance_treeview.AppendColumn(Catalog.GetString("Producer"), new CellRendererText(), "text", (int)Columns.Producer);
            this.substance_treeview.AppendColumn(Catalog.GetString("Batch"), new CellRendererText(), "text", (int)Columns.BatchNumber);
            this.substance_treeview.AppendColumn(Catalog.GetString("Expiry date"), new CellRendererText(), "text", (int)Columns.ExpiryDate);
            this.substance_treeview.AppendColumn(Catalog.GetString("PPN"), new CellRendererText(), "text", (int)Columns.PZN);

            foreach (string file_key in this.Management.Keys)
            {
                ExchangeDataSet item = this.Management[file_key];
                this.Store.AppendValues(
                    item.PrimarySubstanceName,
                    item.Producer,
                    item.BatchNumber,
                    item.ExpiryDate?.ToString("dd.MM.yyyy") ?? String.Empty,
                    item.Ppn,
                    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, (int)Columns.FileKey);
        }

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

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

                return this.answer;
            }
        }

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

