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

#include "core-types.h"

#include "operations/layer-modes/pika-layer-modes.h"

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

#include "pikachannel.h"
#include "pikadrawable.h"
#include "pikadrawable-edit.h"
#include "pikadrawablefilter.h"
#include "pikacontext.h"
#include "pikafilloptions.h"
#include "pikaimage.h"
#include "pikapattern.h"
#include "pikatempbuf.h"

#include "pika-intl.h"


/*  local function prototypes  */

static gboolean   pika_drawable_edit_can_fill_direct (PikaDrawable    *drawable,
                                                      PikaFillOptions *options);
static void       pika_drawable_edit_fill_direct     (PikaDrawable    *drawable,
                                                      PikaFillOptions *options,
                                                      const gchar     *undo_desc);


/*  private functions  */

static gboolean
pika_drawable_edit_can_fill_direct (PikaDrawable    *drawable,
                                    PikaFillOptions *options)
{
  PikaImage                *image;
  PikaContext              *context;
  gdouble                   opacity;
  PikaComponentMask         affect;
  PikaLayerMode             mode;
  PikaLayerCompositeMode    composite_mode;
  PikaLayerCompositeRegion  composite_region;

  image            = pika_item_get_image (PIKA_ITEM (drawable));
  context          = PIKA_CONTEXT (options);
  opacity          = pika_context_get_opacity (context);
  affect           = pika_drawable_get_active_mask (drawable);
  mode             = pika_context_get_paint_mode (context);
  composite_mode   = pika_layer_mode_get_paint_composite_mode (mode);
  composite_region = pika_layer_mode_get_included_region (mode, composite_mode);

  if (pika_channel_is_empty (pika_image_get_mask (image)) &&
      opacity == PIKA_OPACITY_OPAQUE                      &&
      affect  == PIKA_COMPONENT_MASK_ALL                  &&
      pika_layer_mode_is_trivial (mode)                   &&
      (! pika_layer_mode_is_subtractive (mode)            ^
       ! (composite_region & PIKA_LAYER_COMPOSITE_REGION_SOURCE)))
    {
      switch (pika_fill_options_get_style (options))
        {
        case PIKA_FILL_STYLE_FG_COLOR:
        case PIKA_FILL_STYLE_BG_COLOR:
          return TRUE;

        case PIKA_FILL_STYLE_PATTERN:
          {
            PikaPattern *pattern;
            PikaTempBuf *mask;
            const Babl  *format;

            pattern = pika_context_get_pattern (context);
            mask    = pika_pattern_get_mask (pattern);
            format  = pika_temp_buf_get_format (mask);

            return ! babl_format_has_alpha (format);
          }
        }
    }

  return FALSE;
}

static void
pika_drawable_edit_fill_direct (PikaDrawable    *drawable,
                                PikaFillOptions *options,
                                const gchar     *undo_desc)
{
  GeglBuffer    *buffer;
  PikaContext   *context;
  PikaLayerMode  mode;
  gint           width;
  gint           height;

  buffer  = pika_drawable_get_buffer (drawable);
  context = PIKA_CONTEXT (options);
  mode    = pika_context_get_paint_mode (context);
  width   = pika_item_get_width  (PIKA_ITEM (drawable));
  height  = pika_item_get_height (PIKA_ITEM (drawable));

  pika_drawable_push_undo (drawable, undo_desc,
                           NULL, 0, 0, width, height);

  if (! pika_layer_mode_is_subtractive (mode))
    pika_fill_options_fill_buffer (options, drawable, buffer, 0, 0);
  else
    pika_gegl_clear (buffer, NULL);
}


/*  public functions  */

void
pika_drawable_edit_clear (PikaDrawable *drawable,
                          PikaContext  *context)
{
  PikaFillOptions *options;

  g_return_if_fail (PIKA_IS_DRAWABLE (drawable));
  g_return_if_fail (pika_item_is_attached (PIKA_ITEM (drawable)));
  g_return_if_fail (PIKA_IS_CONTEXT (context));

  options = pika_fill_options_new (context->pika, NULL, FALSE);

  if (pika_drawable_has_alpha (drawable))
    pika_fill_options_set_by_fill_type (options, context,
                                        PIKA_FILL_TRANSPARENT, NULL);
  else
    pika_fill_options_set_by_fill_type (options, context,
                                        PIKA_FILL_BACKGROUND, NULL);

  pika_drawable_edit_fill (drawable, options, C_("undo-type", "Clear"));

  g_object_unref (options);
}

void
pika_drawable_edit_fill (PikaDrawable    *drawable,
                         PikaFillOptions *options,
                         const gchar     *undo_desc)
{
  PikaContext *context;
  gint         x, y, width, height;

  g_return_if_fail (PIKA_IS_DRAWABLE (drawable));
  g_return_if_fail (pika_item_is_attached (PIKA_ITEM (drawable)));
  g_return_if_fail (PIKA_IS_FILL_OPTIONS (options));

  if (! pika_item_mask_intersect (PIKA_ITEM (drawable),
                                  &x, &y, &width, &height))
    {
      return;  /*  nothing to do, but the fill succeeded  */
    }

  context = PIKA_CONTEXT (options);

  if (pika_layer_mode_is_alpha_only (pika_context_get_paint_mode (context)))
    {
      if (! pika_drawable_has_alpha (drawable) ||
          ! (pika_drawable_get_active_mask (drawable) &
             PIKA_COMPONENT_MASK_ALPHA))
        {
          return;  /*  nothing to do, but the fill succeeded  */
        }
    }

  if (! undo_desc)
    undo_desc = pika_fill_options_get_undo_desc (options);

  /* check if we can fill the drawable's buffer directly */
  if (pika_drawable_edit_can_fill_direct (drawable, options))
    {
      pika_drawable_edit_fill_direct (drawable, options, undo_desc);

      pika_drawable_update (drawable, x, y, width, height);
    }
  else
    {
      GeglNode               *operation;
      PikaDrawableFilter     *filter;
      gdouble                 opacity;
      PikaLayerMode           mode;
      PikaLayerCompositeMode  composite_mode;

      opacity        = pika_context_get_opacity (context);
      mode           = pika_context_get_paint_mode (context);
      composite_mode = pika_layer_mode_get_paint_composite_mode (mode);

      operation = gegl_node_new_child (NULL,
                                       "operation",        "pika:fill-source",
                                       "options",          options,
                                       "drawable",         drawable,
                                       "pattern-offset-x", -x,
                                       "pattern-offset-y", -y,
                                       NULL);

      filter = pika_drawable_filter_new (drawable, undo_desc, operation, NULL);

      pika_drawable_filter_set_opacity (filter, opacity);
      pika_drawable_filter_set_mode    (filter,
                                        mode,
                                        PIKA_LAYER_COLOR_SPACE_AUTO,
                                        PIKA_LAYER_COLOR_SPACE_AUTO,
                                        composite_mode);

      pika_drawable_filter_apply  (filter, NULL);
      pika_drawable_filter_commit (filter, NULL, FALSE);

      g_object_unref (filter);
      g_object_unref (operation);
    }
}
