/* 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 "libpikacolor/pikacolor.h"
#include "libpikamath/pikamath.h"
#include "libpikawidgets/pikawidgets.h"

#include "widgets-types.h"

#include "config/pikacoreconfig.h"

#include "core/pika.h"
#include "core/pikachannel-select.h"
#include "core/pikacontainer.h"
#include "core/pikaimage.h"
#include "core/pikaimage-pick-color.h"
#include "core/pikaselection.h"
#include "core/pikatoolinfo.h"

/* FIXME: #include "tools/tools-types.h" */
#include "tools/tools-types.h"
#include "tools/pikaregionselectoptions.h"

#include "pikaselectioneditor.h"
#include "pikadnd.h"
#include "pikadocked.h"
#include "pikamenufactory.h"
#include "pikaview.h"
#include "pikaviewrenderer.h"
#include "pikawidgets-utils.h"

#include "pika-intl.h"


static void  pika_selection_editor_docked_iface_init (PikaDockedInterface *iface);

static void   pika_selection_editor_constructed    (GObject             *object);

static void   pika_selection_editor_set_image      (PikaImageEditor     *editor,
                                                    PikaImage           *image);

static void   pika_selection_editor_set_context    (PikaDocked          *docked,
                                                    PikaContext         *context);

static gboolean pika_selection_view_button_press   (GtkWidget           *widget,
                                                    GdkEventButton      *bevent,
                                                    PikaSelectionEditor *editor);
static void   pika_selection_editor_drop_color     (GtkWidget           *widget,
                                                    gint                 x,
                                                    gint                 y,
                                                    const PikaRGB       *color,
                                                    gpointer             data);

static void   pika_selection_editor_mask_changed   (PikaImage           *image,
                                                    PikaSelectionEditor *editor);


G_DEFINE_TYPE_WITH_CODE (PikaSelectionEditor, pika_selection_editor,
                         PIKA_TYPE_IMAGE_EDITOR,
                         G_IMPLEMENT_INTERFACE (PIKA_TYPE_DOCKED,
                                                pika_selection_editor_docked_iface_init))

#define parent_class pika_selection_editor_parent_class

static PikaDockedInterface *parent_docked_iface = NULL;


static void
pika_selection_editor_class_init (PikaSelectionEditorClass *klass)
{
  GObjectClass         *object_class       = G_OBJECT_CLASS (klass);
  PikaImageEditorClass *image_editor_class = PIKA_IMAGE_EDITOR_CLASS (klass);

  object_class->constructed     = pika_selection_editor_constructed;

  image_editor_class->set_image = pika_selection_editor_set_image;
}

static void
pika_selection_editor_docked_iface_init (PikaDockedInterface *iface)
{
  parent_docked_iface = g_type_interface_peek_parent (iface);

  if (! parent_docked_iface)
    parent_docked_iface = g_type_default_interface_peek (PIKA_TYPE_DOCKED);

  iface->set_context = pika_selection_editor_set_context;
}

static void
pika_selection_editor_init (PikaSelectionEditor *editor)
{
  GtkWidget *frame;

  frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
  gtk_box_pack_start (GTK_BOX (editor), frame, TRUE, TRUE, 0);
  gtk_widget_show (frame);

  editor->view = pika_view_new_by_types (NULL,
                                         PIKA_TYPE_VIEW,
                                         PIKA_TYPE_SELECTION,
                                         PIKA_VIEW_SIZE_HUGE,
                                         0, TRUE);
  pika_view_renderer_set_background (PIKA_VIEW (editor->view)->renderer,
                                     PIKA_ICON_TEXTURE);
  gtk_widget_set_size_request (editor->view,
                               PIKA_VIEW_SIZE_HUGE, PIKA_VIEW_SIZE_HUGE);
  pika_view_set_expand (PIKA_VIEW (editor->view), TRUE);
  gtk_container_add (GTK_CONTAINER (frame), editor->view);
  gtk_widget_show (editor->view);

  g_signal_connect (editor->view, "button-press-event",
                    G_CALLBACK (pika_selection_view_button_press),
                    editor);

  pika_dnd_color_dest_add (editor->view,
                           pika_selection_editor_drop_color,
                           editor);

  gtk_widget_set_sensitive (GTK_WIDGET (editor), FALSE);
}

static void
pika_selection_editor_constructed (GObject *object)
{
  PikaSelectionEditor *editor = PIKA_SELECTION_EDITOR (object);

  G_OBJECT_CLASS (parent_class)->constructed (object);

  editor->all_button =
    pika_editor_add_action_button (PIKA_EDITOR (editor), "select",
                                   "select-all", NULL);

  editor->none_button =
    pika_editor_add_action_button (PIKA_EDITOR (editor), "select",
                                   "select-none", NULL);

  editor->invert_button =
    pika_editor_add_action_button (PIKA_EDITOR (editor), "select",
                                   "select-invert", NULL);

  editor->save_button =
    pika_editor_add_action_button (PIKA_EDITOR (editor), "select",
                                   "select-save", NULL);

  editor->path_button =
    pika_editor_add_action_button (PIKA_EDITOR (editor), "vectors",
                                   "vectors-selection-to-vectors",
                                   "vectors-selection-to-vectors-advanced",
                                   GDK_SHIFT_MASK,
                                   NULL);

  editor->stroke_button =
    pika_editor_add_action_button (PIKA_EDITOR (editor), "select",
                                   "select-stroke",
                                   "select-stroke-last-values",
                                   GDK_SHIFT_MASK,
                                   NULL);
}

static void
pika_selection_editor_set_image (PikaImageEditor *image_editor,
                                 PikaImage       *image)
{
  PikaSelectionEditor *editor = PIKA_SELECTION_EDITOR (image_editor);

  if (image_editor->image)
    {
      g_signal_handlers_disconnect_by_func (image_editor->image,
                                            pika_selection_editor_mask_changed,
                                            editor);
    }

  PIKA_IMAGE_EDITOR_CLASS (parent_class)->set_image (image_editor, image);

  if (image)
    {
      g_signal_connect (image, "mask-changed",
                        G_CALLBACK (pika_selection_editor_mask_changed),
                        editor);

      pika_view_set_viewable (PIKA_VIEW (editor->view),
                              PIKA_VIEWABLE (pika_image_get_mask (image)));
    }
  else
    {
      pika_view_set_viewable (PIKA_VIEW (editor->view), NULL);
    }
}

static void
pika_selection_editor_set_context (PikaDocked  *docked,
                                   PikaContext *context)
{
  PikaSelectionEditor *editor = PIKA_SELECTION_EDITOR (docked);

  parent_docked_iface->set_context (docked, context);

  pika_view_renderer_set_context (PIKA_VIEW (editor->view)->renderer,
                                  context);
}


/*  public functions  */

GtkWidget *
pika_selection_editor_new (PikaMenuFactory *menu_factory)
{
  g_return_val_if_fail (PIKA_IS_MENU_FACTORY (menu_factory), NULL);

  return g_object_new (PIKA_TYPE_SELECTION_EDITOR,
                       "menu-factory",    menu_factory,
                       "menu-identifier", "<Selection>",
                       "ui-path",         "/selection-popup",
                       NULL);
}

static gboolean
pika_selection_view_button_press (GtkWidget           *widget,
                                  GdkEventButton      *bevent,
                                  PikaSelectionEditor *editor)
{
  PikaImageEditor         *image_editor = PIKA_IMAGE_EDITOR (editor);
  PikaViewRenderer        *renderer;
  PikaToolInfo            *tool_info;
  PikaSelectionOptions    *sel_options;
  PikaRegionSelectOptions *options;
  PikaChannelOps           operation;
  GList                   *drawables;
  gint                     x, y;
  PikaRGB                  color;

  if (! image_editor->image)
    return TRUE;

  renderer = PIKA_VIEW (editor->view)->renderer;

  tool_info = pika_get_tool_info (image_editor->image->pika,
                                  "pika-by-color-select-tool");

  if (! tool_info)
    return TRUE;

  sel_options = PIKA_SELECTION_OPTIONS (tool_info->tool_options);
  options     = PIKA_REGION_SELECT_OPTIONS (tool_info->tool_options);

  drawables = pika_image_get_selected_drawables (image_editor->image);

  if (! drawables)
    return TRUE;

  operation = pika_modifiers_to_channel_op (bevent->state);

  x = pika_image_get_width  (image_editor->image) * bevent->x / renderer->width;
  y = pika_image_get_height (image_editor->image) * bevent->y / renderer->height;

  if (pika_image_pick_color (image_editor->image, drawables, x, y,
                             FALSE, options->sample_merged,
                             FALSE, 0.0,
                             NULL,
                             NULL, &color))
    {
      pika_channel_select_by_color (pika_image_get_mask (image_editor->image),
                                    drawables,
                                    options->sample_merged,
                                    &color,
                                    options->threshold / 255.0,
                                    options->select_transparent,
                                    options->select_criterion,
                                    operation,
                                    sel_options->antialias,
                                    sel_options->feather,
                                    sel_options->feather_radius,
                                    sel_options->feather_radius);
      pika_image_flush (image_editor->image);
    }
  g_list_free (drawables);

  return TRUE;
}

static void
pika_selection_editor_drop_color (GtkWidget     *widget,
                                  gint           x,
                                  gint           y,
                                  const PikaRGB *color,
                                  gpointer       data)
{
  PikaImageEditor         *editor = PIKA_IMAGE_EDITOR (data);
  PikaToolInfo            *tool_info;
  PikaSelectionOptions    *sel_options;
  PikaRegionSelectOptions *options;
  GList                   *drawables;

  if (! editor->image)
    return;

  tool_info = pika_get_tool_info (editor->image->pika,
                                  "pika-by-color-select-tool");
  if (! tool_info)
    return;

  sel_options = PIKA_SELECTION_OPTIONS (tool_info->tool_options);
  options     = PIKA_REGION_SELECT_OPTIONS (tool_info->tool_options);

  drawables = pika_image_get_selected_drawables (editor->image);

  if (! drawables)
    return;

  pika_channel_select_by_color (pika_image_get_mask (editor->image),
                                drawables,
                                options->sample_merged,
                                color,
                                options->threshold / 255.0,
                                options->select_transparent,
                                options->select_criterion,
                                sel_options->operation,
                                sel_options->antialias,
                                sel_options->feather,
                                sel_options->feather_radius,
                                sel_options->feather_radius);
  pika_image_flush (editor->image);
  g_list_free (drawables);
}

static void
pika_selection_editor_mask_changed (PikaImage           *image,
                                    PikaSelectionEditor *editor)
{
  pika_view_renderer_invalidate (PIKA_VIEW (editor->view)->renderer);
}
