/* 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
 *
 * pikafilter.c
 *
 * 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.h"
#include "pika-memsize.h"
#include "pikafilter.h"


enum
{
  ACTIVE_CHANGED,
  LAST_SIGNAL
};

enum
{
  PROP_0,
  PROP_ACTIVE,
  PROP_IS_LAST_NODE
};


typedef struct _PikaFilterPrivate PikaFilterPrivate;

struct _PikaFilterPrivate
{
  GeglNode       *node;

  guint           active       : 1;
  guint           is_last_node : 1;

  PikaApplicator *applicator;
};

#define GET_PRIVATE(filter) ((PikaFilterPrivate *) pika_filter_get_instance_private ((PikaFilter *) (filter)))


/*  local function prototypes  */

static void       pika_filter_finalize      (GObject      *object);
static void       pika_filter_set_property  (GObject      *object,
                                             guint         property_id,
                                             const GValue *value,
                                             GParamSpec   *pspec);
static void       pika_filter_get_property  (GObject      *object,
                                             guint         property_id,
                                             GValue       *value,
                                             GParamSpec   *pspec);

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

static GeglNode * pika_filter_real_get_node (PikaFilter   *filter);


G_DEFINE_TYPE_WITH_PRIVATE (PikaFilter, pika_filter, PIKA_TYPE_VIEWABLE)

#define parent_class pika_filter_parent_class

static guint pika_filter_signals[LAST_SIGNAL] = { 0 };


static void
pika_filter_class_init (PikaFilterClass *klass)
{
  GObjectClass    *object_class      = G_OBJECT_CLASS (klass);
  PikaObjectClass *pika_object_class = PIKA_OBJECT_CLASS (klass);

  pika_filter_signals[ACTIVE_CHANGED] =
    g_signal_new ("active-changed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (PikaFilterClass, active_changed),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 0);

  object_class->finalize         = pika_filter_finalize;
  object_class->set_property     = pika_filter_set_property;
  object_class->get_property     = pika_filter_get_property;

  pika_object_class->get_memsize = pika_filter_get_memsize;

  klass->active_changed          = NULL;
  klass->get_node                = pika_filter_real_get_node;

  g_object_class_install_property (object_class, PROP_ACTIVE,
                                   g_param_spec_boolean ("active", NULL, NULL,
                                                         TRUE,
                                                         PIKA_PARAM_READWRITE));

  g_object_class_install_property (object_class, PROP_IS_LAST_NODE,
                                   g_param_spec_boolean ("is-last-node",
                                                         NULL, NULL,
                                                         FALSE,
                                                         PIKA_PARAM_READWRITE));
}

static void
pika_filter_init (PikaFilter *filter)
{
  PikaFilterPrivate *private = GET_PRIVATE (filter);

  private->active = TRUE;
}

static void
pika_filter_finalize (GObject *object)
{
  PikaFilterPrivate *private = GET_PRIVATE (object);

  g_clear_object (&private->node);

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

static void
pika_filter_set_property (GObject      *object,
                          guint         property_id,
                          const GValue *value,
                          GParamSpec   *pspec)
{
  PikaFilter *filter = PIKA_FILTER (object);

  switch (property_id)
    {
    case PROP_ACTIVE:
      pika_filter_set_active (filter, g_value_get_boolean (value));
      break;
    case PROP_IS_LAST_NODE:
      pika_filter_set_is_last_node (filter, g_value_get_boolean (value));
      break;

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

static void
pika_filter_get_property (GObject    *object,
                          guint       property_id,
                          GValue     *value,
                          GParamSpec *pspec)
{
  PikaFilterPrivate *private = GET_PRIVATE (object);

  switch (property_id)
    {
    case PROP_ACTIVE:
      g_value_set_boolean (value, private->active);
      break;
    case PROP_IS_LAST_NODE:
      g_value_set_boolean (value, private->is_last_node);
      break;

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

static gint64
pika_filter_get_memsize (PikaObject *object,
                         gint64     *gui_size)
{
  PikaFilterPrivate *private = GET_PRIVATE (object);
  gint64             memsize = 0;

  memsize += pika_g_object_get_memsize (G_OBJECT (private->node));

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

static GeglNode *
pika_filter_real_get_node (PikaFilter *filter)
{
  PikaFilterPrivate *private = GET_PRIVATE (filter);

  private->node = gegl_node_new ();

  return private->node;
}


/*  public functions  */

PikaFilter *
pika_filter_new (const gchar *name)
{
  g_return_val_if_fail (name != NULL, NULL);

  return g_object_new (PIKA_TYPE_FILTER,
                       "name", name,
                       NULL);
}

GeglNode *
pika_filter_get_node (PikaFilter *filter)
{
  PikaFilterPrivate *private;

  g_return_val_if_fail (PIKA_IS_FILTER (filter), NULL);

  private = GET_PRIVATE (filter);

  if (private->node)
    return private->node;

  return PIKA_FILTER_GET_CLASS (filter)->get_node (filter);
}

GeglNode *
pika_filter_peek_node (PikaFilter *filter)
{
  g_return_val_if_fail (PIKA_IS_FILTER (filter), NULL);

  return GET_PRIVATE (filter)->node;
}

void
pika_filter_set_active (PikaFilter *filter,
                        gboolean    active)
{
  g_return_if_fail (PIKA_IS_FILTER (filter));

  active = active ? TRUE : FALSE;

  if (active != pika_filter_get_active (filter))
    {
      GET_PRIVATE (filter)->active = active;

      g_signal_emit (filter, pika_filter_signals[ACTIVE_CHANGED], 0);

      g_object_notify (G_OBJECT (filter), "active");
    }
}

gboolean
pika_filter_get_active (PikaFilter *filter)
{
  g_return_val_if_fail (PIKA_IS_FILTER (filter), FALSE);

  return GET_PRIVATE (filter)->active;
}

void
pika_filter_set_is_last_node (PikaFilter *filter,
                              gboolean    is_last_node)
{
  g_return_if_fail (PIKA_IS_FILTER (filter));

  is_last_node = is_last_node ? TRUE : FALSE;

  if (is_last_node != pika_filter_get_is_last_node (filter))
    {
      GET_PRIVATE (filter)->is_last_node = is_last_node;

      g_object_notify (G_OBJECT (filter), "is-last-node");
    }
}

gboolean
pika_filter_get_is_last_node (PikaFilter *filter)
{
  g_return_val_if_fail (PIKA_IS_FILTER (filter), FALSE);

  return GET_PRIVATE (filter)->is_last_node;
}

void
pika_filter_set_applicator (PikaFilter     *filter,
                            PikaApplicator *applicator)
{
  PikaFilterPrivate *private;

  g_return_if_fail (PIKA_IS_FILTER (filter));

  private = GET_PRIVATE (filter);

  private->applicator = applicator;
}

PikaApplicator *
pika_filter_get_applicator (PikaFilter *filter)
{
  g_return_val_if_fail (PIKA_IS_FILTER (filter), NULL);

  return GET_PRIVATE (filter)->applicator;
}
