/* 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-1997 Spencer Kimball and Peter Mattis
 *
 * pikalayerstack.c
 * Copyright (C) 2017 Ell
 *
 * 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 "pikalayer.h"
#include "pikalayerstack.h"


/*  local function prototypes  */

static void   pika_layer_stack_constructed             (GObject       *object);

static void   pika_layer_stack_add                     (PikaContainer *container,
                                                        PikaObject    *object);
static void   pika_layer_stack_remove                  (PikaContainer *container,
                                                        PikaObject    *object);
static void   pika_layer_stack_reorder                 (PikaContainer *container,
                                                        PikaObject    *object,
                                                        gint           new_index);

static void   pika_layer_stack_layer_active            (PikaLayer      *layer,
                                                        PikaLayerStack *stack);
static void   pika_layer_stack_layer_excludes_backdrop (PikaLayer      *layer,
                                                        PikaLayerStack *stack);

static void   pika_layer_stack_update_backdrop         (PikaLayerStack *stack,
                                                        PikaLayer      *layer,
                                                        gboolean        ignore_active,
                                                        gboolean        ignore_excludes_backdrop);
static void   pika_layer_stack_update_range            (PikaLayerStack *stack,
                                                        gint            first,
                                                        gint            last);


G_DEFINE_TYPE (PikaLayerStack, pika_layer_stack, PIKA_TYPE_DRAWABLE_STACK)

#define parent_class pika_layer_stack_parent_class


static void
pika_layer_stack_class_init (PikaLayerStackClass *klass)
{
  GObjectClass       *object_class    = G_OBJECT_CLASS (klass);
  PikaContainerClass *container_class = PIKA_CONTAINER_CLASS (klass);

  object_class->constructed = pika_layer_stack_constructed;

  container_class->add      = pika_layer_stack_add;
  container_class->remove   = pika_layer_stack_remove;
  container_class->reorder  = pika_layer_stack_reorder;
}

static void
pika_layer_stack_init (PikaLayerStack *stack)
{
}

static void
pika_layer_stack_constructed (GObject *object)
{
  PikaContainer *container = PIKA_CONTAINER (object);

  G_OBJECT_CLASS (parent_class)->constructed (object);

  pika_assert (g_type_is_a (pika_container_get_children_type (container),
                            PIKA_TYPE_LAYER));

  pika_container_add_handler (container, "active-changed",
                              G_CALLBACK (pika_layer_stack_layer_active),
                              container);
  pika_container_add_handler (container, "excludes-backdrop-changed",
                              G_CALLBACK (pika_layer_stack_layer_excludes_backdrop),
                              container);
}

static void
pika_layer_stack_add (PikaContainer *container,
                      PikaObject    *object)
{
  PikaLayerStack *stack = PIKA_LAYER_STACK (container);

  PIKA_CONTAINER_CLASS (parent_class)->add (container, object);

  pika_layer_stack_update_backdrop (stack, PIKA_LAYER (object), FALSE, FALSE);
}

static void
pika_layer_stack_remove (PikaContainer *container,
                         PikaObject    *object)
{
  PikaLayerStack *stack = PIKA_LAYER_STACK (container);
  gboolean        update_backdrop;
  gint            index;

  update_backdrop = pika_filter_get_active (PIKA_FILTER (object)) &&
                    pika_layer_get_excludes_backdrop (PIKA_LAYER (object));

  if (update_backdrop)
    index = pika_container_get_child_index (container, object);

  PIKA_CONTAINER_CLASS (parent_class)->remove (container, object);

  if (update_backdrop)
    pika_layer_stack_update_range (stack, index, -1);
}

static void
pika_layer_stack_reorder (PikaContainer *container,
                          PikaObject    *object,
                          gint           new_index)
{
  PikaLayerStack *stack = PIKA_LAYER_STACK (container);
  gboolean        update_backdrop;
  gint            index;

  update_backdrop = pika_filter_get_active (PIKA_FILTER (object)) &&
                    pika_layer_get_excludes_backdrop (PIKA_LAYER (object));

  if (update_backdrop)
    index = pika_container_get_child_index (container, object);

  PIKA_CONTAINER_CLASS (parent_class)->reorder (container, object, new_index);

  if (update_backdrop)
    pika_layer_stack_update_range (stack, index, new_index);
}


/*  public functions  */

PikaContainer *
pika_layer_stack_new (GType layer_type)
{
  g_return_val_if_fail (g_type_is_a (layer_type, PIKA_TYPE_LAYER), NULL);

  return g_object_new (PIKA_TYPE_LAYER_STACK,
                       "name",          g_type_name (layer_type),
                       "children-type", layer_type,
                       "policy",        PIKA_CONTAINER_POLICY_STRONG,
                       NULL);
}


/*  private functions  */

static void
pika_layer_stack_layer_active (PikaLayer      *layer,
                               PikaLayerStack *stack)
{
  pika_layer_stack_update_backdrop (stack, layer, TRUE, FALSE);
}

static void
pika_layer_stack_layer_excludes_backdrop (PikaLayer      *layer,
                                          PikaLayerStack *stack)
{
  pika_layer_stack_update_backdrop (stack, layer, FALSE, TRUE);
}

static void
pika_layer_stack_update_backdrop (PikaLayerStack *stack,
                                  PikaLayer      *layer,
                                  gboolean        ignore_active,
                                  gboolean        ignore_excludes_backdrop)
{
  if ((ignore_active            || pika_filter_get_active (PIKA_FILTER (layer))) &&
      (ignore_excludes_backdrop || pika_layer_get_excludes_backdrop (layer)))
    {
      gint index;

      index = pika_container_get_child_index (PIKA_CONTAINER (stack),
                                              PIKA_OBJECT (layer));

      pika_layer_stack_update_range (stack, index + 1, -1);
    }
}

static void
pika_layer_stack_update_range (PikaLayerStack *stack,
                               gint            first,
                               gint            last)
{
  GList *iter;

  g_return_if_fail (first >= 0 && last >= -1);

  /* if the range is reversed, flip first and last; note that last == -1 is
   * used to update all layers from first onward.
   */
  if (last >= 0 && last < first)
    {
      gint temp = first;

      first = last + 1;
      last  = temp + 1;
    }

  iter = pika_item_stack_get_item_iter (PIKA_ITEM_STACK (stack));

  for (iter = g_list_nth (iter, first);
       iter && first != last;
       iter = g_list_next (iter), first++)
    {
      PikaItem *item = iter->data;

      if (pika_filter_get_active (PIKA_FILTER (item)))
        {
          GeglRectangle bounding_box;

          bounding_box = pika_drawable_get_bounding_box (PIKA_DRAWABLE (item));

          bounding_box.x += pika_item_get_offset_x (item);
          bounding_box.y += pika_item_get_offset_y (item);

          pika_drawable_stack_update (PIKA_DRAWABLE_STACK (stack),
                                      bounding_box.x,     bounding_box.y,
                                      bounding_box.width, bounding_box.height);
        }
    }
}
