/* 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 .
 */
#include "config.h"
#include 
#include 
#include "core-types.h"
#include "pika.h"
#include "pikaimage.h"
#include "pikaguide.h"
#include "pikaimage-guides.h"
#include "pikaimage-private.h"
#include "pikaimage-undo-push.h"
#include "pika-intl.h"
/*  public functions  */
PikaGuide *
pika_image_add_hguide (PikaImage *image,
                       gint       position,
                       gboolean   push_undo)
{
  PikaGuide *guide;
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  guide = pika_guide_new (PIKA_ORIENTATION_HORIZONTAL,
                          image->pika->next_guide_id++);
  if (push_undo)
    pika_image_undo_push_guide (image,
                                C_("undo-type", "Add Horizontal Guide"), guide);
  pika_image_add_guide (image, guide, position);
  g_object_unref (G_OBJECT (guide));
  return guide;
}
PikaGuide *
pika_image_add_vguide (PikaImage *image,
                       gint       position,
                       gboolean   push_undo)
{
  PikaGuide *guide;
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  guide = pika_guide_new (PIKA_ORIENTATION_VERTICAL,
                          image->pika->next_guide_id++);
  if (push_undo)
    pika_image_undo_push_guide (image,
                                C_("undo-type", "Add Vertical Guide"), guide);
  pika_image_add_guide (image, guide, position);
  g_object_unref (guide);
  return guide;
}
void
pika_image_add_guide (PikaImage *image,
                      PikaGuide *guide,
                      gint       position)
{
  PikaImagePrivate *private;
  g_return_if_fail (PIKA_IS_IMAGE (image));
  g_return_if_fail (PIKA_IS_GUIDE (guide));
  private = PIKA_IMAGE_GET_PRIVATE (image);
  private->guides = g_list_prepend (private->guides, guide);
  pika_guide_set_position (guide, position);
  g_object_ref (guide);
  pika_image_guide_added (image, guide);
}
void
pika_image_remove_guide (PikaImage *image,
                         PikaGuide *guide,
                         gboolean   push_undo)
{
  PikaImagePrivate *private;
  g_return_if_fail (PIKA_IS_IMAGE (image));
  g_return_if_fail (PIKA_IS_GUIDE (guide));
  private = PIKA_IMAGE_GET_PRIVATE (image);
  if (pika_guide_is_custom (guide))
    push_undo = FALSE;
  if (push_undo)
    pika_image_undo_push_guide (image, C_("undo-type", "Remove Guide"), guide);
  private->guides = g_list_remove (private->guides, guide);
  pika_aux_item_removed (PIKA_AUX_ITEM (guide));
  pika_image_guide_removed (image, guide);
  pika_guide_set_position (guide, PIKA_GUIDE_POSITION_UNDEFINED);
  g_object_unref (guide);
}
void
pika_image_move_guide (PikaImage *image,
                       PikaGuide *guide,
                       gint       position,
                       gboolean   push_undo)
{
  g_return_if_fail (PIKA_IS_IMAGE (image));
  g_return_if_fail (PIKA_IS_GUIDE (guide));
  if (pika_guide_is_custom (guide))
    push_undo = FALSE;
  if (push_undo)
    pika_image_undo_push_guide (image, C_("undo-type", "Move Guide"), guide);
  pika_guide_set_position (guide, position);
  pika_image_guide_moved (image, guide);
}
GList *
pika_image_get_guides (PikaImage *image)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  return PIKA_IMAGE_GET_PRIVATE (image)->guides;
}
PikaGuide *
pika_image_get_guide (PikaImage *image,
                      guint32    id)
{
  GList *guides;
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  for (guides = PIKA_IMAGE_GET_PRIVATE (image)->guides;
       guides;
       guides = g_list_next (guides))
    {
      PikaGuide *guide = guides->data;
      if (pika_aux_item_get_id (PIKA_AUX_ITEM (guide)) == id)
        return guide;
    }
  return NULL;
}
PikaGuide *
pika_image_get_next_guide (PikaImage *image,
                           guint32    id,
                           gboolean  *guide_found)
{
  GList *guides;
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (guide_found != NULL, NULL);
  if (id == 0)
    *guide_found = TRUE;
  else
    *guide_found = FALSE;
  for (guides = PIKA_IMAGE_GET_PRIVATE (image)->guides;
       guides;
       guides = g_list_next (guides))
    {
      PikaGuide *guide = guides->data;
      if (*guide_found) /* this is the first guide after the found one */
        return guide;
      if (pika_aux_item_get_id (PIKA_AUX_ITEM (guide)) == id) /* found it, next one will be returned */
        *guide_found = TRUE;
    }
  return NULL;
}