/* 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
 *
 * pikapickableselect.c
 * Copyright (C) 2023 Jehan
 *
 * 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 "gegl/pika-babl-compat.h"

#include "core/pika.h"
#include "core/pikacontext.h"
#include "core/pikadrawable.h"
#include "core/pikapickable.h"
#include "core/pikaparamspecs.h"
#include "core/pikatempbuf.h"

#include "pdb/pikapdb.h"

#include "pikapickablechooser.h"
#include "pikapickableselect.h"
#include "pikacontainerbox.h"

#include "pika-intl.h"


static void             pika_pickable_select_constructed     (GObject             *object);

static PikaValueArray * pika_pickable_select_run_callback    (PikaPdbDialog       *dialog,
                                                              PikaObject          *object,
                                                              gboolean             closing,
                                                              GError             **error);
static PikaObject     * pika_pickable_select_get_object      (PikaPdbDialog       *dialog);
static void             pika_pickable_select_set_object      (PikaPdbDialog       *dialog,
                                                              PikaObject          *object);

static void             pika_pickable_select_activate        (PikaPickableSelect  *select);
static void             pika_pickable_select_notify_pickable (PikaPickableSelect  *select);


G_DEFINE_TYPE (PikaPickableSelect, pika_pickable_select, PIKA_TYPE_PDB_DIALOG)

#define parent_class pika_pickable_select_parent_class


static void
pika_pickable_select_class_init (PikaPickableSelectClass *klass)
{
  GObjectClass       *object_class = G_OBJECT_CLASS (klass);
  PikaPdbDialogClass *pdb_class    = PIKA_PDB_DIALOG_CLASS (klass);

  object_class->constructed  = pika_pickable_select_constructed;

  pdb_class->run_callback    = pika_pickable_select_run_callback;
  pdb_class->get_object      = pika_pickable_select_get_object;
  pdb_class->set_object      = pika_pickable_select_set_object;
}

static void
pika_pickable_select_init (PikaPickableSelect *select)
{
}

static void
pika_pickable_select_constructed (GObject *object)
{
  PikaPdbDialog      *dialog = PIKA_PDB_DIALOG (object);
  PikaPickableSelect *select = PIKA_PICKABLE_SELECT (object);
  GtkWidget          *content_area;

  G_OBJECT_CLASS (parent_class)->constructed (object);

  content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));

  select->chooser = pika_pickable_chooser_new (dialog->context, dialog->select_type,
                                               PIKA_VIEW_SIZE_LARGE, 1);
  pika_pickable_chooser_set_pickable (PIKA_PICKABLE_CHOOSER (select->chooser),
                                      PIKA_PICKABLE (dialog->initial_object));
  g_signal_connect_swapped (select->chooser, "notify::pickable",
                            G_CALLBACK (pika_pickable_select_notify_pickable),
                            select);
  g_signal_connect_swapped (select->chooser, "activate",
                            G_CALLBACK (pika_pickable_select_activate),
                            select);
  gtk_box_pack_start (GTK_BOX (content_area), select->chooser, TRUE, TRUE, 0);
  gtk_widget_show (select->chooser);
}

static PikaValueArray *
pika_pickable_select_run_callback (PikaPdbDialog  *dialog,
                                   PikaObject     *object,
                                   gboolean        closing,
                                   GError        **error)
{
  PikaPickable   *pickable = PIKA_PICKABLE (object);
  PikaValueArray *return_vals;

  return_vals =
    pika_pdb_execute_procedure_by_name (dialog->pdb,
                                        dialog->caller_context,
                                        NULL, error,
                                        dialog->callback_name,
                                        PIKA_TYPE_DRAWABLE, pickable,
                                        G_TYPE_BOOLEAN,     closing,
                                        G_TYPE_NONE);

  return return_vals;
}

static PikaObject *
pika_pickable_select_get_object (PikaPdbDialog *dialog)
{
  PikaPickableSelect *select = PIKA_PICKABLE_SELECT (dialog);

  return (PikaObject *) pika_pickable_chooser_get_pickable (PIKA_PICKABLE_CHOOSER (select->chooser));
}

static void
pika_pickable_select_set_object (PikaPdbDialog *dialog,
                                 PikaObject    *object)
{
  PikaPickableSelect *select = PIKA_PICKABLE_SELECT (dialog);

  pika_pickable_chooser_set_pickable (PIKA_PICKABLE_CHOOSER (select->chooser), PIKA_PICKABLE (object));
}

static void
pika_pickable_select_activate (PikaPickableSelect *select)
{
  pika_pdb_dialog_run_callback ((PikaPdbDialog **) &select, TRUE);
  gtk_widget_destroy (GTK_WIDGET (select));
}

static void
pika_pickable_select_notify_pickable (PikaPickableSelect *select)
{
  pika_pdb_dialog_run_callback ((PikaPdbDialog **) &select, FALSE);
}
