/* 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-2003 Spencer Kimball and Peter Mattis
 *
 * 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/>.
 */

/* NOTE: This file is auto-generated by pdbgen.pl. */

#include "config.h"

#include "stamp-pdbgen.h"

#include <gegl.h>

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

#include "libpikabase/pikabase.h"

#include "pdb-types.h"

#include "core/pikaimage-sample-points.h"
#include "core/pikaimage.h"
#include "core/pikaparamspecs.h"
#include "core/pikasamplepoint.h"

#include "pikapdb.h"
#include "pikapdberror.h"
#include "pikapdb-utils.h"
#include "pikaprocedure.h"
#include "internal-procs.h"

#include "pika-intl.h"


static PikaValueArray *
image_add_sample_point_invoker (PikaProcedure         *procedure,
                                Pika                  *pika,
                                PikaContext           *context,
                                PikaProgress          *progress,
                                const PikaValueArray  *args,
                                GError               **error)
{
  gboolean success = TRUE;
  PikaValueArray *return_vals;
  PikaImage *image;
  gint position_x;
  gint position_y;
  guint sample_point = 0;

  image = g_value_get_object (pika_value_array_index (args, 0));
  position_x = g_value_get_int (pika_value_array_index (args, 1));
  position_y = g_value_get_int (pika_value_array_index (args, 2));

  if (success)
    {
      if (position_x <= pika_image_get_width  (image) &&
          position_y <= pika_image_get_height (image))
        {
          PikaSamplePoint *sp;

          sp = pika_image_add_sample_point_at_pos (image, position_x, position_y,
                                                   TRUE);
          sample_point = pika_aux_item_get_id (PIKA_AUX_ITEM (sp));
        }
      else
        success = FALSE;
    }

  return_vals = pika_procedure_get_return_values (procedure, success,
                                                  error ? *error : NULL);

  if (success)
    g_value_set_uint (pika_value_array_index (return_vals, 1), sample_point);

  return return_vals;
}

static PikaValueArray *
image_delete_sample_point_invoker (PikaProcedure         *procedure,
                                   Pika                  *pika,
                                   PikaContext           *context,
                                   PikaProgress          *progress,
                                   const PikaValueArray  *args,
                                   GError               **error)
{
  gboolean success = TRUE;
  PikaImage *image;
  guint sample_point;

  image = g_value_get_object (pika_value_array_index (args, 0));
  sample_point = g_value_get_uint (pika_value_array_index (args, 1));

  if (success)
    {
      PikaSamplePoint *sp = pika_pdb_image_get_sample_point (image, sample_point,
                                                             error);

      if (sp)
        pika_image_remove_sample_point (image, sp, TRUE);
      else
        success = FALSE;
    }

  return pika_procedure_get_return_values (procedure, success,
                                           error ? *error : NULL);
}

static PikaValueArray *
image_find_next_sample_point_invoker (PikaProcedure         *procedure,
                                      Pika                  *pika,
                                      PikaContext           *context,
                                      PikaProgress          *progress,
                                      const PikaValueArray  *args,
                                      GError               **error)
{
  gboolean success = TRUE;
  PikaValueArray *return_vals;
  PikaImage *image;
  guint sample_point;
  guint next_sample_point = 0;

  image = g_value_get_object (pika_value_array_index (args, 0));
  sample_point = g_value_get_uint (pika_value_array_index (args, 1));

  if (success)
    {
      PikaSamplePoint *sp = pika_image_get_next_sample_point (image, sample_point,
                                                              &success);

      if (sp)
        next_sample_point = pika_aux_item_get_id (PIKA_AUX_ITEM (sp));

      if (! success)
        g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                     _("Image '%s' (%d) does not contain sample point with ID %d"),
                     pika_image_get_display_name (image),
                     pika_image_get_id (image),
                     sample_point);
    }

  return_vals = pika_procedure_get_return_values (procedure, success,
                                                  error ? *error : NULL);

  if (success)
    g_value_set_uint (pika_value_array_index (return_vals, 1), next_sample_point);

  return return_vals;
}

static PikaValueArray *
image_get_sample_point_position_invoker (PikaProcedure         *procedure,
                                         Pika                  *pika,
                                         PikaContext           *context,
                                         PikaProgress          *progress,
                                         const PikaValueArray  *args,
                                         GError               **error)
{
  gboolean success = TRUE;
  PikaValueArray *return_vals;
  PikaImage *image;
  guint sample_point;
  gint position_x = 0;
  gint position_y = 0;

  image = g_value_get_object (pika_value_array_index (args, 0));
  sample_point = g_value_get_uint (pika_value_array_index (args, 1));

  if (success)
    {
      PikaSamplePoint *sp = pika_pdb_image_get_sample_point (image, sample_point,
                                                             error);

      if (sp)
        pika_sample_point_get_position (sp, &position_x, &position_y);
      else
        success = FALSE;
    }

  return_vals = pika_procedure_get_return_values (procedure, success,
                                                  error ? *error : NULL);

  if (success)
    {
      g_value_set_int (pika_value_array_index (return_vals, 1), position_x);
      g_value_set_int (pika_value_array_index (return_vals, 2), position_y);
    }

  return return_vals;
}

void
register_image_sample_points_procs (PikaPDB *pdb)
{
  PikaProcedure *procedure;

  /*
   * pika-image-add-sample-point
   */
  procedure = pika_procedure_new (image_add_sample_point_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-image-add-sample-point");
  pika_procedure_set_static_help (procedure,
                                  "Add a sample point to an image.",
                                  "This procedure adds a sample point to an image. It takes the input image and the position of the new sample points as parameters. It returns the sample point ID of the new sample point.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Michael Natterer <mitch@gimp.org>",
                                         "Michael Natterer",
                                         "2016");
  pika_procedure_add_argument (procedure,
                               pika_param_spec_image ("image",
                                                      "image",
                                                      "The image",
                                                      FALSE,
                                                      PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_int ("position-x",
                                                 "position x",
                                                 "The sample point's x-offset from left of image",
                                                 0, G_MAXINT32, 0,
                                                 PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_int ("position-y",
                                                 "position y",
                                                 "The sample point's y-offset from top of image",
                                                 0, G_MAXINT32, 0,
                                                 PIKA_PARAM_READWRITE));
  pika_procedure_add_return_value (procedure,
                                   g_param_spec_uint ("sample-point",
                                                      "sample point",
                                                      "The new sample point",
                                                      1, G_MAXUINT32, 1,
                                                      PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);

  /*
   * pika-image-delete-sample-point
   */
  procedure = pika_procedure_new (image_delete_sample_point_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-image-delete-sample-point");
  pika_procedure_set_static_help (procedure,
                                  "Deletes a sample point from an image.",
                                  "This procedure takes an image and a sample point ID as input and removes the specified sample point from the specified image.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Michael Natterer <mitch@gimp.org>",
                                         "Michael Natterer",
                                         "2016");
  pika_procedure_add_argument (procedure,
                               pika_param_spec_image ("image",
                                                      "image",
                                                      "The image",
                                                      FALSE,
                                                      PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_uint ("sample-point",
                                                  "sample point",
                                                  "The ID of the sample point to be removed",
                                                  1, G_MAXUINT32, 1,
                                                  PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);

  /*
   * pika-image-find-next-sample-point
   */
  procedure = pika_procedure_new (image_find_next_sample_point_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-image-find-next-sample-point");
  pika_procedure_set_static_help (procedure,
                                  "Find next sample point on an image.",
                                  "This procedure takes an image and a sample point ID as input and finds the sample point ID of the successor of the given sample point ID in the image's sample point list. If the supplied sample point ID is 0, the procedure will return the first sample point. The procedure will return 0 if given the final sample point ID as an argument or the image has no sample points.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Michael Natterer <mitch@gimp.org>",
                                         "Michael Natterer",
                                         "2016");
  pika_procedure_add_argument (procedure,
                               pika_param_spec_image ("image",
                                                      "image",
                                                      "The image",
                                                      FALSE,
                                                      PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_uint ("sample-point",
                                                  "sample point",
                                                  "The ID of the current sample point (0 if first invocation)",
                                                  1, G_MAXUINT32, 1,
                                                  PIKA_PARAM_READWRITE | PIKA_PARAM_NO_VALIDATE));
  pika_procedure_add_return_value (procedure,
                                   g_param_spec_uint ("next-sample-point",
                                                      "next sample point",
                                                      "The next sample point's ID",
                                                      1, G_MAXUINT32, 1,
                                                      PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);

  /*
   * pika-image-get-sample-point-position
   */
  procedure = pika_procedure_new (image_get_sample_point_position_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-image-get-sample-point-position");
  pika_procedure_set_static_help (procedure,
                                  "Get position of a sample point on an image.",
                                  "This procedure takes an image and a sample point ID as input and returns the position of the sample point relative to the top and left of the image.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Michael Natterer <mitch@gimp.org>",
                                         "Michael Natterer",
                                         "2016");
  pika_procedure_add_argument (procedure,
                               pika_param_spec_image ("image",
                                                      "image",
                                                      "The image",
                                                      FALSE,
                                                      PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_uint ("sample-point",
                                                  "sample point",
                                                  "The guide",
                                                  1, G_MAXUINT32, 1,
                                                  PIKA_PARAM_READWRITE));
  pika_procedure_add_return_value (procedure,
                                   g_param_spec_int ("position-x",
                                                     "position x",
                                                     "The sample point's x-offset relative to left of image",
                                                     G_MININT32, G_MAXINT32, 0,
                                                     PIKA_PARAM_READWRITE));
  pika_procedure_add_return_value (procedure,
                                   g_param_spec_int ("position-y",
                                                     "position y",
                                                     "The sample point's y-offset relative to top of image",
                                                     G_MININT32, G_MAXINT32, 0,
                                                     PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);
}
