/* 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 <gegl.h>
#include <gtk/gtk.h>

#include "libpikamath/pikamath.h"
#include "libpikawidgets/pikawidgets.h"

#include "actions-types.h"

#include "config/pikadialogconfig.h"

#include "core/pika.h"
#include "core/pikachannel.h"
#include "core/pikaimage.h"
#include "core/pikaselection.h"

#include "widgets/pikahelp-ids.h"
#include "widgets/pikadialogfactory.h"
#include "widgets/pikawidgets-utils.h"
#include "widgets/pikawindowstrategy.h"

#include "display/pikadisplay.h"
#include "display/pikadisplayshell.h"

#include "dialogs/dialogs.h"

#include "actions.h"
#include "items-commands.h"
#include "select-commands.h"

#include "pika-intl.h"


/*  local function prototypes  */

static void   select_feather_callback (GtkWidget *widget,
                                       gdouble    size,
                                       PikaUnit   unit,
                                       gpointer   data);
static void   select_border_callback  (GtkWidget *widget,
                                       gdouble    size,
                                       PikaUnit   unit,
                                       gpointer   data);
static void   select_grow_callback    (GtkWidget *widget,
                                       gdouble    size,
                                       PikaUnit   unit,
                                       gpointer   data);
static void   select_shrink_callback  (GtkWidget *widget,
                                       gdouble    size,
                                       PikaUnit   unit,
                                       gpointer   data);


/*  public functions  */

void
select_all_cmd_callback (PikaAction *action,
                         GVariant   *value,
                         gpointer    data)
{
  PikaImage *image;
  return_if_no_image (image, data);

  pika_channel_all (pika_image_get_mask (image), TRUE);
  pika_image_flush (image);
}

void
select_none_cmd_callback (PikaAction *action,
                          GVariant   *value,
                          gpointer    data)
{
  PikaImage *image;
  return_if_no_image (image, data);

  pika_channel_clear (pika_image_get_mask (image), NULL, TRUE);
  pika_image_flush (image);
}

void
select_invert_cmd_callback (PikaAction *action,
                            GVariant   *value,
                            gpointer    data)
{
  PikaImage *image;
  return_if_no_image (image, data);

  pika_channel_invert (pika_image_get_mask (image), TRUE);
  pika_image_flush (image);
}

void
select_float_cmd_callback (PikaAction *action,
                           GVariant   *value,
                           gpointer    data)
{
  PikaImage *image;
  GtkWidget *widget;
  GList     *drawables;
  GError    *error = NULL;
  return_if_no_image (image, data);
  return_if_no_widget (widget, data);

  drawables = pika_image_get_selected_drawables (image);
  if (pika_selection_float (PIKA_SELECTION (pika_image_get_mask (image)),
                            drawables,
                            action_data_get_context (data),
                            TRUE, 0, 0, &error))
    {
      pika_image_flush (image);
    }
  else
    {
      pika_message_literal (image->pika,
                            G_OBJECT (widget), PIKA_MESSAGE_WARNING,
                            error->message);
      g_clear_error (&error);
    }
  g_list_free (drawables);
}

void
select_feather_cmd_callback (PikaAction *action,
                             GVariant   *value,
                             gpointer    data)
{
  PikaDisplay *display;
  PikaImage   *image;
  GtkWidget   *dialog;
  return_if_no_display (display, data);

  image = pika_display_get_image (display);

#define FEATHER_DIALOG_KEY "pika-selection-feather-dialog"

  dialog = dialogs_get_dialog (G_OBJECT (image), FEATHER_DIALOG_KEY);

  if (! dialog)
    {
      PikaDialogConfig *config = PIKA_DIALOG_CONFIG (image->pika->config);
      GtkWidget        *button;
      gdouble           xres;
      gdouble           yres;

      pika_image_get_resolution (image, &xres, &yres);

      dialog = pika_query_size_box (_("Feather Selection"),
                                    GTK_WIDGET (pika_display_get_shell (display)),
                                    pika_standard_help_func,
                                    PIKA_HELP_SELECTION_FEATHER,
                                    _("Feather selection by"),
                                    config->selection_feather_radius, 0, 32767, 3,
                                    pika_display_get_shell (display)->unit,
                                    MIN (xres, yres),
                                    FALSE,
                                    G_OBJECT (image), "disconnect",
                                    select_feather_callback,
                                    image, NULL);

      /* Edge lock button */
      button = gtk_check_button_new_with_mnemonic (_("_Selected areas continue outside the image"));
      g_object_set_data (G_OBJECT (dialog), "edge-lock-toggle", button);
      pika_help_set_help_data (button,
                               _("When feathering, act as if selected areas "
                                 "continued outside the image."),
                               NULL);
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
                                    config->selection_feather_edge_lock);
      gtk_box_pack_start (GTK_BOX (PIKA_QUERY_BOX_VBOX (dialog)), button,
                          FALSE, FALSE, 0);
      gtk_widget_show (button);

      dialogs_attach_dialog (G_OBJECT (image), FEATHER_DIALOG_KEY, dialog);
    }

  gtk_window_present (GTK_WINDOW (dialog));
}

void
select_sharpen_cmd_callback (PikaAction *action,
                             GVariant   *value,
                             gpointer    data)
{
  PikaImage *image;
  return_if_no_image (image, data);

  pika_channel_sharpen (pika_image_get_mask (image), TRUE);
  pika_image_flush (image);
}

void
select_shrink_cmd_callback (PikaAction *action,
                            GVariant   *value,
                            gpointer    data)
{
  PikaDisplay *display;
  PikaImage   *image;
  GtkWidget   *dialog;
  return_if_no_display (display, data);

  image = pika_display_get_image (display);

#define SHRINK_DIALOG_KEY "pika-selection-shrink-dialog"

  dialog = dialogs_get_dialog (G_OBJECT (image), SHRINK_DIALOG_KEY);

  if (! dialog)
    {
      PikaDialogConfig *config = PIKA_DIALOG_CONFIG (image->pika->config);
      GtkWidget        *button;
      gint              width;
      gint              height;
      gint              max_value;
      gdouble           xres;
      gdouble           yres;

      pika_item_bounds (PIKA_ITEM (pika_image_get_mask (image)),
                        NULL, NULL, &width, &height);
      max_value = MIN (width, height) / 2;

      pika_image_get_resolution (image, &xres, &yres);

      dialog = pika_query_size_box (_("Shrink Selection"),
                                    GTK_WIDGET (pika_display_get_shell (display)),
                                    pika_standard_help_func,
                                    PIKA_HELP_SELECTION_SHRINK,
                                    _("Shrink selection by"),
                                    config->selection_shrink_radius,
                                    1, max_value, 0,
                                    pika_display_get_shell (display)->unit,
                                    MIN (xres, yres),
                                    FALSE,
                                    G_OBJECT (image), "disconnect",
                                    select_shrink_callback,
                                    image, NULL);

      /* Edge lock button */
      button = gtk_check_button_new_with_mnemonic (_("_Selected areas continue outside the image"));
      g_object_set_data (G_OBJECT (dialog), "edge-lock-toggle", button);
      pika_help_set_help_data (button,
                               _("When shrinking, act as if selected areas "
                                 "continued outside the image."),
                               NULL);
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
                                    config->selection_shrink_edge_lock);
      gtk_box_pack_start (GTK_BOX (PIKA_QUERY_BOX_VBOX (dialog)), button,
                          FALSE, FALSE, 0);
      gtk_widget_show (button);

      dialogs_attach_dialog (G_OBJECT (image), SHRINK_DIALOG_KEY, dialog);
    }

  gtk_window_present (GTK_WINDOW (dialog));
}

void
select_grow_cmd_callback (PikaAction *action,
                          GVariant   *value,
                          gpointer    data)
{
  PikaDisplay *display;
  PikaImage   *image;
  GtkWidget   *dialog;
  return_if_no_display (display, data);

  image = pika_display_get_image (display);

#define GROW_DIALOG_KEY "pika-selection-grow-dialog"

  dialog = dialogs_get_dialog (G_OBJECT (image), GROW_DIALOG_KEY);

  if (! dialog)
    {
      PikaDialogConfig *config = PIKA_DIALOG_CONFIG (image->pika->config);
      gint              width;
      gint              height;
      gint              max_value;
      gdouble           xres;
      gdouble           yres;

      width  = pika_image_get_width  (image);
      height = pika_image_get_height (image);
      max_value = MAX (width, height);

      pika_image_get_resolution (image, &xres, &yres);

      dialog = pika_query_size_box (_("Grow Selection"),
                                    GTK_WIDGET (pika_display_get_shell (display)),
                                    pika_standard_help_func,
                                    PIKA_HELP_SELECTION_GROW,
                                    _("Grow selection by"),
                                    config->selection_grow_radius,
                                    1, max_value, 0,
                                    pika_display_get_shell (display)->unit,
                                    MIN (xres, yres),
                                    FALSE,
                                    G_OBJECT (image), "disconnect",
                                    select_grow_callback,
                                    image, NULL);

      dialogs_attach_dialog (G_OBJECT (image), GROW_DIALOG_KEY, dialog);
    }

  gtk_window_present (GTK_WINDOW (dialog));
}

void
select_border_cmd_callback (PikaAction *action,
                            GVariant   *value,
                            gpointer    data)
{
  PikaDisplay *display;
  PikaImage   *image;
  GtkWidget   *dialog;
  return_if_no_display (display, data);

  image = pika_display_get_image (display);

#define BORDER_DIALOG_KEY "pika-selection-border-dialog"

  dialog = dialogs_get_dialog (G_OBJECT (image), BORDER_DIALOG_KEY);

  if (! dialog)
    {
      PikaDialogConfig *config = PIKA_DIALOG_CONFIG (image->pika->config);
      GtkWidget        *combo;
      GtkWidget        *button;
      gint              width;
      gint              height;
      gint              max_value;
      gdouble           xres;
      gdouble           yres;

      pika_item_bounds (PIKA_ITEM (pika_image_get_mask (image)),
                        NULL, NULL, &width, &height);
      max_value = MIN (width, height) / 2;

      pika_image_get_resolution (image, &xres, &yres);

      dialog = pika_query_size_box (_("Border Selection"),
                                    GTK_WIDGET (pika_display_get_shell (display)),
                                    pika_standard_help_func,
                                    PIKA_HELP_SELECTION_BORDER,
                                    _("Border selection by"),
                                    config->selection_border_radius,
                                    1, max_value, 0,
                                    pika_display_get_shell (display)->unit,
                                    MIN (xres, yres),
                                    FALSE,
                                    G_OBJECT (image), "disconnect",
                                    select_border_callback,
                                    image, NULL);

      /* Border style combo */
      combo = pika_enum_combo_box_new (PIKA_TYPE_CHANNEL_BORDER_STYLE);
      pika_int_combo_box_set_label (PIKA_INT_COMBO_BOX (combo),
                                    _("Border style"));

      gtk_box_pack_start (GTK_BOX (PIKA_QUERY_BOX_VBOX (dialog)), combo,
                          FALSE, FALSE, 0);

      g_object_set_data (G_OBJECT (dialog), "border-style-combo", combo);
      pika_int_combo_box_set_active (PIKA_INT_COMBO_BOX (combo),
                                     config->selection_border_style);
      gtk_widget_show (combo);

      /* Edge lock button */
      button = gtk_check_button_new_with_mnemonic (_("_Selected areas continue outside the image"));
      g_object_set_data (G_OBJECT (dialog), "edge-lock-toggle", button);
      pika_help_set_help_data (button,
                               _("When bordering, act as if selected areas "
                                 "continued outside the image."),
                               NULL);
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
                                    config->selection_border_edge_lock);
      gtk_box_pack_start (GTK_BOX (PIKA_QUERY_BOX_VBOX (dialog)), button,
                          FALSE, FALSE, 0);
      gtk_widget_show (button);

      dialogs_attach_dialog (G_OBJECT (image), BORDER_DIALOG_KEY, dialog);
    }

  gtk_window_present (GTK_WINDOW (dialog));
}

void
select_flood_cmd_callback (PikaAction *action,
                           GVariant   *value,
                           gpointer    data)
{
  PikaImage *image;
  return_if_no_image (image, data);

  pika_channel_flood (pika_image_get_mask (image), TRUE);
  pika_image_flush (image);
}

void
select_save_cmd_callback (PikaAction *action,
                          GVariant   *value,
                          gpointer    data)
{
  PikaImage   *image;
  PikaChannel *channel;
  GtkWidget   *widget;
  return_if_no_image (image, data);
  return_if_no_widget (widget, data);

  channel = PIKA_CHANNEL (pika_item_duplicate (PIKA_ITEM (pika_image_get_mask (image)),
                                               PIKA_TYPE_CHANNEL));

  /*  saved selections are not visible by default  */
  pika_item_set_visible (PIKA_ITEM (channel), FALSE, FALSE);

  pika_image_add_channel (image, channel,
                          PIKA_IMAGE_ACTIVE_PARENT, -1, TRUE);
  pika_image_flush (image);

  pika_window_strategy_show_dockable_dialog (PIKA_WINDOW_STRATEGY (pika_get_window_strategy (image->pika)),
                                             image->pika,
                                             pika_dialog_factory_get_singleton (),
                                             pika_widget_get_monitor (widget),
                                             "pika-channel-list");
}

void
select_fill_cmd_callback (PikaAction *action,
                          GVariant   *value,
                          gpointer    data)
{
  PikaImage *image;
  GList     *selection;
  return_if_no_image (image, data);

  selection = g_list_prepend (NULL, pika_image_get_mask (image)),
  items_fill_cmd_callback (action,
                           image, selection,
                           _("Fill Selection Outline"),
                           PIKA_ICON_TOOL_BUCKET_FILL,
                           PIKA_HELP_SELECTION_FILL,
                           data);
  g_list_free (selection);
}

void
select_fill_last_vals_cmd_callback (PikaAction *action,
                                    GVariant   *value,
                                    gpointer    data)
{
  PikaImage *image;
  GList     *selection;
  return_if_no_image (image, data);

  selection = g_list_prepend (NULL, pika_image_get_mask (image)),
  items_fill_last_vals_cmd_callback (action, image, selection, data);
  g_list_free (selection);
}

void
select_stroke_cmd_callback (PikaAction *action,
                            GVariant   *value,
                            gpointer    data)
{
  PikaImage *image;
  GList     *selection;
  return_if_no_image (image, data);

  selection = g_list_prepend (NULL, pika_image_get_mask (image)),
  items_stroke_cmd_callback (action, image, selection,
                             _("Stroke Selection"),
                             PIKA_ICON_SELECTION_STROKE,
                             PIKA_HELP_SELECTION_STROKE,
                             data);
  g_list_free (selection);
}

void
select_stroke_last_vals_cmd_callback (PikaAction *action,
                                      GVariant   *value,
                                      gpointer    data)
{
  PikaImage *image;
  GList     *selection;
  return_if_no_image (image, data);

  selection = g_list_prepend (NULL, pika_image_get_mask (image)),
  items_stroke_last_vals_cmd_callback (action, image, selection, data);
  g_list_free (selection);
}


/*  private functions  */

static void
select_feather_callback (GtkWidget *widget,
                         gdouble    size,
                         PikaUnit   unit,
                         gpointer   data)
{
  PikaImage        *image  = PIKA_IMAGE (data);
  PikaDialogConfig *config = PIKA_DIALOG_CONFIG (image->pika->config);
  GtkWidget        *button;
  gdouble           radius_x;
  gdouble           radius_y;

  button = g_object_get_data (G_OBJECT (widget), "edge-lock-toggle");

  g_object_set (config,
                "selection-feather-radius", size,
                "selection-feather-edge-lock",
                gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)),
                NULL);

  radius_x = config->selection_feather_radius;
  radius_y = config->selection_feather_radius;

  if (unit != PIKA_UNIT_PIXEL)
    {
      gdouble xres;
      gdouble yres;
      gdouble factor;

      pika_image_get_resolution (image, &xres, &yres);

      factor = (MAX (xres, yres) /
                MIN (xres, yres));

      if (xres == MIN (xres, yres))
        radius_y *= factor;
      else
        radius_x *= factor;
    }

  pika_channel_feather (pika_image_get_mask (image), radius_x, radius_y,
                        config->selection_feather_edge_lock,
                        TRUE);
  pika_image_flush (image);
}

static void
select_border_callback (GtkWidget *widget,
                        gdouble    size,
                        PikaUnit   unit,
                        gpointer   data)
{
  PikaImage        *image  = PIKA_IMAGE (data);
  PikaDialogConfig *config = PIKA_DIALOG_CONFIG (image->pika->config);
  GtkWidget        *combo;
  GtkWidget        *button;
  gdouble           radius_x;
  gdouble           radius_y;
  gint              border_style;

  combo  = g_object_get_data (G_OBJECT (widget), "border-style-combo");
  button = g_object_get_data (G_OBJECT (widget), "edge-lock-toggle");

  pika_int_combo_box_get_active (PIKA_INT_COMBO_BOX (combo), &border_style);

  g_object_set (config,
                "selection-border-radius", size,
                "selection-border-style",  border_style,
                "selection-border-edge-lock",
                gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)),
                NULL);

  radius_x = ROUND (config->selection_border_radius);
  radius_y = ROUND (config->selection_border_radius);

  if (unit != PIKA_UNIT_PIXEL)
    {
      gdouble xres;
      gdouble yres;
      gdouble factor;

      pika_image_get_resolution (image, &xres, &yres);

      factor = (MAX (xres, yres) /
                MIN (xres, yres));

      if (xres == MIN (xres, yres))
        radius_y *= factor;
      else
        radius_x *= factor;
    }

  pika_channel_border (pika_image_get_mask (image), radius_x, radius_y,
                       config->selection_border_style,
                       config->selection_border_edge_lock,
                       TRUE);
  pika_image_flush (image);
}

static void
select_grow_callback (GtkWidget *widget,
                      gdouble    size,
                      PikaUnit   unit,
                      gpointer   data)
{
  PikaImage        *image  = PIKA_IMAGE (data);
  PikaDialogConfig *config = PIKA_DIALOG_CONFIG (image->pika->config);
  gdouble           radius_x;
  gdouble           radius_y;

  g_object_set (config,
                "selection-grow-radius", size,
                NULL);

  radius_x = ROUND (config->selection_grow_radius);
  radius_y = ROUND (config->selection_grow_radius);

  if (unit != PIKA_UNIT_PIXEL)
    {
      gdouble xres;
      gdouble yres;
      gdouble factor;

      pika_image_get_resolution (image, &xres, &yres);

      factor = (MAX (xres, yres) /
                MIN (xres, yres));

      if (xres == MIN (xres, yres))
        radius_y *= factor;
      else
        radius_x *= factor;
    }

  pika_channel_grow (pika_image_get_mask (image), radius_x, radius_y, TRUE);
  pika_image_flush (image);
}

static void
select_shrink_callback (GtkWidget *widget,
                        gdouble    size,
                        PikaUnit   unit,
                        gpointer   data)
{
  PikaImage        *image  = PIKA_IMAGE (data);
  PikaDialogConfig *config = PIKA_DIALOG_CONFIG (image->pika->config);
  GtkWidget        *button;
  gint              radius_x;
  gint              radius_y;

  button = g_object_get_data (G_OBJECT (widget), "edge-lock-toggle");

  g_object_set (config,
                "selection-shrink-radius", size,
                "selection-shrink-edge-lock",
                gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)),
                NULL);

  radius_x = ROUND (config->selection_shrink_radius);
  radius_y = ROUND (config->selection_shrink_radius);

  if (unit != PIKA_UNIT_PIXEL)
    {
      gdouble xres;
      gdouble yres;
      gdouble factor;

      pika_image_get_resolution (image, &xres, &yres);

      factor = (MAX (xres, yres) /
                MIN (xres, yres));

      if (xres == MIN (xres, yres))
        radius_y *= factor;
      else
        radius_x *= factor;
    }

  pika_channel_shrink (pika_image_get_mask (image), radius_x, radius_y,
                       config->selection_shrink_edge_lock,
                       TRUE);
  pika_image_flush (image);
}
