/* 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 "libpikawidgets/pikawidgets.h"

#include "tools-types.h"

#include "paint/pikadodgeburnoptions.h"

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

#include "pikadodgeburntool.h"
#include "pikapaintoptions-gui.h"
#include "pikatoolcontrol.h"

#include "pika-intl.h"


static void   pika_dodge_burn_tool_modifier_key  (PikaTool          *tool,
                                                  GdkModifierType    key,
                                                  gboolean           press,
                                                  GdkModifierType    state,
                                                  PikaDisplay       *display);
static void   pika_dodge_burn_tool_cursor_update (PikaTool          *tool,
                                                  const PikaCoords  *coords,
                                                  GdkModifierType    state,
                                                  PikaDisplay       *display);
static void   pika_dodge_burn_tool_oper_update   (PikaTool          *tool,
                                                  const PikaCoords  *coords,
                                                  GdkModifierType    state,
                                                  gboolean           proximity,
                                                  PikaDisplay       *display);
static void   pika_dodge_burn_tool_status_update (PikaTool          *tool,
                                                  PikaDodgeBurnType  type);

static GtkWidget * pika_dodge_burn_options_gui   (PikaToolOptions   *tool_options);


G_DEFINE_TYPE (PikaDodgeBurnTool, pika_dodge_burn_tool, PIKA_TYPE_BRUSH_TOOL)

#define parent_class pika_dodge_burn_tool_parent_class


void
pika_dodge_burn_tool_register (PikaToolRegisterCallback  callback,
                               gpointer                  data)
{
  (* callback) (PIKA_TYPE_DODGE_BURN_TOOL,
                PIKA_TYPE_DODGE_BURN_OPTIONS,
                pika_dodge_burn_options_gui,
                PIKA_PAINT_OPTIONS_CONTEXT_MASK,
                "pika-dodge-burn-tool",
                _("Dodge / Burn"),
                _("Dodge / Burn Tool: Selectively lighten or darken using a brush"),
                N_("Dod_ge / Burn"), "<shift>D",
                NULL, PIKA_HELP_TOOL_DODGE_BURN,
                PIKA_ICON_TOOL_DODGE,
                data);
}

static void
pika_dodge_burn_tool_class_init (PikaDodgeBurnToolClass *klass)
{
  PikaToolClass *tool_class = PIKA_TOOL_CLASS (klass);

  tool_class->modifier_key  = pika_dodge_burn_tool_modifier_key;
  tool_class->cursor_update = pika_dodge_burn_tool_cursor_update;
  tool_class->oper_update   = pika_dodge_burn_tool_oper_update;
}

static void
pika_dodge_burn_tool_init (PikaDodgeBurnTool *dodgeburn)
{
  PikaTool *tool = PIKA_TOOL (dodgeburn);

  pika_tool_control_set_tool_cursor        (tool->control,
                                            PIKA_TOOL_CURSOR_DODGE);
  pika_tool_control_set_toggle_tool_cursor (tool->control,
                                            PIKA_TOOL_CURSOR_BURN);

  pika_dodge_burn_tool_status_update (tool, PIKA_DODGE_BURN_TYPE_BURN);
}

static void
pika_dodge_burn_tool_modifier_key (PikaTool        *tool,
                                   GdkModifierType  key,
                                   gboolean         press,
                                   GdkModifierType  state,
                                   PikaDisplay     *display)
{
  PikaDodgeBurnTool    *dodgeburn   = PIKA_DODGE_BURN_TOOL (tool);
  PikaDodgeBurnOptions *options     = PIKA_DODGE_BURN_TOOL_GET_OPTIONS (tool);
  GdkModifierType       line_mask   = PIKA_PAINT_TOOL_LINE_MASK;
  GdkModifierType       toggle_mask = pika_get_toggle_behavior_mask ();

  if ((key == toggle_mask   &&
      ! (state & line_mask) && /* leave stuff untouched in line draw mode */
       press != dodgeburn->toggled)

      ||

      (key == line_mask   && /* toggle back after keypresses CTRL(hold)->  */
       ! press            && /* SHIFT(hold)->CTRL(release)->SHIFT(release) */
       dodgeburn->toggled &&
       ! (state & toggle_mask)))
    {
      dodgeburn->toggled = press;

      switch (options->type)
        {
        case PIKA_DODGE_BURN_TYPE_DODGE:
          g_object_set (options, "type", PIKA_DODGE_BURN_TYPE_BURN, NULL);
          break;

        case PIKA_DODGE_BURN_TYPE_BURN:
          g_object_set (options, "type", PIKA_DODGE_BURN_TYPE_DODGE, NULL);
          break;

        default:
          break;
        }
    }

  PIKA_TOOL_CLASS (parent_class)->modifier_key (tool, key, press, state,
                                                display);
}

static void
pika_dodge_burn_tool_cursor_update (PikaTool         *tool,
                                    const PikaCoords *coords,
                                    GdkModifierType   state,
                                    PikaDisplay      *display)
{
  PikaDodgeBurnOptions *options = PIKA_DODGE_BURN_TOOL_GET_OPTIONS (tool);

  pika_tool_control_set_toggled (tool->control,
                                 options->type == PIKA_DODGE_BURN_TYPE_BURN);

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

static void
pika_dodge_burn_tool_oper_update (PikaTool         *tool,
                                  const PikaCoords *coords,
                                  GdkModifierType   state,
                                  gboolean          proximity,
                                  PikaDisplay      *display)
{
  PikaDodgeBurnOptions *options = PIKA_DODGE_BURN_TOOL_GET_OPTIONS (tool);

  pika_dodge_burn_tool_status_update (tool, options->type);

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

static void
pika_dodge_burn_tool_status_update (PikaTool          *tool,
                                    PikaDodgeBurnType  type)
{
  PikaPaintTool *paint_tool = PIKA_PAINT_TOOL (tool);

  switch (type)
    {
    case PIKA_DODGE_BURN_TYPE_DODGE:
      paint_tool->status      = _("Click to dodge");
      paint_tool->status_line = _("Click to dodge the line");
      paint_tool->status_ctrl = _("%s to burn");
      break;

    case PIKA_DODGE_BURN_TYPE_BURN:
      paint_tool->status      = _("Click to burn");
      paint_tool->status_line = _("Click to burn the line");
      paint_tool->status_ctrl = _("%s to dodge");
      break;

    default:
      break;
    }
}


/*  tool options stuff  */

static GtkWidget *
pika_dodge_burn_options_gui (PikaToolOptions *tool_options)
{
  GObject         *config = G_OBJECT (tool_options);
  GtkWidget       *vbox   = pika_paint_options_gui (tool_options);
  GtkWidget       *frame;
  GtkWidget       *scale;
  gchar           *str;
  GdkModifierType  toggle_mask;

  toggle_mask = pika_get_toggle_behavior_mask ();

  /* the type (dodge or burn) */
  str = g_strdup_printf (_("Type  (%s)"),
                         pika_get_mod_string (toggle_mask));
  frame = pika_prop_enum_radio_frame_new (config, "type",
                                          str, 0, 0);
  gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
  g_free (str);

  /*  mode (highlights, midtones, or shadows)  */
  frame = pika_prop_enum_radio_frame_new (config, "mode", NULL,
                                          0, 0);
  gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);

  /*  the exposure scale  */
  scale = pika_prop_spin_scale_new (config, "exposure",
                                    1.0, 10.0, 1);
  gtk_box_pack_start (GTK_BOX (vbox), scale, FALSE, FALSE, 0);

  return vbox;
}
