//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2024, 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.Collections.Generic;
using Gdk;
using Gtk;
using Hiperscan.AnalysisModule;
using Hiperscan.ApoIdentCommon;
using Hiperscan.GtkExtensions;
using Hiperscan.Unix;
using static Hiperscan.Common.I18n.CatalogLocale;

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


            bool quantifier_is_installed = this.Control.GlobalStates.IsQuantifierModuleAvailable;
            bool only_quantifier_installed = this.Control.GlobalStates.IsOnlyQuantifierModuleAvailable;

            bool is_sensitive = this.Control.SubstanceManager.IsUpdatingDocuments == false;

            List<string> data = new List<string>();
            if (quantifier_is_installed)
            {
                foreach (string model_name in this.Control.QuantifierModuleSettings.AdditionalTestedModels)
                    data.Add(model_name);
            }

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

            if (only_quantifier_installed)
            {
                ImageMenuItem manual_cannabis_item = new ImageMenuItem(Catalog.GetString("Operating Manual"));
                manual_cannabis_item.Image = new Gtk.Image(Stock.File, size);
                manual_cannabis_item.Activated += this.OnUserManualCannabisActivated;
                manual_cannabis_item.ShowAll();
                cannabis_menu.Append(manual_cannabis_item);
            }

            if (only_quantifier_installed)
            {
                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();
                cannabis_menu.Append(export_item);
            }

            if (quantifier_is_installed)
            {
                foreach (string model_class in data)
                {
                    string title = string.Format(Catalog.GetString("Documentation {0}"), model_class);
                    ImageMenuItem help_cannabis_item = new ImageMenuItem(title);
                    help_cannabis_item.Image = new Gtk.Image(Stock.File, size);
                    help_cannabis_item.Data["class_name"] = model_class;
                    help_cannabis_item.Activated += this.OnShowCannabisHelpDocument;
                    help_cannabis_item.Sensitive = is_sensitive;
                    help_cannabis_item.ShowAll();
                    cannabis_menu.Append(help_cannabis_item);
                }
            }

            if (only_quantifier_installed)
            {
                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();
                    cannabis_menu.Append(upgrade_item);
                }
            }

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

        private void OnUserManualCannabisActivated(object sender, EventArgs e)
        {
            this.Control.ShowPdf("cannabis_manual.pdf");
        }

        private void OnShowCannabisHelpDocument(object sender, EventArgs e)
        {
            string model_class = (string)((MenuItem)sender).Data["class_name"];
            this.Control.ShowAdditionalTestPdf(AnalysisType.Quantification, model_class);
        }

        private Menu CannabisMenu { get; set; }
    }
}
