/* 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 "libpikawidgets/pikawidgets.h"

#include "actions-types.h"

#include "config/pikadisplayconfig.h"
#include "config/pikaguiconfig.h"

#include "core/pika.h"
#include "core/pikacontainer.h"

#include "widgets/pikaactiongroup.h"
#include "widgets/pikadialogfactory.h"
#include "widgets/pikasessioninfo.h"
#include "widgets/pikawidgets-utils.h"

#include "display/pikadisplay.h"
#include "display/pikadisplayshell.h"

#include "dialogs/dialogs.h"

#include "actions.h"
#include "dialogs-actions.h"
#include "windows-commands.h"

#include "pika-intl.h"


void
windows_hide_docks_cmd_callback (PikaAction *action,
                                 GVariant   *value,
                                 gpointer    data)
{
  Pika     *pika;
  gboolean  active;
  return_if_no_pika (pika, data);

  active = g_variant_get_boolean (value);

  if (active != PIKA_GUI_CONFIG (pika->config)->hide_docks)
    g_object_set (pika->config,
                  "hide-docks", active,
                  NULL);
}

void
windows_use_single_window_mode_cmd_callback (PikaAction *action,
                                             GVariant   *value,
                                             gpointer    data)
{
  Pika     *pika;
  gboolean  active;
  return_if_no_pika (pika, data);

  active = g_variant_get_boolean (value);

  if (active != PIKA_GUI_CONFIG (pika->config)->single_window_mode)
    g_object_set (pika->config,
                  "single-window-mode", active,
                  NULL);
}

void
windows_show_tabs_cmd_callback (PikaAction *action,
                                GVariant   *value,
                                gpointer    data)
{
  Pika     *pika;
  gboolean  active;
  return_if_no_pika (pika, data);

  active = g_variant_get_boolean (value);

  if (active != PIKA_GUI_CONFIG (pika->config)->show_tabs)
    g_object_set (pika->config,
                  "show-tabs", active,
                  NULL);
}


void
windows_set_tabs_position_cmd_callback (PikaAction *action,
                                        GVariant   *value,
                                        gpointer    data)
{
  Pika         *pika;
  PikaPosition  position;
  return_if_no_pika (pika, data);

  position = (PikaPosition) g_variant_get_int32 (value);

  if (position != PIKA_GUI_CONFIG (pika->config)->tabs_position)
    g_object_set (pika->config,
                  "tabs-position", position,
                  NULL);
}

void
windows_show_display_next_cmd_callback (PikaAction *action,
                                        GVariant   *value,
                                        gpointer    data)
{
  PikaDisplay *display;
  Pika        *pika;
  gint         index;
  return_if_no_display (display, data);
  return_if_no_pika (pika, data);

  index = pika_container_get_child_index (pika->displays,
                                          PIKA_OBJECT (display));
  index++;

  if (index >= pika_container_get_n_children (pika->displays))
    index = 0;

  display = PIKA_DISPLAY (pika_container_get_child_by_index (pika->displays,
                                                             index));
  pika_display_shell_present (pika_display_get_shell (display));
}

void
windows_show_display_previous_cmd_callback (PikaAction *action,
                                            GVariant   *value,
                                            gpointer    data)
{
  PikaDisplay *display;
  Pika        *pika;
  gint         index;
  return_if_no_display (display, data);
  return_if_no_pika (pika, data);

  index = pika_container_get_child_index (pika->displays,
                                          PIKA_OBJECT (display));
  index--;

  if (index < 0)
    index = pika_container_get_n_children (pika->displays) - 1;

  display = PIKA_DISPLAY (pika_container_get_child_by_index (pika->displays,
                                                             index));
  pika_display_shell_present (pika_display_get_shell (display));
}

void
windows_show_display_cmd_callback (PikaAction *action,
                                   GVariant   *value,
                                   gpointer    data)
{
  PikaDisplay *display = g_object_get_data (G_OBJECT (action), "display");

  pika_display_shell_present (pika_display_get_shell (display));
}

void
windows_show_dock_cmd_callback (PikaAction *action,
                                GVariant   *value,
                                gpointer    data)
{
  GtkWindow *dock_window = g_object_get_data (G_OBJECT (action), "dock-window");

  gtk_window_present (dock_window);
}

void
windows_open_recent_cmd_callback (PikaAction *action,
                                  GVariant   *value,
                                  gpointer    data)
{
  PikaSessionInfo        *info;
  PikaDialogFactoryEntry *entry;
  Pika                   *pika;
  GtkWidget              *widget;
  return_if_no_pika (pika, data);
  return_if_no_widget (widget, data);

  info  = g_object_get_data (G_OBJECT (action), "info");
  entry = pika_session_info_get_factory_entry (info);

  if (entry && strcmp ("pika-toolbox-window", entry->identifier) == 0 &&
      dialogs_actions_toolbox_exists (pika))
    {
      pika_message (pika,
                    G_OBJECT (action_data_get_widget (data)),
                    PIKA_MESSAGE_WARNING,
                    _("The chosen recent dock contains a toolbox. Please "
                      "close the currently open toolbox and try again."));
      return;
    }

  g_object_ref (info);

  pika_container_remove (global_recent_docks, PIKA_OBJECT (info));

  pika_dialog_factory_add_session_info (pika_dialog_factory_get_singleton (),
                                        info);

  pika_session_info_restore (info, pika_dialog_factory_get_singleton (),
                             pika_widget_get_monitor (widget));

  g_object_unref (info);
}
