/* 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 "actions-types.h"

#include "config/pikacoreconfig.h"

#include "core/pika.h"
#include "core/pikacontainer.h"
#include "core/pikacontext.h"
#include "core/pikaimage-new.h"
#include "core/pikatemplate.h"

#include "widgets/pikacontainerview.h"
#include "widgets/pikadialogfactory.h"
#include "widgets/pikahelp-ids.h"
#include "widgets/pikamessagebox.h"
#include "widgets/pikamessagedialog.h"
#include "widgets/pikatemplateeditor.h"
#include "widgets/pikatemplateview.h"
#include "widgets/pikawidgets-utils.h"

#include "dialogs/dialogs.h"
#include "dialogs/template-options-dialog.h"

#include "actions.h"
#include "templates-commands.h"

#include "pika-intl.h"


typedef struct
{
  PikaContext   *context;
  PikaContainer *container;
  PikaTemplate  *template;
} TemplateDeleteData;


/*  local function prototypes  */

static void   templates_new_callback     (GtkWidget          *dialog,
                                          PikaTemplate       *template,
                                          PikaTemplate       *edit_template,
                                          PikaContext        *context,
                                          gpointer            user_data);
static void   templates_edit_callback    (GtkWidget          *dialog,
                                          PikaTemplate       *template,
                                          PikaTemplate       *edit_template,
                                          PikaContext        *context,
                                          gpointer            user_data);
static void   templates_delete_response  (GtkWidget          *dialog,
                                          gint                response_id,
                                          TemplateDeleteData *delete_data);
static void   templates_delete_data_free (TemplateDeleteData *delete_data);


/*  public functions */

void
templates_create_image_cmd_callback (PikaAction *action,
                                     GVariant   *value,
                                     gpointer    data)
{
  Pika                *pika;
  PikaContainerEditor *editor = PIKA_CONTAINER_EDITOR (data);
  PikaContainer       *container;
  PikaContext         *context;
  PikaTemplate        *template;
  return_if_no_pika (pika, data);

  container = pika_container_view_get_container (editor->view);
  context   = pika_container_view_get_context (editor->view);

  template = pika_context_get_template (context);

  if (template && pika_container_have (container, PIKA_OBJECT (template)))
    {
      GtkWidget *widget = GTK_WIDGET (editor);
      PikaImage *image;

      image = pika_image_new_from_template (pika, template, context);
      pika_create_display (pika, image, pika_template_get_unit (template), 1.0,
                           G_OBJECT (pika_widget_get_monitor (widget)));
      g_object_unref (image);

      pika_image_new_set_last_template (pika, template);
    }
}

void
templates_new_cmd_callback (PikaAction *action,
                            GVariant   *value,
                            gpointer    data)
{
  PikaContainerEditor *editor = PIKA_CONTAINER_EDITOR (data);
  PikaContext         *context;
  GtkWidget           *dialog;

  context = pika_container_view_get_context (editor->view);

#define NEW_DIALOG_KEY "pika-template-new-dialog"

  dialog = dialogs_get_dialog (G_OBJECT (context->pika), NEW_DIALOG_KEY);

  if (! dialog)
    {
      dialog = template_options_dialog_new (NULL, context,
                                            GTK_WIDGET (editor),
                                            _("New Template"),
                                            "pika-template-new",
                                            PIKA_ICON_TEMPLATE,
                                            _("Create a New Template"),
                                            PIKA_HELP_TEMPLATE_NEW,
                                            templates_new_callback,
                                            NULL);

      dialogs_attach_dialog (G_OBJECT (context->pika), NEW_DIALOG_KEY, dialog);
    }

  gtk_window_present (GTK_WINDOW (dialog));
}

void
templates_duplicate_cmd_callback (PikaAction *action,
                                  GVariant   *value,
                                  gpointer    data)
{
  PikaContainerEditor *editor = PIKA_CONTAINER_EDITOR (data);
  PikaContainer       *container;
  PikaContext         *context;
  PikaTemplate        *template;

  container = pika_container_view_get_container (editor->view);
  context   = pika_container_view_get_context (editor->view);

  template = pika_context_get_template (context);

  if (template && pika_container_have (container, PIKA_OBJECT (template)))
    {
      PikaTemplate *new_template;

      new_template = pika_config_duplicate (PIKA_CONFIG (template));

      pika_container_add (container, PIKA_OBJECT (new_template));
      pika_context_set_by_type (context,
                                pika_container_get_children_type (container),
                                PIKA_OBJECT (new_template));
      g_object_unref (new_template);

      templates_edit_cmd_callback (action, value, data);
    }
}

void
templates_edit_cmd_callback (PikaAction *action,
                             GVariant   *value,
                             gpointer    data)
{
  PikaContainerEditor *editor = PIKA_CONTAINER_EDITOR (data);
  PikaContainer       *container;
  PikaContext         *context;
  PikaTemplate        *template;

  container = pika_container_view_get_container (editor->view);
  context   = pika_container_view_get_context (editor->view);

  template = pika_context_get_template (context);

  if (template && pika_container_have (container, PIKA_OBJECT (template)))
    {
      GtkWidget *dialog;

#define EDIT_DIALOG_KEY "pika-template-edit-dialog"

      dialog = dialogs_get_dialog (G_OBJECT (template), EDIT_DIALOG_KEY);

      if (! dialog)
        {
          dialog = template_options_dialog_new (template, context,
                                                GTK_WIDGET (editor),
                                                _("Edit Template"),
                                                "pika-template-edit",
                                                PIKA_ICON_EDIT,
                                                _("Edit Template"),
                                                PIKA_HELP_TEMPLATE_EDIT,
                                                templates_edit_callback,
                                                NULL);

          dialogs_attach_dialog (G_OBJECT (template), EDIT_DIALOG_KEY, dialog);
        }

      gtk_window_present (GTK_WINDOW (dialog));
    }
}

void
templates_delete_cmd_callback (PikaAction *action,
                               GVariant   *value,
                               gpointer    data)
{
  PikaContainerEditor *editor = PIKA_CONTAINER_EDITOR (data);
  PikaContainer       *container;
  PikaContext         *context;
  PikaTemplate        *template;

  container = pika_container_view_get_container (editor->view);
  context   = pika_container_view_get_context (editor->view);

  template = pika_context_get_template (context);

  if (template && pika_container_have (container, PIKA_OBJECT (template)))
    {
      TemplateDeleteData *delete_data = g_slice_new (TemplateDeleteData);
      GtkWidget          *dialog;

      delete_data->context   = context;
      delete_data->container = container;
      delete_data->template  = template;

      dialog =
        pika_message_dialog_new (_("Delete Template"), PIKA_ICON_EDIT_DELETE,
                                 GTK_WIDGET (editor), 0,
                                 pika_standard_help_func, NULL,

                                 _("_Cancel"), GTK_RESPONSE_CANCEL,
                                 _("_Delete"), GTK_RESPONSE_OK,

                                 NULL);

      pika_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
                                               GTK_RESPONSE_OK,
                                               GTK_RESPONSE_CANCEL,
                                               -1);

      g_object_weak_ref (G_OBJECT (dialog),
                         (GWeakNotify) templates_delete_data_free, delete_data);

      g_signal_connect_object (template, "disconnect",
                               G_CALLBACK (gtk_widget_destroy),
                               dialog, G_CONNECT_SWAPPED);

      g_signal_connect (dialog, "response",
                        G_CALLBACK (templates_delete_response),
                        delete_data);

      pika_message_box_set_primary_text (PIKA_MESSAGE_DIALOG (dialog)->box,
                                         _("Are you sure you want to delete "
                                           "template '%s' from the list and "
                                           "from disk?"),
                                         pika_object_get_name (template));
      gtk_widget_show (dialog);
    }
}


/*  private functions  */

static void
templates_new_callback (GtkWidget    *dialog,
                        PikaTemplate *template,
                        PikaTemplate *edit_template,
                        PikaContext  *context,
                        gpointer      user_data)
{
  pika_container_add (context->pika->templates, PIKA_OBJECT (edit_template));
  pika_context_set_template (pika_get_user_context (context->pika),
                             edit_template);

  gtk_widget_destroy (dialog);
}

static void
templates_edit_callback (GtkWidget    *dialog,
                         PikaTemplate *template,
                         PikaTemplate *edit_template,
                         PikaContext  *context,
                         gpointer      user_data)
{
  pika_config_sync (G_OBJECT (edit_template),
                    G_OBJECT (template), 0);

  gtk_widget_destroy (dialog);
}

static void
templates_delete_response (GtkWidget          *dialog,
                           gint                response_id,
                           TemplateDeleteData *delete_data)
{
  if (response_id == GTK_RESPONSE_OK)
    {
      PikaObject *new_active = NULL;

      if (delete_data->template ==
          pika_context_get_template (delete_data->context))
        {
          new_active = pika_container_get_neighbor_of (delete_data->container,
                                                       PIKA_OBJECT (delete_data->template));
        }

      if (pika_container_have (delete_data->container,
                               PIKA_OBJECT (delete_data->template)))
        {
          if (new_active)
            pika_context_set_by_type (delete_data->context,
                                      pika_container_get_children_type (delete_data->container),
                                      new_active);

          pika_container_remove (delete_data->container,
                                 PIKA_OBJECT (delete_data->template));
        }
    }

  gtk_widget_destroy (dialog);
}

static void
templates_delete_data_free (TemplateDeleteData *delete_data)
{
  g_slice_free (TemplateDeleteData, delete_data);
}
