/* 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
 *
 * pikafilterstack.c
 * Copyright (C) 2008-2013 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 <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>

#include "core-types.h"

#include "pikafilter.h"
#include "pikafilterstack.h"


/*  local function prototypes  */

static void   pika_filter_stack_constructed      (GObject         *object);
static void   pika_filter_stack_finalize         (GObject         *object);

static void   pika_filter_stack_add              (PikaContainer   *container,
                                                  PikaObject      *object);
static void   pika_filter_stack_remove           (PikaContainer   *container,
                                                  PikaObject      *object);
static void   pika_filter_stack_reorder          (PikaContainer   *container,
                                                  PikaObject      *object,
                                                  gint             new_index);

static void   pika_filter_stack_add_node         (PikaFilterStack *stack,
                                                  PikaFilter      *filter);
static void   pika_filter_stack_remove_node      (PikaFilterStack *stack,
                                                  PikaFilter      *filter);
static void   pika_filter_stack_update_last_node (PikaFilterStack *stack);

static void   pika_filter_stack_filter_active    (PikaFilter      *filter,
                                                  PikaFilterStack *stack);


G_DEFINE_TYPE (PikaFilterStack, pika_filter_stack, PIKA_TYPE_LIST);

#define parent_class pika_filter_stack_parent_class


static void
pika_filter_stack_class_init (PikaFilterStackClass *klass)
{
  GObjectClass       *object_class    = G_OBJECT_CLASS (klass);
  PikaContainerClass *container_class = PIKA_CONTAINER_CLASS (klass);

  object_class->constructed = pika_filter_stack_constructed;
  object_class->finalize    = pika_filter_stack_finalize;

  container_class->add      = pika_filter_stack_add;
  container_class->remove   = pika_filter_stack_remove;
  container_class->reorder  = pika_filter_stack_reorder;
}

static void
pika_filter_stack_init (PikaFilterStack *stack)
{
}

static void
pika_filter_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_FILTER));

  pika_container_add_handler (container, "active-changed",
                              G_CALLBACK (pika_filter_stack_filter_active),
                              container);
}

static void
pika_filter_stack_finalize (GObject *object)
{
  PikaFilterStack *stack = PIKA_FILTER_STACK (object);

  g_clear_object (&stack->graph);

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

static void
pika_filter_stack_add (PikaContainer *container,
                       PikaObject    *object)
{
  PikaFilterStack *stack  = PIKA_FILTER_STACK (container);
  PikaFilter      *filter = PIKA_FILTER (object);

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

  if (pika_filter_get_active (filter))
    {
      if (stack->graph)
        {
          gegl_node_add_child (stack->graph, pika_filter_get_node (filter));
          pika_filter_stack_add_node (stack, filter);
        }

      pika_filter_stack_update_last_node (stack);
    }
}

static void
pika_filter_stack_remove (PikaContainer *container,
                          PikaObject    *object)
{
  PikaFilterStack *stack  = PIKA_FILTER_STACK (container);
  PikaFilter      *filter = PIKA_FILTER (object);

  if (stack->graph && pika_filter_get_active (filter))
    {
      pika_filter_stack_remove_node (stack, filter);
      gegl_node_remove_child (stack->graph, pika_filter_get_node (filter));
    }

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

  if (pika_filter_get_active (filter))
    {
      pika_filter_set_is_last_node (filter, FALSE);
      pika_filter_stack_update_last_node (stack);
    }
}

static void
pika_filter_stack_reorder (PikaContainer *container,
                           PikaObject    *object,
                           gint           new_index)
{
  PikaFilterStack *stack  = PIKA_FILTER_STACK (container);
  PikaFilter      *filter = PIKA_FILTER (object);

  if (stack->graph && pika_filter_get_active (filter))
    pika_filter_stack_remove_node (stack, filter);

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

  if (pika_filter_get_active (filter))
    {
      pika_filter_stack_update_last_node (stack);

      if (stack->graph)
        pika_filter_stack_add_node (stack, filter);
    }
}


/*  public functions  */

PikaContainer *
pika_filter_stack_new (GType filter_type)
{
  g_return_val_if_fail (g_type_is_a (filter_type, PIKA_TYPE_FILTER), NULL);

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

GeglNode *
pika_filter_stack_get_graph (PikaFilterStack *stack)
{
  GList    *list;
  GeglNode *previous;
  GeglNode *output;

  g_return_val_if_fail (PIKA_IS_FILTER_STACK (stack), NULL);

  if (stack->graph)
    return stack->graph;

  stack->graph = gegl_node_new ();

  previous = gegl_node_get_input_proxy (stack->graph, "input");

  for (list = PIKA_LIST (stack)->queue->tail;
       list;
       list = g_list_previous (list))
    {
      PikaFilter *filter = list->data;
      GeglNode   *node;

      if (! pika_filter_get_active (filter))
        continue;

      node = pika_filter_get_node (filter);

      gegl_node_add_child (stack->graph, node);

      gegl_node_link (previous, node);

      previous = node;
    }

  output = gegl_node_get_output_proxy (stack->graph, "output");

  gegl_node_link (previous, output);

  return stack->graph;
}


/*  private functions  */

static void
pika_filter_stack_add_node (PikaFilterStack *stack,
                            PikaFilter      *filter)
{
  GeglNode *node;
  GeglNode *node_above = NULL;
  GeglNode *node_below = NULL;
  GList    *iter;

  node = pika_filter_get_node (filter);


  iter = g_list_find (PIKA_LIST (stack)->queue->head, filter);

  while ((iter = g_list_previous (iter)))
    {
      PikaFilter *filter_above = iter->data;

      if (pika_filter_get_active (filter_above))
        {
          node_above = pika_filter_get_node (filter_above);

          break;
        }
    }

  if (! node_above)
    node_above = gegl_node_get_output_proxy (stack->graph, "output");

  node_below = gegl_node_get_producer (node_above, "input", NULL);

  gegl_node_link (node_below, node);
  gegl_node_link (node, node_above);
}

static void
pika_filter_stack_remove_node (PikaFilterStack *stack,
                               PikaFilter      *filter)
{
  GeglNode *node;
  GeglNode *node_above = NULL;
  GeglNode *node_below = NULL;
  GList    *iter;

  node = pika_filter_get_node (filter);

  iter = g_list_find (PIKA_LIST (stack)->queue->head, filter);

  while ((iter = g_list_previous (iter)))
    {
      PikaFilter *filter_above = iter->data;

      if (pika_filter_get_active (filter_above))
        {
          node_above = pika_filter_get_node (filter_above);

          break;
        }
    }

  if (! node_above)
    node_above = gegl_node_get_output_proxy (stack->graph, "output");

  node_below = gegl_node_get_producer (node, "input", NULL);

  gegl_node_disconnect (node, "input");

  gegl_node_link (node_below, node_above);
}

static void
pika_filter_stack_update_last_node (PikaFilterStack *stack)
{
  GList    *list;
  gboolean  found_last = FALSE;

  for (list = PIKA_LIST (stack)->queue->tail;
       list;
       list = g_list_previous (list))
    {
      PikaFilter *filter = list->data;

      if (! found_last && pika_filter_get_active (filter))
        {
          pika_filter_set_is_last_node (filter, TRUE);
          found_last = TRUE;
        }
      else
        {
          pika_filter_set_is_last_node (filter, FALSE);
        }
    }
}

static void
pika_filter_stack_filter_active (PikaFilter      *filter,
                                 PikaFilterStack *stack)
{
  if (stack->graph)
    {
      if (pika_filter_get_active (filter))
        {
          gegl_node_add_child (stack->graph, pika_filter_get_node (filter));
          pika_filter_stack_add_node (stack, filter);
        }
      else
        {
          pika_filter_stack_remove_node (stack, filter);
          gegl_node_remove_child (stack->graph, pika_filter_get_node (filter));
        }
    }

  pika_filter_stack_update_last_node (stack);

  if (! pika_filter_get_active (filter))
    pika_filter_set_is_last_node (filter, FALSE);
}
