// 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 System.IO;
using Hiperscan.Unix;

namespace Hiperscan.Extensions.ApoIdent
{
    public partial class ApoIdentApplication
    {
        private void StartManuellSoftwareLauncherRaspian()  
        {
            SoftwareUpgradeRaspian upgrader = new SoftwareUpgradeRaspian(Plugin.replace_gui);
            upgrader.OnError += this.OnRaspianError;
            upgrader.Run();
        }

        private void OnRaspianError(object sender, EventArgs e)
        {
            SoftwareUpgradeRaspian upgrader = sender as SoftwareUpgradeRaspian;
             
                string message = String.Format(Catalog.GetString("Cannot start update process: {0}"),
                                               upgrader.Exception.Message);

            this.Update(ApoIdentApplicationState.ShowErrorDialogWithServiceDialog, message, upgrader.Exception);
        }

        private void StartAutomaticSoftwareLauncherWindows()
        {
            bool upgrade_is_active = Plugin.replace_gui;
            upgrade_is_active &= this.Config.SoftwareUpgradeSettings.SearchAutomatically;
    
            int delay = this.Config.SoftwareUpgradeSettings.DelayInDays;

            this.LaunchForNewSoftwareWindows(upgrade_is_active, delay, true);
        }

        private void StartManuellSoftwarLauncherWindows()
        {
            this.LaunchForNewSoftwareWindows(Plugin.replace_gui, 0, false);
        }

        private void LaunchForNewSoftwareWindows(bool can_upgrade, int delay_in_days, bool is_automatic)
        {
            DateTime start_execution_time = DateTime.Now;

            SoftwareUpgradeWindows upgrader = new SoftwareUpgradeWindows(can_upgrade,
                                                           delay_in_days);

            bool new_upgrades = upgrader.LaunchForUpgrades();

            if (new_upgrades)
                this.ShowUpgradeMessageDialog(upgrader, is_automatic);
            else
                this.ShowNoUpgradeMessageDialog(is_automatic);

            if (is_automatic)
                upgrader.DownloadUpgrades();

            Console.WriteLine("Software upgrader execution time: {0}",
                              (DateTime.Now - start_execution_time).TotalMilliseconds);
        }

        private void ShowUpgradeMessageDialog(SoftwareUpgradeWindows upgrader, bool is_automatic)
        {
            string message = Catalog.GetString("There are following upgrades available:\n\n");
            bool both = true;

            if (String.IsNullOrEmpty(upgrader.AvailableQuickStepUpgrade) == false)
            {
                message += String.Format(Catalog.GetString("\t- QuickStep Apo-Ident {0}\n\t  (program optimization / bugfix release)"),
                                                        upgrader.AvailableQuickStepUpgrade) + "\n";
            }
            else
            {
                both = false;
            }

            if (String.IsNullOrEmpty(upgrader.AvailableIdentModuleUpgrade) == false)
            {
                if (both)
                    message += "\n";

                message += String.Format(Catalog.GetString("\t- IdentModule {0}\n\t  (reference database / reception of certification)"),
                                                        upgrader.AvailableIdentModuleUpgrade) + "\n";
                both &= true;
            }
            else
            {
                both = false;
            }

            if (both)
                message += "\n\n" + Catalog.GetString("Do you want install them now?");
            else
                message += "\n\n" + Catalog.GetString("Do you want install it now?");

            Gtk.Window window = is_automatic ? this.GetTmpWinodow() : this.MainWindow;
            Gtk.MessageDialog mdlg = new Gtk.MessageDialog(window,
                                                           Gtk.DialogFlags.Modal,
                                                           Gtk.MessageType.Info,
                                                           Gtk.ButtonsType.YesNo,
                                                           message);

            mdlg.Title = Catalog.GetString("Upgrade Information");

            if (is_automatic)
                mdlg.WindowPosition = Gtk.WindowPosition.CenterAlways;

            mdlg.IconList = ApoIdentStock.FinderIcons;
            mdlg.TransientFor = window;

            mdlg.ShowAll();
            mdlg.Present();
            mdlg.KeepAbove = true;

            Gtk.ResponseType answer = (Gtk.ResponseType)mdlg.Run();
            mdlg.Destroy();

            if (is_automatic)
                window.Destroy();


            if (answer == Gtk.ResponseType.Yes)
            {
                if (is_automatic == false)
                {
                    if (this.CheckForNotSavedProtocolDetails() == false)
                        return;
                }

                upgrader.InstallUpgrades();

                if (is_automatic)
                    this.Control.Host.Quit();
                else
                    this.Exit(true);
            }
        }

        private void ShowNoUpgradeMessageDialog(bool is_automatic)
        {
            if (is_automatic == true)
                return;

            string message = Catalog.GetString("There are no upgrades available.");

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

            mdlg.Title = Catalog.GetString("Upgrade Information");

            mdlg.IconList = ApoIdentStock.FinderIcons;
            mdlg.TransientFor = window;

            mdlg.ShowAll();
            mdlg.Present();
            mdlg.KeepAbove = true;

            mdlg.Run();
            mdlg.Destroy();
        }
    }
}
