// 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 System.Collections.Generic;

using Glade;

using Gtk;
using Hiperscan.AnalysisModule;
using Hiperscan.ApoIdentCore.GuiControl;
using Hiperscan.Common.I18n;
using Hiperscan.Extensions.ApoIdent.Widgets;
using static Hiperscan.Common.I18n.CatalogLocale;

namespace Hiperscan.Extensions.ApoIdent
{
    public partial class ApoIdentApplication
    {
        [Widget] Gtk.Toolbar menu_toolbar = null;

        [Obsolete("Do not use this field")]
        [Widget] Gtk.ToolButton menu_quickstep_toolbutton = null;
        [Obsolete("Do not use this field")]
        [Widget] Gtk.ToolButton menu_exit_toolbutton = null;
        [Obsolete("Do not use this field")]
        [Widget] Gtk.ToolButton menu_archive_toolbutton = null;
        [Obsolete("Do not use this field")]
        [Widget] Gtk.ToolButton menu_query_toolbutton = null;
        [Obsolete("Do not use this field")]
        [Widget] Gtk.ToolButton menu_reset_toolbutton = null;
        [Obsolete("Do not use this field")]
        [Widget] Gtk.ToolButton menu_settings_toolbutton = null;
        [Obsolete("Do not use this field")]
        [Widget] Gtk.ToolButton menu_connect_toolbutton = null;
        [Obsolete("Do not use this field")]
        [Widget] Gtk.ToolButton menu_substance_toolbutton = null;
        [Obsolete("Do not use this field")]
        [Widget] Gtk.ToolButton menu_help_toolbutton = null;
        [Obsolete("Do not use this field")]
        [Widget] Gtk.ToolButton menu_cannabis_toolbutton = null;
        [Obsolete("Do not use this field")]
        [Widget] Gtk.ToolButton menu_info_toolbutton = null;
        [Obsolete("Do not use this field")]
        [Widget] Gtk.ToolButton menu_validation_toolbutton = null;

        [Widget] Gtk.ToolItem menu_archive_spacer = null;
        [Widget] Gtk.ToolItem menu_substance_spacer = null;
        [Widget] Gtk.ToolItem menu_help_spacer = null;
        [Widget] Gtk.ToolItem menu_cannabis_spacer = null;

        private void UpdateMenueSection(UpdateArgs update_arg)
        {
            if (update_arg.Menu.HasFlag(UpdateCategory.SetValues))
                this.SetMenuSensitives();

            if (update_arg.Menu.HasFlag(UpdateCategory.ReloadValues))
            {
                this.SetMenuVisibilities();
                this.SetMenuSpaces();
            }
        }

        private void SetMenuSensitives()
        {
            bool sensitive = this.Control.GlobalStates.QuitRequested == false &&
                                 this.Control.MeasuringManager.IsReferencing == false &&
                                this.Control.GlobalStates.Busy == false;
            

            this.MenuQuickStepToolButton.Sensitive = sensitive;
            this.MenuExitToolButton.Sensitive = sensitive;
            this.MenuArchiveToolButton.Sensitive = sensitive;
            this.MenuQueryToolButton.Sensitive = sensitive;
            this.MenuHelpTolButton.Sensitive = sensitive;
            this.MenuCannabisTolButton.Sensitive = sensitive;
            this.MenuInfoToolButton.Sensitive = sensitive;
            this.MenuSettingsToolButton.Sensitive = sensitive;

            this.MenuConnectToolButton.Sensitive = sensitive && 
                this.Control.GlobalStates.ConnectionSuccess == false;

            this.MenuSubstanceToolButton.Sensitive = sensitive &&
                (this.Control.ProfileManager?.CurrentProfile?.HasSubstances ?? false);

            this.MenuResetToolButton.Sensitive = sensitive &&
                this.Control.GlobalStates.ConnectionSuccess == true &&
                this.Control.MeasuringManager.Finder.IsAnyFinderExternalReferenced == true;

            this.MenuValidationToolButton.Sensitive = sensitive &&
                (this.Control.IdentModuleSettings.TestVersion == false ||
                 this.Control.QuantifierModuleSettings.TestVersion == false);
        }

        private void SetMenuVisibilities()
        {
            bool is_only_qm_mode = this.Control.GlobalStates.IsOnlyQuantifierModuleAvailable;
            bool is_qm_installed = this.Control.GlobalStates.IsQuantifierModuleAvailable;

            this.menu_archive_spacer.Visible = this.Control.FileSystem.WindowsProfileChanged == false;
            this.MenuArchiveToolButton.Visible = this.Control.FileSystem.WindowsProfileChanged == false;

            this.menu_substance_spacer.Visible = is_only_qm_mode == false;  
            this.MenuSubstanceToolButton.Visible = is_only_qm_mode == false;

            this.menu_help_spacer.Visible = is_only_qm_mode == false;
            this.MenuHelpTolButton.Visible = is_only_qm_mode == false;

            this.menu_cannabis_spacer.Visible = is_qm_installed;
            this.MenuCannabisTolButton.Visible = is_qm_installed;
        }

        public void SetMenuSpaces()
        {
            double space = 8;
            if (Environment.OSVersion.Platform != PlatformID.Unix)
                space = 17;

            if (this.Control.GlobalStates.IsOnlyQuantifierModuleAvailable)
                space *= 2;
            else if (this.Control.GlobalStates.IsOnlyIdentModuleAvailable)
                space *= 1.2;

            foreach (Gtk.Widget widget in this.menu_toolbar.Children)
            {
                if (widget is Gtk.ToolButton)
                    continue;

                widget.WidthRequest = (int)space;
            }
        }

        private ApoIdentToolButton MenuQuickStepToolButton { get; set; }
        private ApoIdentToolButton MenuExitToolButton { get; set; }
        private ApoIdentToolButton MenuArchiveToolButton { get; set; }
        private ApoIdentToolButton MenuQueryToolButton { get; set; }
        private ApoIdentToolButton MenuResetToolButton { get; set; }
        private ApoIdentToolButton MenuSettingsToolButton { get; set; }
        private ApoIdentToolButton MenuConnectToolButton { get; set; }
        private ApoIdentToolButton MenuSubstanceToolButton { get; set; }
        private ApoIdentToolButton MenuHelpTolButton { get; set; }
        private ApoIdentToolButton MenuCannabisTolButton { get; set; }
        private ApoIdentToolButton MenuInfoToolButton { get; set; }
        private ApoIdentToolButton MenuValidationToolButton { get; set; }
    }
}
