/* 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
 *
 * pikaprogressdialog.c
 * Copyright (C) 2004 Michael Natterer <mitch@gimp.org>
 *
 * 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 <gegl.h>
#include <gtk/gtk.h>

#include "libpikawidgets/pikawidgets.h"

#include "widgets-types.h"

#include "core/pikaprogress.h"

#include "pikaprogressbox.h"
#include "pikaprogressdialog.h"

#include "pika-intl.h"


#define PROGRESS_DIALOG_WIDTH  400


static void    pika_progress_dialog_progress_iface_init (PikaProgressInterface *iface);

static void     pika_progress_dialog_response           (GtkDialog    *dialog,
                                                         gint          response_id);

static PikaProgress *
                pika_progress_dialog_progress_start     (PikaProgress *progress,
                                                         gboolean      cancellable,
                                                         const gchar  *message);
static void     pika_progress_dialog_progress_end       (PikaProgress *progress);
static gboolean pika_progress_dialog_progress_is_active (PikaProgress *progress);
static void     pika_progress_dialog_progress_set_text  (PikaProgress *progress,
                                                         const gchar  *message);
static void     pika_progress_dialog_progress_set_value (PikaProgress *progress,
                                                         gdouble       percentage);
static gdouble  pika_progress_dialog_progress_get_value (PikaProgress *progress);
static void     pika_progress_dialog_progress_pulse     (PikaProgress *progress);


G_DEFINE_TYPE_WITH_CODE (PikaProgressDialog, pika_progress_dialog,
                         PIKA_TYPE_DIALOG,
                         G_IMPLEMENT_INTERFACE (PIKA_TYPE_PROGRESS,
                                                pika_progress_dialog_progress_iface_init))

#define parent_class pika_progress_dialog_parent_class


static void
pika_progress_dialog_class_init (PikaProgressDialogClass *klass)
{
  GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (klass);

  dialog_class->response = pika_progress_dialog_response;
}

static void
pika_progress_dialog_init (PikaProgressDialog *dialog)
{
  GtkWidget *content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));

  dialog->box = pika_progress_box_new ();
  gtk_container_set_border_width (GTK_CONTAINER (dialog->box), 12);
  gtk_box_pack_start (GTK_BOX (content_area), dialog->box, TRUE, TRUE, 0);
  gtk_widget_show (dialog->box);

  g_signal_connect (dialog->box, "destroy",
                    G_CALLBACK (gtk_widget_destroyed),
                    &dialog->box);

  gtk_dialog_add_button (GTK_DIALOG (dialog),
                         _("_Cancel"), GTK_RESPONSE_CANCEL);
  gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CANCEL);

  gtk_widget_set_size_request (GTK_WIDGET (dialog), PROGRESS_DIALOG_WIDTH, -1);
}

static void
pika_progress_dialog_progress_iface_init (PikaProgressInterface *iface)
{
  iface->start     = pika_progress_dialog_progress_start;
  iface->end       = pika_progress_dialog_progress_end;
  iface->is_active = pika_progress_dialog_progress_is_active;
  iface->set_text  = pika_progress_dialog_progress_set_text;
  iface->set_value = pika_progress_dialog_progress_set_value;
  iface->get_value = pika_progress_dialog_progress_get_value;
  iface->pulse     = pika_progress_dialog_progress_pulse;
}

static void
pika_progress_dialog_response (GtkDialog *dialog,
                               gint       response_id)
{
  PikaProgressDialog *progress_dialog = PIKA_PROGRESS_DIALOG (dialog);

  if (PIKA_PROGRESS_BOX (progress_dialog->box)->cancellable)
    pika_progress_cancel (PIKA_PROGRESS (dialog));
}

static PikaProgress *
pika_progress_dialog_progress_start (PikaProgress *progress,
                                     gboolean      cancellable,
                                     const gchar  *message)
{
  PikaProgressDialog *dialog = PIKA_PROGRESS_DIALOG (progress);

  if (! dialog->box)
    return NULL;

  if (pika_progress_start (PIKA_PROGRESS (dialog->box), cancellable,
                           "%s", message))
    {
      gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
                                         GTK_RESPONSE_CANCEL, cancellable);

      gtk_window_present (GTK_WINDOW (dialog));

      return progress;
    }

  return NULL;
}

static void
pika_progress_dialog_progress_end (PikaProgress *progress)
{
  PikaProgressDialog *dialog = PIKA_PROGRESS_DIALOG (progress);

  if (! dialog->box)
    return;

  if (PIKA_PROGRESS_BOX (dialog->box)->active)
    {
      pika_progress_end (PIKA_PROGRESS (dialog->box));

      gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
                                         GTK_RESPONSE_CANCEL, FALSE);

      gtk_widget_hide (GTK_WIDGET (dialog));
    }
}

static gboolean
pika_progress_dialog_progress_is_active (PikaProgress *progress)
{
  PikaProgressDialog *dialog = PIKA_PROGRESS_DIALOG (progress);

  if (! dialog->box)
    return FALSE;

  return pika_progress_is_active (PIKA_PROGRESS (dialog->box));
}

static void
pika_progress_dialog_progress_set_text (PikaProgress *progress,
                                        const gchar  *message)
{
  PikaProgressDialog *dialog = PIKA_PROGRESS_DIALOG (progress);

  if (! dialog->box)
    return;

  pika_progress_set_text_literal (PIKA_PROGRESS (dialog->box), message);
}

static void
pika_progress_dialog_progress_set_value (PikaProgress *progress,
                                         gdouble       percentage)
{
  PikaProgressDialog *dialog = PIKA_PROGRESS_DIALOG (progress);

  if (! dialog->box)
    return;

  pika_progress_set_value (PIKA_PROGRESS (dialog->box), percentage);
}

static gdouble
pika_progress_dialog_progress_get_value (PikaProgress *progress)
{
  PikaProgressDialog *dialog = PIKA_PROGRESS_DIALOG (progress);

  if (! dialog->box)
    return 0.0;

  return pika_progress_get_value (PIKA_PROGRESS (dialog->box));
}

static void
pika_progress_dialog_progress_pulse (PikaProgress *progress)
{
  PikaProgressDialog *dialog = PIKA_PROGRESS_DIALOG (progress);

  if (! dialog->box)
    return;

  pika_progress_pulse (PIKA_PROGRESS (dialog->box));
}

GtkWidget *
pika_progress_dialog_new (void)
{
  gboolean use_header_bar;

  g_object_get (gtk_settings_get_default (),
                "gtk-dialogs-use-header", &use_header_bar,
                NULL);

  return g_object_new (PIKA_TYPE_PROGRESS_DIALOG,
                       "title",             _("Progress"),
                       "role",              "progress",
                       "skip-taskbar-hint", TRUE,
                       "skip-pager-hint",   TRUE,
                       "resizable",         FALSE,
                       "focus-on-map",      FALSE,
                       "window-position",   GTK_WIN_POS_CENTER,
                       "use-header-bar",    use_header_bar,
                       NULL);
}
