/* 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
 *
 * pikamybrush.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 <cairo.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>

#include "core-types.h"

#include "pika-memsize.h"
#include "pikamybrush.h"
#include "pikamybrush-load.h"
#include "pikamybrush-private.h"
#include "pikatagged.h"

#include "pika-intl.h"


static void          pika_mybrush_tagged_iface_init     (PikaTaggedInterface  *iface);

static void          pika_mybrush_finalize              (GObject              *object);
static void          pika_mybrush_set_property          (GObject              *object,
                                                         guint                 property_id,
                                                         const GValue         *value,
                                                         GParamSpec           *pspec);
static void          pika_mybrush_get_property          (GObject              *object,
                                                         guint                 property_id,
                                                         GValue               *value,
                                                         GParamSpec           *pspec);

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

static gchar       * pika_mybrush_get_description       (PikaViewable         *viewable,
                                                         gchar               **tooltip);

static void          pika_mybrush_dirty                 (PikaData             *data);
static const gchar * pika_mybrush_get_extension         (PikaData             *data);

static gchar       * pika_mybrush_get_checksum          (PikaTagged           *tagged);


G_DEFINE_TYPE_WITH_CODE (PikaMybrush, pika_mybrush, PIKA_TYPE_DATA,
                         G_ADD_PRIVATE (PikaMybrush)
                         G_IMPLEMENT_INTERFACE (PIKA_TYPE_TAGGED,
                                                pika_mybrush_tagged_iface_init))

#define parent_class pika_mybrush_parent_class


static void
pika_mybrush_class_init (PikaMybrushClass *klass)
{
  GObjectClass      *object_class      = G_OBJECT_CLASS (klass);
  PikaObjectClass   *pika_object_class = PIKA_OBJECT_CLASS (klass);
  PikaViewableClass *viewable_class    = PIKA_VIEWABLE_CLASS (klass);
  PikaDataClass     *data_class        = PIKA_DATA_CLASS (klass);

  object_class->finalize            = pika_mybrush_finalize;
  object_class->get_property        = pika_mybrush_get_property;
  object_class->set_property        = pika_mybrush_set_property;

  pika_object_class->get_memsize    = pika_mybrush_get_memsize;

  viewable_class->default_icon_name = "pika-tool-mypaint-brush";
  viewable_class->get_description   = pika_mybrush_get_description;

  data_class->dirty                 = pika_mybrush_dirty;
  data_class->get_extension         = pika_mybrush_get_extension;
}

static void
pika_mybrush_tagged_iface_init (PikaTaggedInterface *iface)
{
  iface->get_checksum = pika_mybrush_get_checksum;
}

static void
pika_mybrush_init (PikaMybrush *brush)
{
  brush->priv = pika_mybrush_get_instance_private (brush);

  brush->priv->radius   = 1.0;
  brush->priv->opaque   = 1.0;
  brush->priv->hardness = 1.0;
  brush->priv->eraser   = FALSE;
}

static void
pika_mybrush_finalize (GObject *object)
{
  PikaMybrush *brush = PIKA_MYBRUSH (object);

  g_clear_pointer (&brush->priv->brush_json, g_free);

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

static void
pika_mybrush_set_property (GObject      *object,
                           guint         property_id,
                           const GValue *value,
                           GParamSpec   *pspec)
{
  switch (property_id)
    {
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
pika_mybrush_get_property (GObject    *object,
                           guint       property_id,
                           GValue     *value,
                           GParamSpec *pspec)
{
  switch (property_id)
    {
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static gint64
pika_mybrush_get_memsize (PikaObject *object,
                          gint64     *gui_size)
{
  PikaMybrush *brush   = PIKA_MYBRUSH (object);
  gint64       memsize = 0;

  memsize += pika_string_get_memsize (brush->priv->brush_json);

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

static gchar *
pika_mybrush_get_description (PikaViewable  *viewable,
                              gchar        **tooltip)
{
  PikaMybrush *brush = PIKA_MYBRUSH (viewable);

  return g_strdup_printf ("%s",
                          pika_object_get_name (brush));
}

static void
pika_mybrush_dirty (PikaData *data)
{
  PIKA_DATA_CLASS (parent_class)->dirty (data);
}

static const gchar *
pika_mybrush_get_extension (PikaData *data)
{
  return PIKA_MYBRUSH_FILE_EXTENSION;
}

static gchar *
pika_mybrush_get_checksum (PikaTagged *tagged)
{
  PikaMybrush *brush           = PIKA_MYBRUSH (tagged);
  gchar       *checksum_string = NULL;

  if (brush->priv->brush_json)
    {
      GChecksum *checksum = g_checksum_new (G_CHECKSUM_MD5);

      g_checksum_update (checksum,
                         (const guchar *) brush->priv->brush_json,
                         strlen (brush->priv->brush_json));

      checksum_string = g_strdup (g_checksum_get_string (checksum));

      g_checksum_free (checksum);
    }

  return checksum_string;
}

/*  public functions  */

PikaData *
pika_mybrush_new (PikaContext *context,
                  const gchar *name)
{
  g_return_val_if_fail (name != NULL, NULL);

  return g_object_new (PIKA_TYPE_MYBRUSH,
                       "name",      name,
                       "mime-type", "image/x-pika-myb",
                       NULL);
}

PikaData *
pika_mybrush_get_standard (PikaContext *context)
{
  static PikaData *standard_mybrush = NULL;

  if (! standard_mybrush)
    {
      g_set_weak_pointer (&standard_mybrush,
                          pika_mybrush_new (context, "Standard"));

      pika_data_clean (standard_mybrush);
      pika_data_make_internal (standard_mybrush, "pika-mybrush-standard");
    }

  return standard_mybrush;
}

const gchar *
pika_mybrush_get_brush_json (PikaMybrush *brush)
{
  g_return_val_if_fail (PIKA_IS_MYBRUSH (brush), NULL);

  return brush->priv->brush_json;
}

gdouble
pika_mybrush_get_radius (PikaMybrush *brush)
{
  g_return_val_if_fail (PIKA_IS_MYBRUSH (brush), 1.0);

  return brush->priv->radius;
}

gdouble
pika_mybrush_get_opaque (PikaMybrush *brush)
{
  g_return_val_if_fail (PIKA_IS_MYBRUSH (brush), 1.0);

  return brush->priv->opaque;
}

gdouble
pika_mybrush_get_hardness (PikaMybrush *brush)
{
  g_return_val_if_fail (PIKA_IS_MYBRUSH (brush), 1.0);

  return brush->priv->hardness;
}

gdouble
pika_mybrush_get_offset_by_random (PikaMybrush *brush)
{
  g_return_val_if_fail (PIKA_IS_MYBRUSH (brush), 1.0);

  return brush->priv->offset_by_random;
}

gboolean
pika_mybrush_get_is_eraser (PikaMybrush *brush)
{
  g_return_val_if_fail (PIKA_IS_MYBRUSH (brush), FALSE);

  return brush->priv->eraser;
}
