/* 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
 *
 * pikacolorbalanceconfig.c
 * Copyright (C) 2007 Michael Natterer <mitch@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 <cairo.h>
#include <gegl.h>
#include <gdk-pixbuf/gdk-pixbuf.h>

#include "libpikacolor/pikacolor.h"
#include "libpikamath/pikamath.h"
#include "libpikaconfig/pikaconfig.h"

#include "operations-types.h"

#include "pikacolorbalanceconfig.h"

#include "pika-intl.h"


enum
{
  PROP_0,
  PROP_RANGE,
  PROP_CYAN_RED,
  PROP_MAGENTA_GREEN,
  PROP_YELLOW_BLUE,
  PROP_PRESERVE_LUMINOSITY
};


static void     pika_color_balance_config_iface_init   (PikaConfigInterface *iface);

static void     pika_color_balance_config_get_property (GObject          *object,
                                                        guint             property_id,
                                                        GValue           *value,
                                                        GParamSpec       *pspec);
static void     pika_color_balance_config_set_property (GObject          *object,
                                                        guint             property_id,
                                                        const GValue     *value,
                                                        GParamSpec       *pspec);

static gboolean pika_color_balance_config_serialize    (PikaConfig       *config,
                                                        PikaConfigWriter *writer,
                                                        gpointer          data);
static gboolean pika_color_balance_config_deserialize  (PikaConfig       *config,
                                                        GScanner         *scanner,
                                                        gint              nest_level,
                                                        gpointer          data);
static gboolean pika_color_balance_config_equal        (PikaConfig       *a,
                                                        PikaConfig       *b);
static void     pika_color_balance_config_reset        (PikaConfig       *config);
static gboolean pika_color_balance_config_copy         (PikaConfig       *src,
                                                        PikaConfig       *dest,
                                                        GParamFlags       flags);


G_DEFINE_TYPE_WITH_CODE (PikaColorBalanceConfig, pika_color_balance_config,
                         PIKA_TYPE_OPERATION_SETTINGS,
                         G_IMPLEMENT_INTERFACE (PIKA_TYPE_CONFIG,
                                                pika_color_balance_config_iface_init))

#define parent_class pika_color_balance_config_parent_class


static void
pika_color_balance_config_class_init (PikaColorBalanceConfigClass *klass)
{
  GObjectClass      *object_class   = G_OBJECT_CLASS (klass);
  PikaViewableClass *viewable_class = PIKA_VIEWABLE_CLASS (klass);

  object_class->set_property        = pika_color_balance_config_set_property;
  object_class->get_property        = pika_color_balance_config_get_property;

  viewable_class->default_icon_name = "pika-tool-color-balance";

  PIKA_CONFIG_PROP_ENUM (object_class, PROP_RANGE,
                         "range",
                         _("Range"),
                         _("The affected range"),
                         PIKA_TYPE_TRANSFER_MODE,
                         PIKA_TRANSFER_MIDTONES, 0);

  PIKA_CONFIG_PROP_DOUBLE (object_class, PROP_CYAN_RED,
                           "cyan-red",
                           _("Cyan-Red"),
                           _("Cyan-Red"),
                           -1.0, 1.0, 0.0, 0);

  PIKA_CONFIG_PROP_DOUBLE (object_class, PROP_MAGENTA_GREEN,
                           "magenta-green",
                           _("Magenta-Green"),
                           _("Magenta-Green"),
                           -1.0, 1.0, 0.0, 0);

  PIKA_CONFIG_PROP_DOUBLE (object_class, PROP_YELLOW_BLUE,
                           "yellow-blue",
                           _("Yellow-Blue"),
                           _("Yellow-Blue"),
                           -1.0, 1.0, 0.0, 0);

  PIKA_CONFIG_PROP_BOOLEAN (object_class, PROP_PRESERVE_LUMINOSITY,
                            "preserve-luminosity",
                            _("Preserve Luminosity"),
                            _("Preserve Luminosity"),
                            TRUE, 0);
}

static void
pika_color_balance_config_iface_init (PikaConfigInterface *iface)
{
  iface->serialize   = pika_color_balance_config_serialize;
  iface->deserialize = pika_color_balance_config_deserialize;
  iface->equal       = pika_color_balance_config_equal;
  iface->reset       = pika_color_balance_config_reset;
  iface->copy        = pika_color_balance_config_copy;
}

static void
pika_color_balance_config_init (PikaColorBalanceConfig *self)
{
  pika_config_reset (PIKA_CONFIG (self));
}

static void
pika_color_balance_config_get_property (GObject    *object,
                                        guint       property_id,
                                        GValue     *value,
                                        GParamSpec *pspec)
{
  PikaColorBalanceConfig *self = PIKA_COLOR_BALANCE_CONFIG (object);

  switch (property_id)
    {
    case PROP_RANGE:
      g_value_set_enum (value, self->range);
      break;

    case PROP_CYAN_RED:
      g_value_set_double (value, self->cyan_red[self->range]);
      break;

    case PROP_MAGENTA_GREEN:
      g_value_set_double (value, self->magenta_green[self->range]);
      break;

    case PROP_YELLOW_BLUE:
      g_value_set_double (value, self->yellow_blue[self->range]);
      break;

    case PROP_PRESERVE_LUMINOSITY:
      g_value_set_boolean (value, self->preserve_luminosity);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
pika_color_balance_config_set_property (GObject      *object,
                                        guint         property_id,
                                        const GValue *value,
                                        GParamSpec   *pspec)
{
  PikaColorBalanceConfig *self = PIKA_COLOR_BALANCE_CONFIG (object);

  switch (property_id)
    {
    case PROP_RANGE:
      self->range = g_value_get_enum (value);
      g_object_notify (object, "cyan-red");
      g_object_notify (object, "magenta-green");
      g_object_notify (object, "yellow-blue");
      break;

    case PROP_CYAN_RED:
      self->cyan_red[self->range] = g_value_get_double (value);
      break;

    case PROP_MAGENTA_GREEN:
      self->magenta_green[self->range] = g_value_get_double (value);
      break;

    case PROP_YELLOW_BLUE:
      self->yellow_blue[self->range] = g_value_get_double (value);
      break;

    case PROP_PRESERVE_LUMINOSITY:
      self->preserve_luminosity = g_value_get_boolean (value);
      break;

   default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static gboolean
pika_color_balance_config_serialize (PikaConfig       *config,
                                     PikaConfigWriter *writer,
                                     gpointer          data)
{
  PikaColorBalanceConfig *bc_config = PIKA_COLOR_BALANCE_CONFIG (config);
  PikaTransferMode        range;
  PikaTransferMode        old_range;
  gboolean                success = TRUE;

  if (! pika_operation_settings_config_serialize_base (config, writer, data))
    return FALSE;

  old_range = bc_config->range;

  for (range = PIKA_TRANSFER_SHADOWS;
       range <= PIKA_TRANSFER_HIGHLIGHTS;
       range++)
    {
      bc_config->range = range;

      success = (pika_config_serialize_property_by_name (config,
                                                         "range",
                                                         writer) &&
                 pika_config_serialize_property_by_name (config,
                                                         "cyan-red",
                                                         writer) &&
                 pika_config_serialize_property_by_name (config,
                                                         "magenta-green",
                                                         writer) &&
                 pika_config_serialize_property_by_name (config,
                                                         "yellow-blue",
                                                         writer));

      if (! success)
        break;
    }

  if (success)
    success = pika_config_serialize_property_by_name (config,
                                                      "preserve-luminosity",
                                                      writer);

  bc_config->range = old_range;

  return success;
}

static gboolean
pika_color_balance_config_deserialize (PikaConfig *config,
                                       GScanner   *scanner,
                                       gint        nest_level,
                                       gpointer    data)
{
  PikaColorBalanceConfig *cb_config = PIKA_COLOR_BALANCE_CONFIG (config);
  PikaTransferMode        old_range;
  gboolean                success = TRUE;

  old_range = cb_config->range;

  success = pika_config_deserialize_properties (config, scanner, nest_level);

  g_object_set (config, "range", old_range, NULL);

  return success;
}

static gboolean
pika_color_balance_config_equal (PikaConfig *a,
                                 PikaConfig *b)
{
  PikaColorBalanceConfig *config_a = PIKA_COLOR_BALANCE_CONFIG (a);
  PikaColorBalanceConfig *config_b = PIKA_COLOR_BALANCE_CONFIG (b);
  PikaTransferMode        range;

  if (! pika_operation_settings_config_equal_base (a, b))
    return FALSE;

  for (range = PIKA_TRANSFER_SHADOWS;
       range <= PIKA_TRANSFER_HIGHLIGHTS;
       range++)
    {
      if (config_a->cyan_red[range]      != config_b->cyan_red[range]      ||
          config_a->magenta_green[range] != config_b->magenta_green[range] ||
          config_a->yellow_blue[range]   != config_b->yellow_blue[range])
        return FALSE;
    }

  /* don't compare "range" */

  if (config_a->preserve_luminosity != config_b->preserve_luminosity)
    return FALSE;

  return TRUE;
}

static void
pika_color_balance_config_reset (PikaConfig *config)
{
  PikaColorBalanceConfig *cb_config = PIKA_COLOR_BALANCE_CONFIG (config);
  PikaTransferMode        range;

  pika_operation_settings_config_reset_base (config);

  for (range = PIKA_TRANSFER_SHADOWS;
       range <= PIKA_TRANSFER_HIGHLIGHTS;
       range++)
    {
      cb_config->range = range;
      pika_color_balance_config_reset_range (cb_config);
    }

  pika_config_reset_property (G_OBJECT (config), "range");
  pika_config_reset_property (G_OBJECT (config), "preserve-luminosity");
}

static gboolean
pika_color_balance_config_copy (PikaConfig  *src,
                                PikaConfig  *dest,
                                GParamFlags  flags)
{
  PikaColorBalanceConfig *src_config  = PIKA_COLOR_BALANCE_CONFIG (src);
  PikaColorBalanceConfig *dest_config = PIKA_COLOR_BALANCE_CONFIG (dest);
  PikaTransferMode        range;

  if (! pika_operation_settings_config_copy_base (src, dest, flags))
    return FALSE;

  for (range = PIKA_TRANSFER_SHADOWS;
       range <= PIKA_TRANSFER_HIGHLIGHTS;
       range++)
    {
      dest_config->cyan_red[range]      = src_config->cyan_red[range];
      dest_config->magenta_green[range] = src_config->magenta_green[range];
      dest_config->yellow_blue[range]   = src_config->yellow_blue[range];
    }

  g_object_notify (G_OBJECT (dest), "cyan-red");
  g_object_notify (G_OBJECT (dest), "magenta-green");
  g_object_notify (G_OBJECT (dest), "yellow-blue");

  dest_config->range               = src_config->range;
  dest_config->preserve_luminosity = src_config->preserve_luminosity;

  g_object_notify (G_OBJECT (dest), "range");
  g_object_notify (G_OBJECT (dest), "preserve-luminosity");

  return TRUE;
}


/*  public functions  */

void
pika_color_balance_config_reset_range (PikaColorBalanceConfig *config)
{
  g_return_if_fail (PIKA_IS_COLOR_BALANCE_CONFIG (config));

  g_object_freeze_notify (G_OBJECT (config));

  pika_config_reset_property (G_OBJECT (config), "cyan-red");
  pika_config_reset_property (G_OBJECT (config), "magenta-green");
  pika_config_reset_property (G_OBJECT (config), "yellow-blue");

  g_object_thaw_notify (G_OBJECT (config));
}
