/* 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 "operations/layer-modes/pika-layer-modes.h"

#include "paint/pikainkoptions.h"

#include "widgets/pikahelp-ids.h"

#include "pikainkoptions-gui.h"
#include "pikainktool.h"
#include "pikatoolcontrol.h"

#include "pika-intl.h"


static PikaCanvasItem * pika_ink_tool_get_outline    (PikaPaintTool    *paint_tool,
                                                      PikaDisplay      *display,
                                                      gdouble           x,
                                                      gdouble           y);
static gboolean         pika_ink_tool_is_alpha_only  (PikaPaintTool    *paint_tool,
                                                      PikaDrawable     *drawable);

static void             pika_ink_tool_options_notify (PikaTool         *tool,
                                                      PikaToolOptions  *options,
                                                      const GParamSpec *pspec);


G_DEFINE_TYPE (PikaInkTool, pika_ink_tool, PIKA_TYPE_PAINT_TOOL)

#define parent_class pika_ink_tool_parent_class


void
pika_ink_tool_register (PikaToolRegisterCallback  callback,
                        gpointer                  data)
{
  (* callback) (PIKA_TYPE_INK_TOOL,
                PIKA_TYPE_INK_OPTIONS,
                pika_ink_options_gui,
                PIKA_CONTEXT_PROP_MASK_FOREGROUND |
                PIKA_CONTEXT_PROP_MASK_BACKGROUND |
                PIKA_CONTEXT_PROP_MASK_OPACITY    |
                PIKA_CONTEXT_PROP_MASK_PAINT_MODE,
                "pika-ink-tool",
                _("Ink"),
                _("Ink Tool: Calligraphy-style painting"),
                N_("In_k"), "K",
                NULL, PIKA_HELP_TOOL_INK,
                PIKA_ICON_TOOL_INK,
                data);
}

static void
pika_ink_tool_class_init (PikaInkToolClass *klass)
{
  PikaToolClass      *tool_class       = PIKA_TOOL_CLASS (klass);
  PikaPaintToolClass *paint_tool_class = PIKA_PAINT_TOOL_CLASS (klass);

  tool_class->options_notify    = pika_ink_tool_options_notify;

  paint_tool_class->get_outline   = pika_ink_tool_get_outline;
  paint_tool_class->is_alpha_only = pika_ink_tool_is_alpha_only;
}

static void
pika_ink_tool_init (PikaInkTool *ink_tool)
{
  PikaTool *tool = PIKA_TOOL (ink_tool);

  pika_tool_control_set_tool_cursor   (tool->control, PIKA_TOOL_CURSOR_INK);
  pika_tool_control_set_action_pixel_size (tool->control,
                                           "tools-ink-blob-pixel-size-set");
  pika_tool_control_set_action_size       (tool->control,
                                           "tools-ink-blob-size-set");
  pika_tool_control_set_action_aspect     (tool->control,
                                           "tools-ink-blob-aspect-set");
  pika_tool_control_set_action_angle      (tool->control,
                                           "tools-ink-blob-angle-set");

  pika_paint_tool_enable_color_picker (PIKA_PAINT_TOOL (ink_tool),
                                       PIKA_COLOR_PICK_TARGET_FOREGROUND);
}

static void
pika_ink_tool_options_notify (PikaTool         *tool,
                              PikaToolOptions  *options,
                              const GParamSpec *pspec)
{
  PIKA_TOOL_CLASS (parent_class)->options_notify (tool, options, pspec);

  if (g_strcmp0 (pspec->name, "size") == 0 &&
      PIKA_PAINT_TOOL (tool)->draw_brush)
    {
      /* This triggers a redraw of the tool pointer, especially useful
       * here when we change the pen size with on-canvas interaction.
       */
      pika_draw_tool_pause (PIKA_DRAW_TOOL (tool));
      pika_draw_tool_resume (PIKA_DRAW_TOOL (tool));
    }
}

static PikaCanvasItem *
pika_ink_tool_get_outline (PikaPaintTool *paint_tool,
                           PikaDisplay   *display,
                           gdouble        x,
                           gdouble        y)
{
  PikaInkOptions *options = PIKA_INK_TOOL_GET_OPTIONS (paint_tool);

  pika_paint_tool_set_draw_circle (paint_tool, TRUE,
                                   options->size);

  return NULL;
}

static gboolean
pika_ink_tool_is_alpha_only (PikaPaintTool *paint_tool,
                             PikaDrawable  *drawable)
{
  PikaPaintOptions *paint_options = PIKA_PAINT_TOOL_GET_OPTIONS (paint_tool);
  PikaContext      *context       = PIKA_CONTEXT (paint_options);
  PikaLayerMode     paint_mode    = pika_context_get_paint_mode (context);

  return pika_layer_mode_is_alpha_only (paint_mode);
}
