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

namespace Hiperscan.Extensions.ApoIdent
{
    public partial class Preferences
    {
        [Widget] Gtk.Alignment identmodule_alignment = null;
        [Widget] Gtk.Entry  identmodule_address_entry = null;
        [Widget] Gtk.Entry identmodule_port_entry = null;
        [Widget] Gtk.ComboBox identmodule_identification_combobox = null;
        [Widget] Gtk.Label identmodule_address_label = null;
        [Widget] Gtk.Label identmodule_port_label = null;
        [Widget] Gtk.Button identmodule_check_button = null;

        private void AddIdentModuleEvents()
        {
            this.identmodule_identification_combobox.Changed += this.OnIdentificationComboboxChanged;
            this.identmodule_address_entry.Changed += this.OnIdentModuleAddressEntryChanged;
            this.identmodule_port_entry.Changed += this.OnIdentModulePortEntryChanged;
            this.identmodule_check_button.Clicked += this.OnIdentModuleCheckButtonClicked;
        }

        private void RemoveIdentModuleEvents()
        {
            this.identmodule_identification_combobox.Changed -= this.OnIdentificationComboboxChanged;
            this.identmodule_address_entry.Changed -= this.OnIdentModuleAddressEntryChanged;
            this.identmodule_port_entry.Changed -= this.OnIdentModulePortEntryChanged;
            this.identmodule_check_button.Clicked -= this.OnIdentModuleCheckButtonClicked;
        }

        private void OnIdentificationComboboxChanged(object sender, EventArgs e)
        {
            this.Control.IdentModuleSettings.ModuleLocation = (RemoteModuleLocationTypes)this.identmodule_identification_combobox.Active;
            this.Update(PreferencesUpdateStatus.IdentModuleSettingsChanged);
        }

        private void OnIdentModuleAddressEntryChanged(object sender, EventArgs e)
        {
            this.Control.IdentModuleSettings.Url = StringHelper.CleanUserInputString(this.identmodule_address_entry.Text);
        }

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

        private void UpdateIdentModuleSection()
        {
            this.identmodule_alignment.Visible = this.Control.GlobalStates.IsIdentModuleAvailable;
            this.identmodule_identification_combobox.Active = (int)this.Control.IdentModuleSettings.ModuleLocation;
            this.identmodule_address_entry.Text = this.Control.IdentModuleSettings.Url;
            this.identmodule_port_entry.Text = String.Empty;
            if (this.Control.IdentModuleSettings.TcpPort > -1)
                this.identmodule_port_entry.Text = this.Control.IdentModuleSettings.TcpPort.ToString();

            if (this.Control.IdentModuleSettings.ModuleLocation == RemoteModuleLocationTypes.Local)
            {
                this.identmodule_address_entry.Sensitive = false;
                this.identmodule_address_label.Sensitive = false;
                this.identmodule_port_entry.Sensitive = false;
                this.identmodule_port_label.Sensitive = false;
                this.identmodule_check_button.Sensitive = false;
            }
            else
            {
                this.identmodule_address_entry.Sensitive = true;
                this.identmodule_address_label.Sensitive = true;
                this.identmodule_port_entry.Sensitive = true;
                this.identmodule_port_label.Sensitive = true;
                this.identmodule_check_button.Sensitive = true;
            }
        }

        private void OnIdentModuleCheckButtonClicked(object sender, EventArgs e)
        {
            string message = Catalog.GetString("Connection check: 0K");
            try
            {
                IdentModuleClient tmp = new IdentModuleClient(this.Control.IdentModuleSettings.RemoteAddress,
                                                                 this.Control.IdentModuleSettings.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();
            }
        }
    }
}
