/* 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
 *
 * pikapickablebutton.c
 * Copyright (C) 2013 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 <gegl.h>
#include <gtk/gtk.h>

#include "libpikabase/pikabase.h"
#include "libpikawidgets/pikawidgets.h"

#include "widgets-types.h"

#include "core/pikacontext.h"
#include "core/pikaimage.h"
#include "core/pikalayer.h"
#include "core/pikalayermask.h"
#include "core/pikapickable.h"

#include "pikadnd.h"
#include "pikaview.h"
#include "pikaviewrenderer.h"
#include "pikapickablebutton.h"
#include "pikapickablepopup.h"


enum
{
  PROP_0,
  PROP_CONTEXT,
  PROP_PICKABLE
};

struct _PikaPickableButtonPrivate
{
  gint          view_size;
  gint          view_border_width;

  PikaContext  *context;
  PikaPickable *pickable;

  GtkWidget    *view;

  GBinding     *popup_binding;
  GtkWidget    *popup;
  PikaPickable *initial_pickable;
};


static void     pika_pickable_button_constructed   (GObject            *object);
static void     pika_pickable_button_dispose       (GObject            *object);
static void     pika_pickable_button_finalize      (GObject            *object);
static void     pika_pickable_button_set_property  (GObject            *object,
                                                    guint               property_id,
                                                    const GValue       *value,
                                                    GParamSpec         *pspec);
static void     pika_pickable_button_get_property  (GObject            *object,
                                                    guint               property_id,
                                                    GValue             *value,
                                                    GParamSpec         *pspec);

static void     pika_pickable_button_clicked       (GtkButton          *button);

static void     pika_pickable_button_popup_confirm (PikaPickableButton *button);
static void     pika_pickable_button_popup_cancel  (PikaPickableButton *button);
static void     pika_pickable_button_drop_pickable (GtkWidget          *widget,
                                                    gint                x,
                                                    gint                y,
                                                    PikaViewable       *viewable,
                                                    gpointer            data);
static void     pika_pickable_button_notify_buffer (PikaPickable       *pickable,
                                                    const GParamSpec   *pspec,
                                                    PikaPickableButton *button);


G_DEFINE_TYPE_WITH_PRIVATE (PikaPickableButton, pika_pickable_button,
                            PIKA_TYPE_BUTTON)

#define parent_class pika_pickable_button_parent_class


static void
pika_pickable_button_class_init (PikaPickableButtonClass *klass)
{
  GObjectClass   *object_class = G_OBJECT_CLASS (klass);
  GtkButtonClass *button_class = GTK_BUTTON_CLASS (klass);

  object_class->constructed  = pika_pickable_button_constructed;
  object_class->dispose      = pika_pickable_button_dispose;
  object_class->finalize     = pika_pickable_button_finalize;
  object_class->get_property = pika_pickable_button_get_property;
  object_class->set_property = pika_pickable_button_set_property;

  button_class->clicked      = pika_pickable_button_clicked;

  g_object_class_install_property (object_class, PROP_CONTEXT,
                                   g_param_spec_object ("context",
                                                        NULL, NULL,
                                                        PIKA_TYPE_CONTEXT,
                                                        PIKA_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY));

  g_object_class_install_property (object_class, PROP_PICKABLE,
                                   g_param_spec_object ("pickable",
                                                        NULL, NULL,
                                                        PIKA_TYPE_PICKABLE,
                                                        PIKA_PARAM_READWRITE));
}

static void
pika_pickable_button_init (PikaPickableButton *button)
{
  button->private = pika_pickable_button_get_instance_private (button);

  button->private->view_size         = PIKA_VIEW_SIZE_LARGE;
  button->private->view_border_width = 1;
  button->private->popup_binding     = NULL;

  pika_dnd_viewable_dest_add  (GTK_WIDGET (button), PIKA_TYPE_LAYER,
                               pika_pickable_button_drop_pickable,
                               NULL);
  pika_dnd_viewable_dest_add  (GTK_WIDGET (button), PIKA_TYPE_LAYER_MASK,
                               pika_pickable_button_drop_pickable,
                               NULL);
  pika_dnd_viewable_dest_add  (GTK_WIDGET (button), PIKA_TYPE_CHANNEL,
                               pika_pickable_button_drop_pickable,
                               NULL);
  pika_dnd_viewable_dest_add  (GTK_WIDGET (button), PIKA_TYPE_IMAGE,
                               pika_pickable_button_drop_pickable,
                               NULL);
}

static void
pika_pickable_button_constructed (GObject *object)
{
  PikaPickableButton *button = PIKA_PICKABLE_BUTTON (object);

  G_OBJECT_CLASS (parent_class)->constructed (object);

  pika_assert (PIKA_IS_CONTEXT (button->private->context));

  button->private->view =
    pika_view_new_by_types (button->private->context,
                            PIKA_TYPE_VIEW,
                            PIKA_TYPE_VIEWABLE,
                            button->private->view_size,
                            button->private->view_border_width,
                            FALSE);
  gtk_container_add (GTK_CONTAINER (button), button->private->view);
  gtk_widget_show (button->private->view);
}

static void
pika_pickable_button_dispose (GObject *object)
{
  PikaPickableButton *button = PIKA_PICKABLE_BUTTON (object);

  pika_pickable_button_set_pickable (button, NULL);

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

static void
pika_pickable_button_finalize (GObject *object)
{
  PikaPickableButton *button = PIKA_PICKABLE_BUTTON (object);

  g_clear_object (&button->private->context);
  g_clear_object (&button->private->initial_pickable);
  if (button->private->popup)
    gtk_widget_destroy (button->private->popup);

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

static void
pika_pickable_button_set_property (GObject      *object,
                                   guint         property_id,
                                   const GValue *value,
                                   GParamSpec   *pspec)
{
  PikaPickableButton *button = PIKA_PICKABLE_BUTTON (object);

  switch (property_id)
    {
    case PROP_CONTEXT:
      if (button->private->context)
        g_object_unref (button->private->context);
      button->private->context = g_value_dup_object (value);
      break;
    case PROP_PICKABLE:
      pika_pickable_button_set_pickable (button, g_value_get_object (value));
      break;

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

static void
pika_pickable_button_get_property (GObject    *object,
                                   guint       property_id,
                                   GValue     *value,
                                   GParamSpec *pspec)
{
  PikaPickableButton *button = PIKA_PICKABLE_BUTTON (object);

  switch (property_id)
    {
    case PROP_CONTEXT:
      g_value_set_object (value, button->private->context);
      break;
    case PROP_PICKABLE:
      g_value_set_object (value, button->private->pickable);
      break;

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

static void
pika_pickable_button_clicked (GtkButton *button)
{
  PikaPickableButton *pickable_button = PIKA_PICKABLE_BUTTON (button);
  GtkWidget          *popup;

  popup = pika_pickable_popup_new (pickable_button->private->context,
                                   pickable_button->private->view_size,
                                   pickable_button->private->view_border_width);

  g_signal_connect_swapped (popup, "confirm",
                            G_CALLBACK (pika_pickable_button_popup_confirm),
                            button);
  g_signal_connect_swapped (popup, "cancel",
                            G_CALLBACK (pika_pickable_button_popup_cancel),
                            button);
  pickable_button->private->popup_binding = g_object_bind_property (G_OBJECT (button), "pickable",
                                                                    G_OBJECT (popup),  "pickable",
                                                                    G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
  pickable_button->private->initial_pickable = pickable_button->private->pickable;
  if (pickable_button->private->initial_pickable)
    g_object_ref (pickable_button->private->initial_pickable);

  pickable_button->private->popup = popup;
  g_signal_connect (popup, "destroy",
                    G_CALLBACK (gtk_widget_destroyed),
                    &pickable_button->private->popup);
  pika_popup_show (PIKA_POPUP (popup), GTK_WIDGET (button));
}

static void
pika_pickable_button_popup_confirm (PikaPickableButton *button)
{
  g_clear_pointer (&button->private->popup_binding, g_binding_unbind);
  g_clear_object (&button->private->initial_pickable);
}

static void
pika_pickable_button_popup_cancel (PikaPickableButton *button)
{
  pika_pickable_button_set_pickable (button, button->private->initial_pickable);
  pika_pickable_button_popup_confirm (button);
}
static void
pika_pickable_button_drop_pickable (GtkWidget    *widget,
                                    gint          x,
                                    gint          y,
                                    PikaViewable *viewable,
                                    gpointer      data)
{
  pika_pickable_button_set_pickable (PIKA_PICKABLE_BUTTON (widget),
                                     PIKA_PICKABLE (viewable));
}

static void
pika_pickable_button_notify_buffer (PikaPickable       *pickable,
                                    const GParamSpec   *pspec,
                                    PikaPickableButton *button)
{
  GeglBuffer *buffer = pika_pickable_get_buffer (pickable);

  if (buffer)
    g_object_notify (G_OBJECT (button), "pickable");
  else
    pika_pickable_button_set_pickable (button, NULL);
}


/*  public functions  */

GtkWidget *
pika_pickable_button_new (PikaContext *context,
                          gint         view_size,
                          gint         view_border_width)
{
  PikaPickableButton *button;

  g_return_val_if_fail (PIKA_IS_CONTEXT (context), NULL);
  g_return_val_if_fail (view_size >  0 &&
                        view_size <= PIKA_VIEWABLE_MAX_BUTTON_SIZE, NULL);
  g_return_val_if_fail (view_border_width >= 0 &&
                        view_border_width <= PIKA_VIEW_MAX_BORDER_WIDTH,
                        NULL);

  button = g_object_new (PIKA_TYPE_PICKABLE_BUTTON,
                         "context", context,
                         NULL);

  button->private->view_size         = view_size;
  button->private->view_border_width = view_border_width;

  return GTK_WIDGET (button);
}

PikaPickable *
pika_pickable_button_get_pickable (PikaPickableButton *button)
{
  g_return_val_if_fail (PIKA_IS_PICKABLE_BUTTON (button), NULL);

  return button->private->pickable;
}

void
pika_pickable_button_set_pickable (PikaPickableButton *button,
                                   PikaPickable       *pickable)
{
  g_return_if_fail (PIKA_IS_PICKABLE_BUTTON (button));

  if (pickable != button->private->pickable)
    {
      if (button->private->pickable)
        g_signal_handlers_disconnect_by_func (button->private->pickable,
                                              pika_pickable_button_notify_buffer,
                                              button);

      g_set_object (&button->private->pickable, pickable);

      if (button->private->pickable)
        g_signal_connect (button->private->pickable, "notify::buffer",
                          G_CALLBACK (pika_pickable_button_notify_buffer),
                          button);

      pika_view_set_viewable (PIKA_VIEW (button->private->view),
                              PIKA_VIEWABLE (pickable));

      g_object_notify (G_OBJECT (button), "pickable");
    }
}
