/* 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 "core-types.h"

#include "pikaimage.h"
#include "pikalist.h"
#include "pikaundo.h"
#include "pikaundostack.h"


static void    pika_undo_stack_finalize    (GObject             *object);

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

static void    pika_undo_stack_pop         (PikaUndo            *undo,
                                            PikaUndoMode         undo_mode,
                                            PikaUndoAccumulator *accum);
static void    pika_undo_stack_free        (PikaUndo            *undo,
                                            PikaUndoMode         undo_mode);


G_DEFINE_TYPE (PikaUndoStack, pika_undo_stack, PIKA_TYPE_UNDO)

#define parent_class pika_undo_stack_parent_class


static void
pika_undo_stack_class_init (PikaUndoStackClass *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->finalize         = pika_undo_stack_finalize;

  pika_object_class->get_memsize = pika_undo_stack_get_memsize;

  undo_class->pop                = pika_undo_stack_pop;
  undo_class->free               = pika_undo_stack_free;
}

static void
pika_undo_stack_init (PikaUndoStack *stack)
{
  stack->undos = pika_list_new (PIKA_TYPE_UNDO, FALSE);
}

static void
pika_undo_stack_finalize (GObject *object)
{
  PikaUndoStack *stack = PIKA_UNDO_STACK (object);

  g_clear_object (&stack->undos);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static gint64
pika_undo_stack_get_memsize (PikaObject *object,
                             gint64     *gui_size)
{
  PikaUndoStack *stack   = PIKA_UNDO_STACK (object);
  gint64         memsize = 0;

  memsize += pika_object_get_memsize (PIKA_OBJECT (stack->undos), gui_size);

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

static void
pika_undo_stack_pop (PikaUndo            *undo,
                     PikaUndoMode         undo_mode,
                     PikaUndoAccumulator *accum)
{
  PikaUndoStack *stack = PIKA_UNDO_STACK (undo);
  GList         *list;

  for (list = PIKA_LIST (stack->undos)->queue->head;
       list;
       list = g_list_next (list))
    {
      PikaUndo *child = list->data;

      pika_undo_pop (child, undo_mode, accum);
    }
}

static void
pika_undo_stack_free (PikaUndo     *undo,
                      PikaUndoMode  undo_mode)
{
  PikaUndoStack *stack = PIKA_UNDO_STACK (undo);
  GList         *list;

  for (list = PIKA_LIST (stack->undos)->queue->head;
       list;
       list = g_list_next (list))
    {
      PikaUndo *child = list->data;

      pika_undo_free (child, undo_mode);
      g_object_unref (child);
    }

  pika_container_clear (stack->undos);
}

PikaUndoStack *
pika_undo_stack_new (PikaImage *image)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);

  return g_object_new (PIKA_TYPE_UNDO_STACK,
                       "image", image,
                       NULL);
}

void
pika_undo_stack_push_undo (PikaUndoStack *stack,
                           PikaUndo      *undo)
{
  g_return_if_fail (PIKA_IS_UNDO_STACK (stack));
  g_return_if_fail (PIKA_IS_UNDO (undo));

  pika_container_add (stack->undos, PIKA_OBJECT (undo));
}

PikaUndo *
pika_undo_stack_pop_undo (PikaUndoStack       *stack,
                          PikaUndoMode         undo_mode,
                          PikaUndoAccumulator *accum)
{
  PikaUndo *undo;

  g_return_val_if_fail (PIKA_IS_UNDO_STACK (stack), NULL);
  g_return_val_if_fail (accum != NULL, NULL);

  undo = PIKA_UNDO (pika_container_get_first_child (stack->undos));

  if (undo)
    {
      pika_container_remove (stack->undos, PIKA_OBJECT (undo));
      pika_undo_pop (undo, undo_mode, accum);

      return undo;
    }

  return NULL;
}

PikaUndo *
pika_undo_stack_free_bottom (PikaUndoStack *stack,
                             PikaUndoMode   undo_mode)
{
  PikaUndo *undo;

  g_return_val_if_fail (PIKA_IS_UNDO_STACK (stack), NULL);

  undo = PIKA_UNDO (pika_container_get_last_child (stack->undos));

  if (undo)
    {
      pika_container_remove (stack->undos, PIKA_OBJECT (undo));
      pika_undo_free (undo, undo_mode);

      return undo;
    }

  return NULL;
}

PikaUndo *
pika_undo_stack_peek (PikaUndoStack *stack)
{
  g_return_val_if_fail (PIKA_IS_UNDO_STACK (stack), NULL);

  return PIKA_UNDO (pika_container_get_first_child (stack->undos));
}

gint
pika_undo_stack_get_depth (PikaUndoStack *stack)
{
  g_return_val_if_fail (PIKA_IS_UNDO_STACK (stack), 0);

  return pika_container_get_n_children (stack->undos);
}
