// 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 System.IO;
using Gtk;

using Hiperscan.Unix;


namespace Hiperscan.Extensions.ApoIdent
{
	public partial class FinderWindow
	{
		
		private void OnArchiveButtonClicked(object sender, EventArgs e)
		{
			this.ShowPreSelectionArchive(sender);
		}

		private void ShowPreSelectionArchive(object sender)
		{
			switch (this.Config.Profiles.Count)
			{

			case 0:
				this.host.ShowInfoMessage(this.finder_window,
					Catalog.GetString("There isn't any profile for showing the archive."));
				break;

			case 1:
                    if (Directory.Exists(FilesAndDirs.Archive) == true)
                        this.host.OpenFileBrowser(FilesAndDirs.Archive);	
				else
                        this.ShowMissingArchiveDirectoryMessage(FilesAndDirs.Archive);
				break;

			default:
				this.CreateArchiveContextMenu(sender);	
				break;

			}
		}

		private void CreateArchiveContextMenu(object sender)
		{
			Menu archive_menu = new Menu();

			foreach (Profile profile in this.Config.Profiles.Values)
			{
				MenuItem item = new MenuItem(profile.Name);
				item.Activated += this.OnArchivePreSelectionActivated;
				item.Data["archive"] = profile.Archive;
				archive_menu.Append(item);
			}

			ToolButton archive_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;

			archive_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 + archive_tool_button.Allocation.Height;
         
			archive_menu.ShowAll();
			archive_menu.Popup();

			Gtk.Window parent_window = (Gtk.Window)archive_menu.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 - archive_tool_button.Allocation.Height - parent_window.Allocation.Height;
			
			parent_window.Move(x, y);
		}

		private void OnArchivePreSelectionActivated(object sender, EventArgs e)
		{
			MenuItem menu_item = sender as MenuItem;
			string archive = (string) menu_item.Data["archive"];

			if (Directory.Exists(archive) == true)
				this.host.OpenFileBrowser(archive);	
			else
				this.ShowMissingArchiveDirectoryMessage(archive);
		}

		private void ShowMissingArchiveDirectoryMessage(string path)
		{
			this.host.ShowInfoMessage(this.finder_window,
				Catalog.GetString("An archive directory with path '{0}' does not exist."),
				path);
		}
	}
}

