/* 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 "tools-types.h"

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

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

#include "pikahandletransformoptions.h"
#include "pikahandletransformtool.h"
#include "pikatoolcontrol.h"

#include "pika-intl.h"


/* the transformation is defined by 8 points:
 *
 * 4 points on the original image and 4 corresponding points on the
 * transformed image. The first N_HANDLES points on the transformed
 * image are visible as handles.
 *
 * For these handles, the constants TRANSFORM_HANDLE_N,
 * TRANSFORM_HANDLE_S, TRANSFORM_HANDLE_E and TRANSFORM_HANDLE_W are
 * used. Actually, it makes no sense to name the handles with north,
 * south, east, and west.  But this way, we don't need to define even
 * more enum constants.
 */

/*  index into trans_info array  */
enum
{
  X0,
  Y0,
  X1,
  Y1,
  X2,
  Y2,
  X3,
  Y3,
  OX0,
  OY0,
  OX1,
  OY1,
  OX2,
  OY2,
  OX3,
  OY3,
  N_HANDLES
};


/*  local function prototypes  */

static void             pika_handle_transform_tool_modifier_key   (PikaTool                 *tool,
                                                                   GdkModifierType           key,
                                                                   gboolean                  press,
                                                                   GdkModifierType           state,
                                                                   PikaDisplay              *display);

static void             pika_handle_transform_tool_matrix_to_info (PikaTransformGridTool    *tg_tool,
                                                                   const PikaMatrix3        *transform);
static void             pika_handle_transform_tool_prepare        (PikaTransformGridTool    *tg_tool);
static PikaToolWidget * pika_handle_transform_tool_get_widget     (PikaTransformGridTool    *tg_tool);
static void             pika_handle_transform_tool_update_widget  (PikaTransformGridTool    *tg_tool);
static void             pika_handle_transform_tool_widget_changed (PikaTransformGridTool    *tg_tool);

static void             pika_handle_transform_tool_info_to_points (PikaGenericTransformTool *generic);


G_DEFINE_TYPE (PikaHandleTransformTool, pika_handle_transform_tool,
               PIKA_TYPE_GENERIC_TRANSFORM_TOOL)

#define parent_class pika_handle_transform_tool_parent_class


void
pika_handle_transform_tool_register (PikaToolRegisterCallback  callback,
                                     gpointer                  data)
{
  (* callback) (PIKA_TYPE_HANDLE_TRANSFORM_TOOL,
                PIKA_TYPE_HANDLE_TRANSFORM_OPTIONS,
                pika_handle_transform_options_gui,
                PIKA_CONTEXT_PROP_MASK_BACKGROUND,
                "pika-handle-transform-tool",
                _("Handle Transform"),
                _("Handle Transform Tool: "
                  "Deform the layer, selection or path with handles"),
                N_("_Handle Transform"), "<shift>L",
                NULL, PIKA_HELP_TOOL_HANDLE_TRANSFORM,
                PIKA_ICON_TOOL_HANDLE_TRANSFORM,
                data);
}

static void
pika_handle_transform_tool_class_init (PikaHandleTransformToolClass *klass)
{
  PikaToolClass                 *tool_class    = PIKA_TOOL_CLASS (klass);
  PikaTransformToolClass        *tr_class   = PIKA_TRANSFORM_TOOL_CLASS (klass);
  PikaTransformGridToolClass    *tg_class   = PIKA_TRANSFORM_GRID_TOOL_CLASS (klass);
  PikaGenericTransformToolClass *generic_class = PIKA_GENERIC_TRANSFORM_TOOL_CLASS (klass);

  tool_class->modifier_key      = pika_handle_transform_tool_modifier_key;

  tg_class->matrix_to_info      = pika_handle_transform_tool_matrix_to_info;
  tg_class->prepare             = pika_handle_transform_tool_prepare;
  tg_class->get_widget          = pika_handle_transform_tool_get_widget;
  tg_class->update_widget       = pika_handle_transform_tool_update_widget;
  tg_class->widget_changed      = pika_handle_transform_tool_widget_changed;

  generic_class->info_to_points = pika_handle_transform_tool_info_to_points;

  tr_class->undo_desc           = C_("undo-type", "Handle transform");
  tr_class->progress_text       = _("Handle transformation");
}

static void
pika_handle_transform_tool_init (PikaHandleTransformTool *ht_tool)
{
  ht_tool->saved_handle_mode = PIKA_HANDLE_MODE_ADD_TRANSFORM;
}

static void
pika_handle_transform_tool_modifier_key (PikaTool        *tool,
                                         GdkModifierType  key,
                                         gboolean         press,
                                         GdkModifierType  state,
                                         PikaDisplay     *display)
{
  PikaHandleTransformTool    *ht_tool = PIKA_HANDLE_TRANSFORM_TOOL (tool);
  PikaHandleTransformOptions *options;
  GdkModifierType             shift   = pika_get_extend_selection_mask ();
  GdkModifierType             ctrl    = pika_get_constrain_behavior_mask ();
  PikaTransformHandleMode     handle_mode;

  options = PIKA_HANDLE_TRANSFORM_TOOL_GET_OPTIONS (tool);

  handle_mode = options->handle_mode;

  if (press)
    {
      if (key == (state & (shift | ctrl)))
        {
          /*  first modifier pressed  */
          ht_tool->saved_handle_mode = options->handle_mode;
        }
    }
  else
    {
      if (! (state & (shift | ctrl)))
        {
          /*  last modifier released  */
          handle_mode = ht_tool->saved_handle_mode;
        }
    }

  if (state & shift)
    {
      handle_mode = PIKA_HANDLE_MODE_MOVE;
    }
  else if (state & ctrl)
    {
      handle_mode = PIKA_HANDLE_MODE_REMOVE;
    }

  if (handle_mode != options->handle_mode)
    {
      g_object_set (options,
                    "handle-mode", handle_mode,
                    NULL);
    }

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

static void
pika_handle_transform_tool_matrix_to_info (PikaTransformGridTool *tg_tool,
                                           const PikaMatrix3     *transform)
{
  pika_matrix3_transform_point (transform,
                                tg_tool->trans_info[OX0],
                                tg_tool->trans_info[OY0],
                                &tg_tool->trans_info[X0],
                                &tg_tool->trans_info[Y0]);
  pika_matrix3_transform_point (transform,
                                tg_tool->trans_info[OX1],
                                tg_tool->trans_info[OY1],
                                &tg_tool->trans_info[X1],
                                &tg_tool->trans_info[Y1]);
  pika_matrix3_transform_point (transform,
                                tg_tool->trans_info[OX2],
                                tg_tool->trans_info[OY2],
                                &tg_tool->trans_info[X2],
                                &tg_tool->trans_info[Y2]);
  pika_matrix3_transform_point (transform,
                                tg_tool->trans_info[OX3],
                                tg_tool->trans_info[OY3],
                                &tg_tool->trans_info[X3],
                                &tg_tool->trans_info[Y3]);
}

static void
pika_handle_transform_tool_prepare (PikaTransformGridTool *tg_tool)
{
  PikaTransformTool *tr_tool = PIKA_TRANSFORM_TOOL (tg_tool);

  PIKA_TRANSFORM_GRID_TOOL_CLASS (parent_class)->prepare (tg_tool);

  tg_tool->trans_info[X0]        = (gdouble) tr_tool->x1;
  tg_tool->trans_info[Y0]        = (gdouble) tr_tool->y1;
  tg_tool->trans_info[X1]        = (gdouble) tr_tool->x2;
  tg_tool->trans_info[Y1]        = (gdouble) tr_tool->y1;
  tg_tool->trans_info[X2]        = (gdouble) tr_tool->x1;
  tg_tool->trans_info[Y2]        = (gdouble) tr_tool->y2;
  tg_tool->trans_info[X3]        = (gdouble) tr_tool->x2;
  tg_tool->trans_info[Y3]        = (gdouble) tr_tool->y2;
  tg_tool->trans_info[OX0]       = (gdouble) tr_tool->x1;
  tg_tool->trans_info[OY0]       = (gdouble) tr_tool->y1;
  tg_tool->trans_info[OX1]       = (gdouble) tr_tool->x2;
  tg_tool->trans_info[OY1]       = (gdouble) tr_tool->y1;
  tg_tool->trans_info[OX2]       = (gdouble) tr_tool->x1;
  tg_tool->trans_info[OY2]       = (gdouble) tr_tool->y2;
  tg_tool->trans_info[OX3]       = (gdouble) tr_tool->x2;
  tg_tool->trans_info[OY3]       = (gdouble) tr_tool->y2;
  tg_tool->trans_info[N_HANDLES] = 0;
}

static PikaToolWidget *
pika_handle_transform_tool_get_widget (PikaTransformGridTool *tg_tool)
{
  PikaTool                   *tool    = PIKA_TOOL (tg_tool);
  PikaTransformTool          *tr_tool = PIKA_TRANSFORM_TOOL (tg_tool);
  PikaHandleTransformOptions *options;
  PikaDisplayShell           *shell   = pika_display_get_shell (tool->display);
  PikaToolWidget             *widget;

  options = PIKA_HANDLE_TRANSFORM_TOOL_GET_OPTIONS (tg_tool);

  widget = pika_tool_handle_grid_new (shell,
                                      tr_tool->x1,
                                      tr_tool->y1,
                                      tr_tool->x2,
                                      tr_tool->y2);

  g_object_set (widget,
                "n-handles", (gint) tg_tool->trans_info[N_HANDLES],
                "orig-x1",   tg_tool->trans_info[OX0],
                "orig-y1",   tg_tool->trans_info[OY0],
                "orig-x2",   tg_tool->trans_info[OX1],
                "orig-y2",   tg_tool->trans_info[OY1],
                "orig-x3",   tg_tool->trans_info[OX2],
                "orig-y3",   tg_tool->trans_info[OY2],
                "orig-x4",   tg_tool->trans_info[OX3],
                "orig-y4",   tg_tool->trans_info[OY3],
                "trans-x1",  tg_tool->trans_info[X0],
                "trans-y1",  tg_tool->trans_info[Y0],
                "trans-x2",  tg_tool->trans_info[X1],
                "trans-y2",  tg_tool->trans_info[Y1],
                "trans-x3",  tg_tool->trans_info[X2],
                "trans-y3",  tg_tool->trans_info[Y2],
                "trans-x4",  tg_tool->trans_info[X3],
                "trans-y4",  tg_tool->trans_info[Y3],
                NULL);

  g_object_bind_property (G_OBJECT (options), "handle-mode",
                          G_OBJECT (widget),  "handle-mode",
                          G_BINDING_SYNC_CREATE |
                          G_BINDING_BIDIRECTIONAL);

  return widget;
}

static void
pika_handle_transform_tool_update_widget (PikaTransformGridTool *tg_tool)
{
  PikaMatrix3 transform;
  gboolean    transform_valid;

  PIKA_TRANSFORM_GRID_TOOL_CLASS (parent_class)->update_widget (tg_tool);

  transform_valid = pika_transform_grid_tool_info_to_matrix (tg_tool,
                                                             &transform);

  g_object_set (tg_tool->widget,
                "show-guides", transform_valid,
                "n-handles",   (gint) tg_tool->trans_info[N_HANDLES],
                "orig-x1",     tg_tool->trans_info[OX0],
                "orig-y1",     tg_tool->trans_info[OY0],
                "orig-x2",     tg_tool->trans_info[OX1],
                "orig-y2",     tg_tool->trans_info[OY1],
                "orig-x3",     tg_tool->trans_info[OX2],
                "orig-y3",     tg_tool->trans_info[OY2],
                "orig-x4",     tg_tool->trans_info[OX3],
                "orig-y4",     tg_tool->trans_info[OY3],
                "trans-x1",    tg_tool->trans_info[X0],
                "trans-y1",    tg_tool->trans_info[Y0],
                "trans-x2",    tg_tool->trans_info[X1],
                "trans-y2",    tg_tool->trans_info[Y1],
                "trans-x3",    tg_tool->trans_info[X2],
                "trans-y3",    tg_tool->trans_info[Y2],
                "trans-x4",    tg_tool->trans_info[X3],
                "trans-y4",    tg_tool->trans_info[Y3],
                NULL);
}

static void
pika_handle_transform_tool_widget_changed (PikaTransformGridTool *tg_tool)
{
  gint n_handles;

  g_object_get (tg_tool->widget,
                "n-handles", &n_handles,
                "orig-x1",   &tg_tool->trans_info[OX0],
                "orig-y1",   &tg_tool->trans_info[OY0],
                "orig-x2",   &tg_tool->trans_info[OX1],
                "orig-y2",   &tg_tool->trans_info[OY1],
                "orig-x3",   &tg_tool->trans_info[OX2],
                "orig-y3",   &tg_tool->trans_info[OY2],
                "orig-x4",   &tg_tool->trans_info[OX3],
                "orig-y4",   &tg_tool->trans_info[OY3],
                "trans-x1",  &tg_tool->trans_info[X0],
                "trans-y1",  &tg_tool->trans_info[Y0],
                "trans-x2",  &tg_tool->trans_info[X1],
                "trans-y2",  &tg_tool->trans_info[Y1],
                "trans-x3",  &tg_tool->trans_info[X2],
                "trans-y3",  &tg_tool->trans_info[Y2],
                "trans-x4",  &tg_tool->trans_info[X3],
                "trans-y4",  &tg_tool->trans_info[Y3],
                NULL);

  tg_tool->trans_info[N_HANDLES] = n_handles;

  PIKA_TRANSFORM_GRID_TOOL_CLASS (parent_class)->widget_changed (tg_tool);
}

static void
pika_handle_transform_tool_info_to_points (PikaGenericTransformTool *generic)
{
  PikaTransformGridTool *tg_tool = PIKA_TRANSFORM_GRID_TOOL (generic);

  generic->input_points[0]  = (PikaVector2) {tg_tool->trans_info[OX0],
                                             tg_tool->trans_info[OY0]};
  generic->input_points[1]  = (PikaVector2) {tg_tool->trans_info[OX1],
                                             tg_tool->trans_info[OY1]};
  generic->input_points[2]  = (PikaVector2) {tg_tool->trans_info[OX2],
                                             tg_tool->trans_info[OY2]};
  generic->input_points[3]  = (PikaVector2) {tg_tool->trans_info[OX3],
                                             tg_tool->trans_info[OY3]};

  generic->output_points[0] = (PikaVector2) {tg_tool->trans_info[X0],
                                             tg_tool->trans_info[Y0]};
  generic->output_points[1] = (PikaVector2) {tg_tool->trans_info[X1],
                                             tg_tool->trans_info[Y1]};
  generic->output_points[2] = (PikaVector2) {tg_tool->trans_info[X2],
                                             tg_tool->trans_info[Y2]};
  generic->output_points[3] = (PikaVector2) {tg_tool->trans_info[X3],
                                             tg_tool->trans_info[Y3]};
}
