// 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.Common.I18n;
using Hiperscan.Extensions.ApoIdentCommon;
using Hiperscan.Unix;


namespace Hiperscan.Extensions.ApoIdent
{
    public partial class ApoIdentApplication
    {
        private void InitHelpMenue()
        {
            bool show_manual = true;


            this.HelpMenu = new Menu();

            IconSize size = IconSize.Button;

            ImageMenuItem manual_item = new ImageMenuItem(Catalog.GetString("User Manual"));
            manual_item.Image = new Gtk.Image(Stock.File,size);
            manual_item.Activated += this.OnUserManualActivated;
            manual_item.NoShowAll = true;
            manual_item.Visible = show_manual;
            this.HelpMenu.Append(manual_item);


            ImageMenuItem quick_references_item = new ImageMenuItem(Catalog.GetString("Service-Center Online Help"));
            quick_references_item.Image = new Gtk.Image(Stock.Help, size);
            this.HelpMenu.Append(quick_references_item);
            quick_references_item.Activated += this.OnOnlineHelpActivated;

            this.help_validation_doc_menuitem = new ImageMenuItem(Catalog.GetString("Validation Documents"));           
            this.help_validation_doc_menuitem.Sensitive = false;
            this.HelpMenu.Append(this.help_validation_doc_menuitem);

            ImageMenuItem export_item = new ImageMenuItem(Catalog.GetString("Data Backup"));
            export_item.Image = new Gtk.Image(Stock.SaveAs, size);
            export_item.Activated += this.OnExportServiceDataActivated;
            this.HelpMenu.Append(export_item);

            this.help_additional_tests_item = new ImageMenuItem(Catalog.GetString("Additional Tests"));
            this.help_additional_tests_item.NoShowAll = true;
            this.help_additional_tests_item.Visible = false;
            this.help_additional_tests_item.Image = new Gtk.Image(Stock.File, size);
            this.help_additional_tests_item.Activated += this.OnShowAdditionalTests;
            this.HelpMenu.Append(this.help_additional_tests_item);

            this.help_available_substances_menuitem = new ImageMenuItem(Catalog.GetString("Show Subscribed Substances"));
            this.help_available_substances_menuitem.Image = new Gtk.Image(Stock.File, size);
            this.help_available_substances_menuitem.Data["overview"] = SubstancePdfs.PdfListType.Available;
            this.help_available_substances_menuitem.Activated += this.OnShowSubstanceOverviewActivated;
            this.help_available_substances_menuitem.Sensitive = false;
            this.HelpMenu.Append(this.help_available_substances_menuitem);

            this.help_available_insert_substances_menuitem = new ImageMenuItem(Catalog.GetString("Show Subscribed Substances With Insert"));
            this.help_available_insert_substances_menuitem.Image = new Gtk.Image(Stock.File, size);
            this.help_available_insert_substances_menuitem.Data["overview"] = SubstancePdfs.PdfListType.Insert;
            this.help_available_insert_substances_menuitem.Activated += this.OnShowSubstanceOverviewActivated;
            this.HelpMenu.Append(this.help_available_insert_substances_menuitem);

            this.help_substance_update_info_menuitem = new ImageMenuItem(Catalog.GetString("Substance and Batch Maintenance"));
            this.help_substance_update_info_menuitem.Image = new Gtk.Image(Stock.File, size);
            this.help_substance_update_info_menuitem.Data["overview"] = SubstancePdfs.PdfListType.Added;
            this.help_substance_update_info_menuitem.Sensitive = false;
            this.help_substance_update_info_menuitem.Activated += this.OnShowSubstanceOverviewActivated;
            this.HelpMenu.Append(this.help_substance_update_info_menuitem);

            ImageMenuItem form_item = new ImageMenuItem(Catalog.GetString("Order Form for Substances"));
            form_item.Image = new Gtk.Image(Stock.Edit, size);
            form_item.Activated += this.OnShowOrderFormActivated;
            form_item.NoShowAll = true;
            form_item.Visible = Locale.Language == "de";
            this.HelpMenu.Append(form_item);

            ImageMenuItem upgrade_item = new ImageMenuItem(Catalog.GetString("Check for Upgrades"));
            upgrade_item.Image = new Gtk.Image(Stock.Edit, size);
            upgrade_item.Activated += this.OnUpgradeSoftwareActivated;
            upgrade_item.NoShowAll = true;

            ApoIdentExecutionMode mode = ApoIdentExecutionModeRecognizer.GetMode();
            upgrade_item.Visible = (mode == ApoIdentExecutionMode.Windows || 
                                    mode == ApoIdentExecutionMode.RaspianStandAlone) && 
                                    Plugin.replace_gui;

            this.HelpMenu.Append(upgrade_item);

            if (ApoIdentEnvs.GuiColorOn)
            {
                ImageMenuItem color_item = new ImageMenuItem(Catalog.GetString("Color Settings"));
                color_item.Image = new Gtk.Image(Stock.SelectColor, size);
                color_item.Activated += this.OnShowColorDialogActivated;
                this.HelpMenu.Append(color_item);
            }

            GtkExtensions.Widgets.Font.SetFont(this.HelpMenu, ApoIdentApplication.FONT_FAMILIY);
            this.HelpMenu.ShowAll();
        }

        private void OnUserManualActivated(object sender, EventArgs e)
        {
            lock(this.Control.Transaction)
            {
                this.ShowPDF("manual.pdf");
            }
        }

        private void OnOnlineHelpActivated(object sender, EventArgs e)
        {
            lock (this.Control.Transaction)
            {
                try
                {
                    Process.OpenUri(Uris.OnlineHelp(ReferrerId.HelpMenu, false));
                }
                catch (Exception ex)
                {
                    string message = String.Format(Catalog.GetString("Cannot show online quick references:{0}"),
                                                   ex.Message);

                    Gtk.MessageDialog mdlg = new MessageDialog(this.main_window,
                                                               DialogFlags.Modal,
                                                               Gtk.MessageType.Error,
                                                               ButtonsType.Ok,
                                                               message)
                    {
                        IconList = ApoIdentStock.FinderIcons
                    };
                    mdlg.Run();
                    mdlg.Destroy();
                }
            }
        }

        private void OnExportServiceDataActivated(object sender, EventArgs e)
        {
            DataBackupDialog.Start(this);
        }
      
        private void OnShowOrderFormActivated(object sender, EventArgs e)
        {
            lock (this.Control.Transaction)
            {
                this.ShowPDF("orderform.pdf");
            }
        }



        private void OnPdfDataChanged(object sender, EventArgs e)
        {
            this.Update(ApoIdentApplicationState.SubstanceManagementPdfLoading);
        }
      
        private void OnShowSubstanceOverviewActivated(object sender, EventArgs e)
        {
            lock (this.Control.Transaction)
            {
                try
                {
                    SubstancePdfs.PdfListType list_type = (SubstancePdfs.PdfListType)((MenuItem)sender).Data["overview"];
                    this.SubstanceManagement.Pdfs.ShowSubstanceOverviewPdf(list_type);
                }
                catch (Exception ex)
                {

                    string message = String.Format("Cannot open Pdf: {0}", ex.Message);
                    this.Update(ApoIdentApplicationState.ShowErrorDialogWithoutSerivceDialog, message, ex);
                }
            }
        }

        private void OnShowAdditionalTests(object sender, EventArgs e)
        {
            lock (this.Control.Transaction)
            {
                try
                {
                    this.SubstanceManagement.Pdfs.ShowAdditionalTestPdf();
                }
                catch (Exception ex)
                {
                    string message = String.Format("Cannot open Pdf: {0}", ex.Message);
                    this.Update(ApoIdentApplicationState.ShowErrorDialogWithoutSerivceDialog, message, ex);
                }
            }
        }

        [GLib.ConnectBefore]
        private void OnShowValidationDocumentActivated(object sender, EventArgs e)
        {
            lock (this.Control.Transaction)
            {
                if (this.IsShowingValidationRunning == true)
                    return;

                try
                {
                    this.IsShowingValidationRunning = true;

                    string class_name = (string)((MenuItem)sender).Data["class_name"];
                    this.SubstanceManagement.Pdfs.ShowValidationPdf(class_name);
                }
                catch (Exception ex)
                {
                    string message = String.Format("Cannot open Pdf: {0}", ex.Message);
                    this.Update(ApoIdentApplicationState.ShowErrorDialogWithoutSerivceDialog, message, ex);

                }
                finally
                {
                    this.IsShowingValidationRunning = false;
                }
            }
        }

        private void OnUpgradeSoftwareActivated(object sender, EventArgs e)
        {
            switch (ApoIdentExecutionModeRecognizer.GetMode())
            {
                case ApoIdentExecutionMode.Windows:
                    this.StartManuellSoftwarLauncherWindows();
                    break;

                case ApoIdentExecutionMode.RaspianStandAlone:
                    this.StartManuellSoftwareLauncherRaspian();
                    break;

                default:
                    break;
            }
        }

        private void OnShowColorDialogActivated(object sender, EventArgs e)
        {
            DesignerDialog.Start(this);
        }

        private Menu HelpMenu { get; set; }
        private bool IsShowingValidationRunning { get; set; } = false;
    }
}

