// 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.ApoIdentCommon;
using Hiperscan.Unix;
using Hiperscan.ApoIdentCore.SectionManager.Substance;
using Hiperscan.ApoIdentCore.Misc;
using Hiperscan.GtkExtensions;
using static Hiperscan.Common.I18n.CatalogLocale;
using Hiperscan.AnalysisModule;

namespace Hiperscan.Extensions.ApoIdent
{
    public partial class ApoIdentApplication
    {
        private void ShowHelpMenu(Widget parent)
        {
            ApoIdentExecutionMode mode = ApoIdentExecutionModeRecognizer.GetMode();
           bool can_upgrade = (mode == ApoIdentExecutionMode.Windows ||
                                    mode == ApoIdentExecutionMode.RaspianStandAlone) &&
                                    Plugin.replace_gui;

            bool show_manual = true; //CatalogLocale.Language == "de";
            bool identifier_is_installed = this.Control.GlobalStates.IsIdentModuleAvailable;
            bool is_de_locale = this.Control.Locale == LocaleType.de_DE;
            bool is_es20 = this.Control.MeasuringManager.Finder.IsES20;

            bool is_sensitive = this.Control.SubstanceManager.Pdfs.IsUpdatingSubstancePdfs == false;
            bool is_sensitive_docu = this.Control.SubstanceManager.IsUpdatingDocuments == false;

            Menu help_menu = new Menu();
            IconSize size = IconSize.Button;

            if (show_manual)
            {
                ImageMenuItem manual_item = new ImageMenuItem(Catalog.GetString("Operating Manual"));
                manual_item.Image = new Gtk.Image(Stock.File, size);
                manual_item.Activated += this.OnUserManualActivated;
                manual_item.Visible = show_manual;
                manual_item.ShowAll();
                help_menu.Append(manual_item);
            }

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

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

            if (identifier_is_installed && is_de_locale)
            {
                ImageMenuItem help_additional_tests_item = new ImageMenuItem(Catalog.GetString("Additional Tests"));
                help_additional_tests_item.Image = new Gtk.Image(Stock.File, size);
                help_additional_tests_item.Activated += this.OnShowAdditionalTests;
                help_additional_tests_item.Sensitive = is_sensitive_docu;
                help_additional_tests_item.ShowAll();
                help_menu.Append(help_additional_tests_item);
            }

            if (identifier_is_installed)
            {
                ImageMenuItem help_available_substances_menuitem = new ImageMenuItem(Catalog.GetString("Subscribed Substances"));
                help_available_substances_menuitem.Image = new Gtk.Image(Stock.File, size);
                help_available_substances_menuitem.Data["overview"] = SubstancePdfs.PdfListType.Available;
                help_available_substances_menuitem.Activated += this.OnShowSubstanceOverviewActivated;
                help_available_substances_menuitem.Sensitive = is_sensitive;
                help_available_substances_menuitem.ShowAll();
                help_menu.Append(help_available_substances_menuitem);
            }

            if (identifier_is_installed && is_es20 ==false)
            {
                ImageMenuItem help_available_insert_substances_menuitem = new ImageMenuItem(Catalog.GetString("Subscribed Substances with Sample Insert"));
                help_available_insert_substances_menuitem.Image = new Gtk.Image(Stock.File, size);
                help_available_insert_substances_menuitem.Data["overview"] = SubstancePdfs.PdfListType.Insert;
                help_available_insert_substances_menuitem.Activated += this.OnShowSubstanceOverviewActivated;
                help_available_insert_substances_menuitem.Sensitive = is_sensitive;
                help_available_insert_substances_menuitem.ShowAll();
                help_menu.Append(help_available_insert_substances_menuitem);
            }

            if (identifier_is_installed)
            {
                ImageMenuItem help_substance_update_info_menuitem = new ImageMenuItem(Catalog.GetString("Update Changes"));
                help_substance_update_info_menuitem.Image = new Gtk.Image(Stock.File, size);
                help_substance_update_info_menuitem.Data["overview"] = SubstancePdfs.PdfListType.Added;
                help_substance_update_info_menuitem.Activated += this.OnShowSubstanceOverviewActivated;
                help_substance_update_info_menuitem.Sensitive = is_sensitive;
                help_substance_update_info_menuitem.ShowAll();
                help_menu.Append(help_substance_update_info_menuitem);
            }

            if (identifier_is_installed && is_de_locale)
            {
                ImageMenuItem form_item = new ImageMenuItem(Catalog.GetString("Admission Form for Substances"));
                form_item.Image = new Gtk.Image(Stock.Edit, size);
                form_item.Activated += this.OnShowOrderFormActivated;
                form_item.Sensitive = true;
                form_item.ShowAll();
                help_menu.Append(form_item);
            }

            if (can_upgrade)
            {
                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.ShowAll();
                help_menu.Append(upgrade_item);
            }

            this.HelpMenu = help_menu;
            this.HelpMenu.Destroyed += (sender, e) => this.HelpMenu = null;
            GuiDesigner.SetSchema(help_menu.Parent as Gtk.Window);
            PopupPositioner.Show(help_menu, parent, false);
        }

        private void OnUserManualActivated(object sender, EventArgs e)
        {
            if (this.Control.GlobalStates.IsOnlyQuantifierModuleAvailable)
            {
                this.Control.ShowPdf("cannabis_manual.pdf");
            }
            else if (this.Control.GlobalStates.IsIdentModuleAvailable &&
                     this.Control.GlobalStates.IsQuantifierModuleAvailable) 
            {
                this.Control.ShowPdf("combined_manual.pdf");
            }
            else 
            {
                this.Control.ShowPdf("manual.pdf");
            }
        }

        private void OnOnlineHelpActivated(object sender, EventArgs e)
        {
            this.Control.OpenSupportUrl(UriReferrerId.HelpMenu);
        }

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

        private void OnShowSubstanceOverviewActivated(object sender, EventArgs e)
        {
            SubstancePdfs.PdfListType list_type = (SubstancePdfs.PdfListType)((MenuItem)sender).Data["overview"];
            this.Control.DocumentManager.ShowSubstancePdf(list_type); 
        }

        private void OnShowAdditionalTests(object sender, EventArgs e)
        {
            this.Control.ShowAdditionalTestPdf(AnalysisType.Identification);
        }

        private void OnUpgradeSoftwareActivated(object sender, EventArgs e)
        {
            this.Control.LookForSoftwareUpgrade(Plugin.replace_gui);
        }

        private Menu HelpMenu { get; set; }
    }
}

