/* 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 <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>

#include "paint-types.h"

#include "gegl/pika-gegl-utils.h"

#include "core/pika.h"
#include "core/pika-palettes.h"
#include "core/pikadrawable.h"
#include "core/pikadynamics.h"
#include "core/pikaimage.h"
#include "core/pikapickable.h"
#include "core/pikasymmetry.h"

#include "pikaeraser.h"
#include "pikaeraseroptions.h"

#include "pika-intl.h"


static gboolean   pika_eraser_get_color_history_color (PikaPaintbrush            *paintbrush,
                                                       PikaDrawable              *drawable,
                                                       PikaPaintOptions          *paint_options,
                                                       PikaRGB                   *color);
static void       pika_eraser_get_paint_params        (PikaPaintbrush            *paintbrush,
                                                       PikaDrawable              *drawable,
                                                       PikaPaintOptions          *paint_options,
                                                       PikaSymmetry              *sym,
                                                       gdouble                    grad_point,
                                                       PikaLayerMode             *paint_mode,
                                                       PikaPaintApplicationMode  *paint_appl_mode,
                                                       const PikaTempBuf        **paint_pixmap,
                                                       PikaRGB                   *paint_color);


G_DEFINE_TYPE (PikaEraser, pika_eraser, PIKA_TYPE_PAINTBRUSH)


void
pika_eraser_register (Pika                      *pika,
                      PikaPaintRegisterCallback  callback)
{
  (* callback) (pika,
                PIKA_TYPE_ERASER,
                PIKA_TYPE_ERASER_OPTIONS,
                "pika-eraser",
                _("Eraser"),
                "pika-tool-eraser");
}

static void
pika_eraser_class_init (PikaEraserClass *klass)
{
  PikaPaintbrushClass *paintbrush_class = PIKA_PAINTBRUSH_CLASS (klass);

  paintbrush_class->get_color_history_color = pika_eraser_get_color_history_color;
  paintbrush_class->get_paint_params        = pika_eraser_get_paint_params;
}

static void
pika_eraser_init (PikaEraser *eraser)
{
}

static gboolean
pika_eraser_get_color_history_color (PikaPaintbrush   *paintbrush,
                                     PikaDrawable     *drawable,
                                     PikaPaintOptions *paint_options,
                                     PikaRGB          *color)
{
  /* Erasing on a drawable without alpha is equivalent to
   * drawing with background color. So let's save history.
   */
  if (! pika_drawable_has_alpha (drawable))
    {
      PikaContext *context = PIKA_CONTEXT (paint_options);

      pika_context_get_background (context, color);

      return TRUE;
    }

  return FALSE;
}

static void
pika_eraser_get_paint_params (PikaPaintbrush            *paintbrush,
                              PikaDrawable              *drawable,
                              PikaPaintOptions          *paint_options,
                              PikaSymmetry              *sym,
                              gdouble                    grad_point,
                              PikaLayerMode             *paint_mode,
                              PikaPaintApplicationMode  *paint_appl_mode,
                              const PikaTempBuf        **paint_pixmap,
                              PikaRGB                   *paint_color)
{
  PikaEraserOptions *options = PIKA_ERASER_OPTIONS (paint_options);
  PikaContext       *context = PIKA_CONTEXT (paint_options);

  pika_context_get_background (context, paint_color);
  pika_pickable_srgb_to_image_color (PIKA_PICKABLE (drawable),
                                     paint_color, paint_color);

  if (options->anti_erase)
    *paint_mode = PIKA_LAYER_MODE_ANTI_ERASE;
  else if (pika_drawable_has_alpha (drawable))
    *paint_mode = PIKA_LAYER_MODE_ERASE;
  else
    *paint_mode = PIKA_LAYER_MODE_NORMAL_LEGACY;
}
