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

#include "config.h"

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

#include "display-types.h"

#include "core/pikaprogress.h"

#include "widgets/pikawidgets-utils.h"

#include "pikadisplayshell.h"
#include "pikadisplayshell-progress.h"
#include "pikastatusbar.h"


static PikaProgress *
pika_display_shell_progress_start (PikaProgress *progress,
                                   gboolean      cancellable,
                                   const gchar  *message)
{
  PikaDisplayShell *shell     = PIKA_DISPLAY_SHELL (progress);
  PikaStatusbar    *statusbar = pika_display_shell_get_statusbar (shell);

  return pika_progress_start (PIKA_PROGRESS (statusbar), cancellable,
                              "%s", message);
}

static void
pika_display_shell_progress_end (PikaProgress *progress)
{
  PikaDisplayShell *shell     = PIKA_DISPLAY_SHELL (progress);
  PikaStatusbar    *statusbar = pika_display_shell_get_statusbar (shell);

  pika_progress_end (PIKA_PROGRESS (statusbar));
}

static gboolean
pika_display_shell_progress_is_active (PikaProgress *progress)
{
  PikaDisplayShell *shell     = PIKA_DISPLAY_SHELL (progress);
  PikaStatusbar    *statusbar = pika_display_shell_get_statusbar (shell);

  return pika_progress_is_active (PIKA_PROGRESS (statusbar));
}

static void
pika_display_shell_progress_set_text (PikaProgress *progress,
                                      const gchar  *message)
{
  PikaDisplayShell *shell     = PIKA_DISPLAY_SHELL (progress);
  PikaStatusbar    *statusbar = pika_display_shell_get_statusbar (shell);

  pika_progress_set_text_literal (PIKA_PROGRESS (statusbar), message);
}

static void
pika_display_shell_progress_set_value (PikaProgress *progress,
                                       gdouble       percentage)
{
  PikaDisplayShell *shell     = PIKA_DISPLAY_SHELL (progress);
  PikaStatusbar    *statusbar = pika_display_shell_get_statusbar (shell);

  pika_progress_set_value (PIKA_PROGRESS (statusbar), percentage);
}

static gdouble
pika_display_shell_progress_get_value (PikaProgress *progress)
{
  PikaDisplayShell *shell     = PIKA_DISPLAY_SHELL (progress);
  PikaStatusbar    *statusbar = pika_display_shell_get_statusbar (shell);

  return pika_progress_get_value (PIKA_PROGRESS (statusbar));
}

static void
pika_display_shell_progress_pulse (PikaProgress *progress)
{
  PikaDisplayShell *shell     = PIKA_DISPLAY_SHELL (progress);
  PikaStatusbar    *statusbar = pika_display_shell_get_statusbar (shell);

  pika_progress_pulse (PIKA_PROGRESS (statusbar));
}

static GBytes *
pika_display_shell_progress_get_window_id (PikaProgress *progress)
{
  PikaDisplayShell *shell = PIKA_DISPLAY_SHELL (progress);
  GBytes           *handle   = NULL;

  if (shell->window_handle)
    handle = g_bytes_ref (shell->window_handle);

  return handle;
}

static gboolean
pika_display_shell_progress_message (PikaProgress        *progress,
                                     Pika                *pika,
                                     PikaMessageSeverity  severity,
                                     const gchar         *domain,
                                     const gchar         *message)
{
  PikaDisplayShell *shell     = PIKA_DISPLAY_SHELL (progress);
  PikaStatusbar    *statusbar = pika_display_shell_get_statusbar (shell);

  switch (severity)
    {
    case PIKA_MESSAGE_ERROR:
    case PIKA_MESSAGE_BUG_WARNING:
    case PIKA_MESSAGE_BUG_CRITICAL:
      /* error messages are never handled here */
      break;

    case PIKA_MESSAGE_WARNING:
      /* warning messages go to the statusbar, if it's visible */
      if (! pika_statusbar_get_visible (statusbar))
        break;
      else
        return pika_progress_message (PIKA_PROGRESS (statusbar), pika,
                                      severity, domain, message);

    case PIKA_MESSAGE_INFO:
      /* info messages go to the statusbar;
       * if they are not handled there, they are swallowed
       */
      pika_progress_message (PIKA_PROGRESS (statusbar), pika,
                             severity, domain, message);
      return TRUE;
    }

  return FALSE;
}

void
pika_display_shell_progress_iface_init (PikaProgressInterface *iface)
{
  iface->start         = pika_display_shell_progress_start;
  iface->end           = pika_display_shell_progress_end;
  iface->is_active     = pika_display_shell_progress_is_active;
  iface->set_text      = pika_display_shell_progress_set_text;
  iface->set_value     = pika_display_shell_progress_set_value;
  iface->get_value     = pika_display_shell_progress_get_value;
  iface->pulse         = pika_display_shell_progress_pulse;
  iface->get_window_id = pika_display_shell_progress_get_window_id;
  iface->message       = pika_display_shell_progress_message;
}
