/* 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 "libpikamath/pikamath.h"

#include "core-types.h"

#include "pika.h"
#include "pikachannel.h"
#include "pikacontainer.h"
#include "pikacontext.h"
#include "pikaguide.h"
#include "pikaimage.h"
#include "pikaimage-flip.h"
#include "pikaimage-guides.h"
#include "pikaimage-sample-points.h"
#include "pikaimage-undo.h"
#include "pikaimage-undo-push.h"
#include "pikaitem.h"
#include "pikaobjectqueue.h"
#include "pikaprogress.h"
#include "pikasamplepoint.h"


/*  local function prototypes  */

static void    pika_image_flip_guides        (PikaImage           *image,
                                              PikaOrientationType  flip_type,
                                              gdouble              axis);
static void    pika_image_flip_sample_points (PikaImage           *image,
                                              PikaOrientationType  flip_type,
                                              gdouble              axis);


/*  private functions  */

static void
pika_image_flip_guides (PikaImage           *image,
                        PikaOrientationType  flip_type,
                        gdouble              axis)
{
  gint   width  = pika_image_get_width  (image);
  gint   height = pika_image_get_height (image);
  GList *iter;

  for (iter = pika_image_get_guides (image); iter;)
    {
      PikaGuide *guide    = iter->data;
      gint       position = pika_guide_get_position (guide);

      iter = g_list_next (iter);

      position = SIGNED_ROUND (2.0 * axis - position);

      switch (pika_guide_get_orientation (guide))
        {
        case PIKA_ORIENTATION_HORIZONTAL:
          if (flip_type == PIKA_ORIENTATION_VERTICAL)
            {
              if (position >= 0 && position <= height)
                pika_image_move_guide (image, guide, position, TRUE);
              else
                pika_image_remove_guide (image, guide, TRUE);
            }
          break;

        case PIKA_ORIENTATION_VERTICAL:
          if (flip_type == PIKA_ORIENTATION_HORIZONTAL)
            {
              if (position >= 0 && position <= width)
                pika_image_move_guide (image, guide, position, TRUE);
              else
                pika_image_remove_guide (image, guide, TRUE);
            }
          break;

        case PIKA_ORIENTATION_UNKNOWN:
          g_return_if_reached ();
        }
    }
}

static void
pika_image_flip_sample_points (PikaImage           *image,
                               PikaOrientationType  flip_type,
                               gdouble              axis)
{
  gint   width  = pika_image_get_width  (image);
  gint   height = pika_image_get_height (image);
  GList *iter;

  for (iter = pika_image_get_sample_points (image); iter;)
    {
      PikaSamplePoint *sample_point = iter->data;
      gint             x;
      gint             y;

      iter = g_list_next (iter);

      pika_sample_point_get_position (sample_point, &x, &y);

      switch (flip_type)
        {
        case PIKA_ORIENTATION_HORIZONTAL:
          x = SIGNED_ROUND (2.0 * axis - x);
          break;

        case PIKA_ORIENTATION_VERTICAL:
          y = SIGNED_ROUND (2.0 * axis - y);
          break;

        case PIKA_ORIENTATION_UNKNOWN:
          g_return_if_reached ();
        }

      if (x >= 0 && x < width &&
          y >= 0 && y < height)
        {
          pika_image_move_sample_point (image, sample_point, x, y, TRUE);
        }
      else
        {
          pika_image_remove_sample_point (image, sample_point, TRUE);
        }
    }
}


/*  public functions  */

void
pika_image_flip (PikaImage           *image,
                 PikaContext         *context,
                 PikaOrientationType  flip_type,
                 PikaProgress        *progress)
{
  gdouble axis = 0.0;

  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));

  switch (flip_type)
    {
    case PIKA_ORIENTATION_HORIZONTAL:
      axis = pika_image_get_width (image) / 2.0;
      break;

    case PIKA_ORIENTATION_VERTICAL:
      axis = pika_image_get_height (image) / 2.0;
      break;

    case PIKA_ORIENTATION_UNKNOWN:
      g_return_if_reached ();
    }

  pika_image_flip_full (image, context, flip_type, axis,
                        PIKA_TRANSFORM_RESIZE_CLIP, progress);
}

void
pika_image_flip_full (PikaImage           *image,
                      PikaContext         *context,
                      PikaOrientationType  flip_type,
                      gdouble              axis,
                      gboolean             clip_result,
                      PikaProgress        *progress)
{
  PikaObjectQueue *queue;
  PikaItem        *item;
  gint             width;
  gint             height;
  gint             offset_x = 0;
  gint             offset_y = 0;

  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));

  width  = pika_image_get_width  (image);
  height = pika_image_get_height (image);

  if (! clip_result)
    {
      switch (flip_type)
        {
        case PIKA_ORIENTATION_HORIZONTAL:
          offset_x = SIGNED_ROUND (2.0 * axis - width);
          axis     = width / 2.0;
          break;

        case PIKA_ORIENTATION_VERTICAL:
          offset_y = SIGNED_ROUND (2.0 * axis - height);
          axis     = height / 2.0;
          break;

        case PIKA_ORIENTATION_UNKNOWN:
          g_return_if_reached ();
        }
    }

  pika_set_busy (image->pika);

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

  pika_object_queue_push_container (queue, pika_image_get_layers (image));
  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));

  pika_image_undo_group_start (image, PIKA_UNDO_GROUP_IMAGE_FLIP, NULL);

  /*  Flip all layers, channels (including selection mask), and vectors  */
  while ((item = pika_object_queue_pop (queue)))
    {
      gboolean clip = FALSE;

      if (PIKA_IS_CHANNEL (item))
        clip = clip_result;

      pika_item_flip (item, context, flip_type, axis, clip);

      pika_progress_set_value (progress, 1.0);
    }

  /*  Flip all Guides  */
  pika_image_flip_guides (image, flip_type, axis);

  /*  Flip all sample points  */
  pika_image_flip_sample_points (image, flip_type, axis);

  if (offset_x || offset_y)
    {
      pika_image_undo_push_image_size (image,
                                       NULL,
                                       offset_x,
                                       offset_y,
                                       width,
                                       height);
    }

  pika_image_undo_group_end (image);

  g_object_unref (queue);

  if (offset_x || offset_y)
    {
      pika_image_size_changed_detailed (image,
                                        -offset_x,
                                        -offset_y,
                                        width,
                                        height);
    }

  pika_unset_busy (image->pika);
}
