// 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.AnalysisModule;
using Hiperscan.AnalysisModule.Quantification;
using Hiperscan.ApoIdentCore.Misc;
using Hiperscan.ApoIdentCommon.ApoConfig.Enums;
using Hiperscan.Unix;

namespace Hiperscan.Extensions.ApoIdent
{
    public partial class Preferences
    {
        [Widget] Gtk.Alignment quantifiermodule_alignment = null;
        [Widget] Gtk.Entry quantifiermodule_address_entry = null;
        [Widget] Gtk.Entry quantifiermodule_port_entry = null;
        [Widget] Gtk.ComboBox quantifiermodule_quantification_combobox = null;
        [Widget] Gtk.Label quantifiermodule_address_label = null;
        [Widget] Gtk.Label quantifiermodule_port_label = null;
        [Widget] Gtk.Button quantifiermodule_check_button = null;

        private void AddQuantifierModuleEvents()
        {
            this.quantifiermodule_quantification_combobox.Changed += this.OnQuantificationomboboxChanged;
            this.quantifiermodule_address_entry.Changed += this.OnQuantifierModuleAddressEntryChanged;
            this.quantifiermodule_port_entry.Changed += this.OnQuantifierModulePortEntryChanged;
            this.quantifiermodule_check_button.Clicked += this.OnQuantifierModuleCheckButtonClicked;
        }

        private void RemoveQuantifierModuleEvents()
        {
            this.quantifiermodule_quantification_combobox.Changed -= this.OnQuantificationomboboxChanged;
            this.quantifiermodule_address_entry.Changed -= this.OnQuantifierModuleAddressEntryChanged;
            this.quantifiermodule_port_entry.Changed -= this.OnQuantifierModulePortEntryChanged;
            this.quantifiermodule_check_button.Clicked -= this.OnQuantifierModuleCheckButtonClicked;
        }

        private void OnQuantificationomboboxChanged(object sender, EventArgs e)
        {
            this.Control.QuantifierModuleSettings.ModuleLocation = (RemoteModuleLocationTypes)this.quantifiermodule_quantification_combobox.Active;
            this.Update(PreferencesUpdateStatus.QuantifierModuleSettingsChanged);
        }

        private void OnQuantifierModuleAddressEntryChanged(object sender, EventArgs e)
        {
            this.Control.QuantifierModuleSettings.Url = StringHelper.CleanUserInputString(this.quantifiermodule_address_entry.Text);
        }

        private void OnQuantifierModulePortEntryChanged(object sender, EventArgs e)
        {
            try
            {
                this.Control.QuantifierModuleSettings.TcpPort = Int32.Parse(StringHelper.CleanUserInputString(this.quantifiermodule_port_entry.Text));
            }
            catch (Exception)
            {
                this.Control.QuantifierModuleSettings.TcpPort = -1;
            }
        }

        private void UpdateQuantifierModuleSection()
        {
            this.quantifiermodule_alignment.Visible = this.Control.GlobalStates.IsQuantifierModuleAvailable;
            this.quantifiermodule_quantification_combobox.Active = (int)this.Control.QuantifierModuleSettings.ModuleLocation;
            this.quantifiermodule_address_entry.Text = this.Control.QuantifierModuleSettings.Url;
            this.quantifiermodule_port_entry.Text = String.Empty;
            if (this.Control.QuantifierModuleSettings.TcpPort > -1)
                this.quantifiermodule_port_entry.Text = this.Control.QuantifierModuleSettings.TcpPort.ToString();

            if (this.Control.QuantifierModuleSettings.ModuleLocation == RemoteModuleLocationTypes.Local)
            {
                this.quantifiermodule_address_entry.Sensitive = false;
                this.quantifiermodule_address_label.Sensitive = false;
                this.quantifiermodule_port_entry.Sensitive = false;
                this.quantifiermodule_port_label.Sensitive = false;
                this.quantifiermodule_check_button.Sensitive = false;
            }
            else
            {
                this.quantifiermodule_address_entry.Sensitive = true;
                this.quantifiermodule_address_label.Sensitive = true;
                this.quantifiermodule_port_entry.Sensitive = true;
                this.quantifiermodule_port_label.Sensitive = true;
                this.quantifiermodule_check_button.Sensitive = true;
            }
        }

        private void OnQuantifierModuleCheckButtonClicked(object sender, EventArgs e)
        {
            string message = Catalog.GetString("Connection check: 0K");
            try
            {
                QuantifierModuleClient tmp = new QuantifierModuleClient(this.Control.QuantifierModuleSettings.RemoteAddress,
                                                                        this.Control.QuantifierModuleSettings.RemotePort);
               
                tmp.RemoteService.Ping();

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

                mdlg.Title = Catalog.GetString("Check Connection");
                mdlg.IconList = ApoIdentStock.FinderIcons;
                mdlg.Run();
                mdlg.Destroy();

            }
            catch (Exception)
            {
                message = Catalog.GetString("Connection check: Failed");
                Gtk.MessageDialog mdlg = new Gtk.MessageDialog(this.preferences_dialog,
                                                               Gtk.DialogFlags.Modal,
                                                               Gtk.MessageType.Error,
                                                               Gtk.ButtonsType.Ok,
                                                               message);

                mdlg.Title = Catalog.GetString("Check Connection");
                mdlg.IconList = ApoIdentStock.FinderIcons;
                mdlg.Run();
                mdlg.Destroy();
            }
        }
    }
}
