/* 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
 *
 * pikacanvascanvasboundary.c
 * Copyright (C) 2019 Ell
 *
 * 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 "libpikabase/pikabase.h"
#include "libpikamath/pikamath.h"

#include "display-types.h"

#include "core/pikaimage.h"

#include "pikacanvas-style.h"
#include "pikacanvascanvasboundary.h"
#include "pikadisplayshell.h"


enum
{
  PROP_0,
  PROP_IMAGE
};


typedef struct _PikaCanvasCanvasBoundaryPrivate PikaCanvasCanvasBoundaryPrivate;

struct _PikaCanvasCanvasBoundaryPrivate
{
  PikaImage *image;
};

#define GET_PRIVATE(canvas_boundary) \
        ((PikaCanvasCanvasBoundaryPrivate *) pika_canvas_canvas_boundary_get_instance_private ((PikaCanvasCanvasBoundary *) (canvas_boundary)))


/*  local function prototypes  */

static void             pika_canvas_canvas_boundary_set_property (GObject        *object,
                                                                  guint           property_id,
                                                                  const GValue   *value,
                                                                  GParamSpec     *pspec);
static void             pika_canvas_canvas_boundary_get_property (GObject        *object,
                                                                  guint           property_id,
                                                                  GValue         *value,
                                                                  GParamSpec     *pspec);
static void             pika_canvas_canvas_boundary_finalize     (GObject        *object);
static void             pika_canvas_canvas_boundary_draw         (PikaCanvasItem *item,
                                                                  cairo_t        *cr);
static cairo_region_t * pika_canvas_canvas_boundary_get_extents  (PikaCanvasItem *item);
static void             pika_canvas_canvas_boundary_stroke       (PikaCanvasItem *item,
                                                                  cairo_t        *cr);


G_DEFINE_TYPE_WITH_PRIVATE (PikaCanvasCanvasBoundary, pika_canvas_canvas_boundary,
                            PIKA_TYPE_CANVAS_RECTANGLE)

#define parent_class pika_canvas_canvas_boundary_parent_class


static void
pika_canvas_canvas_boundary_class_init (PikaCanvasCanvasBoundaryClass *klass)
{
  GObjectClass        *object_class = G_OBJECT_CLASS (klass);
  PikaCanvasItemClass *item_class   = PIKA_CANVAS_ITEM_CLASS (klass);

  object_class->set_property = pika_canvas_canvas_boundary_set_property;
  object_class->get_property = pika_canvas_canvas_boundary_get_property;
  object_class->finalize     = pika_canvas_canvas_boundary_finalize;

  item_class->draw           = pika_canvas_canvas_boundary_draw;
  item_class->get_extents    = pika_canvas_canvas_boundary_get_extents;
  item_class->stroke         = pika_canvas_canvas_boundary_stroke;

  g_object_class_install_property (object_class, PROP_IMAGE,
                                   g_param_spec_object ("image", NULL, NULL,
                                                        PIKA_TYPE_IMAGE,
                                                        PIKA_PARAM_READWRITE));
}

static void
pika_canvas_canvas_boundary_init (PikaCanvasCanvasBoundary *canvas_boundary)
{
}

static void
pika_canvas_canvas_boundary_finalize (GObject *object)
{
  PikaCanvasCanvasBoundaryPrivate *private = GET_PRIVATE (object);

  g_clear_weak_pointer (&private->image);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
pika_canvas_canvas_boundary_set_property (GObject      *object,
                                          guint         property_id,
                                          const GValue *value,
                                          GParamSpec   *pspec)
{
  PikaCanvasCanvasBoundaryPrivate *private = GET_PRIVATE (object);

  switch (property_id)
    {
    case PROP_IMAGE:
      g_set_weak_pointer (&private->image, g_value_get_object (value));
      break;

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

static void
pika_canvas_canvas_boundary_get_property (GObject    *object,
                                          guint       property_id,
                                          GValue     *value,
                                          GParamSpec *pspec)
{
  PikaCanvasCanvasBoundaryPrivate *private = GET_PRIVATE (object);

  switch (property_id)
    {
    case PROP_IMAGE:
      g_value_set_object (value, private->image);
      break;

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

static void
pika_canvas_canvas_boundary_draw (PikaCanvasItem *item,
                                  cairo_t        *cr)
{
  PikaCanvasCanvasBoundaryPrivate *private = GET_PRIVATE (item);

  if (private->image)
    PIKA_CANVAS_ITEM_CLASS (parent_class)->draw (item, cr);
}

static cairo_region_t *
pika_canvas_canvas_boundary_get_extents (PikaCanvasItem *item)
{
  PikaCanvasCanvasBoundaryPrivate *private = GET_PRIVATE (item);

  if (private->image)
    return PIKA_CANVAS_ITEM_CLASS (parent_class)->get_extents (item);

  return NULL;
}

static void
pika_canvas_canvas_boundary_stroke (PikaCanvasItem *item,
                                    cairo_t        *cr)
{
  PikaDisplayShell *shell = pika_canvas_item_get_shell (item);

  pika_canvas_set_canvas_style (pika_canvas_item_get_canvas (item), cr,
                                shell->offset_x, shell->offset_y);
  cairo_stroke (cr);
}

PikaCanvasItem *
pika_canvas_canvas_boundary_new (PikaDisplayShell *shell)
{
  g_return_val_if_fail (PIKA_IS_DISPLAY_SHELL (shell), NULL);

  return g_object_new (PIKA_TYPE_CANVAS_CANVAS_BOUNDARY,
                       "shell", shell,
                       NULL);
}

void
pika_canvas_canvas_boundary_set_image (PikaCanvasCanvasBoundary *boundary,
                                       PikaImage                *image)
{
  PikaCanvasCanvasBoundaryPrivate *private;

  g_return_if_fail (PIKA_IS_CANVAS_CANVAS_BOUNDARY (boundary));
  g_return_if_fail (image == NULL || PIKA_IS_IMAGE (image));

  private = GET_PRIVATE (boundary);

  if (image != private->image)
    {
      pika_canvas_item_begin_change (PIKA_CANVAS_ITEM (boundary));

      if (image)
        {
          g_object_set (boundary,
                        "x",      (gdouble) 0,
                        "y",      (gdouble) 0,
                        "width",  (gdouble) pika_image_get_width  (image),
                        "height", (gdouble) pika_image_get_height (image),
                        NULL);
        }

      g_object_set (boundary,
                    "image", image,
                    NULL);

      pika_canvas_item_end_change (PIKA_CANVAS_ITEM (boundary));
    }
  else if (image && image == private->image)
    {
      gint    lx, ly, lw, lh;
      gdouble x, y, w ,h;

      lx = 0;
      ly = 0;
      lw = pika_image_get_width  (image);
      lh = pika_image_get_height (image);

      g_object_get (boundary,
                    "x",      &x,
                    "y",      &y,
                    "width",  &w,
                    "height", &h,
                    NULL);

      if (lx != (gint) x ||
          ly != (gint) y ||
          lw != (gint) w ||
          lh != (gint) h)
        {
          pika_canvas_item_begin_change (PIKA_CANVAS_ITEM (boundary));

          g_object_set (boundary,
                        "x",      (gdouble) lx,
                        "y",      (gdouble) ly,
                        "width",  (gdouble) lw,
                        "height", (gdouble) lh,
                        NULL);

          pika_canvas_item_end_change (PIKA_CANVAS_ITEM (boundary));
        }
    }
}
