/* 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-2001 Spencer Kimball, Peter Mattis, and others
 *
 * 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 "libpikacolor/pikacolor.h"
#include "libpikawidgets/pikawidgets.h"

#include "tools-types.h"

#include "config/pikacoreconfig.h"

#include "core/pika.h"
#include "core/pikadrawable.h"
#include "core/pikaimage.h"
#include "core/pikatoolinfo.h"

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

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

#include "pikacolorpickeroptions.h"
#include "pikacolorpickertool.h"
#include "pikatoolcontrol.h"

#include "pika-intl.h"


/*  local function prototypes  */

static void   pika_color_picker_tool_constructed   (GObject             *object);
static void   pika_color_picker_tool_dispose       (GObject             *object);

static void   pika_color_picker_tool_control       (PikaTool            *tool,
                                                    PikaToolAction       action,
                                                    PikaDisplay         *display);
static void   pika_color_picker_tool_modifier_key  (PikaTool            *tool,
                                                    GdkModifierType      key,
                                                    gboolean             press,
                                                    GdkModifierType      state,
                                                    PikaDisplay         *display);
static void   pika_color_picker_tool_oper_update   (PikaTool            *tool,
                                                    const PikaCoords    *coords,
                                                    GdkModifierType      state,
                                                    gboolean             proximity,
                                                    PikaDisplay         *display);

static void   pika_color_picker_tool_picked        (PikaColorTool       *color_tool,
                                                    const PikaCoords    *coords,
                                                    PikaDisplay         *display,
                                                    PikaColorPickState   pick_state,
                                                    const Babl          *sample_format,
                                                    gpointer             pixel,
                                                    const PikaRGB       *color);

static void   pika_color_picker_tool_info_create   (PikaColorPickerTool *picker_tool,
                                                    PikaDisplay         *display);
static void   pika_color_picker_tool_info_response (PikaToolGui         *gui,
                                                    gint                 response_id,
                                                    PikaColorPickerTool *picker_tool);
static void   pika_color_picker_tool_info_update   (PikaColorPickerTool *picker_tool,
                                                    PikaDisplay         *display,
                                                    gboolean             sample_average,
                                                    const Babl          *sample_format,
                                                    gpointer             pixel,
                                                    const PikaRGB       *color,
                                                    gint                 x,
                                                    gint                 y);


G_DEFINE_TYPE (PikaColorPickerTool, pika_color_picker_tool,
               PIKA_TYPE_COLOR_TOOL)

#define parent_class pika_color_picker_tool_parent_class


void
pika_color_picker_tool_register (PikaToolRegisterCallback  callback,
                                 gpointer                  data)
{
  (* callback) (PIKA_TYPE_COLOR_PICKER_TOOL,
                PIKA_TYPE_COLOR_PICKER_OPTIONS,
                pika_color_picker_options_gui,
                PIKA_CONTEXT_PROP_MASK_FOREGROUND |
                PIKA_CONTEXT_PROP_MASK_BACKGROUND,
                "pika-color-picker-tool",
                _("Color Picker"),
                _("Color Picker Tool: Set colors from image pixels"),
                N_("C_olor Picker"), "O",
                NULL, PIKA_HELP_TOOL_COLOR_PICKER,
                PIKA_ICON_TOOL_COLOR_PICKER,
                data);
}

static void
pika_color_picker_tool_class_init (PikaColorPickerToolClass *klass)
{
  GObjectClass       *object_class     = G_OBJECT_CLASS (klass);
  PikaToolClass      *tool_class       = PIKA_TOOL_CLASS (klass);
  PikaColorToolClass *color_tool_class = PIKA_COLOR_TOOL_CLASS (klass);

  object_class->constructed = pika_color_picker_tool_constructed;
  object_class->dispose     = pika_color_picker_tool_dispose;

  tool_class->control       = pika_color_picker_tool_control;
  tool_class->modifier_key  = pika_color_picker_tool_modifier_key;
  tool_class->oper_update   = pika_color_picker_tool_oper_update;

  color_tool_class->picked  = pika_color_picker_tool_picked;
}

static void
pika_color_picker_tool_init (PikaColorPickerTool *picker_tool)
{
  PikaColorTool *color_tool = PIKA_COLOR_TOOL (picker_tool);

  color_tool->pick_target = PIKA_COLOR_PICK_TARGET_FOREGROUND;
}

static void
pika_color_picker_tool_constructed (GObject *object)
{
  PikaTool *tool = PIKA_TOOL (object);

  G_OBJECT_CLASS (parent_class)->constructed (object);

  pika_color_tool_enable (PIKA_COLOR_TOOL (object),
                          PIKA_COLOR_TOOL_GET_OPTIONS (tool));
}

static void
pika_color_picker_tool_dispose (GObject *object)
{
  PikaColorPickerTool *picker_tool = PIKA_COLOR_PICKER_TOOL (object);

  g_clear_object (&picker_tool->gui);
  picker_tool->color_area   = NULL;
  picker_tool->color_frame1 = NULL;
  picker_tool->color_frame2 = NULL;

  G_OBJECT_CLASS (parent_class)->dispose (object);
}

static void
pika_color_picker_tool_control (PikaTool       *tool,
                                PikaToolAction  action,
                                PikaDisplay    *display)
{
  PikaColorPickerTool *picker_tool = PIKA_COLOR_PICKER_TOOL (tool);

  switch (action)
    {
    case PIKA_TOOL_ACTION_PAUSE:
    case PIKA_TOOL_ACTION_RESUME:
      break;

    case PIKA_TOOL_ACTION_HALT:
      if (picker_tool->gui)
        pika_tool_gui_hide (picker_tool->gui);
      break;

    case PIKA_TOOL_ACTION_COMMIT:
      break;
    }

  PIKA_TOOL_CLASS (parent_class)->control (tool, action, display);
}

static void
pika_color_picker_tool_modifier_key (PikaTool        *tool,
                                     GdkModifierType  key,
                                     gboolean         press,
                                     GdkModifierType  state,
                                     PikaDisplay     *display)
{
  PikaColorPickerOptions *options = PIKA_COLOR_PICKER_TOOL_GET_OPTIONS (tool);

  if (key == pika_get_extend_selection_mask ())
    {
      g_object_set (options, "use-info-window", ! options->use_info_window,
                    NULL);
    }
  else if (key == pika_get_toggle_behavior_mask ())
    {
      switch (options->pick_target)
        {
        case PIKA_COLOR_PICK_TARGET_FOREGROUND:
          g_object_set (options,
                        "pick-target", PIKA_COLOR_PICK_TARGET_BACKGROUND,
                        NULL);
          break;

        case PIKA_COLOR_PICK_TARGET_BACKGROUND:
          g_object_set (options,
                        "pick-target", PIKA_COLOR_PICK_TARGET_FOREGROUND,
                        NULL);
          break;

        default:
          break;
        }

    }
}

static void
pika_color_picker_tool_oper_update (PikaTool         *tool,
                                    const PikaCoords *coords,
                                    GdkModifierType   state,
                                    gboolean          proximity,
                                    PikaDisplay      *display)
{
  PikaColorPickerTool    *picker_tool = PIKA_COLOR_PICKER_TOOL (tool);
  PikaColorPickerOptions *options = PIKA_COLOR_PICKER_TOOL_GET_OPTIONS (tool);

  PIKA_COLOR_TOOL (tool)->pick_target = options->pick_target;

  pika_tool_pop_status (tool, display);

  if (proximity)
    {
      gchar           *status_help = NULL;
      GdkModifierType  extend_mask = 0;
      GdkModifierType  toggle_mask;

      if (! picker_tool->gui)
        extend_mask = pika_get_extend_selection_mask ();

      toggle_mask = pika_get_toggle_behavior_mask ();

      switch (options->pick_target)
        {
        case PIKA_COLOR_PICK_TARGET_NONE:
          status_help = pika_suggest_modifiers (_("Click in any image to view"
                                                  " its color"),
                                                extend_mask & ~state,
                                                NULL, NULL, NULL);
          break;

        case PIKA_COLOR_PICK_TARGET_FOREGROUND:
          status_help = pika_suggest_modifiers (_("Click in any image to pick"
                                                  " the foreground color"),
                                                (extend_mask | toggle_mask) &
                                                ~state,
                                                NULL, NULL, NULL);
          break;

        case PIKA_COLOR_PICK_TARGET_BACKGROUND:
          status_help = pika_suggest_modifiers (_("Click in any image to pick"
                                                  " the background color"),
                                                (extend_mask | toggle_mask) &
                                                ~state,
                                                NULL, NULL, NULL);
          break;

        case PIKA_COLOR_PICK_TARGET_PALETTE:
          status_help = pika_suggest_modifiers (_("Click in any image to add"
                                                  " the color to the palette"),
                                                extend_mask & ~state,
                                                NULL, NULL, NULL);
          break;
        }

      if (status_help != NULL)
        {
          pika_tool_push_status (tool, display, "%s", status_help);
          g_free (status_help);
        }
    }

  PIKA_TOOL_CLASS (parent_class)->oper_update (tool, coords, state, proximity,
                                               display);
}

static void
pika_color_picker_tool_picked (PikaColorTool      *color_tool,
                               const PikaCoords   *coords,
                               PikaDisplay        *display,
                               PikaColorPickState  pick_state,
                               const Babl         *sample_format,
                               gpointer            pixel,
                               const PikaRGB      *color)
{
  PikaColorPickerTool    *picker_tool = PIKA_COLOR_PICKER_TOOL (color_tool);
  PikaColorPickerOptions *options;

  options = PIKA_COLOR_PICKER_TOOL_GET_OPTIONS (color_tool);

  if (options->use_info_window && ! picker_tool->gui)
    pika_color_picker_tool_info_create (picker_tool, display);

  if (picker_tool->gui &&
      (options->use_info_window ||
       pika_tool_gui_get_visible (picker_tool->gui)))
    {
      pika_color_picker_tool_info_update (picker_tool, display,
                                          PIKA_COLOR_OPTIONS (options)->sample_average,
                                          sample_format, pixel, color,
                                          (gint) floor (coords->x),
                                          (gint) floor (coords->y));
    }

  PIKA_COLOR_TOOL_CLASS (parent_class)->picked (color_tool,
                                                coords, display, pick_state,
                                                sample_format, pixel, color);
}

static void
pika_color_picker_tool_info_create (PikaColorPickerTool *picker_tool,
                                    PikaDisplay         *display)
{
  Pika             *pika      = pika_display_get_pika (display);
  PikaTool         *tool      = PIKA_TOOL (picker_tool);
  PikaToolOptions  *options   = PIKA_TOOL_GET_OPTIONS (tool);
  PikaContext      *context   = PIKA_CONTEXT (tool->tool_info->tool_options);
  PikaDisplayShell *shell     = pika_display_get_shell (display);
  PikaImage        *image     = pika_display_get_image (display);
  GList            *drawables = pika_image_get_selected_drawables (image);
  GtkWidget        *hbox;
  GtkWidget        *frame;
  PikaRGB           color;

  picker_tool->gui = pika_tool_gui_new (tool->tool_info,
                                        NULL,
                                        _("Color Picker Information"),
                                        NULL, NULL,
                                        pika_widget_get_monitor (GTK_WIDGET (shell)),
                                        TRUE,

                                        _("_Close"), GTK_RESPONSE_CLOSE,

                                        NULL);

  pika_tool_gui_set_auto_overlay (picker_tool->gui, TRUE);
  pika_tool_gui_set_focus_on_map (picker_tool->gui, FALSE);
  pika_tool_gui_set_viewables (picker_tool->gui, drawables);

  g_signal_connect (picker_tool->gui, "response",
                    G_CALLBACK (pika_color_picker_tool_info_response),
                    picker_tool);

  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
  gtk_box_pack_start (GTK_BOX (pika_tool_gui_get_vbox (picker_tool->gui)),
                      hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);

  picker_tool->color_frame1 = pika_color_frame_new (pika);
  pika_color_frame_set_color_config (PIKA_COLOR_FRAME (picker_tool->color_frame1),
                                     context->pika->config->color_management);
  pika_color_frame_set_has_coords (PIKA_COLOR_FRAME (picker_tool->color_frame1),
                                   TRUE);
  g_object_bind_property (options,                   "frame1-mode",
                          picker_tool->color_frame1, "mode",
                          G_BINDING_BIDIRECTIONAL |
                          G_BINDING_SYNC_CREATE);
  gtk_box_pack_start (GTK_BOX (hbox), picker_tool->color_frame1,
                      FALSE, FALSE, 0);
  gtk_widget_show (picker_tool->color_frame1);

  picker_tool->color_frame2 = pika_color_frame_new (pika);
  pika_color_frame_set_color_config (PIKA_COLOR_FRAME (picker_tool->color_frame2),
                                     context->pika->config->color_management);
  g_object_bind_property (options,                   "frame2-mode",
                          picker_tool->color_frame2, "mode",
                          G_BINDING_BIDIRECTIONAL |
                          G_BINDING_SYNC_CREATE);
  gtk_box_pack_start (GTK_BOX (hbox), picker_tool->color_frame2,
                      FALSE, FALSE, 0);
  gtk_widget_show (picker_tool->color_frame2);

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

  pika_rgba_set (&color, 0.0, 0.0, 0.0, 0.0);
  picker_tool->color_area =
    pika_color_area_new (&color,
                         drawables && pika_drawable_has_alpha (drawables->data) ?
                         PIKA_COLOR_AREA_LARGE_CHECKS :
                         PIKA_COLOR_AREA_FLAT,
                         GDK_BUTTON1_MASK | GDK_BUTTON2_MASK);
  pika_color_area_set_color_config (PIKA_COLOR_AREA (picker_tool->color_area),
                                    context->pika->config->color_management);
  gtk_widget_set_size_request (picker_tool->color_area, 48, -1);
  gtk_drag_dest_unset (picker_tool->color_area);
  gtk_container_add (GTK_CONTAINER (frame), picker_tool->color_area);
  gtk_widget_show (picker_tool->color_area);

  g_list_free (drawables);
}

static void
pika_color_picker_tool_info_response (PikaToolGui         *gui,
                                      gint                 response_id,
                                      PikaColorPickerTool *picker_tool)
{
  PikaTool *tool = PIKA_TOOL (picker_tool);

  pika_tool_control (tool, PIKA_TOOL_ACTION_HALT, NULL);
}

static void
pika_color_picker_tool_info_update (PikaColorPickerTool *picker_tool,
                                    PikaDisplay         *display,
                                    gboolean             sample_average,
                                    const Babl          *sample_format,
                                    gpointer             pixel,
                                    const PikaRGB       *color,
                                    gint                 x,
                                    gint                 y)
{
  PikaTool  *tool      = PIKA_TOOL (picker_tool);
  PikaImage *image     = pika_display_get_image (display);
  GList     *drawables = pika_image_get_selected_drawables (image);

  tool->display = display;

  pika_tool_gui_set_shell (picker_tool->gui,
                           pika_display_get_shell (display));
  pika_tool_gui_set_viewables (picker_tool->gui, drawables);
  g_list_free (drawables);

  pika_color_area_set_color (PIKA_COLOR_AREA (picker_tool->color_area),
                             color);

  pika_color_frame_set_color (PIKA_COLOR_FRAME (picker_tool->color_frame1),
                              sample_average, sample_format, pixel, color,
                              x, y);
  pika_color_frame_set_color (PIKA_COLOR_FRAME (picker_tool->color_frame2),
                              sample_average, sample_format, pixel, color,
                              x, y);

  pika_tool_gui_show (picker_tool->gui);
}
