/* 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
 *
 * pikacontainerentry.c
 * Copyright (C) 2003 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 <string.h>

#include <gegl.h>
#include <gtk/gtk.h>

#include "libpikawidgets/pikawidgets.h"

#include "widgets-types.h"

#include "core/pikacontainer.h"
#include "core/pikacontext.h"
#include "core/pikaviewable.h"

#include "pikacellrendererviewable.h"
#include "pikacontainerentry.h"
#include "pikacontainertreestore.h"
#include "pikacontainerview.h"
#include "pikaviewrenderer.h"


static void     pika_container_entry_view_iface_init (PikaContainerViewInterface *iface);

static void     pika_container_entry_finalize     (GObject                *object);

static void     pika_container_entry_set_context  (PikaContainerView      *view,
                                                   PikaContext            *context);
static gpointer pika_container_entry_insert_item  (PikaContainerView      *view,
                                                   PikaViewable           *viewable,
                                                   gpointer                parent_insert_data,
                                                   gint                    index);
static void     pika_container_entry_remove_item  (PikaContainerView      *view,
                                                   PikaViewable           *viewable,
                                                   gpointer                insert_data);
static void     pika_container_entry_reorder_item (PikaContainerView      *view,
                                                   PikaViewable           *viewable,
                                                   gint                    new_index,
                                                   gpointer                insert_data);
static void     pika_container_entry_rename_item  (PikaContainerView      *view,
                                                   PikaViewable           *viewable,
                                                   gpointer                insert_data);
static gboolean  pika_container_entry_select_items(PikaContainerView      *view,
                                                   GList                  *items,
                                                   GList                  *paths);
static void     pika_container_entry_clear_items  (PikaContainerView      *view);
static void    pika_container_entry_set_view_size (PikaContainerView      *view);
static gint     pika_container_entry_get_selected (PikaContainerView      *view,
                                                   GList                 **items,
                                                   GList                 **items_data);

static void     pika_container_entry_changed      (GtkEntry               *entry,
                                                   PikaContainerView      *view);
static void   pika_container_entry_match_selected (GtkEntryCompletion     *widget,
                                                   GtkTreeModel           *model,
                                                   GtkTreeIter            *iter,
                                                   PikaContainerView      *view);


G_DEFINE_TYPE_WITH_CODE (PikaContainerEntry, pika_container_entry,
                         GTK_TYPE_ENTRY,
                         G_IMPLEMENT_INTERFACE (PIKA_TYPE_CONTAINER_VIEW,
                                                pika_container_entry_view_iface_init))

#define parent_class pika_container_entry_parent_class

static PikaContainerViewInterface *parent_view_iface = NULL;


static void
pika_container_entry_class_init (PikaContainerEntryClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->set_property = pika_container_view_set_property;
  object_class->get_property = pika_container_view_get_property;
  object_class->finalize     = pika_container_entry_finalize;

  pika_container_view_install_properties (object_class);
}

static void
pika_container_entry_view_iface_init (PikaContainerViewInterface *iface)
{
  parent_view_iface = g_type_interface_peek_parent (iface);

  if (! parent_view_iface)
    parent_view_iface = g_type_default_interface_peek (PIKA_TYPE_CONTAINER_VIEW);

  iface->set_context   = pika_container_entry_set_context;
  iface->insert_item   = pika_container_entry_insert_item;
  iface->remove_item   = pika_container_entry_remove_item;
  iface->reorder_item  = pika_container_entry_reorder_item;
  iface->rename_item   = pika_container_entry_rename_item;
  iface->select_items  = pika_container_entry_select_items;
  iface->clear_items   = pika_container_entry_clear_items;
  iface->set_view_size = pika_container_entry_set_view_size;
  iface->get_selected  = pika_container_entry_get_selected;

  iface->insert_data_free = (GDestroyNotify) gtk_tree_iter_free;
}

static void
pika_container_entry_init (PikaContainerEntry *entry)
{
  GtkEntryCompletion *completion;
  GtkTreeModel       *model;
  GtkCellRenderer    *cell;
  GType               types[PIKA_CONTAINER_TREE_STORE_N_COLUMNS];
  gint                n_types = 0;

  entry->viewable = NULL;

  completion = g_object_new (GTK_TYPE_ENTRY_COMPLETION,
                             "inline-completion",  FALSE,
                             "popup-single-match", TRUE,
                             "popup-set-width",    FALSE,
                             NULL);

  pika_container_tree_store_columns_init (types, &n_types);

  model = pika_container_tree_store_new (PIKA_CONTAINER_VIEW (entry),
                                         n_types, types);
  pika_container_tree_store_set_use_name (PIKA_CONTAINER_TREE_STORE (model),
                                          TRUE);

  gtk_entry_completion_set_model (completion, model);
  g_object_unref (model);

  gtk_entry_set_completion (GTK_ENTRY (entry), completion);

  g_signal_connect (completion, "match-selected",
                    G_CALLBACK (pika_container_entry_match_selected),
                    entry);

  g_object_unref (completion);

  /*  FIXME: This can be done better with GTK+ 2.6.  */

  cell = pika_cell_renderer_viewable_new ();
  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion), cell, FALSE);
  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (completion), cell,
                                  "renderer",
                                  PIKA_CONTAINER_TREE_STORE_COLUMN_RENDERER,
                                  NULL);

  pika_container_tree_store_add_renderer_cell (PIKA_CONTAINER_TREE_STORE (model),
                                               cell, -1);

  gtk_entry_completion_set_text_column (completion,
                                        PIKA_CONTAINER_TREE_STORE_COLUMN_NAME);

  g_signal_connect (entry, "changed",
                    G_CALLBACK (pika_container_entry_changed),
                    entry);
}

GtkWidget *
pika_container_entry_new (PikaContainer *container,
                          PikaContext   *context,
                          gint           view_size,
                          gint           view_border_width)
{
  GtkWidget         *entry;
  PikaContainerView *view;

  g_return_val_if_fail (container == NULL || PIKA_IS_CONTAINER (container),
                        NULL);
  g_return_val_if_fail (context == NULL || PIKA_IS_CONTEXT (context), NULL);

  entry = g_object_new (PIKA_TYPE_CONTAINER_ENTRY, NULL);

  view = PIKA_CONTAINER_VIEW (entry);

  pika_container_view_set_view_size (view, view_size, view_border_width);

  if (container)
    pika_container_view_set_container (view, container);

  if (context)
    pika_container_view_set_context (view, context);

  return entry;
}


/*  PikaContainerView methods  */

static GtkTreeModel *
pika_container_entry_get_model (PikaContainerView *view)
{
  GtkEntryCompletion *completion;

  completion = gtk_entry_get_completion (GTK_ENTRY (view));

  if (completion)
    return gtk_entry_completion_get_model (completion);

  return NULL;
}

static void
pika_container_entry_finalize (GObject *object)
{
  PikaContainerEntry *entry = PIKA_CONTAINER_ENTRY (object);

  g_clear_weak_pointer (&entry->viewable);

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

static void
pika_container_entry_set_context (PikaContainerView *view,
                                  PikaContext       *context)
{
  GtkTreeModel *model = pika_container_entry_get_model (view);

  parent_view_iface->set_context (view, context);

  if (model)
    pika_container_tree_store_set_context (PIKA_CONTAINER_TREE_STORE (model),
                                           context);
}

static gpointer
pika_container_entry_insert_item (PikaContainerView *view,
                                  PikaViewable      *viewable,
                                  gpointer           parent_insert_data,
                                  gint               index)
{
  GtkTreeModel *model = pika_container_entry_get_model (view);

  return pika_container_tree_store_insert_item (PIKA_CONTAINER_TREE_STORE (model),
                                                viewable,
                                                parent_insert_data,
                                                index);
}

static void
pika_container_entry_remove_item (PikaContainerView *view,
                                  PikaViewable      *viewable,
                                  gpointer           insert_data)
{
  GtkTreeModel *model = pika_container_entry_get_model (view);

  pika_container_tree_store_remove_item (PIKA_CONTAINER_TREE_STORE (model),
                                         viewable,
                                         insert_data);
}

static void
pika_container_entry_reorder_item (PikaContainerView *view,
                                   PikaViewable      *viewable,
                                   gint               new_index,
                                   gpointer           insert_data)
{
  GtkTreeModel *model = pika_container_entry_get_model (view);

  pika_container_tree_store_reorder_item (PIKA_CONTAINER_TREE_STORE (model),
                                          viewable,
                                          new_index,
                                          insert_data);
}

static void
pika_container_entry_rename_item (PikaContainerView *view,
                                  PikaViewable      *viewable,
                                  gpointer           insert_data)
{
  PikaContainerEntry *container_entry = PIKA_CONTAINER_ENTRY (view);
  GtkEntry           *entry           = GTK_ENTRY (view);
  GtkTreeModel       *model           = pika_container_entry_get_model (view);

  if (viewable == container_entry->viewable)
    {
      g_signal_handlers_block_by_func (entry,
                                       pika_container_entry_changed,
                                       view);

      gtk_entry_set_text (entry, pika_object_get_name (viewable));

      g_signal_handlers_unblock_by_func (entry,
                                         pika_container_entry_changed,
                                         view);
    }

  pika_container_tree_store_rename_item (PIKA_CONTAINER_TREE_STORE (model),
                                         viewable,
                                         insert_data);
}

static gboolean
pika_container_entry_select_items (PikaContainerView   *view,
                                   GList               *viewables,
                                   GList               *paths)
{
  PikaContainerEntry *container_entry = PIKA_CONTAINER_ENTRY (view);
  GtkEntry           *entry           = GTK_ENTRY (view);
  PikaViewable       *viewable        = NULL;

  /* XXX Only support 1 selected viewable for now. */
  if (viewables)
    viewable = viewables->data;

  g_signal_handlers_block_by_func (entry,
                                   pika_container_entry_changed,
                                   view);

  g_set_weak_pointer (&container_entry->viewable, viewable);

  if (viewable)
    {
      gtk_entry_set_icon_from_icon_name (entry,
                                         GTK_ENTRY_ICON_SECONDARY,
                                         NULL);
    }
  else
    {
      /* The selected item does not exist. */
      gtk_entry_set_icon_from_icon_name (entry,
                                         GTK_ENTRY_ICON_SECONDARY,
                                         PIKA_ICON_MASCOT_EEK);
    }

  gtk_entry_set_text (entry, viewable? pika_object_get_name (viewable) : "");

  g_signal_handlers_unblock_by_func (entry,
                                     pika_container_entry_changed,
                                     view);

  return TRUE;
}

static void
pika_container_entry_clear_items (PikaContainerView *view)
{
  GtkTreeModel *model = pika_container_entry_get_model (view);

  /* model is NULL in dispose() */
  if (model)
    pika_container_tree_store_clear_items (PIKA_CONTAINER_TREE_STORE (model));

  parent_view_iface->clear_items (view);
}

static void
pika_container_entry_set_view_size (PikaContainerView *view)
{
  GtkTreeModel *model = pika_container_entry_get_model (view);

  pika_container_tree_store_set_view_size (PIKA_CONTAINER_TREE_STORE (model));
}

static gint
pika_container_entry_get_selected (PikaContainerView  *view,
                                   GList             **items,
                                   GList             **items_data)
{
  PikaContainerEntry *container_entry = PIKA_CONTAINER_ENTRY (view);

  if (items)
    {
      if (container_entry->viewable != NULL)
        *items = g_list_prepend (NULL, container_entry->viewable);
      else
        *items = NULL;
    }

  return container_entry->viewable == NULL ? 0 : 1;
}

static void
pika_container_entry_changed (GtkEntry          *entry,
                              PikaContainerView *view)
{
  PikaContainerEntry *container_entry = PIKA_CONTAINER_ENTRY (entry);
  PikaContainer      *container       = pika_container_view_get_container (view);
  PikaObject         *object;
  const gchar        *text;

  if (! container)
    return;

  text = gtk_entry_get_text (entry);
  object = pika_container_get_child_by_name (container, text);

  g_set_weak_pointer (&container_entry->viewable, PIKA_VIEWABLE (object));

  if (container_entry->viewable)
    {
      pika_container_view_item_selected (view, container_entry->viewable);

      gtk_entry_set_icon_from_icon_name (entry,
                                         GTK_ENTRY_ICON_SECONDARY,
                                         NULL);
    }
  else
    {
      /* While editing the entry, show EEK for non-existent item. */
      gtk_entry_set_icon_from_icon_name (entry,
                                         GTK_ENTRY_ICON_SECONDARY,
                                         PIKA_ICON_MASCOT_EEK);
    }
}

static void
pika_container_entry_match_selected (GtkEntryCompletion *widget,
                                     GtkTreeModel       *model,
                                     GtkTreeIter        *iter,
                                     PikaContainerView  *view)
{
  PikaViewRenderer *renderer;

  gtk_tree_model_get (model, iter,
                      PIKA_CONTAINER_TREE_STORE_COLUMN_RENDERER, &renderer,
                      -1);

  pika_container_view_item_selected (view, renderer->viewable);
  g_object_unref (renderer);
}
