/* PIKA - Photo and Image Kooker Application
 * a rebranding of The GNU Image Manipulation Program (created with heckimp)
 * A derived work which may be trivial. However, any changes may be (C)2023 by Aldercone Studio
 *
 * Original copyright, applying to most contents (license remains unchanged): 
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * 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 <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <string.h>
#include <gegl.h>
#include <gtk/gtk.h>

#include "libpikathumb/pikathumb.h"

#include "menus-types.h"

#include "config/pikaguiconfig.h"

#include "core/pika.h"
#include "core/pikaimage.h"
#include "core/pikalist.h"
#include "core/pikaviewable.h"

#include "widgets/pikaaction.h"
#include "widgets/pikaactionimpl.h"
#include "widgets/pikadialogfactory.h"
#include "widgets/pikadock.h"
#include "widgets/pikadockwindow.h"
#include "widgets/pikasessioninfo.h"
#include "widgets/pikauimanager.h"

#include "display/pikadisplay.h"

#include "dialogs/dialogs.h"

#include "actions/windows-actions.h"

#include "windows-menu.h"


static void      windows_menu_display_add                (PikaContainer     *container,
                                                          PikaDisplay       *display,
                                                          PikaUIManager     *manager);
static void      windows_menu_display_remove             (PikaContainer     *container,
                                                          PikaDisplay       *display,
                                                          PikaUIManager     *manager);
static void      windows_menu_display_reorder            (PikaContainer     *container,
                                                          PikaDisplay       *display,
                                                          gint               new_index,
                                                          PikaUIManager     *manager);
static void      windows_menu_image_notify               (PikaDisplay       *display,
                                                          const GParamSpec  *unused,
                                                          PikaUIManager     *manager);
static void      windows_menu_dock_window_added          (PikaDialogFactory *factory,
                                                          PikaDockWindow    *dock_window,
                                                          PikaUIManager     *manager);
static void      windows_menu_dock_window_removed        (PikaDialogFactory *factory,
                                                          PikaDockWindow    *dock_window,
                                                          PikaUIManager     *manager);
static void      windows_menu_recent_add                 (PikaContainer     *container,
                                                          PikaSessionInfo   *info,
                                                          PikaUIManager     *manager);
static void      windows_menu_recent_remove              (PikaContainer     *container,
                                                          PikaSessionInfo   *info,
                                                          PikaUIManager     *manager);


void
windows_menu_setup (PikaUIManager *manager,
                    const gchar   *ui_path)
{
  GList *list;

  g_return_if_fail (PIKA_IS_UI_MANAGER (manager));
  g_return_if_fail (ui_path != NULL);

  g_object_set_data (G_OBJECT (manager), "image-menu-ui-path",
                     (gpointer) ui_path);

  g_signal_connect_object (manager->pika->displays, "add",
                           G_CALLBACK (windows_menu_display_add),
                           manager, 0);
  g_signal_connect_object (manager->pika->displays, "remove",
                           G_CALLBACK (windows_menu_display_remove),
                           manager, 0);
  g_signal_connect_object (manager->pika->displays, "reorder",
                           G_CALLBACK (windows_menu_display_reorder),
                           manager, 0);

  for (list = pika_get_display_iter (manager->pika);
       list;
       list = g_list_next (list))
    {
      PikaDisplay *display = list->data;

      windows_menu_display_add (manager->pika->displays, display, manager);
    }

  g_signal_connect_object (pika_dialog_factory_get_singleton (), "dock-window-added",
                           G_CALLBACK (windows_menu_dock_window_added),
                           manager, 0);
  g_signal_connect_object (pika_dialog_factory_get_singleton (), "dock-window-removed",
                           G_CALLBACK (windows_menu_dock_window_removed),
                           manager, 0);

  for (list = pika_dialog_factory_get_open_dialogs (pika_dialog_factory_get_singleton ());
       list;
       list = g_list_next (list))
    {
      PikaDockWindow *dock_window = list->data;

      if (PIKA_IS_DOCK_WINDOW (dock_window))
        windows_menu_dock_window_added (pika_dialog_factory_get_singleton (),
                                        dock_window,
                                        manager);
    }

  g_signal_connect_object (global_recent_docks, "add",
                           G_CALLBACK (windows_menu_recent_add),
                           manager, 0);
  g_signal_connect_object (global_recent_docks, "remove",
                           G_CALLBACK (windows_menu_recent_remove),
                           manager, 0);

  for (list = g_list_last (PIKA_LIST (global_recent_docks)->queue->head);
       list;
       list = g_list_previous (list))
    {
      PikaSessionInfo *info = list->data;

      windows_menu_recent_add (global_recent_docks, info, manager);
    }
}


/*  private functions  */

static void
windows_menu_display_add (PikaContainer *container,
                          PikaDisplay   *display,
                          PikaUIManager *manager)
{
  g_signal_connect_object (display, "notify::image",
                           G_CALLBACK (windows_menu_image_notify),
                           manager, 0);

  if (pika_display_get_image (display))
    windows_menu_image_notify (display, NULL, manager);
}

static void
windows_menu_display_remove (PikaContainer *container,
                             PikaDisplay   *display,
                             PikaUIManager *manager)
{
  gchar *action_name;

  action_name = pika_display_get_action_name (display);
  pika_ui_manager_remove_ui (manager, action_name);

  g_free (action_name);
}

static void
windows_menu_display_reorder (PikaContainer *container,
                              PikaDisplay   *display,
                              gint           new_index,
                              PikaUIManager *manager)
{
  gint n_display = pika_container_get_n_children (container);
  gint i;

  for (i = new_index; i < n_display; i++)
    {
      PikaObject *d = pika_container_get_child_by_index (container, i);

      windows_menu_display_remove (container, PIKA_DISPLAY (d), manager);
    }

  for (i = new_index; i < n_display; i++)
    {
      PikaObject *d = pika_container_get_child_by_index (container, i);

      windows_menu_display_add (container, PIKA_DISPLAY (d), manager);
    }
}

static void
windows_menu_image_notify (PikaDisplay      *display,
                           const GParamSpec *unused,
                           PikaUIManager    *manager)
{
  windows_menu_display_remove (manager->pika->displays, display, manager);

  if (pika_display_get_image (display))
    {
      gchar *action_name;

      action_name = pika_display_get_action_name (display);

      pika_ui_manager_add_ui (manager, "/Windows/[Images]", action_name, FALSE);

      g_free (action_name);
    }
}

static void
windows_menu_dock_window_added (PikaDialogFactory *factory,
                                PikaDockWindow    *dock_window,
                                PikaUIManager     *manager)
{
  gchar *action_name;

  action_name = windows_actions_dock_window_to_action_name (dock_window);

  /* TODO GMenu: doesn't look like it's working, neither will old or new API. */
  pika_ui_manager_add_ui (manager, "/Windows/[Docks]", action_name, FALSE);

  g_free (action_name);
}

static void
windows_menu_dock_window_removed (PikaDialogFactory *factory,
                                  PikaDockWindow    *dock_window,
                                  PikaUIManager     *manager)
{
  gchar *action_name;

  action_name = windows_actions_dock_window_to_action_name (dock_window);
  pika_ui_manager_remove_ui (manager, action_name);

  g_free (action_name);
}

static void
windows_menu_recent_add (PikaContainer   *container,
                         PikaSessionInfo *info,
                         PikaUIManager   *manager)
{
  gchar *action_name;
  gint   info_id;

  info_id = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (info), "recent-action-id"));

  action_name = g_strdup_printf ("windows-recent-%04d", info_id);

  pika_ui_manager_add_ui (manager, "/Windows/Recently Closed Docks", action_name, TRUE);

  g_free (action_name);
}

static void
windows_menu_recent_remove (PikaContainer   *container,
                            PikaSessionInfo *info,
                            PikaUIManager   *manager)
{
  pika_ui_manager_remove_uis (manager, "windows-recent-");
}
