/* 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 "pikagrouplayer.h"
#include "pikaguide.h"
#include "pikaimage.h"
#include "pikaimage-pick-item.h"
#include "pikaimage-private.h"
#include "pikapickable.h"
#include "pikasamplepoint.h"

#include "text/pikatextlayer.h"

#include "vectors/pikastroke.h"
#include "vectors/pikavectors.h"


PikaLayer *
pika_image_pick_layer (PikaImage *image,
                       gint       x,
                       gint       y,
                       PikaLayer *previously_picked)
{
  GList *all_layers;
  GList *list;
  gint   off_x, off_y;
  gint   tries = 1;

  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);

  all_layers = pika_image_get_layer_list (image);

  if (previously_picked)
    {
      pika_item_get_offset (PIKA_ITEM (previously_picked), &off_x, &off_y);
      if (pika_pickable_get_opacity_at (PIKA_PICKABLE (previously_picked),
                                        x - off_x, y - off_y) <= 0.25)
        previously_picked = NULL;
      else
        tries++;
    }

  while (tries)
    {
      for (list = all_layers; list; list = g_list_next (list))
        {
          PikaLayer *layer = list->data;

          if (previously_picked)
            {
              /* Take the first layer with a pixel at given coordinates
               * after the previously picked one.
               */
              if (layer == previously_picked)
                previously_picked = NULL;
              continue;
            }

          pika_item_get_offset (PIKA_ITEM (layer), &off_x, &off_y);

          if (pika_pickable_get_opacity_at (PIKA_PICKABLE (layer),
                                            x - off_x, y - off_y) > 0.25)
            {
              g_list_free (all_layers);

              return layer;
            }
        }
      tries--;
    }

  g_list_free (all_layers);

  return NULL;
}

PikaLayer *
pika_image_pick_layer_by_bounds (PikaImage *image,
                                 gint       x,
                                 gint       y)
{
  GList *all_layers;
  GList *list;

  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);

  all_layers = pika_image_get_layer_list (image);

  for (list = all_layers; list; list = g_list_next (list))
    {
      PikaLayer *layer = list->data;

      if (pika_item_is_visible (PIKA_ITEM (layer)))
        {
          gint off_x, off_y;
          gint width, height;

          pika_item_get_offset (PIKA_ITEM (layer), &off_x, &off_y);
          width  = pika_item_get_width  (PIKA_ITEM (layer));
          height = pika_item_get_height (PIKA_ITEM (layer));

          if (x >= off_x        &&
              y >= off_y        &&
              x < off_x + width &&
              y < off_y + height)
            {
              g_list_free (all_layers);

              return layer;
            }
        }
    }

  g_list_free (all_layers);

  return NULL;
}

PikaTextLayer *
pika_image_pick_text_layer (PikaImage *image,
                            gint       x,
                            gint       y)
{
  GList *all_layers;
  GList *list;

  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);

  all_layers = pika_image_get_layer_list (image);

  for (list = all_layers; list; list = g_list_next (list))
    {
      PikaLayer *layer = list->data;
      gint       off_x, off_y;

      pika_item_get_offset (PIKA_ITEM (layer), &off_x, &off_y);

      if (PIKA_IS_TEXT_LAYER (layer) &&
          x >= off_x &&
          y >= off_y &&
          x <  off_x + pika_item_get_width  (PIKA_ITEM (layer)) &&
          y <  off_y + pika_item_get_height (PIKA_ITEM (layer)) &&
          pika_item_is_visible (PIKA_ITEM (layer)))
        {
          g_list_free (all_layers);

          return PIKA_TEXT_LAYER (layer);
        }
      else if (pika_pickable_get_opacity_at (PIKA_PICKABLE (layer),
                                             x - off_x, y - off_y) > 0.25)
        {
          /*  a normal layer covers any possible text layers below,
           *  bail out
           */

          break;
        }
    }

  g_list_free (all_layers);

  return NULL;
}

PikaVectors *
pika_image_pick_vectors (PikaImage *image,
                         gdouble    x,
                         gdouble    y,
                         gdouble    epsilon_x,
                         gdouble    epsilon_y)
{
  PikaVectors *ret = NULL;
  GList       *all_vectors;
  GList       *list;
  gdouble      mindist = G_MAXDOUBLE;

  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);

  all_vectors = pika_image_get_vectors_list (image);

  for (list = all_vectors; list; list = g_list_next (list))
    {
      PikaVectors *vectors = list->data;

      if (pika_item_is_visible (PIKA_ITEM (vectors)))
        {
          PikaStroke *stroke = NULL;
          PikaCoords  coords = PIKA_COORDS_DEFAULT_VALUES;

          while ((stroke = pika_vectors_stroke_get_next (vectors, stroke)))
            {
              gdouble dist;

              coords.x = x;
              coords.y = y;

              dist = pika_stroke_nearest_point_get (stroke, &coords, 1.0,
                                                    NULL, NULL, NULL, NULL);

              if (dist >= 0.0 &&
                  dist <  MIN (epsilon_y, mindist))
                {
                  mindist = dist;
                  ret     = vectors;
                }
            }
        }
    }

  g_list_free (all_vectors);

  return ret;
}

static PikaGuide *
pika_image_pick_guide_internal (PikaImage           *image,
                                gdouble              x,
                                gdouble              y,
                                gdouble              epsilon_x,
                                gdouble              epsilon_y,
                                PikaOrientationType  orientation)
{
  GList     *list;
  PikaGuide *ret     = NULL;
  gdouble    mindist = G_MAXDOUBLE;

  for (list = PIKA_IMAGE_GET_PRIVATE (image)->guides;
       list;
       list = g_list_next (list))
    {
      PikaGuide *guide    = list->data;
      gint       position = pika_guide_get_position (guide);
      gdouble    dist;

      switch (pika_guide_get_orientation (guide))
        {
        case PIKA_ORIENTATION_HORIZONTAL:
          if (orientation == PIKA_ORIENTATION_HORIZONTAL ||
              orientation == PIKA_ORIENTATION_UNKNOWN)
            {
              dist = ABS (position - y);
              if (dist < MIN (epsilon_y, mindist))
                {
                  mindist = dist;
                  ret = guide;
                }
            }
          break;

        /* mindist always is in vertical resolution to make it comparable */
        case PIKA_ORIENTATION_VERTICAL:
          if (orientation == PIKA_ORIENTATION_VERTICAL ||
              orientation == PIKA_ORIENTATION_UNKNOWN)
            {
              dist = ABS (position - x);
              if (dist < MIN (epsilon_x, mindist / epsilon_y * epsilon_x))
                {
                  mindist = dist * epsilon_y / epsilon_x;
                  ret = guide;
                }
            }
          break;

        default:
          continue;
        }
    }

  return ret;
}

PikaGuide *
pika_image_pick_guide (PikaImage *image,
                       gdouble    x,
                       gdouble    y,
                       gdouble    epsilon_x,
                       gdouble    epsilon_y)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (epsilon_x > 0 && epsilon_y > 0, NULL);

  return pika_image_pick_guide_internal (image, x, y, epsilon_x, epsilon_y,
                                         PIKA_ORIENTATION_UNKNOWN);
}

GList *
pika_image_pick_guides (PikaImage *image,
                        gdouble    x,
                        gdouble    y,
                        gdouble    epsilon_x,
                        gdouble    epsilon_y)
{
  PikaGuide *guide;
  GList     *result = NULL;

  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (epsilon_x > 0 && epsilon_y > 0, NULL);

  guide = pika_image_pick_guide_internal (image, x, y, epsilon_x, epsilon_y,
                                          PIKA_ORIENTATION_HORIZONTAL);

  if (guide)
    result = g_list_append (result, guide);

  guide = pika_image_pick_guide_internal (image, x, y, epsilon_x, epsilon_y,
                                          PIKA_ORIENTATION_VERTICAL);

  if (guide)
    result = g_list_append (result, guide);

  return result;
}

PikaSamplePoint *
pika_image_pick_sample_point (PikaImage *image,
                              gdouble    x,
                              gdouble    y,
                              gdouble    epsilon_x,
                              gdouble    epsilon_y)
{
  GList           *list;
  PikaSamplePoint *ret     = NULL;
  gdouble          mindist = G_MAXDOUBLE;

  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (epsilon_x > 0 && epsilon_y > 0, NULL);

  if (x < 0 || x >= pika_image_get_width  (image) ||
      y < 0 || y >= pika_image_get_height (image))
    {
      return NULL;
    }

  for (list = PIKA_IMAGE_GET_PRIVATE (image)->sample_points;
       list;
       list = g_list_next (list))
    {
      PikaSamplePoint *sample_point = list->data;
      gint             sp_x;
      gint             sp_y;
      gdouble          dist;

      pika_sample_point_get_position (sample_point, &sp_x, &sp_y);

      if (sp_x < 0 || sp_y < 0)
        continue;

      dist = hypot ((sp_x + 0.5) - x,
                    (sp_y + 0.5) - y);
      if (dist < MIN (epsilon_y, mindist))
        {
          mindist = dist;
          ret = sample_point;
        }
    }

  return ret;
}
