// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2021 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 Hiperscan.ApoIdentCommon;
using Hiperscan.GtkExtensions.Widgets;
using Hiperscan.SGS.SerialInterface;
using Hiperscan.Unix;

namespace Hiperscan.Extensions.ApoIdent
{
    public partial class Preferences
    {
        [Widget] Gtk.CheckButton software_checkbutton = null;
        [Widget] Gtk.Label software_delay_label = null;
        [Widget] Gtk.SpinButton software_spinbutton = null;
        [Widget] Gtk.Alignment software_alignment = null;

        private void AddSoftwareEvents()
        {
            this.software_checkbutton.Toggled += this.OnSoftwareAutomaticCheckbuttoToogled;
            this.software_spinbutton.FocusOutEvent += this.OnSoftwareDelaySppinButtonValueChanged;
            this.software_spinbutton.ValueChanged += this.OnSoftwareDelaySppinButtonValueChanged;
        }

        private void RemoveSoftwareEvents()
        {
            this.software_checkbutton.Toggled -= this.OnSoftwareAutomaticCheckbuttoToogled;
            this.software_spinbutton.FocusOutEvent -= this.OnSoftwareDelaySppinButtonValueChanged;
            this.software_spinbutton.ValueChanged -= this.OnSoftwareDelaySppinButtonValueChanged;
        }

        private void OnSoftwareAutomaticCheckbuttoToogled(object sender, EventArgs e)
        {
            this.Control.SoftwareUpgradeSettings.SearchAutomatically = this.software_checkbutton.Active;
            this.Update(PreferencesUpdateStatus.SoftwareSettingsChanged);
        }

        private void OnSoftwareDelaySppinButtonValueChanged(object sender, EventArgs e)
        {
            try
            {
                this.Control.SoftwareUpgradeSettings.DelayInDays = (int)this.software_spinbutton.Value;
            }
            catch (Exception) { }
        }

        private void UpdateSoftwareSection()
        {
            ApoIdentExecutionMode mode = ApoIdentExecutionModeRecognizer.GetMode();
            this.software_alignment.Visible = mode == ApoIdentExecutionMode.Windows;
            this.software_checkbutton.Active = this.Control.SoftwareUpgradeSettings.SearchAutomatically;
            this.software_spinbutton.Value = this.Control.SoftwareUpgradeSettings.DelayInDays;

            if (this.Control.SoftwareUpgradeSettings.SearchAutomatically == false)
            {
                this.software_spinbutton.Sensitive = false;
                this.software_delay_label.Sensitive = false;
            }
            else
            {
                this.software_spinbutton.Sensitive = true;
                this.software_delay_label.Sensitive = true;
            }
        }

        private void OnSoftwareLinkbuttonClicked(object sender, EventArgs e)
        {
            string title = Catalog.GetString("Description 'Automatically check for Upgrades'");
            string message = Catalog.GetString
            (
"If you choose this option, then the Apo-Ident software will look up possible upgrades online automatically and download them for installation."
            );

            ExtToolTip.SetDialog(sender as Gtk.LinkButton,
                           Gtk.Stock.DialogInfo,
                           title,
                           message);
        }
    }
}
