/* 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
 *
 *
 * 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 "libpikaconfig/pikaconfig.h"
#include "libpikawidgets/pikawidgets.h"

#include "widgets-types.h"

#include "core/pika.h"
#include "core/pikacontext.h"
#include "core/pikadynamics.h"

#include "pikadocked.h"
#include "pikadynamicseditor.h"
#include "pikadynamicsoutputeditor.h"
#include "pikamenufactory.h"
#include "pikapropwidgets.h"

#include "pika-intl.h"


/*  local function prototypes  */

static void   pika_dynamics_editor_constructed     (GObject            *object);
static void   pika_dynamics_editor_finalize        (GObject            *object);

static void   pika_dynamics_editor_set_data        (PikaDataEditor     *editor,
                                                    PikaData           *data);

static void   pika_dynamics_editor_notify_model    (PikaDynamics       *options,
                                                    const GParamSpec   *pspec,
                                                    PikaDynamicsEditor *editor);
static void   pika_dynamics_editor_notify_data     (PikaDynamics       *options,
                                                    const GParamSpec   *pspec,
                                                    PikaDynamicsEditor *editor);

static void   pika_dynamics_editor_add_icon_editor (PikaDynamics       *dynamics,
                                                    Pika               *pika,
                                                    GtkWidget          *vbox);

static void   pika_dynamics_editor_add_output_row  (GObject     *config,
                                                    const gchar *row_label,
                                                    GtkGrid     *grid,
                                                    gint         row);

static void pika_dynamics_editor_init_output_editors (PikaDynamics *dynamics,
                                                      GtkWidget    *view_selector,
                                                      GtkWidget    *notebook,
                                                      GtkWidget    *check_grid);

static GtkWidget * dynamics_check_button_new       (GObject     *config,
                                                    const gchar *property_name,
                                                    GtkGrid     *grid,
                                                    gint         column,
                                                    gint         row);

static void      pika_dynamics_editor_view_changed (GtkComboBox *combo,
                                                    GtkWidget   *notebook);

G_DEFINE_TYPE_WITH_CODE (PikaDynamicsEditor, pika_dynamics_editor,
                         PIKA_TYPE_DATA_EDITOR,
                         G_IMPLEMENT_INTERFACE (PIKA_TYPE_DOCKED, NULL))

#define parent_class pika_dynamics_editor_parent_class


static void
pika_dynamics_editor_class_init (PikaDynamicsEditorClass *klass)
{
  GObjectClass        *object_class = G_OBJECT_CLASS (klass);
  PikaDataEditorClass *editor_class = PIKA_DATA_EDITOR_CLASS (klass);

  object_class->constructed = pika_dynamics_editor_constructed;
  object_class->finalize    = pika_dynamics_editor_finalize;

  editor_class->set_data    = pika_dynamics_editor_set_data;
  editor_class->title       = _("Paint Dynamics Editor");
}

static void
pika_dynamics_editor_init (PikaDynamicsEditor *editor)
{
  PikaDataEditor *data_editor = PIKA_DATA_EDITOR (editor);

  editor->dynamics_model = g_object_new (PIKA_TYPE_DYNAMICS, NULL);

  g_signal_connect (editor->dynamics_model, "notify",
                    G_CALLBACK (pika_dynamics_editor_notify_model),
                    editor);

  editor->view_selector =
    pika_enum_combo_box_new (PIKA_TYPE_DYNAMICS_OUTPUT_TYPE);
  gtk_box_pack_start (GTK_BOX (data_editor), editor->view_selector,
                      FALSE, FALSE, 0);
  gtk_widget_show (editor->view_selector);

  editor->notebook = gtk_notebook_new ();
  gtk_notebook_set_show_border (GTK_NOTEBOOK (editor->notebook), FALSE);
  gtk_notebook_set_show_tabs (GTK_NOTEBOOK (editor->notebook), FALSE);
  gtk_box_pack_start (GTK_BOX (editor), editor->notebook, TRUE, TRUE, 0);
  gtk_widget_show (editor->notebook);
}

static void
pika_dynamics_editor_constructed (GObject *object)
{
  PikaDataEditor     *data_editor = PIKA_DATA_EDITOR (object);
  PikaDynamicsEditor *editor      = PIKA_DYNAMICS_EDITOR (object);
  PikaDynamics       *dynamics    = editor->dynamics_model;
  GtkWidget          *input_labels[7];
  GtkWidget          *vbox;
  GtkWidget          *icon_box;
  GtkWidget          *grid;
  gint                n_inputs    = G_N_ELEMENTS (input_labels);
  gint                i;

  G_OBJECT_CLASS (parent_class)->constructed (object);

  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
  gtk_notebook_append_page (GTK_NOTEBOOK (editor->notebook),
                            vbox, NULL);
  gtk_widget_show (vbox);

  icon_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
  gtk_box_pack_start (GTK_BOX (vbox), icon_box, FALSE, FALSE, 0);
  gtk_widget_show (icon_box);

  pika_dynamics_editor_add_icon_editor (dynamics,
                                        data_editor->context->pika,
                                        vbox);

  grid = gtk_grid_new ();
  gtk_box_pack_start (GTK_BOX (vbox), grid, FALSE, FALSE, 0);
  gtk_widget_show (grid);

  pika_dynamics_editor_init_output_editors (dynamics,
                                            editor->view_selector,
                                            editor->notebook,
                                            grid);

  input_labels[0] = gtk_label_new (_("Pressure"));
  input_labels[1] = gtk_label_new (_("Velocity"));
  input_labels[2] = gtk_label_new (_("Direction"));
  input_labels[3] = gtk_label_new (_("Tilt"));
  input_labels[4] = gtk_label_new (_("Wheel/Rotation"));
  input_labels[5] = gtk_label_new (_("Random"));
  input_labels[6] = gtk_label_new (_("Fade"));

  for (i = 0; i < n_inputs; i++)
    {
      gtk_label_set_angle (GTK_LABEL (input_labels[i]), 90);
      gtk_label_set_yalign (GTK_LABEL (input_labels[i]), 1.0);

      gtk_grid_attach (GTK_GRID (grid), input_labels[i], i + 1, 0, 1, 1);
      gtk_widget_show (input_labels[i]);
    }

  pika_int_combo_box_prepend (PIKA_INT_COMBO_BOX (editor->view_selector),
                              PIKA_INT_STORE_VALUE,     -1,
                              PIKA_INT_STORE_LABEL,     _("Mapping matrix"),
                              PIKA_INT_STORE_USER_DATA, vbox,
                              -1);

  pika_int_combo_box_set_active (PIKA_INT_COMBO_BOX (editor->view_selector), -1);

  pika_docked_set_show_button_bar (PIKA_DOCKED (object), FALSE);
}

static void
pika_dynamics_editor_finalize (GObject *object)
{
  PikaDynamicsEditor *editor = PIKA_DYNAMICS_EDITOR (object);

  g_clear_object (&editor->dynamics_model);

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

static void
pika_dynamics_editor_set_data (PikaDataEditor *editor,
                               PikaData       *data)
{
  PikaDynamicsEditor *dynamics_editor = PIKA_DYNAMICS_EDITOR (editor);

  if (editor->data)
    g_signal_handlers_disconnect_by_func (editor->data,
                                          pika_dynamics_editor_notify_data,
                                          editor);

  PIKA_DATA_EDITOR_CLASS (parent_class)->set_data (editor, data);

  if (editor->data)
    {
      g_signal_handlers_block_by_func (dynamics_editor->dynamics_model,
                                       pika_dynamics_editor_notify_model,
                                       editor);

      pika_config_copy (PIKA_CONFIG (editor->data),
                        PIKA_CONFIG (dynamics_editor->dynamics_model),
                        PIKA_CONFIG_PARAM_SERIALIZE);

      g_signal_handlers_unblock_by_func (dynamics_editor->dynamics_model,
                                         pika_dynamics_editor_notify_model,
                                         editor);

      g_signal_connect (editor->data, "notify",
                        G_CALLBACK (pika_dynamics_editor_notify_data),
                        editor);
    }

  gtk_widget_set_sensitive (dynamics_editor->notebook, editor->data_editable);
}


/*  public functions  */

GtkWidget *
pika_dynamics_editor_new (PikaContext     *context,
                          PikaMenuFactory *menu_factory)
{
  g_return_val_if_fail (PIKA_IS_MENU_FACTORY (menu_factory), NULL);
  g_return_val_if_fail (PIKA_IS_CONTEXT (context), NULL);

  return g_object_new (PIKA_TYPE_DYNAMICS_EDITOR,
                       "menu-factory",    menu_factory,
                       "menu-identifier", "<DynamicsEditor>",
                       "ui-path",         "/dynamics-editor-popup",
                       "data-factory",    context->pika->dynamics_factory,
                       "context",         context,
                       "data",            pika_context_get_dynamics (context),
                       NULL);
}


/*  private functions  */

static void
pika_dynamics_editor_notify_model (PikaDynamics       *options,
                                   const GParamSpec   *pspec,
                                   PikaDynamicsEditor *editor)
{
  PikaDataEditor *data_editor = PIKA_DATA_EDITOR (editor);

  if (data_editor->data)
    {
      g_signal_handlers_block_by_func (data_editor->data,
                                       pika_dynamics_editor_notify_data,
                                       editor);

      pika_config_copy (PIKA_CONFIG (editor->dynamics_model),
                        PIKA_CONFIG (data_editor->data),
                        PIKA_CONFIG_PARAM_SERIALIZE);

      g_signal_handlers_unblock_by_func (data_editor->data,
                                         pika_dynamics_editor_notify_data,
                                         editor);
    }
}

static void
pika_dynamics_editor_notify_data (PikaDynamics       *options,
                                  const GParamSpec   *pspec,
                                  PikaDynamicsEditor *editor)
{
  PikaDataEditor *data_editor = PIKA_DATA_EDITOR (editor);

  g_signal_handlers_block_by_func (editor->dynamics_model,
                                   pika_dynamics_editor_notify_model,
                                   editor);

  pika_config_copy (PIKA_CONFIG (data_editor->data),
                    PIKA_CONFIG (editor->dynamics_model),
                    PIKA_CONFIG_PARAM_SERIALIZE);

  g_signal_handlers_unblock_by_func (editor->dynamics_model,
                                     pika_dynamics_editor_notify_model,
                                     editor);
}

static void
pika_dynamics_editor_add_icon_editor (PikaDynamics *dynamics,
                                      Pika         *pika,
                                      GtkWidget    *vbox)
{
  GtkWidget *hbox;
  GtkWidget *label;
  GtkWidget *button;

  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 2);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);

  label = gtk_label_new (_("Icon:"));
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

  button = pika_prop_icon_picker_new (PIKA_VIEWABLE (dynamics), pika);
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
}

static void
pika_dynamics_editor_add_output_row (GObject     *config,
                                     const gchar *row_label,
                                     GtkGrid     *grid,
                                     gint         row)
{
  GtkWidget *label;
  gint       column = 1;

  label = gtk_label_new (row_label);
  gtk_label_set_xalign (GTK_LABEL (label), 0.0);
  gtk_grid_attach (grid, label, 0, row, 1, 1);
  gtk_widget_show (label);

  dynamics_check_button_new (config, "use-pressure",
                             grid, column, row);
  column++;

  dynamics_check_button_new (config, "use-velocity",
                             grid, column, row);
  column++;

  dynamics_check_button_new (config, "use-direction",
                             grid, column, row);
  column++;

  dynamics_check_button_new (config,  "use-tilt",
                             grid, column, row);
  column++;

  dynamics_check_button_new (config,  "use-wheel",
                             grid, column, row);
  column++;

  dynamics_check_button_new (config, "use-random",
                             grid, column, row);
  column++;

  dynamics_check_button_new (config, "use-fade",
                             grid, column, row);
  column++;
}

static GtkWidget *
dynamics_check_button_new (GObject     *config,
                           const gchar *property_name,
                           GtkGrid     *grid,
                           gint         column,
                           gint         row)
{
  GtkWidget *button;

  button = pika_prop_check_button_new (config, property_name, NULL);
  gtk_widget_destroy (gtk_bin_get_child (GTK_BIN (button)));
  gtk_grid_attach (grid, button, column, row, 1, 1);

  return button;
}

static void
pika_dynamics_editor_init_output_editors (PikaDynamics *dynamics,
                                          GtkWidget    *view_selector,
                                          GtkWidget    *notebook,
                                          GtkWidget    *check_grid)
{
  GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (view_selector));
  PikaIntStore *list  = PIKA_INT_STORE (model);
  GtkTreeIter   iter;
  gboolean      iter_valid;
  gint          i;

  for (iter_valid = gtk_tree_model_get_iter_first (model, &iter), i = 1;
       iter_valid;
       iter_valid = gtk_tree_model_iter_next (model, &iter), i++)
    {
      gint                output_type;
      gchar              *label;
      PikaDynamicsOutput *output;
      GtkWidget          *output_editor;

      gtk_tree_model_get (GTK_TREE_MODEL (model), &iter,
                          PIKA_INT_STORE_VALUE, &output_type,
                          PIKA_INT_STORE_LABEL, &label,
                          -1);

      output = pika_dynamics_get_output (dynamics, output_type);

      output_editor = pika_dynamics_output_editor_new (output);

      gtk_notebook_append_page (GTK_NOTEBOOK (notebook), output_editor, NULL);
      gtk_widget_show (output_editor);

      gtk_list_store_set (GTK_LIST_STORE (list), &iter,
                          PIKA_INT_STORE_USER_DATA, output_editor,
                          -1);

      pika_dynamics_editor_add_output_row (G_OBJECT (output),
                                           label,
                                           GTK_GRID (check_grid),
                                           i);

      g_free (label);
  }

  g_signal_connect (G_OBJECT (view_selector), "changed",
                    G_CALLBACK (pika_dynamics_editor_view_changed),
                    notebook);
}

static void
pika_dynamics_editor_view_changed (GtkComboBox *combo,
                                   GtkWidget   *notebook)
{
  GtkTreeModel *model = gtk_combo_box_get_model (combo);
  GtkTreeIter   iter;
  gpointer      widget;
  gint          page;

  gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo), &iter);

  gtk_tree_model_get (model, &iter,
                      PIKA_INT_STORE_USER_DATA, &widget,
                      -1);
  page = gtk_notebook_page_num (GTK_NOTEBOOK (notebook), widget);

  gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), page);
}
