// 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.SGS.SerialInterface;
using Hiperscan.Unix;

namespace Hiperscan.Extensions.ApoIdent
{
    public partial class Preferences
    {
        [Widget] Gtk.Entry device_address_entry = null;
        [Widget] Gtk.Entry device_port_entry = null;
        [Widget] Gtk.ComboBox device_search_combobox = null;
        [Widget] Gtk.Label device_address_label = null;
        [Widget] Gtk.Label device_port_label = null;
        [Widget] Gtk.Button device_check_button = null;

        private void AddDeviceEvents()
        {
            this.device_search_combobox.Changed += this.OnDeviceSearchComboboxChanged;
            this.device_address_entry.Changed += this.OnDeviceAddressEntryChanged;
            this.device_port_entry.Changed += this.OnDevicePortEntryChanged;
            this.device_check_button.Clicked += this.OndeviceCheckButtonClicked;
        }

        private void RemoveDeviceEvents()
        {
            this.device_search_combobox.Changed -= this.OnDeviceSearchComboboxChanged;
            this.device_address_entry.Changed -= this.OnDeviceAddressEntryChanged;
            this.device_port_entry.Changed -= this.OnDevicePortEntryChanged;
            this.device_check_button.Clicked -= this.OndeviceCheckButtonClicked;
        }

        private void OnDeviceSearchComboboxChanged(object sender, EventArgs e)
        {
            this.Config.DeviceSettings.DeviceSearchMode = (Configurations.DeviceSearchMode)this.device_search_combobox.Active;
            this.Update(PreferencesUpdateStatus.DeviceSettingsChanged);
        }

        private void OnDeviceAddressEntryChanged(object sender, EventArgs e)
        {
            this.Config.DeviceSettings.Url = StringHelper.CleanUserInputString(this.device_address_entry.Text);
        }

        private void OnDevicePortEntryChanged(object sender, EventArgs e)
        {
            try
            {
                this.Config.DeviceSettings.TcpPort = Int32.Parse(StringHelper.CleanUserInputString(this.device_port_entry.Text));
            }
            catch (Exception)
            {
                this.Config.DeviceSettings.TcpPort = -1;
            }
        }

        private void UpdateDeviceSection()
        {   
            this.device_search_combobox.Active = (int)this.Config.DeviceSettings.DeviceSearchMode;
            this.device_address_entry.Text = this.Config.DeviceSettings.Url;
            this.device_port_entry.Text = String.Empty;
            if (this.Config.DeviceSettings.TcpPort > -1)
                this.device_port_entry.Text = this.Config.DeviceSettings.TcpPort.ToString();

            if (this.Config.DeviceSettings.DeviceSearchMode == Configurations.DeviceSearchMode.Automatic)
            {
                this.device_address_entry.Sensitive = false;
                this.device_address_label.Sensitive = false;
                this.device_port_entry.Sensitive = false;
                this.device_port_label.Sensitive = false;
                this.device_check_button.Sensitive = false;
            }
            else
            {
                this.device_address_entry.Sensitive = true;
                this.device_address_label.Sensitive = true;
                this.device_port_entry.Sensitive = true;
                this.device_port_label.Sensitive = true;
                this.device_check_button.Sensitive = true;
            }
        }

        private void OndeviceCheckButtonClicked(object sender, EventArgs e)
        {
            string message = Catalog.GetString("Connection check: 0K");
            try
            {
                RemoteSerialInterface tmp = RemoteSerialInterface.GetRemoteObject(this.Config.DeviceSettings.Url,
                                                                                  (uint)this.Config.DeviceSettings.TcpPort);

                tmp.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();
            }
        }

        private bool DevicrSectionMessageShown { get; set; }
    }
}
