// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2024, 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.ApoIdentCore.GuiControl;
using Hiperscan.ApoIdentCommon.ApoConfig;
using Hiperscan.GtkExtensions;
using Hiperscan.GuiExtensions.Dialog;
using Hiperscan.Unix;

namespace Hiperscan.Extensions.ApoIdent
{
    public partial class ApoIdentApplication
    {
        private void ShowPreSelectionArchive(Widget parent)
        {
            switch (this.Control.ProfileManager.AllProfiles.Count)
            {

                case 0:
                    this.Host.ShowInfoMessage(this.MainWindow,
                    Catalog.GetString("Cannot open archive: No user profile found."));
                    break;

                case 1:
                    if (FileIO.ExistsDirectory(this.Control.ProfileManager.CurrentProfile.Archive))
                        this.Host.OpenFileBrowser(this.Control.ProfileManager.CurrentProfile.Archive);
                    else
                        this.ShowMissingArchiveDirectoryMessage(this.Control.ProfileManager.CurrentProfile.Archive);
                    break;

                default:
                    this.ShowArchiveMenu(parent);
                    break;

            }
        }

        private void ShowArchiveMenu(Widget parent)
        {
            if (this.MainWindow.GdkWindow == null)
                return;

            Menu archive_meu = new Menu();

            foreach (ProfileSettings profile in this.Control.ProfileManager.AllProfiles.Values)
            {
                MenuItem item = new MenuItem(profile.Name.Replace("_", "__"));
                item.Activated += this.OnArchivePreSelectionActivated;
                item.Data["archive"] = profile.Archive;
                archive_meu.Append(item);
            }

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

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

            if (FileIO.ExistsDirectory(archive))
                this.Host.OpenFileBrowser(archive);
            else
                this.ShowMissingArchiveDirectoryMessage(archive);
        }

        private void ShowMissingArchiveDirectoryMessage(string path)
        {
            UpdateArgs arg = new UpdateArgs();
            arg.DialogArgs.StandardDialogType = StandardDialogType.Error;
            arg.DialogArgs.Message = String.Format(Catalog.GetString("An archive directory with path '{0}' does not exist."), path);
            this.Update(arg);
        }

        private Menu ArchiveMenu { get; set; }

    }
}

