/* 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 "pikadrawable-floating-selection.h"
#include "pikafloatingselectionundo.h"
#include "pikaimage.h"
#include "pikalayer.h"
#include "pikalayer-floating-selection.h"


static void   pika_floating_selection_undo_constructed (GObject             *object);

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


G_DEFINE_TYPE (PikaFloatingSelectionUndo, pika_floating_selection_undo,
               PIKA_TYPE_ITEM_UNDO)

#define parent_class pika_floating_selection_undo_parent_class


static void
pika_floating_selection_undo_class_init (PikaFloatingSelectionUndoClass *klass)
{
  GObjectClass  *object_class = G_OBJECT_CLASS (klass);
  PikaUndoClass *undo_class   = PIKA_UNDO_CLASS (klass);

  object_class->constructed = pika_floating_selection_undo_constructed;

  undo_class->pop           = pika_floating_selection_undo_pop;
}

static void
pika_floating_selection_undo_init (PikaFloatingSelectionUndo *undo)
{
}

static void
pika_floating_selection_undo_constructed (GObject *object)
{
  PikaFloatingSelectionUndo *floating_sel_undo;
  PikaLayer                 *layer;

  floating_sel_undo = PIKA_FLOATING_SELECTION_UNDO (object);

  G_OBJECT_CLASS (parent_class)->constructed (object);

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

  layer = PIKA_LAYER (PIKA_ITEM_UNDO (object)->item);

  switch (PIKA_UNDO (object)->undo_type)
    {
    case PIKA_UNDO_FS_TO_LAYER:
      floating_sel_undo->drawable = pika_layer_get_floating_sel_drawable (layer);
      break;

    default:
      g_return_if_reached ();
    }
}

static void
pika_floating_selection_undo_pop (PikaUndo            *undo,
                                  PikaUndoMode         undo_mode,
                                  PikaUndoAccumulator *accum)
{
  PikaFloatingSelectionUndo *floating_sel_undo;
  PikaLayer                 *floating_layer;

  floating_sel_undo = PIKA_FLOATING_SELECTION_UNDO (undo);
  floating_layer    = PIKA_LAYER (PIKA_ITEM_UNDO (undo)->item);

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

  switch (undo->undo_type)
    {
    case PIKA_UNDO_FS_TO_LAYER:
      if (undo_mode == PIKA_UNDO_MODE_UNDO)
        {
          GList *layers;

          /*  Update the preview for the floating selection  */
          pika_viewable_invalidate_preview (PIKA_VIEWABLE (floating_layer));

          pika_layer_set_floating_sel_drawable (floating_layer,
                                                floating_sel_undo->drawable);
          layers = g_list_prepend (NULL, floating_layer);
          pika_image_set_selected_layers (undo->image, layers);
          g_list_free (layers);

          pika_drawable_attach_floating_sel (pika_layer_get_floating_sel_drawable (floating_layer),
                                             floating_layer);
        }
      else
        {
          pika_drawable_detach_floating_sel (pika_layer_get_floating_sel_drawable (floating_layer));
          pika_layer_set_floating_sel_drawable (floating_layer, NULL);
        }

      /* When the floating selection is converted to/from a normal
       * layer it does something resembling a name change, so emit the
       * "name-changed" signal
       */
      pika_object_name_changed (PIKA_OBJECT (floating_layer));

      pika_drawable_update (PIKA_DRAWABLE (floating_layer),
                            0, 0,
                            pika_item_get_width  (PIKA_ITEM (floating_layer)),
                            pika_item_get_height (PIKA_ITEM (floating_layer)));
      break;

    default:
      g_return_if_reached ();
    }
}
