/* 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
 *
 * pikatool-progress.c
 * Copyright (C) 2011 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 <gdk/gdkkeysyms.h>

#include "tools-types.h"

#include "core/pikaprogress.h"

#include "widgets/pikawidgets-utils.h"

#include "display/pikacanvasprogress.h"

#include "display/pikadisplay.h"
#include "display/pikadisplayshell.h"
#include "display/pikadisplayshell-items.h"
#include "display/pikadisplayshell-transform.h"

#include "pikatool.h"
#include "pikatool-progress.h"


/*  local function prototypes  */

static PikaProgress * pika_tool_progress_start     (PikaProgress        *progress,
                                                    gboolean             cancelable,
                                                    const gchar         *message);
static void           pika_tool_progress_end       (PikaProgress        *progress);
static gboolean       pika_tool_progress_is_active (PikaProgress        *progress);
static void           pika_tool_progress_set_text  (PikaProgress        *progress,
                                                    const gchar         *message);
static void           pika_tool_progress_set_value (PikaProgress        *progress,
                                                    gdouble              percentage);
static gdouble        pika_tool_progress_get_value (PikaProgress        *progress);
static void           pika_tool_progress_pulse     (PikaProgress        *progress);
static gboolean       pika_tool_progress_message   (PikaProgress        *progress,
                                                    Pika                *pika,
                                                    PikaMessageSeverity  severity,
                                                    const gchar         *domain,
                                                    const gchar         *message);


/*  public functions  */

void
pika_tool_progress_iface_init (PikaProgressInterface *iface)
{
  iface->start     = pika_tool_progress_start;
  iface->end       = pika_tool_progress_end;
  iface->is_active = pika_tool_progress_is_active;
  iface->set_text  = pika_tool_progress_set_text;
  iface->set_value = pika_tool_progress_set_value;
  iface->get_value = pika_tool_progress_get_value;
  iface->pulse     = pika_tool_progress_pulse;
  iface->message   = pika_tool_progress_message;
}


/*  private functions  */

static gboolean
pika_tool_progress_button_press (GtkWidget            *widget,
                                 const GdkEventButton *bevent,
                                 PikaTool             *tool)
{
  if (tool->progress_cancelable          &&
      bevent->type   == GDK_BUTTON_PRESS &&
      bevent->button == 1)
    {
      GtkWidget        *event_widget;
      PikaDisplayShell *shell;

      event_widget = gtk_get_event_widget ((GdkEvent *) bevent);
      shell        = pika_display_get_shell (tool->progress_display);

      if (shell->canvas == event_widget)
        {
          gint x, y;

          pika_display_shell_unzoom_xy (shell, bevent->x, bevent->y,
                                        &x, &y, FALSE);

          if (pika_canvas_item_hit (tool->progress, x, y))
            {
              pika_progress_cancel (PIKA_PROGRESS (tool));
            }
        }
    }

  return TRUE;
}

static gboolean
pika_tool_progress_key_press (GtkWidget         *widget,
                              const GdkEventKey *kevent,
                              PikaTool          *tool)
{
  if (tool->progress_cancelable &&
      kevent->keyval == GDK_KEY_Escape)
    {
      pika_progress_cancel (PIKA_PROGRESS (tool));
    }

  return TRUE;
}

static PikaProgress *
pika_tool_progress_start (PikaProgress *progress,
                          gboolean      cancelable,
                          const gchar  *message)
{
  PikaTool         *tool = PIKA_TOOL (progress);
  PikaDisplayShell *shell;
  gint              x, y;

  g_return_val_if_fail (PIKA_IS_DISPLAY (tool->display), NULL);
  g_return_val_if_fail (tool->progress == NULL, NULL);

  shell = pika_display_get_shell (tool->display);

  x = shell->disp_width  / 2;
  y = shell->disp_height / 2;

  pika_display_shell_unzoom_xy (shell, x, y, &x, &y, FALSE);

  tool->progress = pika_canvas_progress_new (shell,
                                             PIKA_HANDLE_ANCHOR_CENTER,
                                             x, y);
  pika_display_shell_add_unrotated_item (shell, tool->progress);
  g_object_unref (tool->progress);

  pika_progress_start (PIKA_PROGRESS (tool->progress), FALSE,
                       "%s", message);

  tool->progress_display = tool->display;

  tool->progress_grab_widget = gtk_invisible_new ();
  gtk_widget_show (tool->progress_grab_widget);
  gtk_grab_add (tool->progress_grab_widget);

  g_signal_connect (tool->progress_grab_widget, "button-press-event",
                    G_CALLBACK (pika_tool_progress_button_press),
                    tool);
  g_signal_connect (tool->progress_grab_widget, "key-press-event",
                    G_CALLBACK (pika_tool_progress_key_press),
                    tool);

  tool->progress_cancelable = cancelable;

  return progress;
}

static void
pika_tool_progress_end (PikaProgress *progress)
{
  PikaTool *tool = PIKA_TOOL (progress);

  if (tool->progress)
    {
      PikaDisplayShell *shell = pika_display_get_shell (tool->progress_display);

      pika_progress_end (PIKA_PROGRESS (tool->progress));
      pika_display_shell_remove_unrotated_item (shell, tool->progress);

      gtk_grab_remove (tool->progress_grab_widget);
      gtk_widget_destroy (tool->progress_grab_widget);

      tool->progress             = NULL;
      tool->progress_display     = NULL;
      tool->progress_grab_widget = NULL;
      tool->progress_cancelable  = FALSE;
    }
}

static gboolean
pika_tool_progress_is_active (PikaProgress *progress)
{
  PikaTool *tool = PIKA_TOOL (progress);

  return tool->progress != NULL;
}

static void
pika_tool_progress_set_text (PikaProgress *progress,
                             const gchar  *message)
{
  PikaTool *tool = PIKA_TOOL (progress);

  if (tool->progress)
    pika_progress_set_text_literal (PIKA_PROGRESS (tool->progress), message);
}

static void
pika_tool_progress_set_value (PikaProgress *progress,
                              gdouble       percentage)
{
  PikaTool *tool = PIKA_TOOL (progress);

  if (tool->progress)
    pika_progress_set_value (PIKA_PROGRESS (tool->progress), percentage);
}

static gdouble
pika_tool_progress_get_value (PikaProgress *progress)
{
  PikaTool *tool = PIKA_TOOL (progress);

  if (tool->progress)
    return pika_progress_get_value (PIKA_PROGRESS (tool->progress));

  return 0.0;
}

static void
pika_tool_progress_pulse (PikaProgress *progress)
{
}

static gboolean
pika_tool_progress_message (PikaProgress        *progress,
                            Pika                *pika,
                            PikaMessageSeverity  severity,
                            const gchar         *domain,
                            const gchar         *message)
{
  return FALSE;
}
