/* 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
 *
 * pikapatternclipboard.c
 * Copyright (C) 2006 Michael Natterer <mitch@gimp.org>
 *
 * 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 <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>

#include "libpikabase/pikabase.h"

#include "core-types.h"

#include "pika.h"
#include "pikabuffer.h"
#include "pikapatternclipboard.h"
#include "pikaimage.h"
#include "pikapickable.h"
#include "pikatempbuf.h"

#include "pika-intl.h"


#define PATTERN_MAX_SIZE 1024

enum
{
  PROP_0,
  PROP_PIKA
};


/*  local function prototypes  */

static void       pika_pattern_clipboard_constructed  (GObject      *object);
static void       pika_pattern_clipboard_set_property (GObject      *object,
                                                       guint         property_id,
                                                       const GValue *value,
                                                       GParamSpec   *pspec);
static void       pika_pattern_clipboard_get_property (GObject      *object,
                                                       guint         property_id,
                                                       GValue       *value,
                                                       GParamSpec   *pspec);
static PikaData * pika_pattern_clipboard_duplicate    (PikaData     *data);

static void       pika_pattern_clipboard_changed      (Pika         *pika,
                                                       PikaPattern  *pattern);


G_DEFINE_TYPE (PikaPatternClipboard, pika_pattern_clipboard, PIKA_TYPE_PATTERN)

#define parent_class pika_pattern_clipboard_parent_class


static void
pika_pattern_clipboard_class_init (PikaPatternClipboardClass *klass)
{
  GObjectClass  *object_class = G_OBJECT_CLASS (klass);
  PikaDataClass *data_class   = PIKA_DATA_CLASS (klass);

  object_class->constructed  = pika_pattern_clipboard_constructed;
  object_class->set_property = pika_pattern_clipboard_set_property;
  object_class->get_property = pika_pattern_clipboard_get_property;

  data_class->duplicate      = pika_pattern_clipboard_duplicate;

  g_object_class_install_property (object_class, PROP_PIKA,
                                   g_param_spec_object ("pika", NULL, NULL,
                                                        PIKA_TYPE_PIKA,
                                                        PIKA_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY));
}

static void
pika_pattern_clipboard_init (PikaPatternClipboard *pattern)
{
}

static void
pika_pattern_clipboard_constructed (GObject *object)
{
  PikaPatternClipboard *pattern = PIKA_PATTERN_CLIPBOARD (object);

  G_OBJECT_CLASS (parent_class)->constructed (object);

  pika_assert (PIKA_IS_PIKA (pattern->pika));

  g_signal_connect_object (pattern->pika, "clipboard-changed",
                           G_CALLBACK (pika_pattern_clipboard_changed),
                           pattern, 0);

  pika_pattern_clipboard_changed (pattern->pika, PIKA_PATTERN (pattern));
}

static void
pika_pattern_clipboard_set_property (GObject      *object,
                                     guint         property_id,
                                     const GValue *value,
                                     GParamSpec   *pspec)
{
  PikaPatternClipboard *pattern = PIKA_PATTERN_CLIPBOARD (object);

  switch (property_id)
    {
    case PROP_PIKA:
      pattern->pika = g_value_get_object (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
pika_pattern_clipboard_get_property (GObject    *object,
                                     guint       property_id,
                                     GValue     *value,
                                     GParamSpec *pspec)
{
  PikaPatternClipboard *pattern = PIKA_PATTERN_CLIPBOARD (object);

  switch (property_id)
    {
    case PROP_PIKA:
      g_value_set_object (value, pattern->pika);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static PikaData *
pika_pattern_clipboard_duplicate (PikaData *data)
{
  PikaData *new = g_object_new (PIKA_TYPE_PATTERN, NULL);

  pika_data_copy (new, data);

  return new;
}

PikaData *
pika_pattern_clipboard_new (Pika *pika)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

  return g_object_new (PIKA_TYPE_PATTERN_CLIPBOARD,
                       "name", _("Clipboard Image"),
                       "pika", pika,
                       NULL);
}


/*  private functions  */

static void
pika_pattern_clipboard_changed (Pika        *pika,
                                PikaPattern *pattern)
{
  PikaObject *paste;
  GeglBuffer *buffer = NULL;

  g_clear_pointer (&pattern->mask, pika_temp_buf_unref);

  paste = pika_get_clipboard_object (pika);

  if (PIKA_IS_IMAGE (paste))
    {
      pika_pickable_flush (PIKA_PICKABLE (paste));
      buffer = pika_pickable_get_buffer (PIKA_PICKABLE (paste));
    }
  else if (PIKA_IS_BUFFER (paste))
    {
      buffer = pika_buffer_get_buffer (PIKA_BUFFER (paste));
    }

  if (buffer)
    {
      gint width  = MIN (gegl_buffer_get_width  (buffer), PATTERN_MAX_SIZE);
      gint height = MIN (gegl_buffer_get_height (buffer), PATTERN_MAX_SIZE);

      pattern->mask = pika_temp_buf_new (width, height,
                                         gegl_buffer_get_format (buffer));

      gegl_buffer_get (buffer,
                       GEGL_RECTANGLE (0, 0, width, height), 1.0,
                       NULL,
                       pika_temp_buf_get_data (pattern->mask),
                       GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
    }
  else
    {
      pattern->mask = pika_temp_buf_new (16, 16, babl_format ("R'G'B' u8"));
      memset (pika_temp_buf_get_data (pattern->mask), 255, 16 * 16 * 3);
    }

  pika_data_dirty (PIKA_DATA (pattern));
}
