/* 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 "core/pika-transform-utils.h"

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

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

#include "pikagenerictransformtool.h"
#include "pikatoolcontrol.h"
#include "pikatransformgridoptions.h"

#include "pika-intl.h"


/*  local function prototypes  */

static gboolean   pika_generic_transform_tool_info_to_matrix (PikaTransformGridTool *tg_tool,
                                                              PikaMatrix3           *transform);
static void       pika_generic_transform_tool_dialog         (PikaTransformGridTool *tg_tool);
static void       pika_generic_transform_tool_dialog_update  (PikaTransformGridTool *tg_tool);
static void       pika_generic_transform_tool_prepare        (PikaTransformGridTool *tg_tool);


G_DEFINE_TYPE (PikaGenericTransformTool, pika_generic_transform_tool,
               PIKA_TYPE_TRANSFORM_GRID_TOOL)

#define parent_class pika_generic_transform_tool_parent_class


static void
pika_generic_transform_tool_class_init (PikaGenericTransformToolClass *klass)
{
  PikaTransformGridToolClass *tg_class = PIKA_TRANSFORM_GRID_TOOL_CLASS (klass);

  tg_class->info_to_matrix = pika_generic_transform_tool_info_to_matrix;
  tg_class->dialog         = pika_generic_transform_tool_dialog;
  tg_class->dialog_update  = pika_generic_transform_tool_dialog_update;
  tg_class->prepare        = pika_generic_transform_tool_prepare;
}

static void
pika_generic_transform_tool_init (PikaGenericTransformTool *unified_tool)
{
}

static gboolean
pika_generic_transform_tool_info_to_matrix (PikaTransformGridTool *tg_tool,
                                            PikaMatrix3           *transform)
{
  PikaGenericTransformTool *generic = PIKA_GENERIC_TRANSFORM_TOOL (tg_tool);

  if (PIKA_GENERIC_TRANSFORM_TOOL_GET_CLASS (generic)->info_to_points)
    PIKA_GENERIC_TRANSFORM_TOOL_GET_CLASS (generic)->info_to_points (generic);

  pika_matrix3_identity (transform);

  return pika_transform_matrix_generic (transform,
                                        generic->input_points,
                                        generic->output_points);
}

static void
pika_generic_transform_tool_dialog (PikaTransformGridTool *tg_tool)
{
  PikaGenericTransformTool *generic = PIKA_GENERIC_TRANSFORM_TOOL (tg_tool);
  GtkWidget                *frame;
  GtkWidget                *vbox;
  GtkWidget                *label;
  GtkSizeGroup             *size_group;

  frame = pika_frame_new (_("Transform Matrix"));
  gtk_box_pack_start (GTK_BOX (pika_tool_gui_get_vbox (tg_tool->gui)), frame,
                      FALSE, FALSE, 0);
  gtk_widget_show (frame);

  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
  gtk_container_add (GTK_CONTAINER (frame), vbox);
  gtk_widget_show (vbox);

  size_group = gtk_size_group_new (GTK_SIZE_GROUP_BOTH);

  label = generic->matrix_label = gtk_label_new (" ");
  gtk_size_group_add_widget (size_group, label);
  gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
  pika_label_set_attributes (GTK_LABEL (label), PANGO_ATTR_SCALE, PANGO_SCALE_SMALL, -1);
  gtk_label_set_selectable (GTK_LABEL (label), TRUE);
  gtk_widget_show (label);

  label = generic->invalid_label = gtk_label_new (_("Invalid transform"));
  pika_label_set_attributes (GTK_LABEL (label),
                             PANGO_ATTR_STYLE, PANGO_STYLE_ITALIC,
                             -1);
  gtk_size_group_add_widget (size_group, label);
  gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);

  g_object_unref (size_group);
}

static void
pika_generic_transform_tool_dialog_update (PikaTransformGridTool *tg_tool)
{
  PikaGenericTransformTool *generic = PIKA_GENERIC_TRANSFORM_TOOL (tg_tool);
  PikaMatrix3               transform;
  gboolean                  transform_valid;

  transform_valid = pika_transform_grid_tool_info_to_matrix (tg_tool,
                                                             &transform);

  if (transform_valid)
    {
      gchar buf[256];
      gtk_widget_show (generic->matrix_label);
      gtk_widget_hide (generic->invalid_label);

      g_snprintf (buf, sizeof (buf), "<tt>% 11.4f\t% 11.4f\t% 11.4f\n% 11.4f\t% 11.4f"
                  "\t% 11.4f\n% 11.4f\t% 11.4f\t% 11.4f</tt>",
                  transform.coeff[0][0], transform.coeff[0][1], transform.coeff[0][2],
                  transform.coeff[1][0], transform.coeff[1][1], transform.coeff[1][2],
                  transform.coeff[2][0], transform.coeff[2][1], transform.coeff[2][2]);
      gtk_label_set_markup (GTK_LABEL (generic->matrix_label), buf);
    }
  else
    {
      gtk_widget_show (generic->invalid_label);
      gtk_widget_hide (generic->matrix_label);
    }
}

static void
pika_generic_transform_tool_prepare (PikaTransformGridTool *tg_tool)
{
  PikaTransformTool        *tr_tool = PIKA_TRANSFORM_TOOL (tg_tool);
  PikaGenericTransformTool *generic = PIKA_GENERIC_TRANSFORM_TOOL (tg_tool);

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

  memcpy (generic->output_points, generic->input_points,
          sizeof (generic->input_points));
}
