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

#include "core/pika.h"
#include "core/pikaimage.h"
#include "core/pikadrawable.h"
#include "core/pikaparamspecs.h"
#include "core/pikaprogress.h"

#include "pdb/pikaprocedure.h"

#include "display/pikadisplay.h"

#include "procedure-commands.h"


static inline gboolean
PIKA_IS_PARAM_SPEC_RUN_MODE (GParamSpec *pspec)
{
  return (G_IS_PARAM_SPEC_ENUM (pspec) &&
          pspec->value_type == PIKA_TYPE_RUN_MODE);
}

PikaValueArray *
procedure_commands_get_run_mode_arg (PikaProcedure *procedure)
{
  PikaValueArray *args;
  gint            n_args = 0;

  args = pika_procedure_get_arguments (procedure);

  /* initialize the first argument  */
  if (pika_value_array_length (args) > n_args &&
      PIKA_IS_PARAM_SPEC_RUN_MODE (procedure->args[n_args]))
    {
      g_value_set_enum (pika_value_array_index (args, n_args),
                        PIKA_RUN_INTERACTIVE);
      n_args++;
    }

  if (n_args > 0)
    pika_value_array_truncate (args, n_args);

  return args;
}

PikaValueArray *
procedure_commands_get_data_args (PikaProcedure *procedure,
                                  PikaObject    *object)
{
  PikaValueArray *args;
  gint            n_args = 0;

  args = pika_procedure_get_arguments (procedure);

  /* initialize the first argument  */
  if (pika_value_array_length (args) > n_args &&
      PIKA_IS_PARAM_SPEC_RUN_MODE (procedure->args[n_args]))
    {
      g_value_set_enum (pika_value_array_index (args, n_args),
                        PIKA_RUN_INTERACTIVE);
      n_args++;
    }

  if (pika_value_array_length (args) > n_args &&
      G_IS_PARAM_SPEC_STRING (procedure->args[n_args]))
    {
      if (object)
        {
          g_value_set_string (pika_value_array_index (args, n_args),
                              pika_object_get_name (object));
          n_args++;
        }
      else
        {
          g_warning ("Uh-oh, no active data object for the plug-in!");
          pika_value_array_unref (args);
          return NULL;
        }
    }

  if (n_args > 0)
    pika_value_array_truncate (args, n_args);

  return args;
}

PikaValueArray *
procedure_commands_get_image_args (PikaProcedure   *procedure,
                                   PikaImage       *image)
{
  PikaValueArray *args;
  gint            n_args = 0;

  args = pika_procedure_get_arguments (procedure);

  /* initialize the first argument  */
  if (pika_value_array_length (args) > n_args &&
      PIKA_IS_PARAM_SPEC_RUN_MODE (procedure->args[n_args]))
    {
      g_value_set_enum (pika_value_array_index (args, n_args),
                        PIKA_RUN_INTERACTIVE);
      n_args++;
    }

  if (pika_value_array_length (args) > n_args &&
      PIKA_IS_PARAM_SPEC_IMAGE (procedure->args[n_args]))
    {
      if (image)
        {
          g_value_set_object (pika_value_array_index (args, n_args), image);
          n_args++;
        }
      else
        {
          g_warning ("Uh-oh, no active image for the plug-in!");
          pika_value_array_unref (args);
          return NULL;
        }
    }

  if (n_args)
    pika_value_array_truncate (args, n_args);

  return args;
}

PikaValueArray *
procedure_commands_get_items_args (PikaProcedure *procedure,
                                   PikaImage     *image,
                                   GList         *items_list)
{
  PikaValueArray *args;
  gint            n_args = 0;

  args = pika_procedure_get_arguments (procedure);

  /* initialize the first argument  */
  if (pika_value_array_length (args) > n_args &&
      PIKA_IS_PARAM_SPEC_RUN_MODE (procedure->args[n_args]))
    {
      g_value_set_enum (pika_value_array_index (args, n_args),
                        PIKA_RUN_INTERACTIVE);
      n_args++;
    }

  if (pika_value_array_length (args) > n_args &&
      PIKA_IS_PARAM_SPEC_IMAGE (procedure->args[n_args]))
    {
      if (image)
        {
          g_value_set_object (pika_value_array_index (args, n_args), image);
          n_args++;

          if (pika_value_array_length (args) > n_args &&
              PIKA_IS_PARAM_SPEC_ITEM (procedure->args[n_args]))
            {
              if (items_list)
                {
                  g_printerr ("%s: plug-in procedures expecting a single item are deprecated!\n",
                              G_STRFUNC);
                  g_value_set_object (pika_value_array_index (args, n_args),
                                      items_list->data);
                  n_args++;
                }
              else
                {
                  g_warning ("Uh-oh, no selected items for the plug-in!");
                  pika_value_array_unref (args);
                  return NULL;
                }
            }
          else if (pika_value_array_length (args) > n_args + 1        &&
                   G_IS_PARAM_SPEC_INT (procedure->args[n_args])      &&
                   PIKA_IS_PARAM_SPEC_OBJECT_ARRAY (procedure->args[n_args + 1]))
            {
              PikaItem **items   = NULL;
              gint       n_items;

              n_items = g_list_length (items_list);

              g_value_set_int (pika_value_array_index (args, n_args++),
                               n_items);

              if (items_list)
                {
                  GList *iter;
                  gint   i;

                  items = g_new (PikaItem *, n_items);
                  for (iter = items_list, i = 0; iter; iter = iter->next, i++)
                    items[i] = iter->data;
                }

              pika_value_set_object_array (pika_value_array_index (args, n_args++),
                                           PIKA_TYPE_ITEM,
                                           (GObject **) items, n_items);

              g_free (items);
            }
        }
    }

  if (n_args)
    pika_value_array_truncate (args, n_args);

  return args;
}

PikaValueArray *
procedure_commands_get_display_args (PikaProcedure *procedure,
                                     PikaDisplay   *display,
                                     PikaObject    *settings)
{
  PikaValueArray *args;
  gint            n_args = 0;

  args = pika_procedure_get_arguments (procedure);

  /* initialize the first argument  */
  if (pika_value_array_length (args) > n_args &&
      PIKA_IS_PARAM_SPEC_RUN_MODE (procedure->args[n_args]))
    {
      g_value_set_enum (pika_value_array_index (args, n_args),
                        PIKA_RUN_INTERACTIVE);
      n_args++;
    }

  if (pika_value_array_length (args) > n_args &&
      PIKA_IS_PARAM_SPEC_DISPLAY (procedure->args[n_args]))
    {
      if (display)
        {
          g_value_set_object (pika_value_array_index (args, n_args), display);
          n_args++;
        }
      else
        {
          g_warning ("Uh-oh, no active display for the plug-in!");
          pika_value_array_unref (args);
          return NULL;
        }
    }

  if (pika_value_array_length (args) > n_args &&
      PIKA_IS_PARAM_SPEC_IMAGE (procedure->args[n_args]))
    {
      PikaImage *image = display ? pika_display_get_image (display) : NULL;

      if (image)
        {
          GList *drawables_list = pika_image_get_selected_drawables (image);

          g_value_set_object (pika_value_array_index (args, n_args), image);
          n_args++;

          if (pika_value_array_length (args) > n_args &&
              PIKA_IS_PARAM_SPEC_DRAWABLE (procedure->args[n_args]))
            {
              if (drawables_list)
                {
                  g_printerr ("%s: plug-in procedures expecting a single drawable are deprecated!\n",
                              G_STRFUNC);
                  g_value_set_object (pika_value_array_index (args, n_args),
                                      drawables_list->data);
                  n_args++;
                }
              else
                {
                  g_warning ("Uh-oh, no selected drawables for the plug-in!");

                  pika_value_array_unref (args);
                  g_list_free (drawables_list);

                  return NULL;
                }
            }
          else if (pika_value_array_length (args) > n_args + 1        &&
                   G_IS_PARAM_SPEC_INT (procedure->args[n_args])      &&
                   PIKA_IS_PARAM_SPEC_OBJECT_ARRAY (procedure->args[n_args + 1]))
            {
              PikaDrawable **drawables   = NULL;
              gint           n_drawables;

              n_drawables = g_list_length (drawables_list);

              g_value_set_int (pika_value_array_index (args, n_args++),
                               n_drawables);

              if (drawables_list)
                {
                  GList *iter;
                  gint   i;

                  drawables = g_new (PikaDrawable *, n_drawables);
                  for (iter = drawables_list, i = 0; iter; iter = iter->next, i++)
                    drawables[i] = iter->data;
                }

              pika_value_set_object_array (pika_value_array_index (args, n_args++),
                                           PIKA_TYPE_DRAWABLE,
                                           (GObject **) drawables, n_drawables);

              g_free (drawables);
            }
          g_list_free (drawables_list);
        }
    }

  if (pika_value_array_length (args) > n_args &&
      g_type_is_a (G_PARAM_SPEC_VALUE_TYPE (procedure->args[n_args]),
                   PIKA_TYPE_OBJECT))
    {
      g_value_set_object (pika_value_array_index (args, n_args), settings);
      n_args++;
    }

  if (n_args)
    pika_value_array_truncate (args, n_args);

  return args;
}

gboolean
procedure_commands_run_procedure (PikaProcedure  *procedure,
                                  Pika           *pika,
                                  PikaProgress   *progress,
                                  PikaValueArray *args)
{
  PikaValueArray *return_vals;
  GError         *error = NULL;

  g_return_val_if_fail (PIKA_IS_PROCEDURE (procedure), FALSE);
  g_return_val_if_fail (PIKA_IS_PIKA (pika), FALSE);
  g_return_val_if_fail (progress == NULL || PIKA_IS_PROGRESS (progress), FALSE);
  g_return_val_if_fail (args != NULL, FALSE);

  if (pika_value_array_length (args) > 0 &&
      PIKA_IS_PARAM_SPEC_RUN_MODE (procedure->args[0]))
    g_value_set_enum (pika_value_array_index (args, 0),
                      PIKA_RUN_NONINTERACTIVE);

  return_vals = pika_procedure_execute (procedure, pika,
                                        pika_get_user_context (pika),
                                        progress, args,
                                        &error);
  pika_value_array_unref (return_vals);

  if (error)
    {
      pika_message_literal (pika,
                            G_OBJECT (progress), PIKA_MESSAGE_ERROR,
                            error->message);
      g_clear_error (&error);

      return FALSE;
    }

  return TRUE;
}

gboolean
procedure_commands_run_procedure_async (PikaProcedure  *procedure,
                                        Pika           *pika,
                                        PikaProgress   *progress,
                                        PikaRunMode     run_mode,
                                        PikaValueArray *args,
                                        PikaDisplay    *display)
{
  GError *error = NULL;

  g_return_val_if_fail (PIKA_IS_PROCEDURE (procedure), FALSE);
  g_return_val_if_fail (PIKA_IS_PIKA (pika), FALSE);
  g_return_val_if_fail (progress == NULL || PIKA_IS_PROGRESS (progress), FALSE);
  g_return_val_if_fail (display == NULL || PIKA_IS_DISPLAY (display), FALSE);
  g_return_val_if_fail (args != NULL, FALSE);

  if (pika_value_array_length (args) > 0 &&
      PIKA_IS_PARAM_SPEC_RUN_MODE (procedure->args[0]))
    g_value_set_enum (pika_value_array_index (args, 0),
                      run_mode);

  pika_procedure_execute_async (procedure, pika,
                                pika_get_user_context (pika),
                                progress, args,
                                display, &error);

  if (error)
    {
      pika_message_literal (pika,
                            G_OBJECT (progress), PIKA_MESSAGE_ERROR,
                            error->message);
      g_clear_error (&error);

      return FALSE;
    }

  return TRUE;
}
