/* 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
 *
 * pikabrushselect.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 "gegl/pika-babl-compat.h"

#include "core/pika.h"
#include "core/pikacontext.h"
#include "core/pikabrush.h"
#include "core/pikaparamspecs.h"
#include "core/pikatempbuf.h"

#include "pdb/pikapdb.h"

#include "pikabrushfactoryview.h"
#include "pikabrushselect.h"
#include "pikacontainerbox.h"
#include "pikalayermodebox.h"

#include "pika-intl.h"


enum
{
  PROP_0,
  PROP_OPACITY,
  PROP_PAINT_MODE,
  PROP_SPACING
};


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

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

static void          pika_brush_select_opacity_changed (PikaContext     *context,
                                                        gdouble          opacity,
                                                        PikaBrushSelect *select);
static void          pika_brush_select_mode_changed    (PikaContext     *context,
                                                        PikaLayerMode    paint_mode,
                                                        PikaBrushSelect *select);

static void          pika_brush_select_opacity_update  (GtkAdjustment   *adj,
                                                        PikaBrushSelect *select);
static void          pika_brush_select_spacing_update  (PikaBrushFactoryView *view,
                                                        PikaBrushSelect      *select);


G_DEFINE_TYPE (PikaBrushSelect, pika_brush_select, PIKA_TYPE_PDB_DIALOG)

#define parent_class pika_brush_select_parent_class


static void
pika_brush_select_class_init (PikaBrushSelectClass *klass)
{
  GObjectClass       *object_class = G_OBJECT_CLASS (klass);
  PikaPdbDialogClass *pdb_class    = PIKA_PDB_DIALOG_CLASS (klass);

  object_class->constructed  = pika_brush_select_constructed;
  object_class->set_property = pika_brush_select_set_property;

  pdb_class->run_callback    = pika_brush_select_run_callback;

  g_object_class_install_property (object_class, PROP_OPACITY,
                                   g_param_spec_double ("opacity", NULL, NULL,
                                                        PIKA_OPACITY_TRANSPARENT,
                                                        PIKA_OPACITY_OPAQUE,
                                                        PIKA_OPACITY_OPAQUE,
                                                        PIKA_PARAM_WRITABLE |
                                                        G_PARAM_CONSTRUCT));

  g_object_class_install_property (object_class, PROP_PAINT_MODE,
                                   g_param_spec_enum ("paint-mode", NULL, NULL,
                                                      PIKA_TYPE_LAYER_MODE,
                                                      PIKA_LAYER_MODE_NORMAL,
                                                      PIKA_PARAM_WRITABLE |
                                                      G_PARAM_CONSTRUCT));

  g_object_class_install_property (object_class, PROP_SPACING,
                                   g_param_spec_int ("spacing", NULL, NULL,
                                                     -G_MAXINT, 1000, -1,
                                                     PIKA_PARAM_WRITABLE |
                                                     G_PARAM_CONSTRUCT));
}

static void
pika_brush_select_init (PikaBrushSelect *select)
{
}

static void
pika_brush_select_constructed (GObject *object)
{
  PikaPdbDialog   *dialog = PIKA_PDB_DIALOG (object);
  PikaBrushSelect *select = PIKA_BRUSH_SELECT (object);
  GtkWidget       *content_area;
  GtkWidget       *vbox;
  GtkWidget       *scale;
  GtkWidget       *hbox;
  GtkWidget       *label;
  GtkAdjustment   *spacing_adj;

  G_OBJECT_CLASS (parent_class)->constructed (object);

  pika_context_set_opacity    (dialog->context, select->initial_opacity);
  pika_context_set_paint_mode (dialog->context, select->initial_mode);

  g_signal_connect (dialog->context, "opacity-changed",
                    G_CALLBACK (pika_brush_select_opacity_changed),
                    dialog);
  g_signal_connect (dialog->context, "paint-mode-changed",
                    G_CALLBACK (pika_brush_select_mode_changed),
                    dialog);

  dialog->view =
    pika_brush_factory_view_new (PIKA_VIEW_TYPE_GRID,
                                 dialog->context->pika->brush_factory,
                                 dialog->context,
                                 FALSE,
                                 PIKA_VIEW_SIZE_MEDIUM, 1,
                                 dialog->menu_factory);

  pika_container_box_set_size_request (PIKA_CONTAINER_BOX (PIKA_CONTAINER_EDITOR (dialog->view)->view),
                                       5 * (PIKA_VIEW_SIZE_MEDIUM + 2),
                                       5 * (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);

  vbox = GTK_WIDGET (PIKA_CONTAINER_EDITOR (dialog->view)->view);

  /*  Create the opacity scale widget  */
  select->opacity_data =
    gtk_adjustment_new (pika_context_get_opacity (dialog->context) * 100.0,
                        0.0, 100.0,
                        1.0, 10.0, 0.0);

  scale = pika_spin_scale_new (select->opacity_data,
                               _("Opacity"), 1);
  pika_spin_scale_set_constrain_drag (PIKA_SPIN_SCALE (scale), TRUE);
  gtk_box_pack_end (GTK_BOX (vbox), scale, FALSE, FALSE, 0);
  gtk_widget_show (scale);

  g_signal_connect (select->opacity_data, "value-changed",
                    G_CALLBACK (pika_brush_select_opacity_update),
                    select);

  /*  Create the paint mode option menu  */
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
  gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);

  label = gtk_label_new (_("Mode:"));
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

  select->layer_mode_box = pika_layer_mode_box_new (PIKA_LAYER_MODE_CONTEXT_PAINT);
  gtk_box_pack_start (GTK_BOX (hbox), select->layer_mode_box, TRUE, TRUE, 0);
  gtk_widget_show (select->layer_mode_box);

  g_object_bind_property (G_OBJECT (dialog->context),        "paint-mode",
                          G_OBJECT (select->layer_mode_box), "layer-mode",
                          G_BINDING_BIDIRECTIONAL |
                          G_BINDING_SYNC_CREATE);

  spacing_adj = PIKA_BRUSH_FACTORY_VIEW (dialog->view)->spacing_adjustment;

  /*  Use passed spacing instead of brushes default  */
  if (select->spacing >= 0)
    gtk_adjustment_set_value (spacing_adj, select->spacing);

  g_signal_connect (dialog->view, "spacing-changed",
                    G_CALLBACK (pika_brush_select_spacing_update),
                    select);
}

static void
pika_brush_select_set_property (GObject      *object,
                                guint         property_id,
                                const GValue *value,
                                GParamSpec   *pspec)
{
  PikaPdbDialog   *dialog = PIKA_PDB_DIALOG (object);
  PikaBrushSelect *select = PIKA_BRUSH_SELECT (object);

  switch (property_id)
    {
    case PROP_OPACITY:
      if (dialog->view)
        pika_context_set_opacity (dialog->context, g_value_get_double (value));
      else
        select->initial_opacity = g_value_get_double (value);
      break;
    case PROP_PAINT_MODE:
      if (dialog->view)
        pika_context_set_paint_mode (dialog->context, g_value_get_enum (value));
      else
        select->initial_mode = g_value_get_enum (value);
      break;
    case PROP_SPACING:
      if (dialog->view)
        {
          if (g_value_get_int (value) >= 0)
            gtk_adjustment_set_value (PIKA_BRUSH_FACTORY_VIEW (dialog->view)->spacing_adjustment,
                                      g_value_get_int (value));
        }
      else
        {
          select->spacing = g_value_get_int (value);
        }
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static PikaValueArray *
pika_brush_select_run_callback (PikaPdbDialog  *dialog,
                                PikaObject     *object,
                                gboolean        closing,
                                GError        **error)
{
  PikaBrush      *brush = PIKA_BRUSH (object);
  PikaTempBuf    *mask  = pika_brush_get_mask (brush);
  const Babl     *format;
  gpointer        data;
  GBytes         *bytes;
  PikaValueArray *return_vals;

  format = pika_babl_compat_u8_mask_format (pika_temp_buf_get_format (mask));
  data   = pika_temp_buf_lock (mask, format, GEGL_ACCESS_READ);

  bytes = g_bytes_new_static (data,
                              pika_temp_buf_get_width         (mask) *
                              pika_temp_buf_get_height        (mask) *
                              babl_format_get_bytes_per_pixel (format));

  return_vals =
    pika_pdb_execute_procedure_by_name (dialog->pdb,
                                        dialog->caller_context,
                                        NULL, error,
                                        dialog->callback_name,
                                        PIKA_TYPE_RESOURCE,    object,
                                        G_TYPE_DOUBLE,         pika_context_get_opacity (dialog->context) * 100.0,
                                        G_TYPE_INT,            PIKA_BRUSH_SELECT (dialog)->spacing,
                                        PIKA_TYPE_LAYER_MODE,  pika_context_get_paint_mode (dialog->context),
                                        G_TYPE_INT,            pika_brush_get_width  (brush),
                                        G_TYPE_INT,            pika_brush_get_height (brush),
                                        G_TYPE_BYTES,          bytes,
                                        G_TYPE_BOOLEAN,        closing,
                                        G_TYPE_NONE);

  g_bytes_unref (bytes);

  pika_temp_buf_unlock (mask, data);

  return return_vals;
}

static void
pika_brush_select_opacity_changed (PikaContext     *context,
                                   gdouble          opacity,
                                   PikaBrushSelect *select)
{
  g_signal_handlers_block_by_func (select->opacity_data,
                                   pika_brush_select_opacity_update,
                                   select);

  gtk_adjustment_set_value (select->opacity_data, opacity * 100.0);

  g_signal_handlers_unblock_by_func (select->opacity_data,
                                     pika_brush_select_opacity_update,
                                     select);

  pika_pdb_dialog_run_callback ((PikaPdbDialog **) &select, FALSE);
}

static void
pika_brush_select_mode_changed (PikaContext     *context,
                                PikaLayerMode    paint_mode,
                                PikaBrushSelect *select)
{
  pika_pdb_dialog_run_callback ((PikaPdbDialog **) &select, FALSE);
}

static void
pika_brush_select_opacity_update (GtkAdjustment   *adjustment,
                                  PikaBrushSelect *select)
{
  pika_context_set_opacity (PIKA_PDB_DIALOG (select)->context,
                            gtk_adjustment_get_value (adjustment) / 100.0);
}

static void
pika_brush_select_spacing_update (PikaBrushFactoryView *view,
                                  PikaBrushSelect      *select)
{
  gdouble value = gtk_adjustment_get_value (view->spacing_adjustment);

  if (select->spacing != value)
    {
      select->spacing = value;

      pika_pdb_dialog_run_callback ((PikaPdbDialog **) &select, FALSE);
    }
}
