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

#include "libpikacolor/pikacolor.h"

#include "core-types.h"

#include "pika.h"
#include "pikachannel.h"
#include "pikaimage.h"
#include "pikaimage-private.h"
#include "pikaimage-quick-mask.h"
#include "pikaimage-undo.h"
#include "pikaimage-undo-push.h"
#include "pikalayer.h"
#include "pikalayer-floating-selection.h"

#include "pika-intl.h"


#define CHANNEL_WAS_ACTIVE (0x2)


/*  public functions  */

void
pika_image_set_quick_mask_state (PikaImage *image,
                                 gboolean   active)
{
  PikaImagePrivate *private;
  PikaChannel      *selection;
  PikaChannel      *mask;
  gboolean          channel_was_active;

  g_return_if_fail (PIKA_IS_IMAGE (image));

  if (active == pika_image_get_quick_mask_state (image))
    return;

  private = PIKA_IMAGE_GET_PRIVATE (image);

  /*  Keep track of the state so that we can make the right drawable
   *  active again when deactiviting quick mask (see bug #134371).
   */
  if (private->quick_mask_state)
    channel_was_active = (private->quick_mask_state & CHANNEL_WAS_ACTIVE) != 0;
  else
    channel_was_active = (pika_image_get_selected_channels (image) != NULL);

  /*  Set private->quick_mask_state early so we can return early when
   *  being called recursively.
   */
  private->quick_mask_state = (active
                               ? TRUE | (channel_was_active ?
                                         CHANNEL_WAS_ACTIVE : 0)
                               : FALSE);

  selection = pika_image_get_mask (image);
  mask      = pika_image_get_quick_mask (image);

  if (active)
    {
      if (! mask)
        {
          PikaLayer *floating_sel;

          pika_image_undo_group_start (image, PIKA_UNDO_GROUP_IMAGE_QUICK_MASK,
                                       C_("undo-type", "Enable Quick Mask"));

          floating_sel = pika_image_get_floating_selection (image);

          if (floating_sel)
            floating_sel_to_layer (floating_sel, NULL);

          mask = PIKA_CHANNEL (pika_item_duplicate (PIKA_ITEM (selection),
                                                    PIKA_TYPE_CHANNEL));

          if (! pika_channel_is_empty (selection))
            pika_channel_clear (selection, NULL, TRUE);

          pika_channel_set_color (mask, &private->quick_mask_color, FALSE);
          pika_item_rename (PIKA_ITEM (mask), PIKA_IMAGE_QUICK_MASK_NAME,
                            NULL);

          if (private->quick_mask_inverted)
            pika_channel_invert (mask, FALSE);

          pika_image_add_channel (image, mask, NULL, 0, TRUE);

          pika_image_undo_group_end (image);
        }
    }
  else
    {
      if (mask)
        {
          PikaLayer *floating_sel = pika_image_get_floating_selection (image);

          pika_image_undo_group_start (image, PIKA_UNDO_GROUP_IMAGE_QUICK_MASK,
                                       C_("undo-type", "Disable Quick Mask"));

          if (private->quick_mask_inverted)
            pika_channel_invert (mask, TRUE);

          if (floating_sel &&
              pika_layer_get_floating_sel_drawable (floating_sel) == PIKA_DRAWABLE (mask))
            floating_sel_anchor (floating_sel);

          pika_item_to_selection (PIKA_ITEM (mask),
                                  PIKA_CHANNEL_OP_REPLACE,
                                  TRUE, FALSE, 0.0, 0.0);
          pika_image_remove_channel (image, mask, TRUE, NULL);

          if (! channel_was_active)
            pika_image_unset_selected_channels (image);

          pika_image_undo_group_end (image);
        }
    }

  pika_image_quick_mask_changed (image);
}

gboolean
pika_image_get_quick_mask_state (PikaImage *image)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), FALSE);

  return PIKA_IMAGE_GET_PRIVATE (image)->quick_mask_state;
}

void
pika_image_set_quick_mask_color (PikaImage     *image,
                                 const PikaRGB *color)
{
  PikaChannel *quick_mask;

  g_return_if_fail (PIKA_IS_IMAGE (image));
  g_return_if_fail (color != NULL);

  PIKA_IMAGE_GET_PRIVATE (image)->quick_mask_color = *color;

  quick_mask = pika_image_get_quick_mask (image);
  if (quick_mask)
    pika_channel_set_color (quick_mask, color, TRUE);
}

void
pika_image_get_quick_mask_color (PikaImage *image,
                                 PikaRGB   *color)
{
  g_return_if_fail (PIKA_IS_IMAGE (image));
  g_return_if_fail (color != NULL);

  *color = PIKA_IMAGE_GET_PRIVATE (image)->quick_mask_color;
}

PikaChannel *
pika_image_get_quick_mask (PikaImage *image)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);

  return pika_image_get_channel_by_name (image, PIKA_IMAGE_QUICK_MASK_NAME);
}

void
pika_image_quick_mask_invert (PikaImage *image)
{
  PikaImagePrivate *private;

  g_return_if_fail (PIKA_IS_IMAGE (image));

  private = PIKA_IMAGE_GET_PRIVATE (image);

  if (private->quick_mask_state)
    {
      PikaChannel *quick_mask = pika_image_get_quick_mask (image);

      if (quick_mask)
        pika_channel_invert (quick_mask, TRUE);
    }

  private->quick_mask_inverted = ! private->quick_mask_inverted;
}

gboolean
pika_image_get_quick_mask_inverted (PikaImage *image)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), FALSE);

  return PIKA_IMAGE_GET_PRIVATE (image)->quick_mask_inverted;
}
