/* 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
 *
 * pikasettingseditor.c
 * Copyright (C) 2008-2017 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 "libpikabase/pikabase.h"
#include "libpikaconfig/pikaconfig.h"
#include "libpikawidgets/pikawidgets.h"

#include "widgets-types.h"

#include "operations/pika-operation-config.h"

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

#include "pikacontainertreestore.h"
#include "pikacontainertreeview.h"
#include "pikacontainerview.h"
#include "pikasettingseditor.h"
#include "pikaviewrenderer.h"
#include "pikawidgets-utils.h"

#include "pika-intl.h"


enum
{
  PROP_0,
  PROP_PIKA,
  PROP_CONFIG,
  PROP_CONTAINER
};


typedef struct _PikaSettingsEditorPrivate PikaSettingsEditorPrivate;

struct _PikaSettingsEditorPrivate
{
  Pika          *pika;
  GObject       *config;
  PikaContainer *container;
  GObject       *selected_setting;

  GtkWidget     *view;
  GtkWidget     *import_button;
  GtkWidget     *export_button;
  GtkWidget     *delete_button;
};

#define GET_PRIVATE(item) ((PikaSettingsEditorPrivate *) pika_settings_editor_get_instance_private ((PikaSettingsEditor *) (item)))


static void   pika_settings_editor_constructed    (GObject             *object);
static void   pika_settings_editor_finalize       (GObject             *object);
static void   pika_settings_editor_set_property   (GObject             *object,
                                                   guint                property_id,
                                                   const GValue        *value,
                                                   GParamSpec          *pspec);
static void   pika_settings_editor_get_property   (GObject             *object,
                                                   guint                property_id,
                                                   GValue              *value,
                                                   GParamSpec          *pspec);

static gboolean
          pika_settings_editor_row_separator_func (GtkTreeModel        *model,
                                                   GtkTreeIter         *iter,
                                                   gpointer             data);
static gboolean pika_settings_editor_select_items (PikaContainerView   *view,
                                                   GList               *viewables,
                                                   GList               *paths,
                                                   PikaSettingsEditor  *editor);
static void   pika_settings_editor_import_clicked (GtkWidget           *widget,
                                                   PikaSettingsEditor  *editor);
static void   pika_settings_editor_export_clicked (GtkWidget           *widget,
                                                   PikaSettingsEditor  *editor);
static void   pika_settings_editor_delete_clicked (GtkWidget           *widget,
                                                   PikaSettingsEditor  *editor);
static void   pika_settings_editor_name_edited    (GtkCellRendererText *cell,
                                                   const gchar         *path_str,
                                                   const gchar         *new_name,
                                                   PikaSettingsEditor  *editor);


G_DEFINE_TYPE_WITH_PRIVATE (PikaSettingsEditor, pika_settings_editor,
                            GTK_TYPE_BOX)

#define parent_class pika_settings_editor_parent_class


static void
pika_settings_editor_class_init (PikaSettingsEditorClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->constructed  = pika_settings_editor_constructed;
  object_class->finalize     = pika_settings_editor_finalize;
  object_class->set_property = pika_settings_editor_set_property;
  object_class->get_property = pika_settings_editor_get_property;

  g_object_class_install_property (object_class, PROP_PIKA,
                                   g_param_spec_object ("pika",
                                                        NULL, NULL,
                                                        PIKA_TYPE_PIKA,
                                                        PIKA_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY));

  g_object_class_install_property (object_class, PROP_CONFIG,
                                   g_param_spec_object ("config",
                                                        NULL, NULL,
                                                        PIKA_TYPE_CONFIG,
                                                        PIKA_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY));

  g_object_class_install_property (object_class, PROP_CONTAINER,
                                   g_param_spec_object ("container",
                                                        NULL, NULL,
                                                        PIKA_TYPE_CONTAINER,
                                                        PIKA_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY));
}

static void
pika_settings_editor_init (PikaSettingsEditor *editor)
{
  gtk_orientable_set_orientation (GTK_ORIENTABLE (editor),
                                  GTK_ORIENTATION_VERTICAL);

  gtk_box_set_spacing (GTK_BOX (editor), 6);
}

static void
pika_settings_editor_constructed (GObject *object)
{
  PikaSettingsEditor        *editor  = PIKA_SETTINGS_EDITOR (object);
  PikaSettingsEditorPrivate *private = GET_PRIVATE (object);
  PikaContainerTreeView     *tree_view;

  G_OBJECT_CLASS (parent_class)->constructed (object);

  pika_assert (PIKA_IS_PIKA (private->pika));
  pika_assert (PIKA_IS_CONFIG (private->config));
  pika_assert (PIKA_IS_CONTAINER (private->container));

  private->view = pika_container_tree_view_new (private->container,
                                                pika_get_user_context (private->pika),
                                               16, 0);
  gtk_widget_set_size_request (private->view, 200, 200);
  gtk_box_pack_start (GTK_BOX (editor), private->view, TRUE, TRUE, 0);
  gtk_widget_show (private->view);

  tree_view = PIKA_CONTAINER_TREE_VIEW (private->view);

  gtk_tree_view_set_row_separator_func (tree_view->view,
                                        pika_settings_editor_row_separator_func,
                                        private->view, NULL);

  g_signal_connect (tree_view, "select-items",
                    G_CALLBACK (pika_settings_editor_select_items),
                    editor);

  pika_container_tree_view_connect_name_edited (tree_view,
                                                G_CALLBACK (pika_settings_editor_name_edited),
                                                editor);

  private->import_button =
    pika_editor_add_button (PIKA_EDITOR (tree_view),
                            PIKA_ICON_DOCUMENT_OPEN,
                            _("Import presets from a file"),
                            NULL,
                            G_CALLBACK (pika_settings_editor_import_clicked),
                            NULL,
                            G_OBJECT (editor));

  private->export_button =
    pika_editor_add_button (PIKA_EDITOR (tree_view),
                            PIKA_ICON_DOCUMENT_SAVE,
                            _("Export the selected presets to a file"),
                            NULL,
                            G_CALLBACK (pika_settings_editor_export_clicked),
                            NULL,
                            G_OBJECT (editor));

  private->delete_button =
    pika_editor_add_button (PIKA_EDITOR (tree_view),
                            PIKA_ICON_EDIT_DELETE,
                            _("Delete the selected preset"),
                            NULL,
                            G_CALLBACK (pika_settings_editor_delete_clicked),
                            NULL,
                            G_OBJECT (editor));

  gtk_widget_set_sensitive (private->delete_button, FALSE);
}

static void
pika_settings_editor_finalize (GObject *object)
{
  PikaSettingsEditorPrivate *private = GET_PRIVATE (object);

  g_clear_object (&private->config);
  g_clear_object (&private->container);

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

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

  switch (property_id)
    {
    case PROP_PIKA:
      private->pika = g_value_get_object (value); /* don't dup */
      break;

    case PROP_CONFIG:
      private->config = g_value_dup_object (value);
      break;

    case PROP_CONTAINER:
      private->container = g_value_dup_object (value);
      break;

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

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

  switch (property_id)
    {
    case PROP_PIKA:
      g_value_set_object (value, private->pika);
      break;

    case PROP_CONFIG:
      g_value_set_object (value, private->config);
      break;

    case PROP_CONTAINER:
      g_value_set_object (value, private->container);
      break;

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

static gboolean
pika_settings_editor_row_separator_func (GtkTreeModel *model,
                                         GtkTreeIter  *iter,
                                         gpointer      data)
{
  gchar *name = NULL;

  gtk_tree_model_get (model, iter,
                      PIKA_CONTAINER_TREE_STORE_COLUMN_NAME, &name,
                      -1);
  g_free (name);

  return name == NULL;
}

static gboolean
pika_settings_editor_select_items (PikaContainerView  *view,
                                   GList              *viewables,
                                   GList              *paths,
                                   PikaSettingsEditor *editor)
{
  PikaSettingsEditorPrivate *private = GET_PRIVATE (editor);
  gboolean                   sensitive;

  g_return_val_if_fail (g_list_length (viewables) < 2, FALSE);

  private->selected_setting = viewables ? G_OBJECT (viewables->data) : NULL;

  sensitive = (private->selected_setting != NULL &&
               pika_object_get_name (private->selected_setting));

  gtk_widget_set_sensitive (private->export_button, sensitive);
  gtk_widget_set_sensitive (private->delete_button, sensitive);

  return TRUE;
}

static void
pika_settings_editor_import_clicked (GtkWidget          *widget,
                                     PikaSettingsEditor *editor)
{
}

static void
pika_settings_editor_export_clicked (GtkWidget          *widget,
                                     PikaSettingsEditor *editor)
{
}

static void
pika_settings_editor_delete_clicked (GtkWidget          *widget,
                                     PikaSettingsEditor *editor)
{
  PikaSettingsEditorPrivate *private = GET_PRIVATE (editor);

  if (private->selected_setting)
    {
      PikaObject *new;

      new = pika_container_get_neighbor_of (private->container,
                                            PIKA_OBJECT (private->selected_setting));

      /*  don't select the separator  */
      if (new && ! pika_object_get_name (new))
        new = NULL;

      pika_container_remove (private->container,
                             PIKA_OBJECT (private->selected_setting));

      pika_container_view_select_item (PIKA_CONTAINER_VIEW (private->view),
                                       PIKA_VIEWABLE (new));

      pika_operation_config_serialize (private->pika, private->container, NULL);
    }
}

static void
pika_settings_editor_name_edited (GtkCellRendererText *cell,
                                  const gchar         *path_str,
                                  const gchar         *new_name,
                                  PikaSettingsEditor  *editor)
{
  PikaSettingsEditorPrivate *private = GET_PRIVATE (editor);
  PikaContainerTreeView     *tree_view;
  GtkTreePath               *path;
  GtkTreeIter                iter;

  tree_view = PIKA_CONTAINER_TREE_VIEW (private->view);

  path = gtk_tree_path_new_from_string (path_str);

  if (gtk_tree_model_get_iter (tree_view->model, &iter, path))
    {
      PikaViewRenderer *renderer;
      PikaObject       *object;
      const gchar      *old_name;
      gchar            *name;

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

      object = PIKA_OBJECT (renderer->viewable);

      old_name = pika_object_get_name (object);

      if (! old_name) old_name = "";
      if (! new_name) new_name = "";

      name = g_strstrip (g_strdup (new_name));

      if (strlen (name) && strcmp (old_name, name))
        {
          gint64 t;

          g_object_get (object,
                        "time", &t,
                        NULL);

          if (t > 0)
            g_object_set (object,
                          "time", (gint64) 0,
                          NULL);

          /*  set name after time so the object is reordered correctly  */
          pika_object_take_name (object, name);

          pika_operation_config_serialize (private->pika, private->container,
                                           NULL);
        }
      else
        {
          g_free (name);

          name = pika_viewable_get_description (renderer->viewable, NULL);
          gtk_tree_store_set (GTK_TREE_STORE (tree_view->model), &iter,
                              PIKA_CONTAINER_TREE_STORE_COLUMN_NAME, name,
                              -1);
          g_free (name);
        }

      g_object_unref (renderer);
    }

  gtk_tree_path_free (path);
}


/*  public functions  */

GtkWidget *
pika_settings_editor_new (Pika          *pika,
                          GObject       *config,
                          PikaContainer *container)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);
  g_return_val_if_fail (PIKA_IS_CONFIG (config), NULL);
  g_return_val_if_fail (PIKA_IS_CONTAINER (container), NULL);

  return g_object_new (PIKA_TYPE_SETTINGS_EDITOR,
                       "pika",      pika,
                       "config",    config,
                       "container", container,
                       NULL);
}
