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

#include "actions-types.h"

#include "core/pika.h"
#include "core/pika-filter-history.h"
#include "core/pikacontainer.h"
#include "core/pikacontext.h"
#include "core/pikaimage.h"
#include "core/pikaitem.h"
#include "core/pikaparamspecs.h"
#include "core/pikaprogress.h"

#include "pdb/pikaprocedure.h"

#include "plug-in/pikapluginmanager.h"
#include "plug-in/pikapluginmanager-data.h"

#include "widgets/pikabufferview.h"
#include "widgets/pikacontainerview.h"
#include "widgets/pikadatafactoryview.h"
#include "widgets/pikahelp-ids.h"
#include "widgets/pikaimageeditor.h"
#include "widgets/pikaitemtreeview.h"
#include "widgets/pikamessagebox.h"
#include "widgets/pikamessagedialog.h"

#include "dialogs/dialogs.h"

#include "actions.h"
#include "plug-in-commands.h"
#include "procedure-commands.h"

#include "pika-intl.h"


/*  local function prototypes  */

static void   plug_in_reset_all_response (GtkWidget *dialog,
                                          gint       response_id,
                                          Pika      *pika);


/*  public functions  */

void
plug_in_run_cmd_callback (PikaAction *action,
                          GVariant   *value,
                          gpointer    data)
{
  Pika           *pika;
  PikaValueArray *args    = NULL;
  PikaDisplay    *display = NULL;
  PikaProcedure  *procedure;
  gsize           hack;
  return_if_no_pika (pika, data);

  hack = g_variant_get_uint64 (value);

  procedure = GSIZE_TO_POINTER (hack);

  switch (procedure->proc_type)
    {
    case PIKA_PDB_PROC_TYPE_EXTENSION:
      args = procedure_commands_get_run_mode_arg (procedure);
      break;

    case PIKA_PDB_PROC_TYPE_PLUGIN:
    case PIKA_PDB_PROC_TYPE_TEMPORARY:
      if (PIKA_IS_DATA_FACTORY_VIEW (data) ||
          PIKA_IS_BUFFER_VIEW (data))
        {
          PikaContainerEditor *editor = PIKA_CONTAINER_EDITOR (data);
          PikaContainer       *container;
          PikaContext         *context;
          PikaObject          *object;

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

          object = pika_context_get_by_type (context,
                                             pika_container_get_children_type (container));

          args = procedure_commands_get_data_args (procedure, object);
        }
      else if (PIKA_IS_IMAGE_EDITOR (data))
        {
          PikaImageEditor *editor = PIKA_IMAGE_EDITOR (data);
          PikaImage       *image;

          image = pika_image_editor_get_image (editor);

          args = procedure_commands_get_image_args (procedure, image);
        }
      else if (PIKA_IS_ITEM_TREE_VIEW (data))
        {
          PikaItemTreeView *view = PIKA_ITEM_TREE_VIEW (data);
          PikaImage        *image;
          GList            *items;

          image = pika_item_tree_view_get_image (view);

          if (image)
            items = PIKA_ITEM_TREE_VIEW_GET_CLASS (view)->get_selected_items (image);
          else
            items = NULL;

          args = procedure_commands_get_items_args (procedure, image, items);
        }
      else
        {
          display = action_data_get_display (data);

          args = procedure_commands_get_display_args (procedure, display, NULL);
        }
      break;

    case PIKA_PDB_PROC_TYPE_INTERNAL:
      g_warning ("Unhandled procedure type.");
      break;
    }

  if (args)
    {
      if (procedure_commands_run_procedure_async (procedure, pika,
                                                  PIKA_PROGRESS (display),
                                                  PIKA_RUN_INTERACTIVE, args,
                                                  display))
        {
          /* remember only image plug-ins */
          if (procedure->num_args >= 2 &&
              PIKA_IS_PARAM_SPEC_IMAGE (procedure->args[1]))
            {
              pika_filter_history_add (pika, procedure);
            }
        }

      pika_value_array_unref (args);
    }
}

void
plug_in_reset_all_cmd_callback (PikaAction *action,
                                GVariant   *value,
                                gpointer    data)
{
  Pika      *pika;
  GtkWidget *dialog;
  return_if_no_pika (pika, data);

#define RESET_FILTERS_DIALOG_KEY "pika-reset-all-filters-dialog"

  dialog = dialogs_get_dialog (G_OBJECT (pika), RESET_FILTERS_DIALOG_KEY);

  if (! dialog)
    {
      dialog = pika_message_dialog_new (_("Reset all Filters"),
                                        PIKA_ICON_DIALOG_QUESTION,
                                        NULL, 0,
                                        pika_standard_help_func, NULL,

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

                                        NULL);

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

      g_signal_connect (dialog, "response",
                        G_CALLBACK (plug_in_reset_all_response),
                        pika);

      pika_message_box_set_primary_text (PIKA_MESSAGE_DIALOG (dialog)->box,
                                         _("Do you really want to reset all "
                                           "filters to default values?"));

      dialogs_attach_dialog (G_OBJECT (pika), RESET_FILTERS_DIALOG_KEY, dialog);
    }

  gtk_window_present (GTK_WINDOW (dialog));
}


/*  private functions  */

static void
plug_in_reset_all_response (GtkWidget *dialog,
                            gint       response_id,
                            Pika      *pika)
{
  gtk_widget_destroy (dialog);

  if (response_id == GTK_RESPONSE_OK)
    pika_plug_in_manager_data_free (pika->plug_in_manager);
}
