/* 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
 *
 * pikasymmetryeditor.c
 * Copyright (C) 2015 Jehan <jehan@girinstud.io>
 *
 * 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 "libpikawidgets/pikawidgets.h"

#include "propgui/propgui-types.h" /* ugly, but what the heck */

#include "core/pikaimage.h"
#include "core/pikaimage-symmetry.h"
#include "core/pikasymmetry.h"

#include "propgui/pikapropgui.h"

#include "pikamenufactory.h"
#include "pikasymmetryeditor.h"

#include "pika-intl.h"


struct _PikaSymmetryEditorPrivate
{
  PikaContext *context;

  GtkWidget   *menu;
  GtkWidget   *options_vbox;
};


static void   pika_symmetry_editor_set_image        (PikaImageEditor    *editor,
                                                     PikaImage          *image);

/* Signal handlers on the contextual image. */
static void   pika_symmetry_editor_symmetry_notify  (PikaImage          *image,
                                                     GParamSpec         *pspec,
                                                     PikaSymmetryEditor *editor);

/* Signal handlers on the symmetry. */
static void   pika_symmetry_editor_symmetry_updated (PikaSymmetry       *symmetry,
                                                     PikaImage          *image,
                                                     PikaSymmetryEditor *editor);

/* Private functions. */
static void   pika_symmetry_editor_set_options      (PikaSymmetryEditor *editor,
                                                     PikaSymmetry       *symmetry);


G_DEFINE_TYPE_WITH_PRIVATE (PikaSymmetryEditor, pika_symmetry_editor,
                            PIKA_TYPE_IMAGE_EDITOR)

#define parent_class pika_symmetry_editor_parent_class


static void
pika_symmetry_editor_class_init (PikaSymmetryEditorClass *klass)
{
  PikaImageEditorClass *image_editor_class = PIKA_IMAGE_EDITOR_CLASS (klass);

  image_editor_class->set_image = pika_symmetry_editor_set_image;
}

static void
pika_symmetry_editor_init (PikaSymmetryEditor *editor)
{
  GtkWidget *scrolled_window;
  GtkWidget *viewport;

  editor->p = pika_symmetry_editor_get_instance_private (editor);

  gtk_widget_set_size_request (GTK_WIDGET (editor), -1, 200);

  scrolled_window = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
                                  GTK_POLICY_AUTOMATIC,
                                  GTK_POLICY_AUTOMATIC);
  gtk_box_pack_start (GTK_BOX (editor), scrolled_window, TRUE, TRUE, 0);
  gtk_widget_show (scrolled_window);

  viewport = gtk_viewport_new (NULL, NULL);
  gtk_container_add (GTK_CONTAINER (scrolled_window), viewport);
  gtk_widget_show (viewport);

  editor->p->options_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 2);
  g_object_set (editor->p->options_vbox, "valign", GTK_ALIGN_START, NULL);
  gtk_container_set_border_width (GTK_CONTAINER (editor->p->options_vbox), 2);
  gtk_container_add (GTK_CONTAINER (viewport), editor->p->options_vbox);
  gtk_widget_show (editor->p->options_vbox);

  pika_symmetry_editor_set_image (PIKA_IMAGE_EDITOR (editor), NULL);
}

static void
pika_symmetry_editor_set_image (PikaImageEditor *image_editor,
                                PikaImage       *image)
{
  GtkListStore *store;
  GtkTreeIter   iter;
  GList        *syms;
  GList        *sym_iter;
  PikaSymmetry *symmetry;

  PikaSymmetryEditor *editor = PIKA_SYMMETRY_EDITOR (image_editor);

  if (image_editor->image)
    {
      g_signal_handlers_disconnect_by_func (image_editor->image,
                                            G_CALLBACK (pika_symmetry_editor_symmetry_notify),
                                            editor);
    }

  PIKA_IMAGE_EDITOR_CLASS (parent_class)->set_image (image_editor, image);

  /* Destroy the previous menu. */
  if (editor->p->menu)
    {
      gtk_widget_destroy (editor->p->menu);
      editor->p->menu = NULL;
    }

  store = g_object_new (PIKA_TYPE_INT_STORE, NULL);

  /* The menu of available symmetries. */
  syms = pika_image_symmetry_list ();
  for (sym_iter = syms; sym_iter; sym_iter = g_list_next (sym_iter))
    {
      PikaSymmetryClass *klass;
      GType              type;

      type = (GType) sym_iter->data;
      klass = g_type_class_ref (type);

      gtk_list_store_prepend (store, &iter);
      gtk_list_store_set (store, &iter,
                          PIKA_INT_STORE_LABEL,
                          klass->label,
                          PIKA_INT_STORE_USER_DATA,
                          sym_iter->data,
                          -1);
      g_type_class_unref (klass);
    }
  g_list_free (syms);

  gtk_list_store_prepend (store, &iter);
  gtk_list_store_set (store, &iter,
                      PIKA_INT_STORE_LABEL, _("None"),
                      PIKA_INT_STORE_USER_DATA, PIKA_TYPE_SYMMETRY,
                      -1);
  if (image_editor->image)
    editor->p->menu =
      pika_prop_pointer_combo_box_new (G_OBJECT (image_editor->image),
                                       "symmetry",
                                       PIKA_INT_STORE (store));
  else
    editor->p->menu =
      pika_int_combo_box_new (_("None"), 0,
                              NULL);

  g_object_unref (store);

  pika_int_combo_box_set_label (PIKA_INT_COMBO_BOX (editor->p->menu),
                                _("Symmetry"));
  g_object_set (editor->p->menu, "ellipsize", PANGO_ELLIPSIZE_END, NULL);

  gtk_box_pack_start (GTK_BOX (editor), editor->p->menu,
                      FALSE, FALSE, 0);
  gtk_box_reorder_child (GTK_BOX (editor), editor->p->menu, 0);

  if (image_editor->image)
    {
      /* Connect to symmetry change. */
      g_signal_connect (image_editor->image, "notify::symmetry",
                        G_CALLBACK (pika_symmetry_editor_symmetry_notify),
                        editor);

      /* Update the symmetry options. */
      symmetry = pika_image_get_active_symmetry (image_editor->image);
      pika_symmetry_editor_set_options (editor, symmetry);
    }
  else
    {
      pika_int_combo_box_set_active (PIKA_INT_COMBO_BOX (editor->p->menu), 0);
      gtk_widget_set_sensitive (editor->p->menu, FALSE);
      gtk_widget_show (editor->p->menu);
    }
}

static void
pika_symmetry_editor_symmetry_notify (PikaImage          *image,
                                      GParamSpec         *pspec,
                                      PikaSymmetryEditor *editor)
{
  PikaSymmetry *symmetry = NULL;

  if (image)
    {
      symmetry = pika_image_get_active_symmetry (image);

      if (symmetry)
        g_signal_connect (symmetry, "gui-param-changed",
                          G_CALLBACK (pika_symmetry_editor_symmetry_updated),
                          editor);
    }

  pika_symmetry_editor_set_options (editor, symmetry);
}

static void
pika_symmetry_editor_symmetry_updated (PikaSymmetry       *symmetry,
                                       PikaImage          *image,
                                       PikaSymmetryEditor *editor)
{
  if (image    != pika_image_editor_get_image (PIKA_IMAGE_EDITOR (editor)) ||
      symmetry != pika_image_get_active_symmetry (image))
    {
      g_signal_handlers_disconnect_by_func (symmetry,
                                            pika_symmetry_editor_symmetry_updated,
                                            editor);
      return;
    }

  pika_symmetry_editor_set_options (editor, symmetry);
}

static void
pika_symmetry_editor_set_options (PikaSymmetryEditor *editor,
                                  PikaSymmetry       *symmetry)
{
  gtk_container_foreach (GTK_CONTAINER (editor->p->options_vbox),
                         (GtkCallback) gtk_widget_destroy, NULL);

  if (symmetry && G_TYPE_FROM_INSTANCE (symmetry) != PIKA_TYPE_SYMMETRY)
    {
      PikaImageEditor *image_editor = PIKA_IMAGE_EDITOR (editor);
      PikaImage       *image        = image_editor->image;
      GtkWidget       *gui;

      gui = pika_prop_gui_new (G_OBJECT (symmetry),
                               PIKA_TYPE_SYMMETRY,
                               PIKA_SYMMETRY_PARAM_GUI,
                               GEGL_RECTANGLE (0, 0,
                                               pika_image_get_width  (image),
                                               pika_image_get_height (image)),
                               PIKA_IMAGE_EDITOR (editor)->context,
                               NULL, NULL, NULL);
      gtk_box_pack_start (GTK_BOX (editor->p->options_vbox), gui,
                          FALSE, FALSE, 0);
    }
}


/*  public functions  */

GtkWidget *
pika_symmetry_editor_new (PikaMenuFactory *menu_factory)
{
  g_return_val_if_fail (PIKA_IS_MENU_FACTORY (menu_factory), NULL);

  return g_object_new (PIKA_TYPE_SYMMETRY_EDITOR,
                       "menu-factory",    menu_factory,
                       NULL);
}
