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


namespace Hiperscan.Extensions.ApoIdent
{
    public partial class ApoIdentApplication
    {
        private void __UpdateMessageDialog(ApoIdentApplicationState state, string message, Exception ex = null, string title = null, EventHandler event_handler = null)
        {
            this.DialogResponse = Gtk.ResponseType.None;
            bool dont_show_again = false;
            bool write_info_to_console = false;

            switch (state)
            {
                case ApoIdentApplicationState.ShowErrorDialogWithServiceDialog:
                    message += ApoIdentApplication.ServiceForRepeatedError;
                    this.__DialogShow(Catalog.GetString("Error"),
                                    Gtk.DialogFlags.Modal,
                                    Gtk.MessageType.Error,
                                    Gtk.ButtonsType.Ok,
                                    message);
                    write_info_to_console = true;
                    break;

                case ApoIdentApplicationState.ShowErrorDialogWithoutSerivceDialog:
                    this.__DialogShow(Catalog.GetString("Error"),
                                    Gtk.DialogFlags.Modal,
                                    Gtk.MessageType.Error,
                                    Gtk.ButtonsType.Ok,
                                    message);
                    write_info_to_console = true;
                    break;




                case ApoIdentApplicationState.ConnectionStartIdentServerFailed:
                    message = Catalog.GetString("Cannot start Ident Module. This component is required to perform " +
                                             "substance analyses on your computer locally. Please also check if a compatible version is installed.\n\n" +
                                             "In case you do not have a license for HiperScan IdentModule, please " +
                                             "contact our customer services for further information:\n" +
                                             "Email: kundenservice@apo-ident.de\nTelefon: +49 (0)351 212 496 33");
                    dont_show_again = this.Config.GuiSettings.DontShowServerMessageAgain;
                    this.Config.GuiSettings.DontShowServerMessageAgain = this.__QSShowHintMessage(message, dont_show_again);
                    write_info_to_console = true;
                    break;



                case ApoIdentApplicationState.ConnectionIdentServerVersionRefused:
                    message = String.Format(Catalog.GetString("The connection to the Ident Module on \"{0}\" failed: {1}"),
                                            this.IdentServerClient.Address, message);
                    message += ServiceForRepeatedError;
                    this.__QSShowInfoMessage(message);
                    write_info_to_console = true;
                    break;



                case ApoIdentApplicationState.ConnectionIdentServerRefused:
                    message = String.Format(Catalog.GetString("The connection to the Ident-module on \"{0}\" failed: {1}"),
                                            this.IdentServerClient.Address, message);

                    message += Catalog.GetString("\n" +
                                                         "Please check the connection settings in the preferences dialog. " +
                                                         "You can find these settings in the register \"Acouisition settings -> Identification\". " +
                                                         "After closing the preferences dialog the program tries to connect to the Ident Module " +
                                                         "or you can connect manually with a click on the button " +
                                                         "\"Connect\" below in the toolbar of the Apo-Ident surface");
                    message += ServiceForRepeatedError;
                    this.__QSShowInfoMessage(message);
                    write_info_to_console = true;
                    break;

                case ApoIdentApplicationState.ConnnectionDeviceRegistrationFailed:
                    message = String.Format(Catalog.GetString("The registration for device \"{0}\" failed: {1}"),
                                               this.Finder.Serial, message);
                    message += ServiceForRepeatedError;
                    this.__QSShowInfoMessage(message);
                    write_info_to_console = true;
                    break;

                case ApoIdentApplicationState.ConnectionLoadDataFailed:
                    message = String.Format(Catalog.GetString("Could not load models for this device. (Serial number {0}): {1}"),
                                                this.Finder.Serial, message);
                    message += ApoIdentApplication.ServiceForRepeatedError;
                    this.__QSShowInfoMessage(message);
                    write_info_to_console = true;
                    break;

                case ApoIdentApplicationState.ConnectDeviceWarmupInterrupted:
                    this.__DialogShow(Catalog.GetString("Cancelling Warmup"),
                                    Gtk.DialogFlags.Modal,
                                    Gtk.MessageType.Question,
                                    Gtk.ButtonsType.YesNo,
                               Catalog.GetString("Do you want to cancel start-up and quit application?"),
                                   3000);
                    break;

                case ApoIdentApplicationState.CoonectDeviceWarmupFailed:
                    message = String.Format(Catalog.GetString("The warming up of the device failed: {0}"),
                                            message);
                    message = message + ApoIdentApplication.ServiceForRepeatedError;
                    this.__QSShowInfoMessage(message);
                    write_info_to_console = true;
                    break;

                case ApoIdentApplicationState.ConnectionLost:
                case ApoIdentApplicationState.ConnectionUnknownError:
                case ApoIdentApplicationState.ConnectionCheckIdentModuleFailed:
                case ApoIdentApplicationState.ConnectionInitDeviceFailed:
                case ApoIdentApplicationState.ConnectionInitFinderFailed:
                    message += ApoIdentApplication.ServiceForRepeatedError;
                    this.__QSShowInfoMessage(message);
                    write_info_to_console = true;
                    break;





                case ApoIdentApplicationState.ConnectionRemindOfUpdate:
                    this.ShowRemindOfUpdateDialog();
                    break;

                case ApoIdentApplicationState.ImportantCustomerInfo:
                    if(this.__QSShowHintMessage(message, false))
                        ImportantCustomerInfo.MarkAsRead();
                    break;

                case ApoIdentApplicationState.CheckSettings:
                    Preferences.Start(this);
                    break;

                case ApoIdentApplicationState.SubstanceManagementModelClassDialog:
                    ModelClassDialog.Start(this.MainWindow, this.SubstanceManagement);
                    break;

                case ApoIdentApplicationState.SubstanceManagementInfoButtonClassificationDialog:
                    ClassificationInfoDialog.Start(this.MainWindow, this.SubstanceManagement);
                    break;

                case ApoIdentApplicationState.SubstanceManagementNotMeasurableDialog:
                    dont_show_again = this.Config.Profiles.Current.DontShowNotMeasurableDialogAgain;
                    this.Config.Profiles.Current.DontShowNotMeasurableDialogAgain = this.__QSShowHintMessage(message, dont_show_again);
                    break;

                case ApoIdentApplicationState.SubstanceManagementAdditionalTextDialog:
                    this.__DialogShow(Catalog.GetString("Warning"),
                                    Gtk.DialogFlags.Modal,
                                      Gtk.MessageType.Warning,
                                    Gtk.ButtonsType.Ok,
                                    message);
                    break;

                case ApoIdentApplicationState.SubstanceManagementExchangeNoDataDialog:
                    this.__QSShowInfoMessage(message);
                    break;

                case ApoIdentApplicationState.SubstanceManagementExchangeSelectFileDialog:
                    this.DialogResponse = DataExchangeImportDialog.Run(this.MainWindow, this.Config.Profiles.Current.ExchangeData);
                    break;

                case ApoIdentApplicationState.SubstanceManagementExchangeSelectSubstanceDialog:
                    FuzzyNameDialog.Run(this.MainWindow, this.Config.Profiles.Current.ExchangeData);
                    break;


                case ApoIdentApplicationState.MeasuringTransflectanceFailed:
                case ApoIdentApplicationState.MeasuringBlackFailed:
                case ApoIdentApplicationState.MeasuringWhiteFailed:
                    message = title + message;
                    this.__QSShowInfoMessage(message, event_handler, Gtk.Stock.Info);
                    write_info_to_console = true;
                    break;

                case ApoIdentApplicationState.MeasuringFailed:
                    message = title + message;
                    message += ApoIdentApplication.ServiceForRepeatedError;
                    this.__QSShowInfoMessage(message);
                    write_info_to_console = true;
                    break;

                case ApoIdentApplicationState.MeasuringInterruptedEnd:
                    this.__QSShowInfoMessage(message);
                    break;

                case ApoIdentApplicationState.MeasuringResultCheck:
                    this.__ShowMissIdentificationDialog(message);
                    break;

                case ApoIdentApplicationState.ResultAdditionalTestsWindowStart:
                    AdditionalTestWindow.Start(this);
                    break;

                case ApoIdentApplicationState.ResultShowPlots:
                    this.ShowResultPlots();
                    break;

                case ApoIdentApplicationState.ResultShowValuationList:
                    this.ResultShowValuationList();
                    break;

                case ApoIdentApplicationState.ResultPreSuggestion:
                    this.__QSShowInfoMessage(message);
                    break;

                case ApoIdentApplicationState.ProtocolCheckSave:
                    this.__ShowRemindSavingDialog();
                    break;

                case ApoIdentApplicationState.ProtocolSavingFailed:
                case ApoIdentApplicationState.ProtocolShowingFailed:
                case ApoIdentApplicationState.ProtocolPrintingFailed:
                case ApoIdentApplicationState.ProtocolLabelPrintingFailed:
                    message += ApoIdentApplication.ServiceForRepeatedError;
                    this.__QSShowInfoMessage(message);
                    write_info_to_console = true;
                    break;

                case ApoIdentApplicationState.ProtocolDataExchangeOk:
                    this.__QSShowInfoMessage(message);
                    break;

                case ApoIdentApplicationState.ProtocolDataExchangeFailed:
                    message += ApoIdentApplication.ServiceForRepeatedError;
                    this.__DialogShow(Catalog.GetString("Error"),
                                    Gtk.DialogFlags.Modal,
                                    Gtk.MessageType.Error,
                                    Gtk.ButtonsType.Ok,
                                    message);
                    write_info_to_console = true;
                    break;

                case ApoIdentApplicationState.PreferencesDialog:
                    Preferences.Start(this);
                    break;

                case ApoIdentApplicationState.QueryDialog:
                    QueryDialog.Start(this.Config, this.MainWindow, this.Control.Transaction);
                    break;

                case ApoIdentApplicationState.SubstancesDialog:
                    SubstanceExtensionDialog.Start(this);
                    break;

                default:
                    break;
            }

            if (write_info_to_console)
            {
                string console = String.Format("Error state '{0}': {1}", state, message);
                if (ex != null)
                    console += "\n" + ex.ToString();

                Console.WriteLine(console);
            }
        }

        public  Gtk.ResponseType DialogResponse { get; private set; }
    }
}
