/* 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/>.
 */

/*
 * gbr plug-in version 1.00
 * Loads/exports version 2 PIKA .gbr files, by Tim Newsome <drz@frody.bloke.com>
 * Some bits stolen from the .99.7 source tree.
 *
 * Added in GBR version 1 support after learning that there wasn't a
 * tool to read them.
 * July 6, 1998 by Seth Burgess <sjburges@gimp.org>
 *
 * Dec 17, 2000
 * Load and save PIKA brushes in GRAY or RGBA.  jtl + neo
 *
 *
 * TODO: Give some better error reporting on not opening files/bad headers
 *       etc.
 */

#include "config.h"

#include <libpika/pika.h>
#include <libpika/pikaui.h>

#include "libpika/stdplugins-intl.h"


#define SAVE_PROC      "file-gbr-save"
#define PLUG_IN_BINARY "file-gbr"


typedef struct _Gbr      Gbr;
typedef struct _GbrClass GbrClass;

struct _Gbr
{
  PikaPlugIn      parent_instance;
};

struct _GbrClass
{
  PikaPlugInClass parent_class;
};


#define GBR_TYPE  (gbr_get_type ())
#define GBR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GBR_TYPE, Gbr))

GType                   gbr_get_type         (void) G_GNUC_CONST;

static GList          * gbr_query_procedures (PikaPlugIn           *plug_in);
static PikaProcedure  * gbr_create_procedure (PikaPlugIn           *plug_in,
                                              const gchar          *name);

static PikaValueArray * gbr_save             (PikaProcedure        *procedure,
                                              PikaRunMode           run_mode,
                                              PikaImage            *image,
                                              gint                  n_drawables,
                                              PikaDrawable        **drawables,
                                              GFile                *file,
                                              PikaMetadata         *metadata,
                                              PikaProcedureConfig  *config,
                                              gpointer              run_data);

static gboolean         save_dialog          (PikaProcedure        *procedure,
                                              GObject              *config,
                                              PikaImage            *image);


G_DEFINE_TYPE (Gbr, gbr, PIKA_TYPE_PLUG_IN)

PIKA_MAIN (GBR_TYPE)
DEFINE_STD_SET_I18N


static void
gbr_class_init (GbrClass *klass)
{
  PikaPlugInClass *plug_in_class = PIKA_PLUG_IN_CLASS (klass);

  plug_in_class->query_procedures = gbr_query_procedures;
  plug_in_class->create_procedure = gbr_create_procedure;
  plug_in_class->set_i18n         = STD_SET_I18N;
}

static void
gbr_init (Gbr *gbr)
{
}

static GList *
gbr_query_procedures (PikaPlugIn *plug_in)
{
  return g_list_append (NULL, g_strdup (SAVE_PROC));
}

static PikaProcedure *
gbr_create_procedure (PikaPlugIn  *plug_in,
                      const gchar *name)
{
  PikaProcedure *procedure = NULL;

  if (! strcmp (name, SAVE_PROC))
    {
      procedure = pika_save_procedure_new (plug_in, name,
                                           PIKA_PDB_PROC_TYPE_PLUGIN,
                                           FALSE, gbr_save, NULL, NULL);

      pika_procedure_set_image_types (procedure, "*");

      pika_procedure_set_menu_label (procedure, _("PIKA brush"));
      pika_file_procedure_set_format_name (PIKA_FILE_PROCEDURE (procedure),
                                           _("Brush"));
      pika_procedure_set_icon_name (procedure, PIKA_ICON_BRUSH);

      pika_procedure_set_documentation (procedure,
                                        _("Exports files in the PIKA brush "
                                          "file format"),
                                        _("Exports files in the PIKA brush "
                                          "file format"),
                                        SAVE_PROC);
      pika_procedure_set_attribution (procedure,
                                      "Tim Newsome, Jens Lautenbacher, "
                                      "Sven Neumann",
                                      "Tim Newsome, Jens Lautenbacher, "
                                      "Sven Neumann",
                                      "1997-2000");

      pika_file_procedure_set_mime_types (PIKA_FILE_PROCEDURE (procedure),
                                          "image/x-pika-gbr");
      pika_file_procedure_set_extensions (PIKA_FILE_PROCEDURE (procedure),
                                          "gbr");
      pika_file_procedure_set_handles_remote (PIKA_FILE_PROCEDURE (procedure),
                                              TRUE);

      PIKA_PROC_ARG_INT (procedure, "spacing",
                         _("Sp_acing"),
                         _("Spacing of the brush"),
                         1, 1000, 10,
                         PIKA_PARAM_READWRITE);

      PIKA_PROC_ARG_STRING (procedure, "description",
                            _("_Description"),
                            _("Short description of the brush"),
                            _("PIKA Brush"),
                            PIKA_PARAM_READWRITE);
    }

  return procedure;
}

static PikaValueArray *
gbr_save (PikaProcedure        *procedure,
          PikaRunMode           run_mode,
          PikaImage            *image,
          gint                  n_drawables,
          PikaDrawable        **drawables,
          GFile                *file,
          PikaMetadata         *metadata,
          PikaProcedureConfig  *config,
          gpointer              run_data)
{
  PikaPDBStatusType  status = PIKA_PDB_SUCCESS;
  PikaExportReturn   export = PIKA_EXPORT_CANCEL;
  gchar             *description;
  GError            *error  = NULL;

  g_object_get (config,
                "description", &description,
                NULL);

  if (! description || ! strlen (description))
    {
      gchar *name = g_path_get_basename (pika_file_get_utf8_name (file));

      if (g_str_has_suffix (name, ".gbr"))
        name[strlen (name) - 4] = '\0';

      if (strlen (name))
        g_object_set (config,
                      "description", name,
                      NULL);

      g_free (name);
    }

  g_free (description);

  switch (run_mode)
    {
    case PIKA_RUN_INTERACTIVE:
    case PIKA_RUN_WITH_LAST_VALS:
      pika_ui_init (PLUG_IN_BINARY);

      export = pika_export_image (&image, &n_drawables, &drawables, "GBR",
                                  PIKA_EXPORT_CAN_HANDLE_GRAY    |
                                  PIKA_EXPORT_CAN_HANDLE_RGB     |
                                  PIKA_EXPORT_CAN_HANDLE_INDEXED |
                                  PIKA_EXPORT_CAN_HANDLE_ALPHA);

      if (export == PIKA_EXPORT_CANCEL)
        return pika_procedure_new_return_values (procedure,
                                                 PIKA_PDB_CANCEL,
                                                 NULL);
      break;

    default:
      break;
    }

  if (run_mode == PIKA_RUN_INTERACTIVE)
    {
      if (! save_dialog (procedure, G_OBJECT (config), image))
        status = PIKA_PDB_CANCEL;
    }

  if (status == PIKA_PDB_SUCCESS)
    {
      PikaProcedure   *procedure;
      PikaValueArray  *save_retvals;
      PikaObjectArray *drawables_array;
      gint             spacing;

      g_object_get (config,
                    "description", &description,
                    "spacing",     &spacing,
                    NULL);

      drawables_array = pika_object_array_new (PIKA_TYPE_DRAWABLE, (GObject **) drawables,
                                               n_drawables, FALSE);
      procedure    = pika_pdb_lookup_procedure (pika_get_pdb (),
                                                "file-gbr-save-internal");
      save_retvals = pika_procedure_run (procedure,
                                         "image",         image,
                                         "num-drawables", n_drawables,
                                         "drawables",     drawables_array,
                                         "file",          file,
                                         "spacing",       spacing,
                                         "name",          description,
                                         NULL);

      pika_object_array_free (drawables_array);
      g_free (description);

      if (PIKA_VALUES_GET_ENUM (save_retvals, 0) != PIKA_PDB_SUCCESS)
        {
          g_set_error (&error, 0, 0,
                       "Running procedure 'file-gbr-save-internal' "
                       "failed: %s",
                       pika_pdb_get_last_error (pika_get_pdb ()));

          status = PIKA_PDB_EXECUTION_ERROR;
        }

      pika_value_array_unref (save_retvals);
    }

  if (export == PIKA_EXPORT_EXPORT)
    {
      pika_image_delete (image);
      g_free (drawables);
    }

  return pika_procedure_new_return_values (procedure, status, error);
}

static gboolean
save_dialog (PikaProcedure *procedure,
             GObject       *config,
             PikaImage     *image)
{
  GtkWidget *dialog;
  GtkWidget *vbox;
  GtkWidget *entry;
  GtkWidget *real_entry;
  gboolean   run;

  dialog = pika_save_procedure_dialog_new (PIKA_SAVE_PROCEDURE (procedure),
                                           PIKA_PROCEDURE_CONFIG (config),
                                           image);

  entry = pika_procedure_dialog_get_widget (PIKA_PROCEDURE_DIALOG (dialog),
                                            "description", PIKA_TYPE_LABEL_ENTRY);
  real_entry = pika_label_entry_get_entry (PIKA_LABEL_ENTRY (entry));
  gtk_entry_set_max_length (GTK_ENTRY (real_entry), 256);
  gtk_entry_set_width_chars (GTK_ENTRY (real_entry), 20);
  gtk_entry_set_activates_default (GTK_ENTRY (real_entry), TRUE);

  pika_procedure_dialog_get_scale_entry (PIKA_PROCEDURE_DIALOG (dialog),
                                         "spacing", 1.0);

  vbox = pika_procedure_dialog_fill_box (PIKA_PROCEDURE_DIALOG (dialog),
                                         "gbr-vbox", "description", "spacing",
                                         NULL);
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);

  pika_procedure_dialog_fill (PIKA_PROCEDURE_DIALOG (dialog),
                              "gbr-vbox", NULL);
  gtk_widget_show (dialog);

  run = pika_procedure_dialog_run (PIKA_PROCEDURE_DIALOG (dialog));

  gtk_widget_destroy (dialog);

  return run;
}
