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

using Hiperscan.Unix;


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.HBox invalid_path_hbox                             = 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);

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

            this.report_managment_dialog.TransientFor = parent;
			this.Update();
            this.report_managment_dialog.IconList = ApoIdentStock.FinderIcons;
			this.report_managment_dialog.Run();
			this.report_managment_dialog.Destroy();
		}

		private void Update()
		{
			this.data_exchange_checkbutton.Clicked -= this.OnDataExchangeClicked;
			this.exchange_directory_entry.Changed  -= this.OnExchangeDirectoryEntryChanged;

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



            if (Directory.Exists(this.Management.WorkingDir) == true)
				this.invalid_path_hbox.Visible = false;
			else
				this.invalid_path_hbox.Visible = true;

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

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

		private void OnExchangeDirectoryEditButtonClicked(object sender, EventArgs e)
		{
			using (Gtk.FileChooserDialog chooser = new Gtk.FileChooserDialog(
				Catalog.GetString("Select 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 (String.IsNullOrEmpty(dir) == false && Directory.Exists(dir) == true)
					chooser.SetCurrentFolder(dir);

				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;}
	}
}