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

#include "libpikabase/pikabase.h"

#include "core/core-types.h"

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

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

#include "file-data.h"
#include "file-data-gbr.h"
#include "file-data-gex.h"
#include "file-data-gih.h"
#include "file-data-pat.h"

#include "pika-intl.h"


void
file_data_init (Pika *pika)
{
  PikaPlugInProcedure *proc;
  GFile               *file;
  PikaProcedure       *procedure;

  g_return_if_fail (PIKA_IS_PIKA (pika));

  /*  file-gbr-load  */
  file = g_file_new_for_path ("file-gbr-load");
  procedure = pika_plug_in_procedure_new (PIKA_PDB_PROC_TYPE_PLUGIN, file);
  g_object_unref (file);

  procedure->proc_type    = PIKA_PDB_PROC_TYPE_INTERNAL;
  procedure->marshal_func = file_gbr_load_invoker;

  proc = PIKA_PLUG_IN_PROCEDURE (procedure);
  proc->menu_label = g_strdup (N_("PIKA brush"));
  pika_plug_in_procedure_set_icon (proc, PIKA_ICON_TYPE_ICON_NAME,
                                   (const guint8 *) "pika-brush",
                                   strlen ("pika-brush") + 1,
                                   NULL);
  pika_plug_in_procedure_set_image_types (proc, NULL);
  pika_plug_in_procedure_set_file_proc (proc, "gbr, gbp", "",
                                        "20, string, PIKA");
  pika_plug_in_procedure_set_mime_types (proc, "image/pika-x-gbr");
  pika_plug_in_procedure_set_handles_remote (proc);

  pika_object_set_static_name (PIKA_OBJECT (procedure), "file-gbr-load");
  pika_procedure_set_static_help (procedure,
                                  "Loads PIKA brushes",
                                  "Loads PIKA brushes (1 or 4 bpp "
                                  "and old .gpb format)",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Tim Newsome, Jens Lautenbacher, "
                                         "Sven Neumann, Michael Natterer",
                                         "Tim Newsome, Jens Lautenbacher, "
                                         "Sven Neumann, Michael Natterer",
                                         "1995-2019");

  pika_procedure_add_argument (procedure,
                               pika_param_spec_enum ("run-mode",
                                                     "Dummy Param",
                                                     "Dummy parameter",
                                                     PIKA_TYPE_RUN_MODE,
                                                     PIKA_RUN_INTERACTIVE,
                                                     PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_object ("file",
                                                    "File",
                                                    "The file to load",
                                                    G_TYPE_FILE,
                                                    PIKA_PARAM_READWRITE));

  pika_procedure_add_return_value (procedure,
                                   pika_param_spec_image ("image",
                                                          "Image",
                                                          "Output image",
                                                          FALSE,
                                                          PIKA_PARAM_READWRITE));

  pika_plug_in_manager_add_procedure (pika->plug_in_manager, proc);
  g_object_unref (procedure);

  /*  file-gbr-save-internal  */
  file = g_file_new_for_path ("file-gbr-save-internal");
  procedure = pika_plug_in_procedure_new (PIKA_PDB_PROC_TYPE_PLUGIN, file);
  g_object_unref (file);

  procedure->proc_type    = PIKA_PDB_PROC_TYPE_INTERNAL;
  procedure->marshal_func = file_gbr_save_invoker;

  proc = PIKA_PLUG_IN_PROCEDURE (procedure);
  proc->menu_label = g_strdup (N_("PIKA brush"));
  pika_plug_in_procedure_set_icon (proc, PIKA_ICON_TYPE_ICON_NAME,
                                   (const guint8 *) "pika-brush",
                                   strlen ("pika-brush") + 1,
                                   NULL);

#if 0
  /* do not register as file procedure */
  pika_plug_in_procedure_set_image_types (proc, "RGB*, GRAY*, INDEXED*");
  pika_plug_in_procedure_set_file_proc (proc, "gbr", "", NULL);
  pika_plug_in_procedure_set_mime_types (proc, "image/x-pika-gbr");
  pika_plug_in_procedure_set_handles_remote (proc);
#endif

  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "file-gbr-save-internal");
  pika_procedure_set_static_help (procedure,
                                  "Exports Pika brush file (.GBR)",
                                  "Exports Pika brush file (.GBR)",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Tim Newsome, Michael Natterer",
                                         "Tim Newsome, Michael Natterer",
                                         "1995-2019");

  pika_procedure_add_argument (procedure,
                               pika_param_spec_enum ("run-mode",
                                                     "Dummy Param",
                                                     "Dummy parameter",
                                                     PIKA_TYPE_RUN_MODE,
                                                     PIKA_RUN_INTERACTIVE,
                                                     PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               pika_param_spec_image ("image",
                                                      "Image",
                                                      "Input image",
                                                      FALSE,
                                                      PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_int ("num-drawables",
                                                 "Num drawables",
                                                 "Number of drawables",
                                                 1, G_MAXINT, 1,
                                                 PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               pika_param_spec_object_array ("drawables",
                                                             "Drawables",
                                                             "Selected drawables",
                                                             PIKA_TYPE_DRAWABLE,
                                                             PIKA_PARAM_READWRITE | PIKA_PARAM_NO_VALIDATE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_object ("file",
                                                    "File",
                                                    "The file to export",
                                                    G_TYPE_FILE,
                                                    PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_int ("spacing",
                                                 "spacing",
                                                 "Spacing of the brush",
                                                 1, 1000, 10,
                                                 PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               pika_param_spec_string ("name",
                                                       "name",
                                                       "The name of the "
                                                       "brush",
                                                       FALSE, FALSE, TRUE,
                                                       "PIKA Brush",
                                                       PIKA_PARAM_READWRITE));

  pika_plug_in_manager_add_procedure (pika->plug_in_manager, proc);
  g_object_unref (procedure);

  /*  file-gih-load  */
  file = g_file_new_for_path ("file-gih-load");
  procedure = pika_plug_in_procedure_new (PIKA_PDB_PROC_TYPE_PLUGIN, file);
  g_object_unref (file);

  procedure->proc_type    = PIKA_PDB_PROC_TYPE_INTERNAL;
  procedure->marshal_func = file_gih_load_invoker;

  proc = PIKA_PLUG_IN_PROCEDURE (procedure);
  proc->menu_label = g_strdup (N_("PIKA brush (animated)"));
  pika_plug_in_procedure_set_icon (proc, PIKA_ICON_TYPE_ICON_NAME,
                                   (const guint8 *) "pika-brush",
                                   strlen ("pika-brush") + 1,
                                   NULL);
  pika_plug_in_procedure_set_image_types (proc, NULL);
  pika_plug_in_procedure_set_file_proc (proc, "gih", "", "");
  pika_plug_in_procedure_set_mime_types (proc, "image/pika-x-gih");
  pika_plug_in_procedure_set_handles_remote (proc);

  pika_object_set_static_name (PIKA_OBJECT (procedure), "file-gih-load");
  pika_procedure_set_static_help (procedure,
                                  "Loads PIKA animated brushes",
                                  "This procedure loads a PIKA brush "
                                  "pipe as an image.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Tor Lillqvist, Michael Natterer",
                                         "Tor Lillqvist, Michael Natterer",
                                         "1999-2019");

  pika_procedure_add_argument (procedure,
                               pika_param_spec_enum ("run-mode",
                                                     "Dummy Param",
                                                     "Dummy parameter",
                                                     PIKA_TYPE_RUN_MODE,
                                                     PIKA_RUN_INTERACTIVE,
                                                     PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_object ("file",
                                                    "File",
                                                    "The file to load",
                                                    G_TYPE_FILE,
                                                    PIKA_PARAM_READWRITE));

  pika_procedure_add_return_value (procedure,
                                   pika_param_spec_image ("image",
                                                          "Image",
                                                          "Output image",
                                                          FALSE,
                                                          PIKA_PARAM_READWRITE));

  pika_plug_in_manager_add_procedure (pika->plug_in_manager, proc);
  g_object_unref (procedure);

  /*  file-gih-save-internal  */
  file = g_file_new_for_path ("file-gih-save-internal");
  procedure = pika_plug_in_procedure_new (PIKA_PDB_PROC_TYPE_PLUGIN, file);
  g_object_unref (file);

  procedure->proc_type    = PIKA_PDB_PROC_TYPE_INTERNAL;
  procedure->marshal_func = file_gih_save_invoker;

  proc = PIKA_PLUG_IN_PROCEDURE (procedure);
  proc->menu_label = g_strdup (N_("PIKA brush (animated)"));
  pika_plug_in_procedure_set_icon (proc, PIKA_ICON_TYPE_ICON_NAME,
                                   (const guint8 *) "pika-brush",
                                   strlen ("pika-brush") + 1,
                                   NULL);

#if 0
  /* do not register as file procedure */
  pika_plug_in_procedure_set_image_types (proc, "RGB*, GRAY*, INDEXED*");
  pika_plug_in_procedure_set_file_proc (proc, "gih", "", NULL);
  pika_plug_in_procedure_set_mime_types (proc, "image/x-pika-gih");
  pika_plug_in_procedure_set_handles_remote (proc);
#endif

  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "file-gih-save-internal");
  pika_procedure_set_static_help (procedure,
                                  "Exports Pika animated brush file (.gih)",
                                  "Exports Pika animated brush file (.gih)",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Tor Lillqvist, Michael Natterer",
                                         "Tor Lillqvist, Michael Natterer",
                                         "1999-2019");

  pika_procedure_add_argument (procedure,
                               pika_param_spec_enum ("run-mode",
                                                     "Dummy Param",
                                                     "Dummy parameter",
                                                     PIKA_TYPE_RUN_MODE,
                                                     PIKA_RUN_INTERACTIVE,
                                                     PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               pika_param_spec_image ("image",
                                                      "Image",
                                                      "Input image",
                                                      FALSE,
                                                      PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_int ("num-drawables",
                                                 "num drawables",
                                                 "The number of drawables to save",
                                                 1, G_MAXINT32, 1,
                                                 PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               pika_param_spec_object_array ("drawables",
                                                             "drawables",
                                                             "Drawables to save",
                                                             PIKA_TYPE_DRAWABLE,
                                                             PIKA_PARAM_READWRITE | PIKA_PARAM_NO_VALIDATE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_object ("file",
                                                    "File",
                                                    "The file to export",
                                                    G_TYPE_FILE,
                                                    PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_int ("spacing",
                                                 "spacing",
                                                 "Spacing of the brush",
                                                 1, 1000, 10,
                                                 PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               pika_param_spec_string ("name",
                                                       "name",
                                                       "The name of the "
                                                       "brush",
                                                       FALSE, FALSE, TRUE,
                                                       "PIKA Brush",
                                                       PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               pika_param_spec_string ("params",
                                                       "params",
                                                       "The pipe's parameters",
                                                       FALSE, FALSE, TRUE,
                                                       NULL,
                                                       PIKA_PARAM_READWRITE));

  pika_plug_in_manager_add_procedure (pika->plug_in_manager, proc);
  g_object_unref (procedure);

  /*  file-pat-load  */
  file = g_file_new_for_path ("file-pat-load");
  procedure = pika_plug_in_procedure_new (PIKA_PDB_PROC_TYPE_PLUGIN, file);
  g_object_unref (file);

  procedure->proc_type    = PIKA_PDB_PROC_TYPE_INTERNAL;
  procedure->marshal_func = file_pat_load_invoker;

  proc = PIKA_PLUG_IN_PROCEDURE (procedure);
  proc->menu_label = g_strdup (N_("PIKA pattern"));
  pika_plug_in_procedure_set_icon (proc, PIKA_ICON_TYPE_ICON_NAME,
                                   (const guint8 *) "pika-pattern",
                                   strlen ("pika-pattern") + 1,
                                   NULL);
  pika_plug_in_procedure_set_image_types (proc, NULL);
  pika_plug_in_procedure_set_file_proc (proc, "pat", "",
                                        "20,string,GPAT");
  pika_plug_in_procedure_set_mime_types (proc, "image/pika-x-pat");
  pika_plug_in_procedure_set_handles_remote (proc);

  pika_object_set_static_name (PIKA_OBJECT (procedure), "file-pat-load");
  pika_procedure_set_static_help (procedure,
                                  "Loads PIKA patterns",
                                  "Loads PIKA patterns",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Tim Newsome, Michael Natterer",
                                         "Tim Newsome, Michael Natterer",
                                         "1997-2019");

  pika_procedure_add_argument (procedure,
                               pika_param_spec_enum ("run-mode",
                                                     "Dummy Param",
                                                     "Dummy parameter",
                                                     PIKA_TYPE_RUN_MODE,
                                                     PIKA_RUN_INTERACTIVE,
                                                     PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_object ("file",
                                                    "File",
                                                    "The file to load",
                                                    G_TYPE_FILE,
                                                    PIKA_PARAM_READWRITE));
  pika_procedure_add_return_value (procedure,
                                   pika_param_spec_image ("image",
                                                          "Image",
                                                          "Output image",
                                                          FALSE,
                                                          PIKA_PARAM_READWRITE));

  pika_plug_in_manager_add_procedure (pika->plug_in_manager, proc);
  g_object_unref (procedure);

  /*  file-pat-save-internal  */
  file = g_file_new_for_path ("file-pat-save-internal");
  procedure = pika_plug_in_procedure_new (PIKA_PDB_PROC_TYPE_PLUGIN, file);
  g_object_unref (file);

  procedure->proc_type    = PIKA_PDB_PROC_TYPE_INTERNAL;
  procedure->marshal_func = file_pat_save_invoker;

  proc = PIKA_PLUG_IN_PROCEDURE (procedure);
  proc->menu_label = g_strdup (N_("PIKA pattern"));
  pika_plug_in_procedure_set_icon (proc, PIKA_ICON_TYPE_ICON_NAME,
                                   (const guint8 *) "pika-pattern",
                                   strlen ("pika-pattern") + 1,
                                   NULL);

#if 0
  /* do not register as file procedure */
  pika_plug_in_procedure_set_image_types (proc, "RGB*, GRAY*, INDEXED*");
  pika_plug_in_procedure_set_file_proc (proc, "pat", "", NULL);
  pika_plug_in_procedure_set_mime_types (proc, "image/x-pika-pat");
  pika_plug_in_procedure_set_handles_remote (proc);
#endif

  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "file-pat-save-internal");
  pika_procedure_set_static_help (procedure,
                                  "Exports Pika pattern file (.PAT)",
                                  "Exports Pika pattern file (.PAT)",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Tim Newsome, Michael Natterer",
                                         "Tim Newsome, Michael Natterer",
                                         "1995-2019");

  pika_procedure_add_argument (procedure,
                               pika_param_spec_enum ("run-mode",
                                                     "Dummy Param",
                                                     "Dummy parameter",
                                                     PIKA_TYPE_RUN_MODE,
                                                     PIKA_RUN_INTERACTIVE,
                                                     PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               pika_param_spec_image ("image",
                                                      "Image",
                                                      "Input image",
                                                      FALSE,
                                                      PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_int ("num-drawables",
                                                 "Num drawables",
                                                 "Number of drawables",
                                                 1, G_MAXINT, 1,
                                                 PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               pika_param_spec_object_array ("drawables",
                                                             "Drawables",
                                                             "Selected drawables",
                                                             PIKA_TYPE_DRAWABLE,
                                                             PIKA_PARAM_READWRITE | PIKA_PARAM_NO_VALIDATE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_object ("file",
                                                    "File",
                                                    "The file to export",
                                                    G_TYPE_FILE,
                                                    PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               pika_param_spec_string ("name",
                                                       "name",
                                                       "The name of the "
                                                       "pattern",
                                                       FALSE, FALSE, TRUE,
                                                       "PIKA Pattern",
                                                       PIKA_PARAM_READWRITE));

  pika_plug_in_manager_add_procedure (pika->plug_in_manager, proc);
  g_object_unref (procedure);

  /*  file-gex-load  */
  file = g_file_new_for_path ("file-gex-load");
  procedure = pika_plug_in_procedure_new (PIKA_PDB_PROC_TYPE_PLUGIN, file);
  g_object_unref (file);

  procedure->proc_type    = PIKA_PDB_PROC_TYPE_INTERNAL;
  procedure->marshal_func = file_gex_load_invoker;

  proc = PIKA_PLUG_IN_PROCEDURE (procedure);
  proc->menu_label = g_strdup (N_("PIKA extension"));
  pika_plug_in_procedure_set_icon (proc, PIKA_ICON_TYPE_ICON_NAME,
                                   (const guint8 *) "pika-plugin",
                                   strlen ("pika-plugin") + 1,
                                   NULL);
  pika_plug_in_procedure_set_file_proc (proc, "gex", "",
                                        "20, string, PIKA");
  pika_plug_in_procedure_set_generic_file_proc (proc, TRUE);
  pika_plug_in_procedure_set_mime_types (proc, "image/pika-x-gex");
  pika_plug_in_procedure_set_handles_remote (proc);

  pika_object_set_static_name (PIKA_OBJECT (procedure), "file-gex-load");
  pika_procedure_set_static_help (procedure,
                                  "Loads PIKA extension",
                                  "Loads PIKA extension",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Jehan", "Jehan", "2019");

  pika_procedure_add_argument (procedure,
                               pika_param_spec_enum ("run-mode",
                                                     "Dummy Param",
                                                     "Dummy parameter",
                                                     PIKA_TYPE_RUN_MODE,
                                                     PIKA_RUN_INTERACTIVE,
                                                     PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_object ("file",
                                                    "File",
                                                    "The file to load",
                                                    G_TYPE_FILE,
                                                    PIKA_PARAM_READWRITE));

  pika_procedure_add_return_value (procedure,
                                   pika_param_spec_string ("extension-id",
                                                           "ID of installed extension",
                                                           "Identifier of the newly installed extension",
                                                           FALSE, TRUE, FALSE, NULL,
                                                           PIKA_PARAM_READWRITE));

  pika_plug_in_manager_add_procedure (pika->plug_in_manager, proc);
  g_object_unref (procedure);
}

void
file_data_exit (Pika *pika)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));
}
