/* 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
 *
 * PikaTextLayer
 * Copyright (C) 2002-2003  Sven Neumann <sven@gimp.org>
 *
 * 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 <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>

#include "libpikamath/pikamath.h"

#include "text-types.h"

#include "core/pika-transform-utils.h"
#include "core/pikaimage-undo.h"

#include "pikatext.h"
#include "pikatextlayer.h"
#include "pikatextlayer-transform.h"


static PikaItemClass * pika_text_layer_parent_class (void) G_GNUC_CONST;

static gboolean  pika_text_layer_get_transformation  (PikaTextLayer *layer,
                                                      PikaMatrix3   *matrix);
static gboolean  pika_text_layer_set_transformation  (PikaTextLayer *layer,
                                                      PikaMatrix3   *matrix);


void
pika_text_layer_scale (PikaItem               *item,
                       gint                    new_width,
                       gint                    new_height,
                       gint                    new_offset_x,
                       gint                    new_offset_y,
                       PikaInterpolationType   interpolation_type,
                       PikaProgress           *progress)
{
  /*  TODO  */
}

static gboolean
pika_text_layer_transform_flip (PikaTextLayer       *layer,
                                PikaOrientationType  flip_type,
                                gdouble              axis)
{
  PikaMatrix3  matrix;

  if (! pika_text_layer_get_transformation (layer, &matrix))
    return FALSE;

  pika_transform_matrix_flip (&matrix, flip_type, axis);

  return pika_text_layer_set_transformation (layer, &matrix);
}

void
pika_text_layer_flip (PikaItem            *item,
                      PikaContext         *context,
                      PikaOrientationType  flip_type,
                      gdouble              axis,
                      gboolean             clip_result)
{
  PikaTextLayer *layer = PIKA_TEXT_LAYER (item);

  if (pika_text_layer_transform_flip (layer, flip_type, axis))
    {
      PikaLayerMask *mask = pika_layer_get_mask (PIKA_LAYER (layer));

      if (mask)
        pika_item_flip (PIKA_ITEM (mask), context,
                        flip_type, axis, clip_result);
    }
  else
    {
      pika_text_layer_parent_class ()->flip (item, context,
                                             flip_type, axis, clip_result);
    }
}

static gboolean
pika_text_layer_transform_rotate (PikaTextLayer    *layer,
                                  PikaRotationType  rotate_type,
                                  gdouble           center_x,
                                  gdouble           center_y)
{
  PikaMatrix3  matrix;

  if (! pika_text_layer_get_transformation (layer, &matrix))
    return FALSE;

  pika_transform_matrix_rotate (&matrix, rotate_type, center_x, center_y);

  return pika_text_layer_set_transformation (layer, &matrix);
}

void
pika_text_layer_rotate (PikaItem         *item,
                        PikaContext      *context,
                        PikaRotationType  rotate_type,
                        gdouble           center_x,
                        gdouble           center_y,
                        gboolean          clip_result)
{
  PikaTextLayer *layer = PIKA_TEXT_LAYER (item);

  if (! pika_text_layer_transform_rotate (layer,
                                          rotate_type, center_x, center_y))
    {
      PikaLayerMask *mask = pika_layer_get_mask (PIKA_LAYER (layer));

      if (mask)
        pika_item_rotate (PIKA_ITEM (mask), context,
                          rotate_type, center_x, center_y, clip_result);
    }
  else
    {
      pika_text_layer_parent_class ()->rotate (item, context,
                                               rotate_type, center_x, center_y,
                                               clip_result);
    }
}

void
pika_text_layer_transform (PikaItem               *item,
                           PikaContext            *context,
                           const PikaMatrix3      *matrix,
                           PikaTransformDirection  direction,
                           PikaInterpolationType   interpolation_type,
                           gboolean                supersample,
                           PikaTransformResize     clip_result,
                           PikaProgress           *progress)
{
  /*  TODO  */
}

static PikaItemClass *
pika_text_layer_parent_class (void)
{
  static PikaItemClass *parent_class = NULL;

  if (! parent_class)
    {
      gpointer klass = g_type_class_peek (PIKA_TYPE_TEXT_LAYER);

      parent_class = g_type_class_peek_parent (klass);
    }

  return parent_class;
}

static gboolean
pika_text_layer_get_transformation (PikaTextLayer *layer,
                                    PikaMatrix3   *matrix)
{
  if (! layer->text || layer->modified)
    return FALSE;

  pika_text_get_transformation (layer->text, matrix);

  return TRUE;
}

static gboolean
pika_text_layer_set_transformation (PikaTextLayer *layer,
                                    PikaMatrix3   *matrix)
{
  PikaMatrix2  trafo;

  if (! pika_matrix3_is_affine (matrix))
    return FALSE;

  trafo.coeff[0][0] = matrix->coeff[0][0];
  trafo.coeff[0][1] = matrix->coeff[0][1];
  trafo.coeff[1][0] = matrix->coeff[1][0];
  trafo.coeff[1][1] = matrix->coeff[1][1];

  pika_text_layer_set (PIKA_TEXT_LAYER (layer), NULL,
                       "transformation", &trafo,
                       "offset-x",       matrix->coeff[0][2],
                       "offset-y",       matrix->coeff[1][2],
                       NULL);

  return TRUE;
}
