/* 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 <errno.h>
#include <stdlib.h>
#include <string.h>

#include <gdk-pixbuf/gdk-pixbuf.h>
#include <glib/gstdio.h>
#include <gegl.h>

#include "libpikabase/pikabase.h"

#include "core/core-types.h"

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

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

#include "xcf.h"
#include "xcf-private.h"
#include "xcf-load.h"
#include "xcf-read.h"
#include "xcf-save.h"

#include "pika-intl.h"


typedef PikaImage * PikaXcfLoaderFunc (Pika     *pika,
                                       XcfInfo  *info,
                                       GError  **error);


static PikaValueArray * xcf_load_invoker (PikaProcedure         *procedure,
                                          Pika                  *pika,
                                          PikaContext           *context,
                                          PikaProgress          *progress,
                                          const PikaValueArray  *args,
                                          GError               **error);
static PikaValueArray * xcf_save_invoker (PikaProcedure         *procedure,
                                          Pika                  *pika,
                                          PikaContext           *context,
                                          PikaProgress          *progress,
                                          const PikaValueArray  *args,
                                          GError               **error);


static PikaXcfLoaderFunc * const xcf_loaders[] =
{
  xcf_load_image,   /* version  0 */
  xcf_load_image,   /* version  1 */
  xcf_load_image,   /* version  2 */
  xcf_load_image,   /* version  3 */
  xcf_load_image,   /* version  4 */
  xcf_load_image,   /* version  5 */
  xcf_load_image,   /* version  6 */
  xcf_load_image,   /* version  7 */
  xcf_load_image,   /* version  8 */
  xcf_load_image,   /* version  9 */
  xcf_load_image,   /* version 10 */
  xcf_load_image,   /* version 11 */
  xcf_load_image,   /* version 12 */
  xcf_load_image,   /* version 13 */
  xcf_load_image,   /* version 14 */
  xcf_load_image,   /* version 15 */
  xcf_load_image,   /* version 16 */
  xcf_load_image,   /* version 17 */
  xcf_load_image,   /* version 18 */
  xcf_load_image,   /* version 19 */
};


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

  g_return_if_fail (PIKA_IS_PIKA (pika));

  /* So this is sort of a hack, but its better than it was before.  To
   * do this right there would be a file load-save handler type and
   * the whole interface would change but there isn't, and currently
   * the plug-in structure contains all the load-save info, so it
   * makes sense to use that for the XCF load/save handlers, even
   * though they are internal.  The only thing it requires is using a
   * PlugInProcDef struct.  -josh
   */

  /*  pika-xcf-save  */
  file = g_file_new_for_path ("pika-xcf-save");
  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 = xcf_save_invoker;

  proc = PIKA_PLUG_IN_PROCEDURE (procedure);
  proc->menu_label = g_strdup (N_("PIKA XCF image"));
  pika_plug_in_procedure_set_icon (proc, PIKA_ICON_TYPE_ICON_NAME,
                                   (const guint8 *) "pika-mascot",
                                   strlen ("pika-mascot") + 1,
                                   NULL);
  pika_plug_in_procedure_set_image_types (proc, "RGB*, GRAY*, INDEXED*");
  pika_plug_in_procedure_set_file_proc (proc, "xcf", "", NULL);
  pika_plug_in_procedure_set_mime_types (proc, "image/x-xcf");
  pika_plug_in_procedure_set_handles_remote (proc);

  pika_object_set_static_name (PIKA_OBJECT (procedure), "pika-xcf-save");
  pika_procedure_set_static_help (procedure,
                                  "Saves file in the .xcf file format",
                                  "The XCF file format has been designed "
                                  "specifically for loading and saving "
                                  "tiled and layered images in PIKA. "
                                  "This procedure will save the specified "
                                  "image in the xcf file format.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Spencer Kimball & Peter Mattis",
                                         "Spencer Kimball & Peter Mattis",
                                         "1995-1996");

  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",
                                                 0, G_MAXINT, 0,
                                                 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 save the image in",
                                                    G_TYPE_FILE,
                                                    PIKA_PARAM_READWRITE));
  pika_plug_in_manager_add_procedure (pika->plug_in_manager, proc);
  g_object_unref (procedure);

  /*  pika-xcf-load  */
  file = g_file_new_for_path ("pika-xcf-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 = xcf_load_invoker;

  proc = PIKA_PLUG_IN_PROCEDURE (procedure);
  proc->menu_label = g_strdup (N_("PIKA XCF image"));
  pika_plug_in_procedure_set_icon (proc, PIKA_ICON_TYPE_ICON_NAME,
                                   (const guint8 *) "pika-mascot",
                                   strlen ("pika-mascot") + 1,
                                   NULL);
  pika_plug_in_procedure_set_image_types (proc, NULL);
  pika_plug_in_procedure_set_file_proc (proc, "xcf", "",
                                        "0,string,pika\\040xcf\\040");
  pika_plug_in_procedure_set_mime_types (proc, "image/x-xcf");
  pika_plug_in_procedure_set_handles_remote (proc);

  pika_object_set_static_name (PIKA_OBJECT (procedure), "pika-xcf-load");
  pika_procedure_set_static_help (procedure,
                                  "Loads file saved in the .xcf file format",
                                  "The XCF file format has been designed "
                                  "specifically for loading and saving "
                                  "tiled and layered images in PIKA. "
                                  "This procedure will load the specified "
                                  "file.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Spencer Kimball & Peter Mattis",
                                         "Spencer Kimball & Peter Mattis",
                                         "1995-1996");

  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);
}

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

PikaImage *
xcf_load_stream (Pika          *pika,
                 GInputStream  *input,
                 GFile         *input_file,
                 PikaProgress  *progress,
                 GError       **error)
{
  XcfInfo      info  = { 0, };
  const gchar *filename;
  PikaImage   *image = NULL;
  gchar        id[14];
  gboolean     success;

  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);
  g_return_val_if_fail (G_IS_INPUT_STREAM (input), NULL);
  g_return_val_if_fail (input_file == NULL || G_IS_FILE (input_file), NULL);
  g_return_val_if_fail (progress == NULL || PIKA_IS_PROGRESS (progress), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  if (input_file)
    filename = pika_file_get_utf8_name (input_file);
  else
    filename = _("Memory Stream");

  info.pika             = pika;
  info.input            = input;
  info.seekable         = G_SEEKABLE (input);
  info.bytes_per_offset = 4;
  info.progress         = progress;
  info.file             = input_file;
  info.compression      = COMPRESS_NONE;

  if (progress)
    pika_progress_start (progress, FALSE, _("Opening '%s'"), filename);

  success = TRUE;

  xcf_read_int8 (&info, (guint8 *) id, 14);

  if (! g_str_has_prefix (id, "gimp xcf "))
    {
      success = FALSE;
    }
  else if (strcmp (id + 9, "file") == 0)
    {
      info.file_version = 0;
    }
  else if (id[9]  == 'v' &&
           id[13] == '\0')
    {
      info.file_version = atoi (id + 10);
    }
  else
    {
      success = FALSE;
    }

  if (info.file_version >= 11)
    info.bytes_per_offset = 8;

  if (success)
    {
      if (info.file_version >= 0 &&
          info.file_version < G_N_ELEMENTS (xcf_loaders))
        {
          image = (*(xcf_loaders[info.file_version])) (pika, &info, error);

          if (! image)
            success = FALSE;

          g_input_stream_close (info.input, NULL, NULL);
        }
      else
        {
          g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                       _("XCF error: unsupported XCF file version %d "
                         "encountered"), info.file_version);
          success = FALSE;
        }
    }

  if (progress)
    pika_progress_end (progress);

  return image;
}

gboolean
xcf_save_stream (Pika           *pika,
                 PikaImage      *image,
                 GOutputStream  *output,
                 GFile          *output_file,
                 PikaProgress   *progress,
                 GError        **error)
{
  XcfInfo       info     = { 0, };
  const gchar  *filename;
  gboolean      success  = FALSE;
  GError       *my_error = NULL;
  GCancellable *cancellable;

  g_return_val_if_fail (PIKA_IS_PIKA (pika), FALSE);
  g_return_val_if_fail (PIKA_IS_IMAGE (image), FALSE);
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (output), FALSE);
  g_return_val_if_fail (output_file == NULL || G_IS_FILE (output_file), FALSE);
  g_return_val_if_fail (progress == NULL || PIKA_IS_PROGRESS (progress), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  if (output_file)
    filename = pika_file_get_utf8_name (output_file);
  else
    filename = _("Memory Stream");

  info.pika             = pika;
  info.output           = output;
  info.seekable         = G_SEEKABLE (output);
  info.bytes_per_offset = 4;
  info.progress         = progress;
  info.file             = output_file;

  if (pika_image_get_xcf_compression (image))
    info.compression = COMPRESS_ZLIB;
  else
    info.compression = COMPRESS_RLE;

  info.file_version = pika_image_get_xcf_version (image,
                                                  info.compression ==
                                                  COMPRESS_ZLIB,
                                                  NULL, NULL, NULL);

  if (info.file_version >= 11)
    info.bytes_per_offset = 8;

  if (progress)
    pika_progress_start (progress, FALSE, _("Saving '%s'"), filename);

  success = xcf_save_image (&info, image, &my_error);

  cancellable = g_cancellable_new ();
  if (success)
    {
      if (progress)
        pika_progress_set_text (progress, _("Closing '%s'"), filename);
    }
  else
    {
      /* When closing the stream, the image will be actually saved,
       * unless we properly cancel it with a GCancellable.
       * Not closing the stream is not an option either, as this will
       * happen anyway when finalizing the output.
       * So let's make sure now that we don't overwrite the XCF file
       * when an error occurred.
       */
      g_cancellable_cancel (cancellable);
    }
  success = g_output_stream_close (info.output, cancellable, &my_error);
  g_object_unref (cancellable);

  if (! success && my_error)
    g_propagate_prefixed_error (error, my_error,
                                _("Error writing '%s': "), filename);

  if (progress)
    pika_progress_end (progress);

  return success;
}


/*  private functions  */

static PikaValueArray *
xcf_load_invoker (PikaProcedure         *procedure,
                  Pika                  *pika,
                  PikaContext           *context,
                  PikaProgress          *progress,
                  const PikaValueArray  *args,
                  GError               **error)
{
  PikaValueArray *return_vals;
  PikaImage      *image = NULL;
  GFile          *file;
  GInputStream   *input;
  GError         *my_error = NULL;

  pika_set_busy (pika);

  file = g_value_get_object (pika_value_array_index (args, 1));

  input = G_INPUT_STREAM (g_file_read (file, NULL, &my_error));

  if (input)
    {
      image = xcf_load_stream (pika, input, file, progress, error);

      g_object_unref (input);
    }
  else
    {
      g_propagate_prefixed_error (error, my_error,
                                  _("Could not open '%s' for reading: "),
                                  pika_file_get_utf8_name (file));
    }

  return_vals = pika_procedure_get_return_values (procedure, image != NULL,
                                                  error ? *error : NULL);

  if (image)
    g_value_set_object (pika_value_array_index (return_vals, 1), image);

  pika_unset_busy (pika);

  return return_vals;
}

static PikaValueArray *
xcf_save_invoker (PikaProcedure         *procedure,
                  Pika                  *pika,
                  PikaContext           *context,
                  PikaProgress          *progress,
                  const PikaValueArray  *args,
                  GError               **error)
{
  PikaValueArray *return_vals;
  PikaImage      *image;
  GFile          *file;
  GOutputStream  *output;
  gboolean        success  = FALSE;
  GError         *my_error = NULL;

  pika_set_busy (pika);

  image = g_value_get_object (pika_value_array_index (args, 1));
  file  = g_value_get_object (pika_value_array_index (args, 4));

  output = G_OUTPUT_STREAM (g_file_replace (file,
                                            NULL, FALSE, G_FILE_CREATE_NONE,
                                            NULL, &my_error));

  if (output)
    {
      success = xcf_save_stream (pika, image, output, file, progress, error);

      g_object_unref (output);
    }
  else
    {
      g_propagate_prefixed_error (error, my_error,
                                  _("Error creating '%s': "),
                                  pika_file_get_utf8_name (file));
    }

  return_vals = pika_procedure_get_return_values (procedure, success,
                                                  error ? *error : NULL);

  pika_unset_busy (pika);

  return return_vals;
}
