//
//    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.GtkExtensions;
using Hiperscan.Unix;
using System.Collections.Generic;
using Hiperscan.AnalysisModule;
using System.Linq;

namespace Hiperscan.Extensions.ApoIdent
{
    public partial class ApoIdentApplication
    {
        private void ShowValidationMenu(Widget parent)
        {
            if (this.MainWindow.GdkWindow == null)
                return;

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

            Dictionary<string, AnalysisType> data = new Dictionary<string, AnalysisType>();

            if (this.Control.GlobalStates.IsIdentModuleAvailable)
            {
                foreach (string model_name in this.Control.IdentModuleSettings.ValidatedModels)
                    data.Add(model_name, AnalysisType.Identification);
            }

            if (this.Control.GlobalStates.IsQuantifierModuleAvailable)
            {
                foreach (string model_name in this.Control.QuantifierModuleSettings.ValidatedModels)
                    data.Add(model_name, AnalysisType.Quantification);
            }

            data = data.OrderBy(m => m.Key).ToDictionary(m => m.Key, m => m.Value);

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

            if (data.Keys.Count > 0)
            {
                foreach (string model_name in data.Keys)
                {
                    ImageMenuItem item = new ImageMenuItem(model_name);
                    item.Data["class_name"] = model_name;
                    item.Data["analysis_type"] = data[model_name];
                    item.Image = new Gtk.Image(Stock.File, size);
                    item.Activated += this.OnShowValidationDocumentActivated;
                    item.ButtonReleaseEvent += this.OnShowValidationDocumentActivated;
                    item.Sensitive = is_sensitive;
                    item.ShowAll();
                    validation_menu.Append(item);
                }
            }
            else
            {
                ImageMenuItem item = new ImageMenuItem(Catalog.GetString("Not available"));
                item.Image = new Gtk.Image(Stock.No, size);
                item.Sensitive = false;
                item.ShowAll();
                validation_menu.Append(item);
            }

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

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

        private Menu ValidationMenu { get; set; }
    }
}
