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

using Hiperscan.Unix;
using Hiperscan.ApoIdentCommon.ApoConfig.ThirdPartyDataExchange;

namespace Hiperscan.Extensions.ApoIdent
{
    public class DataExchangeSettingsDialog
    {
        [Widget] Gtk.Dialog report_managment_dialog = null;
        [Widget] Gtk.CheckButton data_exchange_checkbutton = null;
        [Widget] Gtk.Entry exchange_directory_entry = null;
        [Widget] Gtk.Button exchange_directory_button = null;
        [Widget] Gtk.Label directory_exists_label = null;

        public DataExchangeSettingsDialog(DataExchange protocol_manager, Gtk.Window parent)
        {
            this.Management = protocol_manager;
            this.Run(parent);
        }

        private void Run(Gtk.Window parent)
        {
            Glade.XML gui = new Glade.XML(null, "ApoIdent.Resources.Glade.FinderWindow.glade", "report_managment_dialog", "Hiperscan");
            gui.Autoconnect(this);

            ApoIdent.ApoIdentApplication.SetWmcClassName(this.report_managment_dialog);

            this.report_managment_dialog.TransientFor = parent;
            this.report_managment_dialog.IconList = ApoIdentStock.FinderIcons;

            this.data_exchange_checkbutton.Clicked += this.OnDataExchangeClicked;
            this.exchange_directory_entry.Changed += this.OnExchangeDirectoryEntryChanged;

            this.Update();

            GuiDesigner.SetSchema(this.report_managment_dialog);
            this.report_managment_dialog.Run();
            this.report_managment_dialog.Destroy();
        }

        private void Update()
        {

            if (this.Management.IsActivated == false)
            {
                this.exchange_directory_entry.Sensitive = false;
                this.exchange_directory_button.Sensitive = false;
                this.data_exchange_checkbutton.Active = false;
                this.directory_exists_label.Sensitive = false;
            }
            else
            {
                this.exchange_directory_entry.Sensitive = true;
                this.exchange_directory_button.Sensitive = true;
                this.data_exchange_checkbutton.Active = true;
                this.directory_exists_label.Sensitive = true;
            }

            if (FileIO.ExistsDirectory(this.Management.WorkingDir))
                this.directory_exists_label.Text = Catalog.GetString("Directory exists");
            else
                this.directory_exists_label.Text = Catalog.GetString("Directory does not exist");

            this.exchange_directory_entry.Text = this.Management.WorkingDir;
            this.exchange_directory_entry.TooltipText = this.Management.WorkingDir;
        }

        private void OnExchangeDirectoryEditButtonClicked(object sender, EventArgs e)
        {
            using (Gtk.FileChooserDialog chooser = new Gtk.FileChooserDialog(
                Catalog.GetString("Choose data exchange directory"),
                this.report_managment_dialog,
                FileChooserAction.SelectFolder))
            {
                chooser.AddButton(Gtk.Stock.Apply, Gtk.ResponseType.Ok);
                chooser.AddButton(Gtk.Stock.Cancel, Gtk.ResponseType.Cancel);
                chooser.DoOverwriteConfirmation = true;

                string dir = this.Management.WorkingDir;

                if (false == String.IsNullOrEmpty(dir) && FileIO.ExistsDirectory(dir))
                    chooser.SetCurrentFolder(dir);

                GuiDesigner.SetSchema(chooser);

                chooser.ShowAll();

                if ((ResponseType)chooser.Run() == ResponseType.Ok)
                {
                    this.Management.WorkingDir = chooser.Filename;
                    this.Update();
                }

                chooser.Destroy();
            }
        }

        private void OnExchangeDirectoryEntryChanged(object sender, EventArgs e)
        {
            this.Management.WorkingDir = this.exchange_directory_entry.Text;
            this.Update();
        }

        private void OnDataExchangeClicked (object sender, EventArgs e)
        {
            this.Management.IsActivated = this.data_exchange_checkbutton.Active;
            this.Update();
        }

        private DataExchange Management { get; set; }
    }
}
