/* Pika - Photo and Image Kooker Application
 * 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 "libpikabase/pikabase.h"

#include "core-types.h"

#include "pika-memsize.h"
#include "pikaitem.h"
#include "pikaitemtree.h"
#include "pikaitempropundo.h"
#include "pikaparasitelist.h"


enum
{
  PROP_0,
  PROP_PARASITE_NAME
};


static void     pika_item_prop_undo_constructed  (GObject             *object);
static void     pika_item_prop_undo_set_property (GObject             *object,
                                                  guint                property_id,
                                                  const GValue        *value,
                                                  GParamSpec          *pspec);
static void     pika_item_prop_undo_get_property (GObject             *object,
                                                  guint                property_id,
                                                  GValue              *value,
                                                  GParamSpec          *pspec);

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

static void     pika_item_prop_undo_pop          (PikaUndo            *undo,
                                                  PikaUndoMode         undo_mode,
                                                  PikaUndoAccumulator *accum);
static void     pika_item_prop_undo_free         (PikaUndo            *undo,
                                                  PikaUndoMode         undo_mode);


G_DEFINE_TYPE (PikaItemPropUndo, pika_item_prop_undo, PIKA_TYPE_ITEM_UNDO)

#define parent_class pika_item_prop_undo_parent_class


static void
pika_item_prop_undo_class_init (PikaItemPropUndoClass *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_item_prop_undo_constructed;
  object_class->set_property     = pika_item_prop_undo_set_property;
  object_class->get_property     = pika_item_prop_undo_get_property;

  pika_object_class->get_memsize = pika_item_prop_undo_get_memsize;

  undo_class->pop                = pika_item_prop_undo_pop;
  undo_class->free               = pika_item_prop_undo_free;

  g_object_class_install_property (object_class, PROP_PARASITE_NAME,
                                   g_param_spec_string ("parasite-name",
                                                        NULL, NULL,
                                                        NULL,
                                                        PIKA_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY));
}

static void
pika_item_prop_undo_init (PikaItemPropUndo *undo)
{
}

static void
pika_item_prop_undo_constructed (GObject *object)
{
  PikaItemPropUndo *item_prop_undo = PIKA_ITEM_PROP_UNDO (object);
  PikaItem         *item;

  G_OBJECT_CLASS (parent_class)->constructed (object);

  item = PIKA_ITEM_UNDO (object)->item;

  switch (PIKA_UNDO (object)->undo_type)
    {
    case PIKA_UNDO_ITEM_REORDER:
      item_prop_undo->parent   = pika_item_get_parent (item);
      item_prop_undo->position = pika_item_get_index (item);
      break;

    case PIKA_UNDO_ITEM_RENAME:
      item_prop_undo->name = g_strdup (pika_object_get_name (item));
      break;

    case PIKA_UNDO_ITEM_DISPLACE:
      pika_item_get_offset (item,
                            &item_prop_undo->offset_x,
                            &item_prop_undo->offset_y);
      break;

    case PIKA_UNDO_ITEM_VISIBILITY:
      item_prop_undo->visible = pika_item_get_visible (item);
      break;

    case PIKA_UNDO_ITEM_COLOR_TAG:
      item_prop_undo->color_tag = pika_item_get_color_tag (item);
      break;

    case PIKA_UNDO_ITEM_LOCK_CONTENT:
      item_prop_undo->lock_content = pika_item_get_lock_content (item);
      break;

    case PIKA_UNDO_ITEM_LOCK_POSITION:
      item_prop_undo->lock_position = pika_item_get_lock_position (item);
      break;

    case PIKA_UNDO_ITEM_LOCK_VISIBILITY:
      item_prop_undo->lock_visibility = pika_item_get_lock_visibility (item);
      break;

    case PIKA_UNDO_PARASITE_ATTACH:
    case PIKA_UNDO_PARASITE_REMOVE:
      pika_assert (item_prop_undo->parasite_name != NULL);

      item_prop_undo->parasite = pika_parasite_copy
        (pika_item_parasite_find (item, item_prop_undo->parasite_name));
      break;

    default:
      g_return_if_reached ();
    }
}

static void
pika_item_prop_undo_set_property (GObject      *object,
                                  guint         property_id,
                                  const GValue *value,
                                  GParamSpec   *pspec)
{
  PikaItemPropUndo *item_prop_undo = PIKA_ITEM_PROP_UNDO (object);

  switch (property_id)
    {
    case PROP_PARASITE_NAME:
      item_prop_undo->parasite_name = g_value_dup_string (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
pika_item_prop_undo_get_property (GObject    *object,
                                  guint       property_id,
                                  GValue     *value,
                                  GParamSpec *pspec)
{
  PikaItemPropUndo *item_prop_undo = PIKA_ITEM_PROP_UNDO (object);

  switch (property_id)
    {
    case PROP_PARASITE_NAME:
      g_value_set_string (value, item_prop_undo->parasite_name);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static gint64
pika_item_prop_undo_get_memsize (PikaObject *object,
                                 gint64     *gui_size)
{
  PikaItemPropUndo *item_prop_undo = PIKA_ITEM_PROP_UNDO (object);
  gint64            memsize        = 0;

  memsize += pika_string_get_memsize (item_prop_undo->name);
  memsize += pika_string_get_memsize (item_prop_undo->parasite_name);
  memsize += pika_parasite_get_memsize (item_prop_undo->parasite, NULL);

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

static void
pika_item_prop_undo_pop (PikaUndo            *undo,
                         PikaUndoMode         undo_mode,
                         PikaUndoAccumulator *accum)
{
  PikaItemPropUndo *item_prop_undo = PIKA_ITEM_PROP_UNDO (undo);
  PikaItem         *item           = PIKA_ITEM_UNDO (undo)->item;

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

  switch (undo->undo_type)
    {
    case PIKA_UNDO_ITEM_REORDER:
      {
        PikaItem *parent;
        gint      position;

        parent   = pika_item_get_parent (item);
        position = pika_item_get_index (item);

        pika_item_tree_reorder_item (pika_item_get_tree (item), item,
                                     item_prop_undo->parent,
                                     item_prop_undo->position,
                                     FALSE, NULL);

        item_prop_undo->parent   = parent;
        item_prop_undo->position = position;
      }
      break;

    case PIKA_UNDO_ITEM_RENAME:
      {
        gchar *name;

        name = g_strdup (pika_object_get_name (item));

        pika_item_tree_rename_item (pika_item_get_tree (item), item,
                                    item_prop_undo->name,
                                    FALSE, NULL);

        g_free (item_prop_undo->name);
        item_prop_undo->name = name;
      }
      break;

    case PIKA_UNDO_ITEM_DISPLACE:
      {
        gint offset_x;
        gint offset_y;

        pika_item_get_offset (item, &offset_x, &offset_y);

        pika_item_translate (item,
                             item_prop_undo->offset_x - offset_x,
                             item_prop_undo->offset_y - offset_y,
                             FALSE);

        item_prop_undo->offset_x = offset_x;
        item_prop_undo->offset_y = offset_y;
      }
      break;

    case PIKA_UNDO_ITEM_VISIBILITY:
      {
        gboolean visible;

        visible = pika_item_get_visible (item);
        pika_item_set_visible (item, item_prop_undo->visible, FALSE);
        item_prop_undo->visible = visible;
      }
      break;

    case PIKA_UNDO_ITEM_COLOR_TAG:
      {
        PikaColorTag color_tag;

        color_tag = pika_item_get_color_tag (item);
        pika_item_set_color_tag (item, item_prop_undo->color_tag, FALSE);
        item_prop_undo->color_tag = color_tag;
      }
      break;

    case PIKA_UNDO_ITEM_LOCK_CONTENT:
      {
        gboolean lock_content;

        lock_content = pika_item_get_lock_content (item);
        pika_item_set_lock_content (item, item_prop_undo->lock_content, FALSE);
        item_prop_undo->lock_content = lock_content;
      }
      break;

    case PIKA_UNDO_ITEM_LOCK_POSITION:
      {
        gboolean lock_position;

        lock_position = pika_item_get_lock_position (item);
        pika_item_set_lock_position (item, item_prop_undo->lock_position, FALSE);
        item_prop_undo->lock_position = lock_position;
      }
      break;

    case PIKA_UNDO_ITEM_LOCK_VISIBILITY:
      {
        gboolean lock_visibility;

        lock_visibility = pika_item_get_lock_visibility (item);
        pika_item_set_lock_visibility (item, item_prop_undo->lock_visibility, FALSE);
        item_prop_undo->lock_visibility = lock_visibility;
      }
      break;

    case PIKA_UNDO_PARASITE_ATTACH:
    case PIKA_UNDO_PARASITE_REMOVE:
      {
        PikaParasite *parasite;

        parasite = item_prop_undo->parasite;

        item_prop_undo->parasite = pika_parasite_copy
          (pika_item_parasite_find (item, item_prop_undo->parasite_name));

        if (parasite)
          pika_item_parasite_attach (item, parasite, FALSE);
        else
          pika_item_parasite_detach (item, item_prop_undo->parasite_name, FALSE);

        if (parasite)
          pika_parasite_free (parasite);
      }
      break;

    default:
      g_return_if_reached ();
    }
}

static void
pika_item_prop_undo_free (PikaUndo     *undo,
                          PikaUndoMode  undo_mode)
{
  PikaItemPropUndo *item_prop_undo = PIKA_ITEM_PROP_UNDO (undo);

  g_clear_pointer (&item_prop_undo->name,          g_free);
  g_clear_pointer (&item_prop_undo->parasite_name, g_free);
  g_clear_pointer (&item_prop_undo->parasite,      pika_parasite_free);

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