/* 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 "libpikamath/pikamath.h"
#include "libpikawidgets/pikawidgets.h"

#include "tools-types.h"

#include "paint/pikamybrushoptions.h"

#include "display/pikadisplay.h"
#include "display/pikadisplayshell.h"
#include "display/pikacanvasarc.h"

#include "core/pika.h"

#include "widgets/pikahelp-ids.h"

#include "pikamybrushoptions-gui.h"
#include "pikamybrushtool.h"
#include "pikatoolcontrol.h"
#include "core/pikamybrush.h"

#include "pika-intl.h"

G_DEFINE_TYPE (PikaMybrushTool, pika_mybrush_tool, PIKA_TYPE_PAINT_TOOL)

#define parent_class pika_mybrush_tool_parent_class


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

static PikaCanvasItem * pika_mybrush_tool_get_outline (PikaPaintTool *paint_tool,
                                                       PikaDisplay   *display,
                                                       gdouble        x,
                                                       gdouble        y);


void
pika_mybrush_tool_register (PikaToolRegisterCallback  callback,
                            gpointer                  data)
{
  (* callback) (PIKA_TYPE_MYBRUSH_TOOL,
                PIKA_TYPE_MYBRUSH_OPTIONS,
                pika_mybrush_options_gui,
                PIKA_CONTEXT_PROP_MASK_FOREGROUND |
                PIKA_CONTEXT_PROP_MASK_BACKGROUND |
                PIKA_CONTEXT_PROP_MASK_OPACITY    |
                PIKA_CONTEXT_PROP_MASK_PAINT_MODE |
                PIKA_CONTEXT_PROP_MASK_MYBRUSH,
                "pika-mypaint-brush-tool",
                _("MyPaint Brush"),
                _("MyPaint Brush Tool: Use MyPaint brushes in PIKA"),
                N_("M_yPaint Brush"), "Y",
                NULL, PIKA_HELP_TOOL_MYPAINT_BRUSH,
                PIKA_ICON_TOOL_MYPAINT_BRUSH,
                data);
}

static void
pika_mybrush_tool_class_init (PikaMybrushToolClass *klass)
{
  PikaToolClass      *tool_class       = PIKA_TOOL_CLASS (klass);
  PikaPaintToolClass *paint_tool_class = PIKA_PAINT_TOOL_CLASS (klass);

  tool_class->options_notify    = pika_mybrush_tool_options_notify;

  paint_tool_class->get_outline = pika_mybrush_tool_get_outline;
}

static void
pika_mybrush_tool_init (PikaMybrushTool *mybrush_tool)
{
  PikaTool *tool = PIKA_TOOL (mybrush_tool);

  pika_tool_control_set_tool_cursor       (tool->control, PIKA_TOOL_CURSOR_INK);
  pika_tool_control_set_action_size       (tool->control,
                                           "tools-mypaint-brush-radius-set");
  pika_tool_control_set_action_pixel_size (tool->control,
                                           "tools-mypaint-brush-pixel-size-set");
  pika_tool_control_set_action_hardness   (tool->control,
                                           "tools-mypaint-brush-hardness-set");

  pika_paint_tool_enable_color_picker (PIKA_PAINT_TOOL (mybrush_tool),
                                       PIKA_COLOR_PICK_TARGET_FOREGROUND);
}

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

  if (! strcmp (pspec->name, "radius"))
    {
      pika_draw_tool_pause (PIKA_DRAW_TOOL (tool));
      pika_draw_tool_resume (PIKA_DRAW_TOOL (tool));
    }
}

static PikaCanvasItem *
pika_mybrush_tool_get_outline (PikaPaintTool *paint_tool,
                               PikaDisplay   *display,
                               gdouble        x,
                               gdouble        y)
{
  PikaMybrushOptions *options = PIKA_MYBRUSH_TOOL_GET_OPTIONS (paint_tool);
  PikaCanvasItem     *item    = NULL;
  PikaDisplayShell   *shell   = pika_display_get_shell (display);
  gdouble             radius;

  /* Radius size as used by libmypaint is logarithmic.
   * The drawn outline in the MyBrush tool is more of a general idea of
   * the brush size. With each brush having its own logic, actual drawn
   * dabs may be quite different, smaller but also bigger. And there are
   * some random elements in there too.
   * See also mypaint-brush.c code in libmypaint.
   */
  radius = exp (options->radius);
  radius = MAX (MAX (4 / shell->scale_x, 4 / shell->scale_y), radius);

  item = pika_mybrush_tool_create_cursor (paint_tool, display, x, y, radius);

  if (! item)
    {
      pika_paint_tool_set_draw_fallback (paint_tool,
                                         TRUE, radius);
    }

  return item;
}

PikaCanvasItem *
pika_mybrush_tool_create_cursor (PikaPaintTool *paint_tool,
                                 PikaDisplay   *display,
                                 gdouble        x,
                                 gdouble        y,
                                 gdouble        radius)
{

  PikaDisplayShell     *shell;

  g_return_val_if_fail (PIKA_IS_PAINT_TOOL (paint_tool), NULL);
  g_return_val_if_fail (PIKA_IS_DISPLAY (display), NULL);

  shell = pika_display_get_shell (display);

  /*  don't draw the boundary if it becomes too small  */
  if (SCALEX (shell, radius) > 4 &&
      SCALEY (shell, radius) > 4)
    {
      return pika_canvas_arc_new(shell, x, y, radius, radius, 0.0, 2 * G_PI, FALSE);
    }

  return NULL;
}
