/* 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
 *
 * pikagradientselect.c
 * Copyright (C) 2004 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 <gegl.h>
#include <gtk/gtk.h>

#include "libpikabase/pikabase.h"
#include "libpikawidgets/pikawidgets.h"

#include "widgets-types.h"

#include "core/pika.h"
#include "core/pikacontext.h"
#include "core/pikagradient.h"
#include "core/pikaparamspecs.h"

#include "pdb/pikapdb.h"

#include "pikacontainerbox.h"
#include "pikadatafactoryview.h"
#include "pikagradientselect.h"


enum
{
  PROP_0,
  PROP_SAMPLE_SIZE
};


static void             pika_gradient_select_constructed  (GObject        *object);
static void             pika_gradient_select_set_property (GObject        *object,
                                                           guint           property_id,
                                                           const GValue   *value,
                                                           GParamSpec     *pspec);

static PikaValueArray * pika_gradient_select_run_callback (PikaPdbDialog  *dialog,
                                                           PikaObject     *object,
                                                           gboolean        closing,
                                                           GError        **error);


G_DEFINE_TYPE (PikaGradientSelect, pika_gradient_select,
               PIKA_TYPE_PDB_DIALOG)

#define parent_class pika_gradient_select_parent_class


static void
pika_gradient_select_class_init (PikaGradientSelectClass *klass)
{
  GObjectClass       *object_class = G_OBJECT_CLASS (klass);
  PikaPdbDialogClass *pdb_class    = PIKA_PDB_DIALOG_CLASS (klass);

  object_class->constructed  = pika_gradient_select_constructed;
  object_class->set_property = pika_gradient_select_set_property;

  pdb_class->run_callback    = pika_gradient_select_run_callback;

  g_object_class_install_property (object_class, PROP_SAMPLE_SIZE,
                                   g_param_spec_int ("sample-size", NULL, NULL,
                                                     0, 10000, 84,
                                                     PIKA_PARAM_WRITABLE |
                                                     G_PARAM_CONSTRUCT_ONLY));
}

static void
pika_gradient_select_init (PikaGradientSelect *select)
{
}

static void
pika_gradient_select_constructed (GObject *object)
{
  PikaPdbDialog *dialog = PIKA_PDB_DIALOG (object);
  GtkWidget     *content_area;

  G_OBJECT_CLASS (parent_class)->constructed (object);

  dialog->view =
    pika_data_factory_view_new (PIKA_VIEW_TYPE_LIST,
                                dialog->context->pika->gradient_factory,
                                dialog->context,
                                PIKA_VIEW_SIZE_MEDIUM, 1,
                                dialog->menu_factory, "<Gradients>",
                                "/gradients-popup",
                                "gradients");

  pika_container_box_set_size_request (PIKA_CONTAINER_BOX (PIKA_CONTAINER_EDITOR (dialog->view)->view),
                                       6 * (PIKA_VIEW_SIZE_MEDIUM + 2),
                                       6 * (PIKA_VIEW_SIZE_MEDIUM + 2));

  gtk_container_set_border_width (GTK_CONTAINER (dialog->view), 12);

  content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
  gtk_box_pack_start (GTK_BOX (content_area), dialog->view, TRUE, TRUE, 0);
  gtk_widget_show (dialog->view);
}

static void
pika_gradient_select_set_property (GObject      *object,
                                   guint         property_id,
                                   const GValue *value,
                                   GParamSpec   *pspec)
{
  PikaGradientSelect *select = PIKA_GRADIENT_SELECT (object);

  switch (property_id)
    {
    case PROP_SAMPLE_SIZE:
      select->sample_size = g_value_get_int (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static PikaValueArray *
pika_gradient_select_run_callback (PikaPdbDialog  *dialog,
                                   PikaObject     *object,
                                   gboolean        closing,
                                   GError        **error)
{
  PikaGradient        *gradient = PIKA_GRADIENT (object);
  PikaGradientSegment *seg      = NULL;
  gdouble             *values, *pv;
  gdouble              pos, delta;
  PikaRGB              color;
  gint                 i;
  PikaArray           *array;
  PikaValueArray      *return_vals;

  i      = PIKA_GRADIENT_SELECT (dialog)->sample_size;
  pos    = 0.0;
  delta  = 1.0 / (i - 1);

  values = g_new (gdouble, 4 * i);
  pv     = values;

  while (i--)
    {
      seg = pika_gradient_get_color_at (gradient, dialog->caller_context,
                                        seg, pos, FALSE,
                                        PIKA_GRADIENT_BLEND_RGB_PERCEPTUAL,
                                        &color);

      *pv++ = color.r;
      *pv++ = color.g;
      *pv++ = color.b;
      *pv++ = color.a;

      pos += delta;
    }

  array = pika_array_new ((guint8 *) values,
                          PIKA_GRADIENT_SELECT (dialog)->sample_size * 4 *
                          sizeof (gdouble),
                          TRUE);
  array->static_data = FALSE;

  return_vals =
    pika_pdb_execute_procedure_by_name (dialog->pdb,
                                        dialog->caller_context,
                                        NULL, error,
                                        dialog->callback_name,
                                        PIKA_TYPE_RESOURCE,    object,
                                        G_TYPE_INT,            array->length / sizeof (gdouble),
                                        PIKA_TYPE_FLOAT_ARRAY, array->data,
                                        G_TYPE_BOOLEAN,        closing,
                                        G_TYPE_NONE);

  pika_array_free (array);

  return return_vals;
}
