/* 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 "libpikabase/pikabase.h"

#include "core-types.h"

#include "pika.h"
#include "pikacontainer.h"
#include "pikacontext.h"
#include "pikaguide.h"
#include "pikaimage.h"
#include "pikaimage-guides.h"
#include "pikaimage-item-list.h"
#include "pikaimage-resize.h"
#include "pikaimage-sample-points.h"
#include "pikaimage-undo.h"
#include "pikaimage-undo-push.h"
#include "pikalayer.h"
#include "pikaobjectqueue.h"
#include "pikaprogress.h"
#include "pikasamplepoint.h"

#include "text/pikatextlayer.h"

#include "pika-intl.h"


void
pika_image_resize (PikaImage    *image,
                   PikaContext  *context,
                   gint          new_width,
                   gint          new_height,
                   gint          offset_x,
                   gint          offset_y,
                   PikaProgress *progress)
{
  pika_image_resize_with_layers (image, context, PIKA_FILL_TRANSPARENT,
                                 new_width, new_height, offset_x, offset_y,
                                 PIKA_ITEM_SET_NONE, TRUE,
                                 progress);
}

void
pika_image_resize_with_layers (PikaImage    *image,
                               PikaContext  *context,
                               PikaFillType  fill_type,
                               gint          new_width,
                               gint          new_height,
                               gint          offset_x,
                               gint          offset_y,
                               PikaItemSet   layer_set,
                               gboolean      resize_text_layers,
                               PikaProgress *progress)
{
  PikaObjectQueue *queue;
  GList           *resize_layers;
  GList           *list;
  PikaItem        *item;
  gint             old_width, old_height;

  g_return_if_fail (PIKA_IS_IMAGE (image));
  g_return_if_fail (PIKA_IS_CONTEXT (context));
  g_return_if_fail (new_width > 0 && new_height > 0);
  g_return_if_fail (progress == NULL || PIKA_IS_PROGRESS (progress));

  pika_set_busy (image->pika);

  g_object_freeze_notify (G_OBJECT (image));

  pika_image_undo_group_start (image, PIKA_UNDO_GROUP_IMAGE_RESIZE,
                               C_("undo-type", "Resize Image"));

  resize_layers = pika_image_item_list_get_list (image,
                                                 PIKA_ITEM_TYPE_LAYERS,
                                                 layer_set);

  old_width  = pika_image_get_width  (image);
  old_height = pika_image_get_height (image);

  /*  Push the image size to the stack  */
  pika_image_undo_push_image_size (image, NULL,
                                   -offset_x, -offset_y,
                                   new_width, new_height);

  /*  Set the new width and height  */
  g_object_set (image,
                "width",  new_width,
                "height", new_height,
                NULL);

  /*  Reposition all layers  */
  for (list = pika_image_get_layer_iter (image);
       list;
       list = g_list_next (list))
    {
      PikaItem *item = list->data;

      pika_item_translate (item, offset_x, offset_y, TRUE);
    }

  queue    = pika_object_queue_new (progress);
  progress = PIKA_PROGRESS (queue);

  for (list = resize_layers; list; list = g_list_next (list))
    {
      PikaItem *item = list->data;

      /*  group layers can't be resized here  */
      if (pika_viewable_get_children (PIKA_VIEWABLE (item)))
        continue;

      if (! resize_text_layers && pika_item_is_text_layer (item))
        continue;

      /* note that we call pika_item_start_move(), and not
       * pika_item_start_transform().  see the comment in pika_item_resize()
       * for more information.
       */
      pika_item_start_move (item, TRUE);

      pika_object_queue_push (queue, item);
    }

  g_list_free (resize_layers);
  
  pika_object_queue_push (queue, pika_image_get_mask (image));
  pika_object_queue_push_container (queue, pika_image_get_channels (image));
  pika_object_queue_push_container (queue, pika_image_get_vectors (image));

  /*  Resize all resize_layers, channels (including selection mask), and
   *  vectors
   */
  while ((item = pika_object_queue_pop (queue)))
    {
      if (PIKA_IS_LAYER (item))
        {
          gint old_offset_x;
          gint old_offset_y;

          pika_item_get_offset (item, &old_offset_x, &old_offset_y);

          pika_item_resize (item, context, fill_type,
                            new_width, new_height,
                            old_offset_x, old_offset_y);

          pika_item_end_move (item, TRUE);
        }
      else
        {
          pika_item_resize (item, context, PIKA_FILL_TRANSPARENT,
                            new_width, new_height, offset_x, offset_y);
        }
    }

  /*  Reposition or remove all guides  */
  list = pika_image_get_guides (image);

  while (list)
    {
      PikaGuide *guide        = list->data;
      gboolean   remove_guide = FALSE;
      gint       new_position = pika_guide_get_position (guide);

      list = g_list_next (list);

      switch (pika_guide_get_orientation (guide))
        {
        case PIKA_ORIENTATION_HORIZONTAL:
          new_position += offset_y;
          if (new_position < 0 || new_position > new_height)
            remove_guide = TRUE;
          break;

        case PIKA_ORIENTATION_VERTICAL:
          new_position += offset_x;
          if (new_position < 0 || new_position > new_width)
            remove_guide = TRUE;
          break;

        default:
          break;
        }

      if (remove_guide)
        pika_image_remove_guide (image, guide, TRUE);
      else if (new_position != pika_guide_get_position (guide))
        pika_image_move_guide (image, guide, new_position, TRUE);
    }

  /*  Reposition or remove sample points  */
  list = pika_image_get_sample_points (image);

  while (list)
    {
      PikaSamplePoint *sample_point        = list->data;
      gboolean         remove_sample_point = FALSE;
      gint             old_x;
      gint             old_y;
      gint             new_x;
      gint             new_y;

      list = g_list_next (list);

      pika_sample_point_get_position (sample_point, &old_x, &old_y);

      new_y = old_y + offset_y;
      if ((new_y < 0) || (new_y >= new_height))
        remove_sample_point = TRUE;

      new_x = old_x + offset_x;
      if ((new_x < 0) || (new_x >= new_width))
        remove_sample_point = TRUE;

      if (remove_sample_point)
        pika_image_remove_sample_point (image, sample_point, TRUE);
      else if (new_x != old_x || new_y != old_y)
        pika_image_move_sample_point (image, sample_point,
                                      new_x, new_y, TRUE);
    }

  g_object_unref (queue);

  pika_image_undo_group_end (image);

  pika_image_size_changed_detailed (image,
                                    offset_x, offset_y,
                                    old_width, old_height);

  g_object_thaw_notify (G_OBJECT (image));

  pika_unset_busy (image->pika);
}

void
pika_image_resize_to_layers (PikaImage    *image,
                             PikaContext  *context,
                             gint         *offset_x,
                             gint         *offset_y,
                             gint         *new_width,
                             gint         *new_height,
                             PikaProgress *progress)
{
  GList    *list;
  PikaItem *item;
  gint      x, y;
  gint      width, height;

  g_return_if_fail (PIKA_IS_IMAGE (image));
  g_return_if_fail (PIKA_IS_CONTEXT (context));
  g_return_if_fail (progress == NULL || PIKA_IS_PROGRESS (progress));

  list = pika_image_get_layer_iter (image);

  if (! list)
    return;

  /* figure out starting values */
  item = list->data;

  x      = pika_item_get_offset_x (item);
  y      = pika_item_get_offset_y (item);
  width  = pika_item_get_width  (item);
  height = pika_item_get_height (item);

  /*  Respect all layers  */
  for (list = g_list_next (list); list; list = g_list_next (list))
    {
      item = list->data;

      pika_rectangle_union (x, y,
                            width, height,
                            pika_item_get_offset_x (item),
                            pika_item_get_offset_y (item),
                            pika_item_get_width  (item),
                            pika_item_get_height (item),
                            &x, &y,
                            &width, &height);
    }

  pika_image_resize (image, context,
                     width, height, -x, -y,
                     progress);
  if (offset_x)
    *offset_x = -x;
  if (offset_y)
    *offset_y = -y;
  if (new_width)
    *new_width = width;
  if (new_height)
    *new_height = height;
}

void
pika_image_resize_to_selection (PikaImage    *image,
                                PikaContext  *context,
                                PikaProgress *progress)
{
  PikaChannel *selection = pika_image_get_mask (image);
  gint         x, y, w, h;

  if (pika_item_bounds (PIKA_ITEM (selection), &x, &y, &w, &h))
    {
      pika_image_resize (image, context,
                         w, h, -x, -y,
                         progress);
    }
}
