/* 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 "display/pikadisplay.h"
#include "display/pikadisplayshell.h"
#include "display/pikadisplayshell-transform.h"
#include "display/pikatoolgui.h"
#include "display/pikatooltransformgrid.h"

#include "pikaperspectivetool.h"
#include "pikatoolcontrol.h"
#include "pikatransformgridoptions.h"

#include "pika-intl.h"


/*  index into trans_info array  */
enum
{
  X0,
  Y0,
  X1,
  Y1,
  X2,
  Y2,
  X3,
  Y3
};


/*  local function prototypes  */

static void             pika_perspective_tool_matrix_to_info (PikaTransformGridTool    *tg_tool,
                                                              const PikaMatrix3        *transform);
static void             pika_perspective_tool_prepare        (PikaTransformGridTool    *tg_tool);
static void             pika_perspective_tool_readjust       (PikaTransformGridTool    *tg_tool);
static PikaToolWidget * pika_perspective_tool_get_widget     (PikaTransformGridTool    *tg_tool);
static void             pika_perspective_tool_update_widget  (PikaTransformGridTool    *tg_tool);
static void             pika_perspective_tool_widget_changed (PikaTransformGridTool    *tg_tool);

static void             pika_perspective_tool_info_to_points (PikaGenericTransformTool *generic);


G_DEFINE_TYPE (PikaPerspectiveTool, pika_perspective_tool,
               PIKA_TYPE_GENERIC_TRANSFORM_TOOL)

#define parent_class pika_perspective_tool_parent_class


void
pika_perspective_tool_register (PikaToolRegisterCallback  callback,
                                gpointer                  data)
{
  (* callback) (PIKA_TYPE_PERSPECTIVE_TOOL,
                PIKA_TYPE_TRANSFORM_GRID_OPTIONS,
                pika_transform_grid_options_gui,
                PIKA_CONTEXT_PROP_MASK_BACKGROUND,
                "pika-perspective-tool",
                _("Perspective"),
                _("Perspective Tool: "
                  "Change perspective of the layer, selection or path"),
                N_("_Perspective"), "<shift>P",
                NULL, PIKA_HELP_TOOL_PERSPECTIVE,
                PIKA_ICON_TOOL_PERSPECTIVE,
                data);
}

static void
pika_perspective_tool_class_init (PikaPerspectiveToolClass *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);

  tg_class->matrix_to_info      = pika_perspective_tool_matrix_to_info;
  tg_class->prepare             = pika_perspective_tool_prepare;
  tg_class->readjust            = pika_perspective_tool_readjust;
  tg_class->get_widget          = pika_perspective_tool_get_widget;
  tg_class->update_widget       = pika_perspective_tool_update_widget;
  tg_class->widget_changed      = pika_perspective_tool_widget_changed;

  generic_class->info_to_points = pika_perspective_tool_info_to_points;

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

static void
pika_perspective_tool_init (PikaPerspectiveTool *perspective_tool)
{
  PikaTool *tool = PIKA_TOOL (perspective_tool);

  pika_tool_control_set_tool_cursor (tool->control,
                                     PIKA_TOOL_CURSOR_PERSPECTIVE);
}

static void
pika_perspective_tool_matrix_to_info (PikaTransformGridTool *tg_tool,
                                      const PikaMatrix3     *transform)
{
  PikaTransformTool *tr_tool = PIKA_TRANSFORM_TOOL (tg_tool);

  pika_matrix3_transform_point (transform,
                                tr_tool->x1,
                                tr_tool->y1,
                                &tg_tool->trans_info[X0],
                                &tg_tool->trans_info[Y0]);
  pika_matrix3_transform_point (transform,
                                tr_tool->x2,
                                tr_tool->y1,
                                &tg_tool->trans_info[X1],
                                &tg_tool->trans_info[Y1]);
  pika_matrix3_transform_point (transform,
                                tr_tool->x1,
                                tr_tool->y2,
                                &tg_tool->trans_info[X2],
                                &tg_tool->trans_info[Y2]);
  pika_matrix3_transform_point (transform,
                                tr_tool->x2,
                                tr_tool->y2,
                                &tg_tool->trans_info[X3],
                                &tg_tool->trans_info[Y3]);
}

static void
pika_perspective_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;
}

static void
pika_perspective_tool_readjust (PikaTransformGridTool *tg_tool)
{
  PikaTool         *tool  = PIKA_TOOL (tg_tool);
  PikaDisplayShell *shell = pika_display_get_shell (tool->display);
  gdouble           x;
  gdouble           y;
  gdouble           r;

  x = shell->disp_width  / 2.0;
  y = shell->disp_height / 2.0;
  r = MAX (MIN (x, y) / G_SQRT2 -
           PIKA_TOOL_TRANSFORM_GRID_MAX_HANDLE_SIZE / 2.0,
           PIKA_TOOL_TRANSFORM_GRID_MAX_HANDLE_SIZE / 2.0);

  pika_display_shell_untransform_xy_f (shell,
                                       x - r, y - r,
                                       &tg_tool->trans_info[X0],
                                       &tg_tool->trans_info[Y0]);
  pika_display_shell_untransform_xy_f (shell,
                                       x + r, y - r,
                                       &tg_tool->trans_info[X1],
                                       &tg_tool->trans_info[Y1]);
  pika_display_shell_untransform_xy_f (shell,
                                       x - r, y + r,
                                       &tg_tool->trans_info[X2],
                                       &tg_tool->trans_info[Y2]);
  pika_display_shell_untransform_xy_f (shell,
                                       x + r, y + r,
                                       &tg_tool->trans_info[X3],
                                       &tg_tool->trans_info[Y3]);
}

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

  widget = pika_tool_transform_grid_new (shell,
                                         &tr_tool->transform,
                                         tr_tool->x1,
                                         tr_tool->y1,
                                         tr_tool->x2,
                                         tr_tool->y2);

  g_object_set (widget,
                "inside-function",         PIKA_TRANSFORM_FUNCTION_PERSPECTIVE,
                "outside-function",        PIKA_TRANSFORM_FUNCTION_PERSPECTIVE,
                "use-perspective-handles", TRUE,
                "use-center-handle",       TRUE,
                NULL);

  return widget;
}

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

  PIKA_TRANSFORM_GRID_TOOL_CLASS (parent_class)->update_widget (tg_tool);

  g_object_set (tg_tool->widget,
                "x1", (gdouble) tr_tool->x1,
                "y1", (gdouble) tr_tool->y1,
                "x2", (gdouble) tr_tool->x2,
                "y2", (gdouble) tr_tool->y2,
                NULL);
}

static void
pika_perspective_tool_widget_changed (PikaTransformGridTool *tg_tool)
{
  PikaTransformTool *tr_tool = PIKA_TRANSFORM_TOOL (tg_tool);
  PikaMatrix3       *transform;

  g_object_get (tg_tool->widget,
                "transform", &transform,
                NULL);

  pika_matrix3_transform_point (transform,
                                tr_tool->x1, tr_tool->y1,
                                &tg_tool->trans_info[X0],
                                &tg_tool->trans_info[Y0]);
  pika_matrix3_transform_point (transform,
                                tr_tool->x2, tr_tool->y1,
                                &tg_tool->trans_info[X1],
                                &tg_tool->trans_info[Y1]);
  pika_matrix3_transform_point (transform,
                                tr_tool->x1, tr_tool->y2,
                                &tg_tool->trans_info[X2],
                                &tg_tool->trans_info[Y2]);
  pika_matrix3_transform_point (transform,
                                tr_tool->x2, tr_tool->y2,
                                &tg_tool->trans_info[X3],
                                &tg_tool->trans_info[Y3]);

  g_free (transform);

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

static void
pika_perspective_tool_info_to_points (PikaGenericTransformTool *generic)
{
  PikaTransformTool     *tr_tool = PIKA_TRANSFORM_TOOL (generic);
  PikaTransformGridTool *tg_tool = PIKA_TRANSFORM_GRID_TOOL (generic);

  generic->input_points[0]  = (PikaVector2) {tr_tool->x1, tr_tool->y1};
  generic->input_points[1]  = (PikaVector2) {tr_tool->x2, tr_tool->y1};
  generic->input_points[2]  = (PikaVector2) {tr_tool->x1, tr_tool->y2};
  generic->input_points[3]  = (PikaVector2) {tr_tool->x2, tr_tool->y2};

  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]};
}
