// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2022 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.ApoIdentCore;
using Hiperscan.ApoIdentCore.GuiControl;
using Hiperscan.ApoIdentCore.Misc;
using Hiperscan.Unix;

namespace Hiperscan.Extensions.ApoIdent
{
    public class RemindForUpdateDialog
    {
        [Widget] Gtk.Dialog pending_info_dialog = null;
        [Widget] Gtk.Label pending_info_label = null;
        [Widget] Gtk.Button pending_download_button = null;

        public static void Run(Gtk.Window parent, AppControl control, UpdateArgs args)
        {
            var tmp = new RemindForUpdateDialog(parent, control, args);
        }

        private RemindForUpdateDialog(Gtk.Window parent, AppControl control, UpdateArgs args)
        {
            this.Args = args;

            Glade.XML gui = new Glade.XML(null, "ApoIdent.Resources.Glade.FinderWindow.glade", "pending_info_dialog", "Hiperscan");
            gui.Autoconnect(this);

            ApoIdent.ApoIdentApplication.SetWmcClassName(this.pending_info_dialog);

            this.pending_info_dialog.TransientFor = parent;
            this.pending_info_dialog.IconList = ApoIdentStock.FinderIcons;
            this.pending_download_button.Visible = args.DialogArgs.AnalysisType == AnalysisType.Identification;
            this.pending_info_label.Text = String.Format(this.pending_info_label.Text,
                                                         this.Args.DialogArgs.AnalysisType.GetProductName());

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

            pending_info_dialog.Destroy();

            if (response == Gtk.ResponseType.Apply)
                this.OpenServerUrl(control);
        }

        private void OpenServerUrl(AppControl control)
        {
            control.DocumentManager.OpenUri(UriReferrerId.IdentModuleUpdate);
        }

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

        private UpdateArgs Args { get; set; }
    }
}
