// 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;

namespace Hiperscan.Extensions.ApoIdent
{
    public partial class ApoIdentApplication
    {
        [Widget] Gtk.Dialog identmodule_info_dialog = null;

        private void InitServerClientInstance()
        {
            this.IdentServerClient = new IdentServerClient(this.Config);
        }


        private static string GetSpectrumCreatorName()
        {
            string creator = Plugin.name + " " + Plugin.version;
            if (IdentServerClient.Info != null)
                creator += ", IdentModule " + IdentServerClient.Info.Version.IdentModuleVersion;

            return creator;
        }

        private void ShowRemindOfUpdateDialog()
        {
            Glade.XML gui = new Glade.XML(null, "ApoIdent.Resources.Glade.FinderWindow.glade", "identmodule_info_dialog", "Hiperscan");
            gui.Autoconnect(this);

            this.identmodule_info_dialog.SetWmclass(Plugin.name.ToLower(),
                                           Plugin.name.ToLower());

            this.identmodule_info_dialog.TransientFor = this.MainWindow;
            this.identmodule_info_dialog.IconList = ApoIdentStock.FinderIcons;

            Gtk.ResponseType response = (Gtk.ResponseType)this.identmodule_info_dialog.Run();

            identmodule_info_dialog.Destroy();

            if (response == Gtk.ResponseType.Apply)
                this.OpenIdentServerUrl();

            if (this.DontShowIdentModuleDialogAgain == true)
                this.Config.IdentModuleSettings.ReleaseExpirationTime = DateTime.Now.AddMonths(1);
        }

        private void OpenIdentServerUrl()
        {
            try
            {
                Process.OpenUri(Uris.OnlineHelp(ReferrerId.IdentModuleUpdate, false));
            }
            catch (Exception ex)
            {
                string message = String.Format(Catalog.GetString("Cannot open link to Apo-Ident Service Center: {0}"),
                                               ex.Message);

                this.Update(ApoIdentApplicationState.ShowErrorDialogWithoutSerivceDialog, message, ex);
            }
        }

        private void OnIdentModuleInfoToggled(object sender, EventArgs e)
        {
            this.DontShowIdentModuleDialogAgain = (sender as Gtk.CheckButton).Active;
        }


        private bool DontShowIdentModuleDialogAgain { get; set; }
        public IdentServerClient IdentServerClient { get; private set; } = null;
    }
}
