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

#include "vectors-types.h"

#include "pikavectors.h"
#include "pikavectorsmodundo.h"


static void     pika_vectors_mod_undo_constructed (GObject             *object);

static gint64   pika_vectors_mod_undo_get_memsize (PikaObject          *object,
                                                   gint64              *gui_size);

static void     pika_vectors_mod_undo_pop         (PikaUndo            *undo,
                                                   PikaUndoMode         undo_mode,
                                                   PikaUndoAccumulator *accum);
static void     pika_vectors_mod_undo_free        (PikaUndo            *undo,
                                                   PikaUndoMode         undo_mode);


G_DEFINE_TYPE (PikaVectorsModUndo, pika_vectors_mod_undo, PIKA_TYPE_ITEM_UNDO)

#define parent_class pika_vectors_mod_undo_parent_class


static void
pika_vectors_mod_undo_class_init (PikaVectorsModUndoClass *klass)
{
  GObjectClass    *object_class      = G_OBJECT_CLASS (klass);
  PikaObjectClass *pika_object_class = PIKA_OBJECT_CLASS (klass);
  PikaUndoClass   *undo_class        = PIKA_UNDO_CLASS (klass);

  object_class->constructed      = pika_vectors_mod_undo_constructed;

  pika_object_class->get_memsize = pika_vectors_mod_undo_get_memsize;

  undo_class->pop                = pika_vectors_mod_undo_pop;
  undo_class->free               = pika_vectors_mod_undo_free;
}

static void
pika_vectors_mod_undo_init (PikaVectorsModUndo *undo)
{
}

static void
pika_vectors_mod_undo_constructed (GObject *object)
{
  PikaVectorsModUndo *vectors_mod_undo = PIKA_VECTORS_MOD_UNDO (object);
  PikaVectors        *vectors;

  G_OBJECT_CLASS (parent_class)->constructed (object);

  pika_assert (PIKA_IS_VECTORS (PIKA_ITEM_UNDO (object)->item));

  vectors = PIKA_VECTORS (PIKA_ITEM_UNDO (object)->item);

  vectors_mod_undo->vectors =
    PIKA_VECTORS (pika_item_duplicate (PIKA_ITEM (vectors),
                                       G_TYPE_FROM_INSTANCE (vectors)));
}

static gint64
pika_vectors_mod_undo_get_memsize (PikaObject *object,
                                   gint64     *gui_size)
{
  PikaVectorsModUndo *vectors_mod_undo = PIKA_VECTORS_MOD_UNDO (object);
  gint64              memsize          = 0;

  memsize += pika_object_get_memsize (PIKA_OBJECT (vectors_mod_undo->vectors),
                                      gui_size);

  return memsize + PIKA_OBJECT_CLASS (parent_class)->get_memsize (object,
                                                                  gui_size);
}

static void
pika_vectors_mod_undo_pop (PikaUndo            *undo,
                           PikaUndoMode         undo_mode,
                           PikaUndoAccumulator *accum)
{
  PikaVectorsModUndo *vectors_mod_undo = PIKA_VECTORS_MOD_UNDO (undo);
  PikaVectors        *vectors          = PIKA_VECTORS (PIKA_ITEM_UNDO (undo)->item);
  PikaVectors        *temp;
  gint                offset_x;
  gint                offset_y;

  PIKA_UNDO_CLASS (parent_class)->pop (undo, undo_mode, accum);

  temp = vectors_mod_undo->vectors;

  vectors_mod_undo->vectors =
    PIKA_VECTORS (pika_item_duplicate (PIKA_ITEM (vectors),
                                       G_TYPE_FROM_INSTANCE (vectors)));

  pika_vectors_freeze (vectors);

  pika_vectors_copy_strokes (temp, vectors);

  pika_item_get_offset (PIKA_ITEM (temp), &offset_x, &offset_y);
  pika_item_set_offset (PIKA_ITEM (vectors), offset_x, offset_y);

  pika_item_set_size (PIKA_ITEM (vectors),
                      pika_item_get_width  (PIKA_ITEM (temp)),
                      pika_item_get_height (PIKA_ITEM (temp)));

  g_object_unref (temp);

  pika_vectors_thaw (vectors);
}

static void
pika_vectors_mod_undo_free (PikaUndo     *undo,
                            PikaUndoMode  undo_mode)
{
  PikaVectorsModUndo *vectors_mod_undo = PIKA_VECTORS_MOD_UNDO (undo);

  g_clear_object (&vectors_mod_undo->vectors);

  PIKA_UNDO_CLASS (parent_class)->free (undo, undo_mode);
}
