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

namespace Hiperscan.Extensions.ApoIdent
{
    public partial class ApoIdentApplication
    {
        private void RequestQuit()
        {
            if (this.Finder != null)
                this.Finder.QuitRequested = true;

            this.Control.QuitRequested = true;
        }

        private void OnDeleteEvent(System.Object o, DeleteEventArgs e)
        {
            e.RetVal = true;
            this.Exit();
        }

        private void Exit(bool silent = false)
        {
            if (this.Control.ProgramExitCalled == true)
                return;

            Application.Invoke((sender, e) =>
            {
                if (this.CheckForNotSavedProtocolDetails() == false && silent == false)
                    return;

                this.MainWindow.Visible = silent == false;

                if (Plugin.replace_gui && silent == false)
                {
                    if (this.Control.Host.CheckForUnsavedSpectra() == false)
                        return;
                }

                this.Control.ProgramExitCalled = true;
                this.RequestQuit();
                this.Control.Busy = true;
                this.Update(ApoIdentApplicationState.Exit);

                WaitWindow wait_win = null;

                if (silent == false)
                {
                    wait_win = new WaitWindow(this.MainWindow,
                                                             Catalog.GetString("The application is closing ..."),
                                                             500);
                }

                this.Scanner.Exit();

                try
                {
                    this.Finder.Reset();
                }
                catch (Exception)
                {

                }

                if (this.Config.GuiSettings.WindowMaximized == false)
                {
                    this.main_window.GetSize(out int width, out int height);
                    this.Config.GuiSettings.WindowWidth = width;
                    this.Config.GuiSettings.WindowHeight = height;
                }

                try
                {
                    this.Config.WriteToXmlFile();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error while saving apo-ident configuration file: {0}", ex);
                }

                try
                {
                    this.IdentServerClient.StopIdentModule();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Failed to terminate IdentServer: " + ex.Message);
                }

                if (Plugin.replace_gui)
                {
                    this.Control.Host.MainWindow.Destroy();
                    this.Control.Host.Quit(true);
                }
                else
                {
                    ApoIdentApplication.CurrentInstance = null;
                    this.MainWindow.Destroy();
                }

                if (wait_win != null)
                    wait_win.Destroy();

            });
        }
    }
}
