// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2018 Thomas Klose & 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 Glade;
using System.Collections.Generic;
using Hiperscan.Spectroscopy;
using System.IO;
using Hiperscan.I18n;

namespace Hiperscan.Extensions.ApoIdent
{
    
   
    public partial class FinderWindow
    {
        [Widget] Gtk.ToolButton quickstep_button            = null;
        [Widget] Gtk.ToolButton reset_reference_button      = null;
        [Widget] Gtk.ToolButton connect_button              = null;
        [Widget] Gtk.ToolButton substance_management_button = null;
        [Widget] Gtk.ToolButton archive_toolbutton          = null;
        [Widget] Gtk.ToolItem archive_spacer                = null;
        [Widget] Gtk.ToolButton preferences_button          = null;

        ImageMenuItem available_substances_menuitem         = null;
        ImageMenuItem available_insert_substances_menuitem  = null;
        ImageMenuItem changed_substances_menuitem           = null;
        ImageMenuItem validation_doc_menuitem               = null;
        ImageMenuItem additional_tests_item                 = null;
        ImageMenuItem blind_classifier_item                = null;

        private void UpdateMenueSection(bool update_help_menu)
        {
            this.UpdateMenuButtons();
            if(update_help_menu == false)
                return;

            this.SetAvailableSubstanceMenueItem();
            this.SetAvailableInsertSubstanceMenuItem();
            this.SetChangedSubstanceMenueItem();
            this.SetBlindClassifierMenueItem();

            this.SetValidationDocuMenueItem();
            this.SetAdditionalTestsDocuMenuItem();
        }

        private void UpdateMenuButtons()
        {
            this.connect_button.Sensitive               = this.InitState != ServerDeviceState.Success;
            this.substance_management_button.Sensitive = this.InitState == ServerDeviceState.Success &&
                this.SubstanceManagement.ModelClassNameDetailResolver.ContainsKey(SubstanceManagement.LocalizedUserdefinedClassifierName);
            
            this.reset_reference_button.Sensitive       = this.InitState == ServerDeviceState.Success   &&
                this.Finder != null && 
                this.Finder.Current != null && 
                this.Finder.Current.ReferencingIsPending == false;
            this.archive_spacer.Visible = this.host.ProfileChangedMode == false;
            this.archive_toolbutton.Visible = this.host.ProfileChangedMode == false;
            this.preferences_button.Sensitive = this.DetailSectionState.IsSet(DetailSectionStates.SaveReport) == false;
        }

        private void SetAvailableSubstanceMenueItem()
        {
            this.available_substances_menuitem.Sensitive = this.SubstanceManagement.Pdfs.IsUpdatingSubstancePdfs == false;
        }

        private void SetAvailableInsertSubstanceMenuItem()
        {
            this.available_insert_substances_menuitem.Sensitive = this.SubstanceManagement.Pdfs.IsUpdatingSubstancePdfs == false;
        }

        private void SetChangedSubstanceMenueItem()
        {
            this.changed_substances_menuitem.Sensitive = this.SubstanceManagement.Pdfs.IsUpdatingSubstancePdfs == false;
        }

        private void SetBlindClassifierMenueItem()
        {
            this.blind_classifier_item.Sensitive = this.InitState == ServerDeviceState.Success;
        }

        private void SetValidationDocuMenueItem()
        {
            if (this.SubstanceManagement.Pdfs.IsUpdatingValdationPdfs == true || this.Config.AcquisitionSettings.TestVersion == true)
            {
                this.validation_doc_menuitem.Sensitive = false;
            }
            else
            {
                List<string> classifier_names = new List<string>(this.SubstanceManagement.Config.AcquisitionSettings.ValidatedClassifiers);

                if (classifier_names.Count == 0)
                {
                    this.validation_doc_menuitem.Sensitive = false;
                }
                else
                { 
                    classifier_names.Sort();
                    Menu validation_menues = new Menu();
                    validation_doc_menuitem.Submenu = validation_menues;

                    foreach (string classifier_name in classifier_names)
                    {
                        ImageMenuItem item = new ImageMenuItem(classifier_name);
                        item.Data["class_name"] = classifier_name;
						item.Image = new Gtk.Image(Stock.File, this.main_toolbar.IconSize);
                        item.Activated += this.OnShowValidationDocumentActivated;
                        item.ButtonReleaseEvent += this.OnShowValidationDocumentActivated;
                        item.ShowAll();
                        validation_menues.Append(item);
                    }

                    this.validation_doc_menuitem.Sensitive = true;
                }
            }
        }

        private void SetAdditionalTestsDocuMenuItem()
        {
            Locale.LocaleType locale = this.Config.AcquisitionSettings.Locale;

            this.additional_tests_item.Visible = locale == Locale.LocaleType.de_DE;
            this.additional_tests_item.NoShowAll = locale != Locale.LocaleType.de_DE;
            this.additional_tests_item.Sensitive = this.SubstanceManagement.Pdfs.IsUpdatingAdditionalPdfs == false;
        }
    }
}
