/* 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) 2009 Martin Nordholts
 *
 * 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 <gegl.h>
#include <gtk/gtk.h>

#include "widgets/widgets-types.h"

#include "widgets/pikauimanager.h"

#include "core/pika.h"
#include "core/pikacontext.h"
#include "core/pikaimage.h"
#include "core/pikalayer.h"
#include "core/pikalayer-new.h"

#include "operations/pikalevelsconfig.h"

#include "tests.h"

#include "pika-app-test-utils.h"


#define PIKA_TEST_IMAGE_SIZE 100

#define ADD_IMAGE_TEST(function) \
  g_test_add ("/pika-core/" #function, \
              PikaTestFixture, \
              pika, \
              pika_test_image_setup, \
              function, \
              pika_test_image_teardown);

#define ADD_TEST(function) \
  g_test_add ("/pika-core/" #function, \
              PikaTestFixture, \
              pika, \
              NULL, \
              function, \
              NULL);


typedef struct
{
  PikaImage *image;
} PikaTestFixture;


static void pika_test_image_setup    (PikaTestFixture *fixture,
                                      gconstpointer    data);
static void pika_test_image_teardown (PikaTestFixture *fixture,
                                      gconstpointer    data);


/**
 * pika_test_image_setup:
 * @fixture:
 * @data:
 *
 * Test fixture setup for a single image.
 **/
static void
pika_test_image_setup (PikaTestFixture *fixture,
                       gconstpointer    data)
{
  Pika *pika = PIKA (data);

  fixture->image = pika_image_new (pika,
                                   PIKA_TEST_IMAGE_SIZE,
                                   PIKA_TEST_IMAGE_SIZE,
                                   PIKA_RGB,
                                   PIKA_PRECISION_FLOAT_LINEAR);
}

/**
 * pika_test_image_teardown:
 * @fixture:
 * @data:
 *
 * Test fixture teardown for a single image.
 **/
static void
pika_test_image_teardown (PikaTestFixture *fixture,
                          gconstpointer    data)
{
  g_object_unref (fixture->image);
}

/**
 * rotate_non_overlapping:
 * @fixture:
 * @data:
 *
 * Super basic test that makes sure we can add a layer
 * and call pika_item_rotate with center at (0, -10)
 * without triggering a failed assertion .
 **/
static void
rotate_non_overlapping (PikaTestFixture *fixture,
                        gconstpointer    data)
{
  Pika        *pika    = PIKA (data);
  PikaImage   *image   = fixture->image;
  PikaLayer   *layer;
  PikaContext *context = pika_context_new (pika, "Test", NULL /*template*/);
  gboolean     result;

  g_assert_cmpint (pika_image_get_n_layers (image), ==, 0);

  layer = pika_layer_new (image,
                          PIKA_TEST_IMAGE_SIZE,
                          PIKA_TEST_IMAGE_SIZE,
                          babl_format ("R'G'B'A u8"),
                          "Test Layer",
                          PIKA_OPACITY_OPAQUE,
                          PIKA_LAYER_MODE_NORMAL);

  g_assert_cmpint (PIKA_IS_LAYER (layer), ==, TRUE);

  result = pika_image_add_layer (image,
                                 layer,
                                 PIKA_IMAGE_ACTIVE_PARENT,
                                 0,
                                 FALSE);

  pika_item_rotate (PIKA_ITEM (layer), context, PIKA_ROTATE_90, 0., -10., TRUE);

  g_assert_cmpint (result, ==, TRUE);
  g_assert_cmpint (pika_image_get_n_layers (image), ==, 1);
  g_object_unref (context);
}

/**
 * add_layer:
 * @fixture:
 * @data:
 *
 * Super basic test that makes sure we can add a layer.
 **/
static void
add_layer (PikaTestFixture *fixture,
           gconstpointer    data)
{
  PikaImage *image = fixture->image;
  PikaLayer *layer;
  gboolean   result;

  g_assert_cmpint (pika_image_get_n_layers (image), ==, 0);

  layer = pika_layer_new (image,
                          PIKA_TEST_IMAGE_SIZE,
                          PIKA_TEST_IMAGE_SIZE,
                          babl_format ("R'G'B'A u8"),
                          "Test Layer",
                          PIKA_OPACITY_OPAQUE,
                          PIKA_LAYER_MODE_NORMAL);

  g_assert_cmpint (PIKA_IS_LAYER (layer), ==, TRUE);

  result = pika_image_add_layer (image,
                                 layer,
                                 PIKA_IMAGE_ACTIVE_PARENT,
                                 0,
                                 FALSE);

  g_assert_cmpint (result, ==, TRUE);
  g_assert_cmpint (pika_image_get_n_layers (image), ==, 1);
}

/**
 * remove_layer:
 * @fixture:
 * @data:
 *
 * Super basic test that makes sure we can remove a layer.
 **/
static void
remove_layer (PikaTestFixture *fixture,
              gconstpointer    data)
{
  PikaImage *image = fixture->image;
  PikaLayer *layer;
  gboolean   result;

  g_assert_cmpint (pika_image_get_n_layers (image), ==, 0);

  layer = pika_layer_new (image,
                          PIKA_TEST_IMAGE_SIZE,
                          PIKA_TEST_IMAGE_SIZE,
                          babl_format ("R'G'B'A u8"),
                          "Test Layer",
                          PIKA_OPACITY_OPAQUE,
                          PIKA_LAYER_MODE_NORMAL);

  g_assert_cmpint (PIKA_IS_LAYER (layer), ==, TRUE);

  result = pika_image_add_layer (image,
                                 layer,
                                 PIKA_IMAGE_ACTIVE_PARENT,
                                 0,
                                 FALSE);

  g_assert_cmpint (result, ==, TRUE);
  g_assert_cmpint (pika_image_get_n_layers (image), ==, 1);

  pika_image_remove_layer (image,
                           layer,
                           FALSE,
                           NULL);

  g_assert_cmpint (pika_image_get_n_layers (image), ==, 0);
}

/**
 * white_graypoint_in_red_levels:
 * @fixture:
 * @data:
 *
 * Makes sure the levels algorithm can handle when the graypoint is
 * white. It's easy to get a divide by zero problem when trying to
 * calculate what gamma will give a white graypoint.
 **/
static void
white_graypoint_in_red_levels (PikaTestFixture *fixture,
                               gconstpointer    data)
{
  PikaRGB              black   = { 0, 0, 0, 0 };
  PikaRGB              gray    = { 1, 1, 1, 1 };
  PikaRGB              white   = { 1, 1, 1, 1 };
  PikaHistogramChannel channel = PIKA_HISTOGRAM_RED;
  PikaLevelsConfig    *config;

  config = g_object_new (PIKA_TYPE_LEVELS_CONFIG, NULL);

  pika_levels_config_adjust_by_colors (config,
                                       channel,
                                       &black,
                                       &gray,
                                       &white);

  /* Make sure we didn't end up with an invalid gamma value */
  g_object_set (config,
                "gamma", config->gamma[channel],
                NULL);
}

int
main (int    argc,
      char **argv)
{
  Pika *pika;
  int   result;

  g_test_init (&argc, &argv, NULL);

  pika_test_utils_set_pika3_directory ("PIKA_TESTING_ABS_TOP_SRCDIR",
                                       "app/tests/pikadir");

  /* We share the same application instance across all tests */
  pika = pika_init_for_testing ();

  /* Add tests */
  ADD_IMAGE_TEST (add_layer);
  ADD_IMAGE_TEST (remove_layer);
  ADD_IMAGE_TEST (rotate_non_overlapping);
  ADD_TEST (white_graypoint_in_red_levels);

  /* Run the tests */
  result = g_test_run ();

  /* Don't write files to the source dir */
  pika_test_utils_set_pika3_directory ("PIKA_TESTING_ABS_TOP_BUILDDIR",
                                       "app/tests/pikadir-output");

  /* Exit so we don't break script-fu plug-in wire */
  pika_exit (pika, TRUE);

  return result;
}
