/* 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
 *
 * pikatemporaryprocedure.c
 *
 * 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 <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>

#include "libpikabase/pikabase.h"

#include "plug-in-types.h"

#include "core/pika.h"

#include "pikaplugin.h"
#define __YES_I_NEED_PIKA_PLUG_IN_MANAGER_CALL__
#include "pikapluginmanager-call.h"
#include "pikatemporaryprocedure.h"

#include "pika-intl.h"


static void             pika_temporary_procedure_finalize (GObject        *object);

static PikaValueArray * pika_temporary_procedure_execute  (PikaProcedure  *procedure,
                                                           Pika           *pika,
                                                           PikaContext    *context,
                                                           PikaProgress   *progress,
                                                           PikaValueArray *args,
                                                           GError        **error);
static void        pika_temporary_procedure_execute_async (PikaProcedure  *procedure,
                                                           Pika           *pika,
                                                           PikaContext    *context,
                                                           PikaProgress   *progress,
                                                           PikaValueArray *args,
                                                           PikaDisplay    *display);

static GFile     * pika_temporary_procedure_get_file      (PikaPlugInProcedure *procedure);


G_DEFINE_TYPE (PikaTemporaryProcedure, pika_temporary_procedure,
               PIKA_TYPE_PLUG_IN_PROCEDURE)

#define parent_class pika_temporary_procedure_parent_class


static void
pika_temporary_procedure_class_init (PikaTemporaryProcedureClass *klass)
{
  GObjectClass             *object_class = G_OBJECT_CLASS (klass);
  PikaProcedureClass       *proc_class   = PIKA_PROCEDURE_CLASS (klass);
  PikaPlugInProcedureClass *plug_class   = PIKA_PLUG_IN_PROCEDURE_CLASS (klass);

  object_class->finalize    = pika_temporary_procedure_finalize;

  proc_class->execute       = pika_temporary_procedure_execute;
  proc_class->execute_async = pika_temporary_procedure_execute_async;

  plug_class->get_file      = pika_temporary_procedure_get_file;
}

static void
pika_temporary_procedure_init (PikaTemporaryProcedure *proc)
{
  PIKA_PROCEDURE (proc)->proc_type = PIKA_PDB_PROC_TYPE_TEMPORARY;
}

static void
pika_temporary_procedure_finalize (GObject *object)
{
  /* PikaTemporaryProcedure *proc = PIKA_TEMPORARY_PROCEDURE (object); */

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

static PikaValueArray *
pika_temporary_procedure_execute (PikaProcedure   *procedure,
                                  Pika            *pika,
                                  PikaContext     *context,
                                  PikaProgress    *progress,
                                  PikaValueArray  *args,
                                  GError         **error)
{
  return pika_plug_in_manager_call_run_temp (pika->plug_in_manager,
                                             context, progress,
                                             PIKA_TEMPORARY_PROCEDURE (procedure),
                                             args);
}

static void
pika_temporary_procedure_execute_async (PikaProcedure  *procedure,
                                        Pika           *pika,
                                        PikaContext    *context,
                                        PikaProgress   *progress,
                                        PikaValueArray *args,
                                        PikaDisplay    *display)
{
  PikaTemporaryProcedure *temp_procedure = PIKA_TEMPORARY_PROCEDURE (procedure);
  PikaValueArray         *return_vals;

  return_vals = pika_plug_in_manager_call_run_temp (pika->plug_in_manager,
                                                    context, progress,
                                                    temp_procedure,
                                                    args);

  if (return_vals)
    {
      PikaPlugInProcedure *proc = PIKA_PLUG_IN_PROCEDURE (procedure);

      pika_plug_in_procedure_handle_return_values (proc,
                                                   pika, progress,
                                                   return_vals);
      pika_value_array_unref (return_vals);
    }
}

static GFile *
pika_temporary_procedure_get_file (PikaPlugInProcedure *procedure)
{
  return PIKA_TEMPORARY_PROCEDURE (procedure)->plug_in->file;
}


/*  public functions  */

PikaProcedure *
pika_temporary_procedure_new (PikaPlugIn *plug_in)
{
  PikaTemporaryProcedure *proc;

  g_return_val_if_fail (PIKA_IS_PLUG_IN (plug_in), NULL);

  proc = g_object_new (PIKA_TYPE_TEMPORARY_PROCEDURE, NULL);

  proc->plug_in = plug_in;

  PIKA_PLUG_IN_PROCEDURE (proc)->file = g_file_new_for_path ("none");

  return PIKA_PROCEDURE (proc);
}
