/* 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
 *
 * 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 "libpikawidgets/pikawidgets.h"

#include "tools-types.h"

#include "paint/pikaconvolveoptions.h"

#include "widgets/pikahelp-ids.h"
#include "widgets/pikapropwidgets.h"
#include "widgets/pikawidgets-utils.h"

#include "pikaconvolvetool.h"
#include "pikapaintoptions-gui.h"
#include "pikatoolcontrol.h"

#include "pika-intl.h"


static void   pika_convolve_tool_modifier_key  (PikaTool         *tool,
                                                GdkModifierType   key,
                                                gboolean          press,
                                                GdkModifierType   state,
                                                PikaDisplay      *display);
static void   pika_convolve_tool_cursor_update (PikaTool         *tool,
                                                const PikaCoords *coords,
                                                GdkModifierType   state,
                                                PikaDisplay      *display);
static void   pika_convolve_tool_oper_update   (PikaTool         *tool,
                                                const PikaCoords *coords,
                                                GdkModifierType   state,
                                                gboolean          proximity,
                                                PikaDisplay      *display);
static void   pika_convolve_tool_status_update (PikaTool         *tool,
                                                PikaConvolveType  type);

static GtkWidget * pika_convolve_options_gui   (PikaToolOptions  *options);


G_DEFINE_TYPE (PikaConvolveTool, pika_convolve_tool, PIKA_TYPE_BRUSH_TOOL)

#define parent_class pika_convolve_tool_parent_class


void
pika_convolve_tool_register (PikaToolRegisterCallback  callback,
                             gpointer                  data)
{
  (* callback) (PIKA_TYPE_CONVOLVE_TOOL,
                PIKA_TYPE_CONVOLVE_OPTIONS,
                pika_convolve_options_gui,
                PIKA_PAINT_OPTIONS_CONTEXT_MASK,
                "pika-convolve-tool",
                _("Blur / Sharpen"),
                _("Blur / Sharpen Tool: Selective blurring or unblurring using a brush"),
                N_("Bl_ur / Sharpen"), "<shift>U",
                NULL, PIKA_HELP_TOOL_CONVOLVE,
                PIKA_ICON_TOOL_BLUR,
                data);
}

static void
pika_convolve_tool_class_init (PikaConvolveToolClass *klass)
{
  PikaToolClass *tool_class = PIKA_TOOL_CLASS (klass);

  tool_class->modifier_key  = pika_convolve_tool_modifier_key;
  tool_class->cursor_update = pika_convolve_tool_cursor_update;
  tool_class->oper_update   = pika_convolve_tool_oper_update;
}

static void
pika_convolve_tool_init (PikaConvolveTool *convolve)
{
  PikaTool *tool = PIKA_TOOL (convolve);

  pika_tool_control_set_tool_cursor            (tool->control,
                                                PIKA_TOOL_CURSOR_BLUR);
  pika_tool_control_set_toggle_cursor_modifier (tool->control,
                                                PIKA_CURSOR_MODIFIER_MINUS);

  pika_convolve_tool_status_update (tool, PIKA_CONVOLVE_BLUR);
}

static void
pika_convolve_tool_modifier_key (PikaTool        *tool,
                                 GdkModifierType  key,
                                 gboolean         press,
                                 GdkModifierType  state,
                                 PikaDisplay     *display)
{
  PikaConvolveTool    *convolve    = PIKA_CONVOLVE_TOOL (tool);
  PikaConvolveOptions *options     = PIKA_CONVOLVE_TOOL_GET_OPTIONS (tool);
  GdkModifierType      line_mask   = PIKA_PAINT_TOOL_LINE_MASK;
  GdkModifierType      toggle_mask = pika_get_toggle_behavior_mask ();

  if (((key == toggle_mask)  &&
       ! (state & line_mask) && /* leave stuff untouched in line draw mode */
       press != convolve->toggled)

      ||

      (key == line_mask  && /* toggle back after keypresses CTRL(hold)->  */
       ! press           && /* SHIFT(hold)->CTRL(release)->SHIFT(release) */
       convolve->toggled &&
       ! (state & toggle_mask)))
    {
      convolve->toggled = press;

      switch (options->type)
        {
        case PIKA_CONVOLVE_BLUR:
          g_object_set (options, "type", PIKA_CONVOLVE_SHARPEN, NULL);
          break;

        case PIKA_CONVOLVE_SHARPEN:
          g_object_set (options, "type", PIKA_CONVOLVE_BLUR, NULL);
          break;
        }
    }
}

static void
pika_convolve_tool_cursor_update (PikaTool         *tool,
                                  const PikaCoords *coords,
                                  GdkModifierType   state,
                                  PikaDisplay      *display)
{
  PikaConvolveOptions *options = PIKA_CONVOLVE_TOOL_GET_OPTIONS (tool);

  pika_tool_control_set_toggled (tool->control,
                                 options->type == PIKA_CONVOLVE_SHARPEN);

  PIKA_TOOL_CLASS (parent_class)->cursor_update (tool, coords, state, display);
}

static void
pika_convolve_tool_oper_update (PikaTool         *tool,
                                const PikaCoords *coords,
                                GdkModifierType   state,
                                gboolean          proximity,
                                PikaDisplay      *display)
{
  PikaConvolveOptions *options = PIKA_CONVOLVE_TOOL_GET_OPTIONS (tool);

  pika_convolve_tool_status_update (tool, options->type);

  PIKA_TOOL_CLASS (parent_class)->oper_update (tool, coords, state, proximity,
                                               display);
}

static void
pika_convolve_tool_status_update (PikaTool         *tool,
                                  PikaConvolveType  type)
{
  PikaPaintTool *paint_tool = PIKA_PAINT_TOOL (tool);

  switch (type)
    {
    case PIKA_CONVOLVE_BLUR:
      paint_tool->status      = _("Click to blur");
      paint_tool->status_line = _("Click to blur the line");
      paint_tool->status_ctrl = _("%s to sharpen");
      break;

    case PIKA_CONVOLVE_SHARPEN:
      paint_tool->status      = _("Click to sharpen");
      paint_tool->status_line = _("Click to sharpen the line");
      paint_tool->status_ctrl = _("%s to blur");
      break;

    default:
      break;
    }
}


/*  tool options stuff  */

static GtkWidget *
pika_convolve_options_gui (PikaToolOptions *tool_options)
{
  GObject         *config = G_OBJECT (tool_options);
  GtkWidget       *vbox   = pika_paint_options_gui (tool_options);
  GtkWidget       *frame;
  GtkWidget       *scale;
  gchar           *str;
  GdkModifierType  toggle_mask;

  toggle_mask = pika_get_toggle_behavior_mask ();

  /*  the type radio box  */
  str = g_strdup_printf (_("Convolve Type  (%s)"),
                         pika_get_mod_string (toggle_mask));

  frame = pika_prop_enum_radio_frame_new (config, "type",
                                          str, 0, 0);
  gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);

  g_free (str);

  /*  the rate scale  */
  scale = pika_prop_spin_scale_new (config, "rate",
                                    1.0, 10.0, 1);
  gtk_box_pack_start (GTK_BOX (vbox), scale, FALSE, FALSE, 0);

  return vbox;
}
