/* 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 <gegl.h>
#include <gtk/gtk.h>

#include "libpikawidgets/pikawidgets.h"

#include "actions-types.h"

#include "core/pika.h"
#include "core/pika-edit.h"
#include "core/pikacontainer.h"
#include "core/pikacontext.h"
#include "core/pikaimage.h"

#include "widgets/pikacontainereditor.h"
#include "widgets/pikacontainerview.h"
#include "widgets/pikacontainerview-utils.h"

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

#include "buffers-commands.h"

#include "pika-intl.h"


/*  public functions */

void
buffers_paste_cmd_callback (PikaAction *action,
                            GVariant   *value,
                            gpointer    data)
{
  PikaContainerEditor *editor = PIKA_CONTAINER_EDITOR (data);
  PikaContainer       *container;
  PikaContext         *context;
  PikaBuffer          *buffer;
  PikaPasteType        paste_type = (PikaPasteType) g_variant_get_int32 (value);

  container = pika_container_view_get_container (editor->view);
  context   = pika_container_view_get_context (editor->view);

  buffer = pika_context_get_buffer (context);

  if (buffer && pika_container_have (container, PIKA_OBJECT (buffer)))
    {
      PikaDisplay *display = pika_context_get_display (context);
      PikaImage   *image   = NULL;
      gint         x       = -1;
      gint         y       = -1;
      gint         width   = -1;
      gint         height  = -1;

      if (display)
        {
          PikaDisplayShell *shell = pika_display_get_shell (display);

          pika_display_shell_untransform_viewport (
            shell,
            ! pika_display_shell_get_infinite_canvas (shell),
            &x, &y, &width, &height);

          image = pika_display_get_image (display);
        }
      else
        {
          image = pika_context_get_image (context);
        }

      if (image)
        {
          GList *drawables = pika_image_get_selected_drawables (image);

          g_list_free (pika_edit_paste (image, drawables,
                                        PIKA_OBJECT (buffer), paste_type,
                                        context, FALSE,
                                        x, y, width, height));

          pika_image_flush (image);
          g_list_free (drawables);
        }
    }
}

void
buffers_paste_as_new_image_cmd_callback (PikaAction *action,
                                         GVariant   *value,
                                         gpointer    data)
{
  PikaContainerEditor *editor = PIKA_CONTAINER_EDITOR (data);
  PikaContainer       *container;
  PikaContext         *context;
  PikaBuffer          *buffer;

  container = pika_container_view_get_container (editor->view);
  context   = pika_container_view_get_context (editor->view);

  buffer = pika_context_get_buffer (context);

  if (buffer && pika_container_have (container, PIKA_OBJECT (buffer)))
    {
      GtkWidget *widget = GTK_WIDGET (editor);
      PikaImage *new_image;

      new_image = pika_edit_paste_as_new_image (context->pika,
                                                PIKA_OBJECT (buffer),
                                                context);
      pika_create_display (context->pika, new_image,
                           PIKA_UNIT_PIXEL, 1.0,
                           G_OBJECT (pika_widget_get_monitor (widget)));
      g_object_unref (new_image);
    }
}

void
buffers_delete_cmd_callback (PikaAction *action,
                             GVariant   *value,
                             gpointer    data)
{
  PikaContainerEditor *editor = PIKA_CONTAINER_EDITOR (data);

  pika_container_view_remove_active (editor->view);
}
