// 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 Hiperscan.Unix;
using Hiperscan.I18n;
using Hiperscan.FileIO;
using Hiperscan.Common;

namespace Hiperscan.Extensions.ApoIdent
{
	public partial class FinderWindow
	{
        private void InitHelpMenue()
        {
            this.HelpMenu = new Menu();

            ImageMenuItem manual_item = new ImageMenuItem(Catalog.GetString("User Manual"));
            manual_item.Image = new Gtk.Image(Stock.File, this.main_toolbar.IconSize);
            manual_item.Activated += this.OnUserManualActivated;
            this.HelpMenu.Append(manual_item);

            ImageMenuItem quick_references_item = new ImageMenuItem(Catalog.GetString("Service-Center Online Help"));
            quick_references_item.Image = new Gtk.Image(Stock.Help, this.main_toolbar.IconSize);
            this.HelpMenu.Append(quick_references_item);
            quick_references_item.Activated += this.OnOnlineHelpActivated;

            this.validation_doc_menuitem = new ImageMenuItem(Catalog.GetString("Validation Documents"));           
            this.validation_doc_menuitem.Sensitive = false;
            this.HelpMenu.Append(this.validation_doc_menuitem);

            ImageMenuItem export_item = new ImageMenuItem(Catalog.GetString("Data Backup"));
            export_item.Image = new Gtk.Image(Stock.SaveAs, this.main_toolbar.IconSize);
            export_item.Activated += this.OnExportServiceDataActivated;
            this.HelpMenu.Append(export_item);

            this.additional_tests_item = new ImageMenuItem(Catalog.GetString("Additional Tests"));
            this.additional_tests_item.NoShowAll = true;
            this.additional_tests_item.Visible = false;
            this.HelpMenu.Append(this.additional_tests_item);
            Menu additional_tests_sub_menu = new Menu();
            this.additional_tests_item.Submenu = additional_tests_sub_menu;

            ImageMenuItem additional_test_solid_item = new ImageMenuItem(Catalog.GetString("Drugs Solid"));
			additional_test_solid_item.Image = new Gtk.Image(Stock.File, this.main_toolbar.IconSize);
            additional_test_solid_item.Data[Hiperscan.Extensions.ApoIdentCommon.DefaultNames.IdentServer.ADDITIONAL_TESTS_DIRECTORY] = SubstancePdfs.AdditionalListType.Solids;
            additional_test_solid_item.Activated += this.OnShowAdditionalTests;
            additional_test_solid_item.ButtonReleaseEvent += this.OnShowAdditionalTests;
            additional_tests_sub_menu.Append(additional_test_solid_item);

            ImageMenuItem additional_test_fluid1_item = new ImageMenuItem(Catalog.GetString("Drugs Semisolid/Fluid 1"));
			additional_test_fluid1_item.Image = new Gtk.Image(Stock.File, this.main_toolbar.IconSize);
            additional_test_fluid1_item.Data[Hiperscan.Extensions.ApoIdentCommon.DefaultNames.IdentServer.ADDITIONAL_TESTS_DIRECTORY] = SubstancePdfs.AdditionalListType.Liquids1;
            additional_test_fluid1_item.Activated += this.OnShowAdditionalTests;
            additional_test_fluid1_item.ButtonReleaseEvent += this.OnShowAdditionalTests;
            additional_tests_sub_menu.Append(additional_test_fluid1_item);

            ImageMenuItem additional_test_fluid2_item = new ImageMenuItem(Catalog.GetString("Drugs Semisolid/Fluid 2"));
			additional_test_fluid2_item.Image = new Gtk.Image(Stock.File, this.main_toolbar.IconSize);
            additional_test_fluid2_item.Data[Hiperscan.Extensions.ApoIdentCommon.DefaultNames.IdentServer.ADDITIONAL_TESTS_DIRECTORY] = SubstancePdfs.AdditionalListType.Liquids2;
            additional_test_fluid2_item.Activated += this.OnShowAdditionalTests;
            additional_test_fluid2_item.ButtonReleaseEvent += this.OnShowAdditionalTests;
            additional_tests_sub_menu.Append(additional_test_fluid2_item);

            this.available_substances_menuitem = new ImageMenuItem(Catalog.GetString("Show Subscribed Substances"));
			this.available_substances_menuitem.Image = new Gtk.Image(Stock.File, this.main_toolbar.IconSize);
            this.available_substances_menuitem.Data["overview"] = SubstancePdfs.PdfListType.Available;
            this.available_substances_menuitem.Activated += this.OnShowSubstanceOverviewActivated;
            this.available_substances_menuitem.Sensitive = false;
            this.HelpMenu.Append(this.available_substances_menuitem);

            this.available_insert_substances_menuitem = new ImageMenuItem(Catalog.GetString("Show Subscribed Substances With Insert"));
			this.available_insert_substances_menuitem.Image = new Gtk.Image(Stock.File, this.main_toolbar.IconSize);
            this.available_insert_substances_menuitem.Data["overview"] = SubstancePdfs.PdfListType.Insert;
            this.available_insert_substances_menuitem.Activated += this.OnShowSubstanceOverviewActivated;
            this.HelpMenu.Append(this.available_insert_substances_menuitem);

            this.changed_substances_menuitem = new ImageMenuItem(Catalog.GetString("Substance and Batch Maintenance"));
			this.changed_substances_menuitem.Image = new Gtk.Image(Stock.File, this.main_toolbar.IconSize);
            this.changed_substances_menuitem.Data["overview"] = SubstancePdfs.PdfListType.Added;
            this.changed_substances_menuitem.Sensitive = false;
            this.changed_substances_menuitem.Activated += this.OnShowSubstanceOverviewActivated;
            this.HelpMenu.Append(this.changed_substances_menuitem);

            ImageMenuItem form_item = new ImageMenuItem(Catalog.GetString("Order Form for Substances"));
            form_item.Image = new Gtk.Image(Stock.Edit, this.main_toolbar.IconSize);
            form_item.Activated += this.OnShowOrderFormActivated;
            form_item.NoShowAll = true;
            form_item.Visible = Locale.Language == "de";
            this.HelpMenu.Append(form_item);

            this.blind_classifier_item = new ImageMenuItem(Catalog.GetString("Blind Classification (Demo Mode)"));
            this.blind_classifier_item.Image = new Gtk.Image(Stock.MediaPlay, this.main_toolbar.IconSize);
            this.blind_classifier_item.Activated += this.OnBlindClassificationActivated;
            this.blind_classifier_item.NoShowAll = true;
            this.blind_classifier_item.Visible = ApoIdentEnvs.BlindClassificationOn;
            this.HelpMenu.Append(this.blind_classifier_item);

            this.HelpMenu.ShowAll();
        }

        private void OnUserManualActivated(object sender, EventArgs e)
        {
            this.ShowPDF("manual.pdf");
        }

        private void OnOnlineHelpActivated(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start(Urls.OnlineHelp(ReferrerId.HelpMenu, false));
            }
            catch(Exception ex)
            {
                string message = String.Format(Catalog.GetString("Cannot show online quick references:{0}"),
                                               ex.Message);

                Gtk.MessageDialog mdlg = new MessageDialog(this.finder_window,
                                                           DialogFlags.Modal,
                                                           Gtk.MessageType.Error,
                                                           ButtonsType.Ok,
                                                           message);
                
                mdlg.IconList = ApoIdentStock.FinderIcons;
                mdlg.Run();
                mdlg.Destroy();
            }
        }

        private void OnExportServiceDataActivated(object sender, EventArgs e)
        {
            new DataBackupDialog(this);
        }
      
        private void OnShowOrderFormActivated(object sender, EventArgs e)
        {
            this.ShowPDF("orderform.pdf");
        }

        private void OnBlindClassificationActivated(object sender, EventArgs e)
        {
            new BlindClassificationDialog(this);
        }

        private void OnPdfDataChanged(object sender, EventArgs e)
        {
            if (this.MainThreadId != System.Threading.Thread.CurrentThread.ManagedThreadId)
            {
                while (this.IsUpdating == true || this.finder_window.HasToplevelFocus == false)
                    System.Threading.Thread.Sleep(100);

                Application.Invoke(sender, e, this.OnPdfDataChanged);
            }
            else
            {
                this.Update(FinderUpdateState.OnPdfDataChanged);
            }
        }
      
        private void OnShowSubstanceOverviewActivated(object sender, EventArgs e)
        {
            SubstancePdfs.PdfListType list_type = (SubstancePdfs.PdfListType) ((MenuItem) sender).Data["overview"];
            string message = this.SubstanceManagement.Pdfs.ShowSubstanceOverviewPdf(list_type);
            if (String.IsNullOrEmpty(message) == false)
            {
                this.host.ShowInfoMessage(this.finder_window,
                                          Catalog.GetString("Cannot open Pdf: {0}."),
                                          message);
            }
        }

        [GLib.ConnectBefore]
        private void OnShowAdditionalTests(object sender, EventArgs e)
        {
            SubstancePdfs.AdditionalListType addition_type = (SubstancePdfs.AdditionalListType) ((MenuItem) sender).
                Data[Hiperscan.Extensions.ApoIdentCommon.DefaultNames.IdentServer.ADDITIONAL_TESTS_DIRECTORY];
            string message = this.SubstanceManagement.Pdfs.ShowAdditionalTestPdf(addition_type);
            if (String.IsNullOrEmpty(message) == false)
            {
                this.host.ShowInfoMessage(this.finder_window,
                                          Catalog.GetString("Cannot open Pdf: {0}"),
                                          message);
            }
        }


		[GLib.ConnectBefore]
		private void OnShowValidationDocumentActivated(object sender, EventArgs e)
		{
            string class_name = (string) ((MenuItem) sender).Data["class_name"];
            string message = this.SubstanceManagement.Pdfs.ShowValidationPdf(class_name);
            if (String.IsNullOrEmpty(message) == false)
            {
                this.host.ShowInfoMessage(this.finder_window,
                                          Catalog.GetString("Cannot open Pdf: {0}"),
                                          message);
            }
		}

		private void OnHelpMenuButtonClicked(object sender, EventArgs e)
        {
            ToolButton help_tool_button = sender as ToolButton;

            int screen_x = this.finder_window.Screen.Width;
            int screen_y = this.finder_window.Screen.Height;

            int x = 0;
            int y = 0;

			help_tool_button.TranslateCoordinates(this.finder_window, 0, 0, out x, out y);

                int tmp_x;
                int tmp_y;

    			this.finder_window.GdkWindow.GetPosition(out tmp_x, out tmp_y);
            x += tmp_x;
			y += tmp_y + help_tool_button.Allocation.Height;
         
            this.HelpMenu.Popup();
            this.HelpMenu.ShowAll();

			Gtk.Window parent_window = (Gtk.Window)this.HelpMenu.Parent;

            if (x + parent_window.Allocation.Width > screen_x)
                x = screen_x - parent_window.Allocation.Width;

            if (y + parent_window.Allocation.Height > screen_y)
                y = y - help_tool_button.Allocation.Height - parent_window.Allocation.Height;

            parent_window.Move(x, y);
        }

		private Menu HelpMenu { get; set; }      
	}
}

