/* 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
 *
 * file-webp - WebP file format plug-in for the PIKA
 * Copyright (C) 2015  Nathan Osman
 * Copyright (C) 2016  Ben Touchette
 *
 * 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 <libpika/pika.h>
#include <libpika/pikaui.h>

#include <webp/encode.h>

#include "file-webp.h"
#include "file-webp-dialog.h"

#include "libpika/stdplugins-intl.h"


static void
show_maxkeyframe_hints (GObject          *config,
                        const GParamSpec *pspec,
                        GtkLabel         *label)
{
  gint kmax;

  g_object_get (config,
                "keyframe-distance", &kmax,
                NULL);

  if (kmax == 0)
    {
      gtk_label_set_text (label, _("(no keyframes)"));
    }
  else if (kmax == 1)
    {
      gtk_label_set_text (label, _("(all frames are keyframes)"));
    }
  else
    {
      gtk_label_set_text (label, "");
    }
}

gboolean
save_dialog (PikaImage     *image,
             PikaProcedure *procedure,
             GObject       *config)
{
  GtkWidget     *dialog;
  GtkListStore  *store;
  gint32         nlayers;
  gboolean       animation_supported = FALSE;
  gboolean       run;

  g_free (pika_image_get_layers (image, &nlayers));

  animation_supported = nlayers > 1;

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

  /* Create the combobox containing the presets */
  store = pika_int_store_new (_("Default"), WEBP_PRESET_DEFAULT,
                              _("Picture"), WEBP_PRESET_PICTURE,
                              _("Photo"),   WEBP_PRESET_PHOTO,
                              _("Drawing"), WEBP_PRESET_DRAWING,
                              _("Icon"),    WEBP_PRESET_ICON,
                              _("Text"),    WEBP_PRESET_TEXT,
                              NULL);
  pika_procedure_dialog_get_int_combo (PIKA_PROCEDURE_DIALOG (dialog),
                                       "preset", PIKA_INT_STORE (store));

  /* Create scale for image and alpha quality */
  pika_procedure_dialog_get_widget (PIKA_PROCEDURE_DIALOG (dialog),
                                    "quality", PIKA_TYPE_SPIN_SCALE);
  pika_procedure_dialog_get_widget (PIKA_PROCEDURE_DIALOG (dialog),
                                    "alpha-quality", PIKA_TYPE_SPIN_SCALE);

  /* Create frame for quality options */
  pika_procedure_dialog_fill_box (PIKA_PROCEDURE_DIALOG (dialog),
                                  "quality-options",
                                  "quality", "alpha-quality",
                                  NULL);
  pika_procedure_dialog_fill_frame (PIKA_PROCEDURE_DIALOG (dialog),
                                    "quality-frame", "lossless", TRUE,
                                    "quality-options");

  /* Create frame for additional features like Sharp YUV */
  pika_procedure_dialog_get_label (PIKA_PROCEDURE_DIALOG (dialog),
                                   "advanced-title", _("Advanced Options"),
                                   FALSE, FALSE);

  pika_procedure_dialog_set_sensitive (PIKA_PROCEDURE_DIALOG (dialog),
                                       "use-sharp-yuv",
                                       TRUE, config, "lossless", TRUE);
  pika_procedure_dialog_fill_box (PIKA_PROCEDURE_DIALOG (dialog),
                                  "advanced-options",
                                  "use-sharp-yuv",
                                  NULL);

  pika_procedure_dialog_fill_frame (PIKA_PROCEDURE_DIALOG (dialog),
                                    "advanced-frame", "advanced-title", FALSE,
                                    "advanced-options");

  if (animation_supported)
    {
      GtkWidget      *label_kf;

      /* Hint for some special values of keyframe-distance. */
      label_kf = pika_procedure_dialog_get_label (PIKA_PROCEDURE_DIALOG (dialog),
                                                  "keyframe-hint", NULL,
                                                  FALSE, FALSE);
      gtk_label_set_xalign (GTK_LABEL (label_kf), 1.0);
      gtk_label_set_ellipsize (GTK_LABEL (label_kf), PANGO_ELLIPSIZE_END);
      pika_label_set_attributes (GTK_LABEL (label_kf),
                                 PANGO_ATTR_STYLE, PANGO_STYLE_ITALIC,
                                 -1);
      g_signal_connect (config, "notify::keyframe-distance",
                        G_CALLBACK (show_maxkeyframe_hints),
                        label_kf);
      show_maxkeyframe_hints (config, NULL, GTK_LABEL (label_kf));

      /* when minimize-size is true, keyframe-distance and hint are insensitive. */
      pika_procedure_dialog_set_sensitive (PIKA_PROCEDURE_DIALOG (dialog),
                                           "keyframe-distance",
                                           TRUE, config, "minimize-size", TRUE);
      pika_procedure_dialog_set_sensitive (PIKA_PROCEDURE_DIALOG (dialog),
                                           "keyframe-hint",
                                           TRUE, config, "minimize-size", TRUE);

      /* Create frame for animation options */
      pika_procedure_dialog_fill_box (PIKA_PROCEDURE_DIALOG (dialog),
                                      "animation-options",
                                      "animation-loop",
                                      "minimize-size",
                                      "keyframe-distance",
                                      "keyframe-hint",
                                      "default-delay",
                                      "force-delay",
                                      NULL);
      pika_procedure_dialog_fill_expander (PIKA_PROCEDURE_DIALOG (dialog),
                                           "animation-frame", "animation", FALSE,
                                           "animation-options");

      /* Fill dialog with containers*/
      pika_procedure_dialog_fill (PIKA_PROCEDURE_DIALOG (dialog),
                                  "preset", "quality-frame",
                                  "advanced-frame", "animation-frame",
                                  NULL);
    }
  else
    {
      /* Fill dialog with containers*/
      pika_procedure_dialog_fill (PIKA_PROCEDURE_DIALOG (dialog),
                                  "preset", "quality-frame", "advanced-frame",
                                  NULL);
    }

  run = pika_procedure_dialog_run (PIKA_PROCEDURE_DIALOG (dialog));

  gtk_widget_destroy (dialog);

  return run;
}
