/* 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
 *
 * pikaitemstack.c
 * Copyright (C) 2008 Michael Natterer <mitch@gimp.org>
 *
 * 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 <string.h>

#include <cairo.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>

#include "libpikacolor/pikacolor.h"

#include "core-types.h"

#include "pikaitem.h"
#include "pikaitemstack.h"


/*  local function prototypes  */

static void   pika_item_stack_constructed (GObject       *object);

static void   pika_item_stack_add         (PikaContainer *container,
                                           PikaObject    *object);
static void   pika_item_stack_remove      (PikaContainer *container,
                                           PikaObject    *object);


G_DEFINE_TYPE (PikaItemStack, pika_item_stack, PIKA_TYPE_FILTER_STACK)

#define parent_class pika_item_stack_parent_class


static void
pika_item_stack_class_init (PikaItemStackClass *klass)
{
  GObjectClass       *object_class    = G_OBJECT_CLASS (klass);
  PikaContainerClass *container_class = PIKA_CONTAINER_CLASS (klass);

  object_class->constructed = pika_item_stack_constructed;

  container_class->add      = pika_item_stack_add;
  container_class->remove   = pika_item_stack_remove;
}

static void
pika_item_stack_init (PikaItemStack *stack)
{
}

static void
pika_item_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_ITEM));
}

static void
pika_item_stack_add (PikaContainer *container,
                     PikaObject    *object)
{
  g_object_ref_sink (object);

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

  g_object_unref (object);
}

static void
pika_item_stack_remove (PikaContainer *container,
                        PikaObject    *object)
{
  PIKA_CONTAINER_CLASS (parent_class)->remove (container, object);
}


/*  public functions  */

PikaContainer *
pika_item_stack_new (GType item_type)
{
  g_return_val_if_fail (g_type_is_a (item_type, PIKA_TYPE_ITEM), NULL);

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

gint
pika_item_stack_get_n_items (PikaItemStack *stack)
{
  GList *list;
  gint   n_items = 0;

  g_return_val_if_fail (PIKA_IS_ITEM_STACK (stack), 0);

  for (list = PIKA_LIST (stack)->queue->head; list; list = g_list_next (list))
    {
      PikaItem      *item = list->data;
      PikaContainer *children;

      n_items++;

      children = pika_viewable_get_children (PIKA_VIEWABLE (item));

      if (children)
        n_items += pika_item_stack_get_n_items (PIKA_ITEM_STACK (children));
    }

  return n_items;
}

gboolean
pika_item_stack_is_flat (PikaItemStack *stack)
{
  GList *list;

  g_return_val_if_fail (PIKA_IS_ITEM_STACK (stack), TRUE);

  for (list = PIKA_LIST (stack)->queue->head; list; list = g_list_next (list))
    {
      PikaViewable *viewable = list->data;

      if (pika_viewable_get_children (viewable))
        return FALSE;
    }

  return TRUE;
}

GList *
pika_item_stack_get_item_iter (PikaItemStack *stack)
{
  g_return_val_if_fail (PIKA_IS_ITEM_STACK (stack), NULL);

  return PIKA_LIST (stack)->queue->head;
}

GList *
pika_item_stack_get_item_list (PikaItemStack *stack)
{
  GList *list;
  GList *result = NULL;

  g_return_val_if_fail (PIKA_IS_ITEM_STACK (stack), NULL);

  for (list = PIKA_LIST (stack)->queue->head;
       list;
       list = g_list_next (list))
    {
      PikaViewable  *viewable = list->data;
      PikaContainer *children;

      result = g_list_prepend (result, viewable);

      children = pika_viewable_get_children (viewable);

      if (children)
        {
          GList *child_list;

          child_list = pika_item_stack_get_item_list (PIKA_ITEM_STACK (children));

          while (child_list)
            {
              result = g_list_prepend (result, child_list->data);

              child_list = g_list_remove (child_list, child_list->data);
            }
        }
    }

  return g_list_reverse (result);
}

PikaItem *
pika_item_stack_get_item_by_tattoo (PikaItemStack *stack,
                                    PikaTattoo     tattoo)
{
  GList *list;

  g_return_val_if_fail (PIKA_IS_ITEM_STACK (stack), NULL);

  for (list = PIKA_LIST (stack)->queue->head; list; list = g_list_next (list))
    {
      PikaItem      *item = list->data;
      PikaContainer *children;

      if (pika_item_get_tattoo (item) == tattoo)
        return item;

      children = pika_viewable_get_children (PIKA_VIEWABLE (item));

      if (children)
        {
          item = pika_item_stack_get_item_by_tattoo (PIKA_ITEM_STACK (children),
                                                     tattoo);

          if (item)
            return item;
        }
    }

  return NULL;
}

PikaItem *
pika_item_stack_get_item_by_path (PikaItemStack *stack,
                                  GList         *path)
{
  PikaContainer *container;
  PikaItem      *item = NULL;

  g_return_val_if_fail (PIKA_IS_ITEM_STACK (stack), NULL);
  g_return_val_if_fail (path != NULL, NULL);

  container = PIKA_CONTAINER (stack);

  while (path)
    {
      guint32 i = GPOINTER_TO_UINT (path->data);

      item = PIKA_ITEM (pika_container_get_child_by_index (container, i));

      g_return_val_if_fail (PIKA_IS_ITEM (item), item);

      if (path->next)
        {
          container = pika_viewable_get_children (PIKA_VIEWABLE (item));

          g_return_val_if_fail (PIKA_IS_ITEM_STACK (container), item);
        }

      path = path->next;
    }

  return item;
}

PikaItem *
pika_item_stack_get_parent_by_path (PikaItemStack *stack,
                                    GList         *path,
                                    gint          *index)
{
  PikaItem *parent = NULL;
  guint32   i;

  g_return_val_if_fail (PIKA_IS_ITEM_STACK (stack), NULL);
  g_return_val_if_fail (path != NULL, NULL);

  i = GPOINTER_TO_UINT (path->data);

  if (index)
    *index = i;

  while (path->next)
    {
      PikaObject    *child;
      PikaContainer *children;

      child = pika_container_get_child_by_index (PIKA_CONTAINER (stack), i);

      g_return_val_if_fail (PIKA_IS_ITEM (child), parent);

      children = pika_viewable_get_children (PIKA_VIEWABLE (child));

      g_return_val_if_fail (PIKA_IS_ITEM_STACK (children), parent);

      parent = PIKA_ITEM (child);
      stack  = PIKA_ITEM_STACK (children);

      path = path->next;

      i = GPOINTER_TO_UINT (path->data);

      if (index)
        *index = i;
    }

  return parent;
}

static void
pika_item_stack_viewable_invalidate_previews (PikaViewable *viewable)
{
  PikaContainer *children = pika_viewable_get_children (viewable);

  if (children)
    pika_item_stack_invalidate_previews (PIKA_ITEM_STACK (children));

  pika_viewable_invalidate_preview (viewable);
}

void
pika_item_stack_invalidate_previews (PikaItemStack *stack)
{
  g_return_if_fail (PIKA_IS_ITEM_STACK (stack));

  pika_container_foreach (PIKA_CONTAINER (stack),
                          (GFunc) pika_item_stack_viewable_invalidate_previews,
                          NULL);
}

static void
pika_item_stack_viewable_profile_changed (PikaViewable *viewable)
{
  PikaContainer *children = pika_viewable_get_children (viewable);

  if (children)
    pika_item_stack_profile_changed (PIKA_ITEM_STACK (children));

  if (PIKA_IS_COLOR_MANAGED (viewable))
    pika_color_managed_profile_changed (PIKA_COLOR_MANAGED (viewable));
}

void
pika_item_stack_profile_changed (PikaItemStack *stack)
{
  g_return_if_fail (PIKA_IS_ITEM_STACK (stack));

  pika_container_foreach (PIKA_CONTAINER (stack),
                          (GFunc) pika_item_stack_viewable_profile_changed,
                          NULL);
}
