//
//  AcquisitionPreferencesDialog.cs
//
// QuickStep: Acquire, view and identify spectra 
// Copyright (C) 2010-2017  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.Acquisition
{

    [Obsolete("This implementation was just a prototype for " +
              "Hiperscan.SGS.Benchtop.AsyncStabilizedFinder. " +
              "It is outdated und must be reimplemented!")]
    public class AcquisitionSettingsDialog
    {
        [Widget] Gtk.Dialog acquisition_settings = null;
        [Widget] Gtk.SpinButton external_reference_spin = null;
        [Widget] Gtk.SpinButton internal_calibration_spin = null;
        [Widget] Gtk.SpinButton acquisition_spin = null;
        [Widget] Gtk.SpinButton acquisition_sgs_si_spin = null;
        [Widget] Gtk.SpinButton acquisition_es20_si_spin = null;
        [Widget] Gtk.CheckButton test_process_checkbutton = null;
        [Widget] Gtk.Button ok_button2           = null;

        
        public AcquisitionSettingsDialog()
        {
            Glade.XML gui = new Glade.XML(null, "Hiperscan.Extensions.Acquisition.Acquisition.glade", "acquisition_settings", "Hiperscan");
            gui.Autoconnect(this);

            this.UpdateSettingWindow();

            this.acquisition_settings.DeleteEvent += (o, args) =>
            {
                args.RetVal = true;
                this.OnOkButtonClicked(o, args);
            };
            
            this.ok_button2.CanDefault = true;
            this.acquisition_settings.Default = this.ok_button2;
            
            this.acquisition_spin.ActivatesDefault = true;
            this.external_reference_spin.ActivatesDefault   = true;
            
            this.acquisition_settings.ShowAll();
        }
        
        internal void OnOkButtonClicked(object o, EventArgs e)
        {
            Settings.Current.ExternalReferenceAverage   = (ushort)this.external_reference_spin.Value;
            Settings.Current.InternalCalibrationAverage = (ushort)this.internal_calibration_spin.Value;
            Settings.Current.AcquisitionAverage = (ushort)this.acquisition_spin.Value;
            Settings.Current.AcquisitionAverageSgsSI = (ushort)this.acquisition_sgs_si_spin.Value;
            Settings.Current.AcquisitionAverageEs20SI = (ushort)this.acquisition_es20_si_spin.Value;
            Settings.Current.TestProcess = this.test_process_checkbutton.Active;
            this.acquisition_settings.Destroy();
        }

        private void OnResetButtonClicked(object sender, EventArgs e)
        {
            Settings.Current.ExternalReferenceAverage = 2000;
            Settings.Current.InternalCalibrationAverage = 500;
            Settings.Current.AcquisitionAverage = 500;
            Settings.Current.AcquisitionAverageSgsSI = 2000;
            Settings.Current.AcquisitionAverageEs20SI = 500;
            Settings.Current.TestProcess = false;

            this.UpdateSettingWindow();
        }

        private void UpdateSettingWindow()
        {
            this.external_reference_spin.Value = Settings.Current.ExternalReferenceAverage;
            this.internal_calibration_spin.Value = Settings.Current.InternalCalibrationAverage;
            this.acquisition_spin.Value = Settings.Current.AcquisitionAverage;
            this.acquisition_sgs_si_spin.Value = Settings.Current.AcquisitionAverageSgsSI;
            this.acquisition_es20_si_spin.Value = Settings.Current.AcquisitionAverageEs20SI;
            this.test_process_checkbutton.Active = Settings.Current.TestProcess;
        }

        private void OnSettingsInfoButtonClicked(object sender, EventArgs e)
        {
            string message = Catalog.GetString(@"If you want to use this feature, you have to first install the 'correctiontool'.
Do not use non-ASCII characters like 'ä' for the save path, lest test processing will fail!");

            MessageDialog mdlg = new MessageDialog(this.acquisition_settings,
                                                   DialogFlags.Modal,
                                                   MessageType.Info,
                                                   ButtonsType.Ok,
                                                   message);

            mdlg.Run();
            mdlg.Destroy();
        }
    }
}