/* 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
 *
 * pikaimage-symmetry.c
 * Copyright (C) 2015 Jehan <jehan@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 <gegl.h>
#include <gdk-pixbuf/gdk-pixbuf.h>

#include "core-types.h"

#include "pikasymmetry.h"
#include "pikaimage.h"
#include "pikaimage-private.h"
#include "pikaimage-symmetry.h"
#include "pikasymmetry-mandala.h"
#include "pikasymmetry-mirror.h"
#include "pikasymmetry-tiling.h"


/**
 * pika_image_symmetry_list:
 *
 * Returns a list of #GType of all existing symmetries.
 **/
GList *
pika_image_symmetry_list (void)
{
  GList *list = NULL;

  list = g_list_prepend (list, GINT_TO_POINTER (PIKA_TYPE_MIRROR));
  list = g_list_prepend (list, GINT_TO_POINTER (PIKA_TYPE_TILING));
  list = g_list_prepend (list, GINT_TO_POINTER (PIKA_TYPE_MANDALA));

  return list;
}

/**
 * pika_image_symmetry_new:
 * @image: the #PikaImage
 * @type:  the #GType of the symmetry
 *
 * Creates a new #PikaSymmetry of @type attached to @image.
 * @type must be a subtype of `PIKA_TYPE_SYMMETRY`.
 * Note that using the base @type `PIKA_TYPE_SYMMETRY` creates an
 * identity transformation.
 *
 * Returns: the new #PikaSymmetry.
 **/
PikaSymmetry *
pika_image_symmetry_new (PikaImage *image,
                         GType      type)
{
  PikaSymmetry *sym = NULL;

  g_return_val_if_fail (g_type_is_a (type, PIKA_TYPE_SYMMETRY), NULL);

  sym = g_object_new (type,
                      "image", image,
                      NULL);

  return sym;
}

/**
 * pika_image_symmetry_add:
 * @image: the #PikaImage
 * @type:  the #GType of the symmetry
 *
 * Add a symmetry of type @type to @image and make it the
 * active transformation.
 **/
void
pika_image_symmetry_add (PikaImage    *image,
                         PikaSymmetry *sym)
{
  PikaImagePrivate *private;

  g_return_if_fail (PIKA_IS_IMAGE (image));
  g_return_if_fail (PIKA_IS_SYMMETRY (sym));

  private = PIKA_IMAGE_GET_PRIVATE (image);

  private->symmetries = g_list_prepend (private->symmetries,
                                        g_object_ref (sym));
}

/**
 * pika_image_symmetry_remove:
 * @image:   the #PikaImage
 * @sym: the #PikaSymmetry
 *
 * Remove @sym from the list of symmetries of @image.
 * If it was the active transformation, unselect it first.
 **/
void
pika_image_symmetry_remove (PikaImage    *image,
                            PikaSymmetry *sym)
{
  PikaImagePrivate *private;

  g_return_if_fail (PIKA_IS_SYMMETRY (sym));
  g_return_if_fail (PIKA_IS_IMAGE (image));

  private = PIKA_IMAGE_GET_PRIVATE (image);

  if (private->active_symmetry == sym)
    pika_image_set_active_symmetry (image, PIKA_TYPE_SYMMETRY);

  private->symmetries = g_list_remove (private->symmetries, sym);
  g_object_unref (sym);
}

/**
 * pika_image_symmetry_get:
 * @image: the #PikaImage
 *
 * Returns: the list of #PikaSymmetry set on @image.
 * The returned list belongs to @image and should not be freed.
 **/
GList *
pika_image_symmetry_get (PikaImage *image)
{
  PikaImagePrivate *private;

  g_return_val_if_fail (PIKA_IS_IMAGE (image), FALSE);

  private = PIKA_IMAGE_GET_PRIVATE (image);

  return private->symmetries;
}

/**
 * pika_image_set_active_symmetry:
 * @image: the #PikaImage
 * @type:  the #GType of the symmetry
 *
 * Select the symmetry of type @type.
 * Using the GType allows to select a transformation without
 * knowing whether one of the same @type was already created.
 *
 * Returns TRUE on success, FALSE if no such symmetry was found.
 **/
gboolean
pika_image_set_active_symmetry (PikaImage *image,
                                GType      type)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), FALSE);

  g_object_set (image,
                "symmetry", type,
                NULL);

  return TRUE;
}

/**
 * pika_image_get_active_symmetry:
 * @image: the #PikaImage
 *
 * Returns the #PikaSymmetry transformation active on @image.
 **/
PikaSymmetry *
pika_image_get_active_symmetry (PikaImage *image)
{
  PikaImagePrivate *private;

  g_return_val_if_fail (PIKA_IS_IMAGE (image), FALSE);

  private = PIKA_IMAGE_GET_PRIVATE (image);

  return private->active_symmetry;
}
