/* 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, 1996, 1997 Spencer Kimball and Peter Mattis
 * Copyright (C) 1997 Josh MacDonald
 *
 * 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 <string.h>

#include <gegl.h>
#include <gtk/gtk.h>

#include "libpikabase/pikabase.h"
#include "libpikawidgets/pikawidgets.h"

#include "dialogs-types.h"

#include "core/pika.h"
#include "core/pikaimage.h"
#include "core/pikaimage-undo.h"
#include "core/pikalayer.h"
#include "core/pikaprogress.h"

#include "file/file-open.h"
#include "file/pika-file.h"

#include "widgets/pikafiledialog.h"
#include "widgets/pikahelp-ids.h"
#include "widgets/pikaopendialog.h"
#include "widgets/pikawidgets-utils.h"

#include "file-open-dialog.h"

#include "pika-intl.h"


/*  local function prototypes  */

static void       file_open_dialog_response    (GtkWidget           *dialog,
                                                gint                 response_id,
                                                Pika                *pika);
static PikaImage *file_open_dialog_open_image  (GtkWidget           *dialog,
                                                Pika                *pika,
                                                GFile               *file,
                                                PikaPlugInProcedure *load_proc);
static gboolean   file_open_dialog_open_layers (GtkWidget           *dialog,
                                                PikaImage           *image,
                                                GFile               *file,
                                                PikaPlugInProcedure *load_proc);


/*  public functions  */

GtkWidget *
file_open_dialog_new (Pika *pika)
{
  GtkWidget           *dialog;

  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

  dialog = pika_open_dialog_new (pika);

  gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (dialog), TRUE);

  pika_file_dialog_load_state (PIKA_FILE_DIALOG (dialog),
                               "pika-file-open-dialog-state");

  g_signal_connect (dialog, "response",
                    G_CALLBACK (file_open_dialog_response),
                    pika);

  return dialog;
}


/*  private functions  */

static void
file_open_dialog_response (GtkWidget *dialog,
                           gint       response_id,
                           Pika      *pika)
{
  PikaFileDialog *file_dialog = PIKA_FILE_DIALOG (dialog);
  PikaOpenDialog *open_dialog = PIKA_OPEN_DIALOG (dialog);
  GSList         *files;
  GSList         *list;
  gboolean        success = FALSE;

  pika_file_dialog_save_state (PIKA_FILE_DIALOG (dialog),
                               "pika-file-open-dialog-state");

  if (response_id != GTK_RESPONSE_OK)
    {
      if (! file_dialog->busy && response_id != GTK_RESPONSE_HELP)
        gtk_widget_destroy (dialog);

      return;
    }

  files = gtk_file_chooser_get_files (GTK_FILE_CHOOSER (dialog));

  if (files)
    g_object_set_data_full (G_OBJECT (pika), PIKA_FILE_OPEN_LAST_FILE_KEY,
                            g_object_ref (files->data),
                            (GDestroyNotify) g_object_unref);

  pika_file_dialog_set_sensitive (file_dialog, FALSE);

  /* When we are going to open new image windows, unset the transient
   * window. We don't need it since we will use gdk_window_raise() to
   * keep the dialog on top. And if we don't do it, then the dialog
   * will pull the image window it was invoked from on top of all the
   * new opened image windows, and we don't want that to happen.
   */
  if (! open_dialog->open_as_layers)
    gtk_window_set_transient_for (GTK_WINDOW (dialog), NULL);

  if (file_dialog->image)
    g_object_ref (file_dialog->image);

  for (list = files; list; list = g_slist_next (list))
    {
      GFile *file = list->data;

      if (open_dialog->open_as_layers)
        {
          if (! file_dialog->image)
            {
              pika_open_dialog_set_image (
                open_dialog,
                file_open_dialog_open_image (dialog,
                                             pika,
                                             file,
                                             file_dialog->file_proc),
                TRUE);

              if (file_dialog->image)
                {
                  g_object_ref (file_dialog->image);
                  success = TRUE;
                }
            }
          else if (file_open_dialog_open_layers (dialog,
                                                 file_dialog->image,
                                                 file,
                                                 file_dialog->file_proc))
            {
              success = TRUE;
            }
        }
      else
        {
          if (file_open_dialog_open_image (dialog,
                                           pika,
                                           file,
                                           file_dialog->file_proc))
            {
              success = TRUE;

              /* Make the dialog stay on top of all images we open if
               * we open say 10 at once
               */
              gdk_window_raise (gtk_widget_get_window (dialog));
            }
        }

      if (file_dialog->canceled)
        break;
    }

  if (success)
    {
      if (file_dialog->image)
        {
          if (open_dialog->open_as_layers)
            pika_image_flush (file_dialog->image);

          g_object_unref (file_dialog->image);
        }

      gtk_widget_destroy (dialog);
    }
  else
    {
      if (file_dialog->image)
        g_object_unref (file_dialog->image);

      pika_file_dialog_set_sensitive (file_dialog, TRUE);
    }

  g_slist_free_full (files, (GDestroyNotify) g_object_unref);
}

static PikaImage *
file_open_dialog_open_image (GtkWidget           *dialog,
                             Pika                *pika,
                             GFile               *file,
                             PikaPlugInProcedure *load_proc)
{
  PikaImage         *image;
  PikaPDBStatusType  status;
  GError            *error = NULL;

  image = file_open_with_proc_and_display (pika,
                                           pika_get_user_context (pika),
                                           PIKA_PROGRESS (dialog),
                                           file, FALSE,
                                           load_proc,
                                           G_OBJECT (pika_widget_get_monitor (dialog)),
                                           &status, &error);

  if (! image && status != PIKA_PDB_SUCCESS && status != PIKA_PDB_CANCEL)
    {
      pika_message (pika, G_OBJECT (dialog), PIKA_MESSAGE_ERROR,
                    _("Opening '%s' failed:\n\n%s"),
                    pika_file_get_utf8_name (file), error->message);
      g_clear_error (&error);
    }

  return image;
}

static gboolean
file_open_dialog_open_layers (GtkWidget           *dialog,
                              PikaImage           *image,
                              GFile               *file,
                              PikaPlugInProcedure *load_proc)
{
  GList             *new_layers;
  PikaPDBStatusType  status;
  GError            *error = NULL;

  new_layers = file_open_layers (image->pika,
                                 pika_get_user_context (image->pika),
                                 PIKA_PROGRESS (dialog),
                                 image, FALSE,
                                 file, PIKA_RUN_INTERACTIVE, load_proc,
                                 &status, &error);

  if (new_layers)
    {
      pika_image_add_layers (image, new_layers,
                             PIKA_IMAGE_ACTIVE_PARENT, -1,
                             0, 0,
                             pika_image_get_width (image),
                             pika_image_get_height (image),
                             _("Open layers"));

      g_list_free (new_layers);

      return TRUE;
    }
  else if (status != PIKA_PDB_CANCEL)
    {
      pika_message (image->pika, G_OBJECT (dialog), PIKA_MESSAGE_ERROR,
                    _("Opening '%s' failed:\n\n%s"),
                    pika_file_get_utf8_name (file), error->message);
      g_clear_error (&error);
    }

  return FALSE;
}
