/* 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 <gio/gio.h>

#include "libpikabase/pikabase.h"
#include "libpikaconfig/pikaconfig.h"

#include "core-types.h"

#include "pikasamplepoint.h"


enum
{
  PROP_0,
  PROP_POSITION_X,
  PROP_POSITION_Y,
  PROP_PICK_MODE
};


struct _PikaSamplePointPrivate
{
  gint              position_x;
  gint              position_y;
  PikaColorPickMode pick_mode;
};


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


G_DEFINE_TYPE_WITH_PRIVATE (PikaSamplePoint, pika_sample_point,
                            PIKA_TYPE_AUX_ITEM)


static void
pika_sample_point_class_init (PikaSamplePointClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->get_property = pika_sample_point_get_property;
  object_class->set_property = pika_sample_point_set_property;

  PIKA_CONFIG_PROP_INT (object_class, PROP_POSITION_X,
                        "position-x",
                        NULL, NULL,
                        PIKA_SAMPLE_POINT_POSITION_UNDEFINED,
                        PIKA_MAX_IMAGE_SIZE,
                        PIKA_SAMPLE_POINT_POSITION_UNDEFINED,
                        0);

  PIKA_CONFIG_PROP_INT (object_class, PROP_POSITION_Y,
                        "position-y",
                        NULL, NULL,
                        PIKA_SAMPLE_POINT_POSITION_UNDEFINED,
                        PIKA_MAX_IMAGE_SIZE,
                        PIKA_SAMPLE_POINT_POSITION_UNDEFINED,
                        0);

  PIKA_CONFIG_PROP_ENUM (object_class, PROP_PICK_MODE,
                         "pick-mode",
                         NULL, NULL,
                         PIKA_TYPE_COLOR_PICK_MODE,
                         PIKA_COLOR_PICK_MODE_PIXEL,
                         0);
}

static void
pika_sample_point_init (PikaSamplePoint *sample_point)
{
  sample_point->priv = pika_sample_point_get_instance_private (sample_point);
}

static void
pika_sample_point_get_property (GObject    *object,
                                guint       property_id,
                                GValue     *value,
                                GParamSpec *pspec)
{
  PikaSamplePoint *sample_point = PIKA_SAMPLE_POINT (object);

  switch (property_id)
    {
    case PROP_POSITION_X:
      g_value_set_int (value, sample_point->priv->position_x);
      break;
    case PROP_POSITION_Y:
      g_value_set_int (value, sample_point->priv->position_y);
      break;
    case PROP_PICK_MODE:
      g_value_set_enum (value, sample_point->priv->pick_mode);
      break;

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

static void
pika_sample_point_set_property (GObject      *object,
                                guint         property_id,
                                const GValue *value,
                                GParamSpec   *pspec)
{
  PikaSamplePoint *sample_point = PIKA_SAMPLE_POINT (object);

  switch (property_id)
    {
    case PROP_POSITION_X:
      sample_point->priv->position_x = g_value_get_int (value);
      break;
    case PROP_POSITION_Y:
      sample_point->priv->position_y = g_value_get_int (value);
      break;
    case PROP_PICK_MODE:
      sample_point->priv->pick_mode = g_value_get_enum (value);
      break;

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

PikaSamplePoint *
pika_sample_point_new (guint32 sample_point_ID)
{
  return g_object_new (PIKA_TYPE_SAMPLE_POINT,
                       "id", sample_point_ID,
                       NULL);
}

void
pika_sample_point_get_position (PikaSamplePoint *sample_point,
                                gint            *position_x,
                                gint            *position_y)
{
  g_return_if_fail (PIKA_IS_SAMPLE_POINT (sample_point));
  g_return_if_fail (position_x != NULL);
  g_return_if_fail (position_y != NULL);

  *position_x = sample_point->priv->position_x;
  *position_y = sample_point->priv->position_y;
}

void
pika_sample_point_set_position (PikaSamplePoint *sample_point,
                                gint             position_x,
                                gint             position_y)
{
  g_return_if_fail (PIKA_IS_SAMPLE_POINT (sample_point));

  if (sample_point->priv->position_x != position_x ||
      sample_point->priv->position_y != position_y)
    {
      sample_point->priv->position_x = position_x;
      sample_point->priv->position_y = position_y;

      g_object_freeze_notify (G_OBJECT (sample_point));

      g_object_notify (G_OBJECT (sample_point), "position-x");
      g_object_notify (G_OBJECT (sample_point), "position-y");

      g_object_thaw_notify (G_OBJECT (sample_point));
    }
}

PikaColorPickMode
pika_sample_point_get_pick_mode (PikaSamplePoint *sample_point)
{
  g_return_val_if_fail (PIKA_IS_SAMPLE_POINT (sample_point),
                        PIKA_COLOR_PICK_MODE_PIXEL);

  return sample_point->priv->pick_mode;
}

void
pika_sample_point_set_pick_mode (PikaSamplePoint   *sample_point,
                                 PikaColorPickMode  pick_mode)
{
  g_return_if_fail (PIKA_IS_SAMPLE_POINT (sample_point));

  if (sample_point->priv->pick_mode != pick_mode)
    {
      sample_point->priv->pick_mode = pick_mode;

      g_object_notify (G_OBJECT (sample_point), "pick-mode");
    }
}
