/* 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-2002 Spencer Kimball, Peter Mattis, and others
 *
 * 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 <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>

#include "libpikabase/pikabase.h"

#include "core-types.h"

#include "pika.h"
#include "pika-gui.h"
#include "pikacontainer.h"
#include "pikacontext.h"
#include "pikadisplay.h"
#include "pikadrawable.h"
#include "pikaimage.h"
#include "pikaprogress.h"
#include "pikaresource.h"
#include "pikawaitable.h"

#include "about.h"

#include "pika-intl.h"


void
pika_gui_init (Pika *pika)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));

  pika->gui.ungrab                 = NULL;
  pika->gui.set_busy               = NULL;
  pika->gui.unset_busy             = NULL;
  pika->gui.show_message           = NULL;
  pika->gui.help                   = NULL;
  pika->gui.get_program_class      = NULL;
  pika->gui.get_display_name       = NULL;
  pika->gui.get_user_time          = NULL;
  pika->gui.get_theme_dir          = NULL;
  pika->gui.get_icon_theme_dir     = NULL;
  pika->gui.display_get_window_id  = NULL;
  pika->gui.display_create         = NULL;
  pika->gui.display_delete         = NULL;
  pika->gui.displays_reconnect     = NULL;
  pika->gui.progress_new           = NULL;
  pika->gui.progress_free          = NULL;
  pika->gui.pdb_dialog_set         = NULL;
  pika->gui.pdb_dialog_close       = NULL;
  pika->gui.recent_list_add_file   = NULL;
  pika->gui.recent_list_load       = NULL;
  pika->gui.get_mount_operation    = NULL;
  pika->gui.query_profile_policy   = NULL;
  pika->gui.query_rotation_policy  = NULL;
}

void
pika_gui_ungrab (Pika *pika)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));

  if (pika->gui.ungrab)
    pika->gui.ungrab (pika);
}

void
pika_set_busy (Pika *pika)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));

  /* FIXME: pika_busy HACK */
  pika->busy++;

  if (pika->busy == 1)
    {
      if (pika->gui.set_busy)
        pika->gui.set_busy (pika);
    }
}

static gboolean
pika_idle_unset_busy (gpointer data)
{
  Pika *pika = data;

  pika_unset_busy (pika);

  pika->busy_idle_id = 0;

  return FALSE;
}

void
pika_set_busy_until_idle (Pika *pika)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));

  if (! pika->busy_idle_id)
    {
      pika_set_busy (pika);

      pika->busy_idle_id = g_idle_add_full (G_PRIORITY_HIGH,
                                            pika_idle_unset_busy, pika,
                                            NULL);
    }
}

void
pika_unset_busy (Pika *pika)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));
  g_return_if_fail (pika->busy > 0);

  /* FIXME: pika_busy HACK */
  pika->busy--;

  if (pika->busy == 0)
    {
      if (pika->gui.unset_busy)
        pika->gui.unset_busy (pika);
    }
}

void
pika_show_message (Pika                *pika,
                   GObject             *handler,
                   PikaMessageSeverity  severity,
                   const gchar         *domain,
                   const gchar         *message)
{
  const gchar *desc = (severity == PIKA_MESSAGE_ERROR) ? "Error" : "Message";

  g_return_if_fail (PIKA_IS_PIKA (pika));
  g_return_if_fail (handler == NULL || G_IS_OBJECT (handler));
  g_return_if_fail (message != NULL);

  if (! domain)
    domain = PIKA_ACRONYM;

  if (! pika->console_messages)
    {
      if (pika->gui.show_message)
        {
          pika->gui.show_message (pika, handler, severity,
                                  domain, message);
          return;
        }
      else if (PIKA_IS_PROGRESS (handler) &&
               pika_progress_message (PIKA_PROGRESS (handler), pika,
                                      severity, domain, message))
        {
          /* message has been handled by PikaProgress */
          return;
        }
    }

  pika_enum_get_value (PIKA_TYPE_MESSAGE_SEVERITY, severity,
                       NULL, NULL, &desc, NULL);
  g_printerr ("%s-%s: %s\n\n", domain, desc, message);
}

void
pika_wait (Pika         *pika,
           PikaWaitable *waitable,
           const gchar  *format,
           ...)
{
  va_list  args;
  gchar   *message;

  g_return_if_fail (PIKA_IS_PIKA (pika));
  g_return_if_fail (PIKA_IS_WAITABLE (waitable));
  g_return_if_fail (format != NULL);

  if (pika_waitable_wait_for (waitable, 0.5 * G_TIME_SPAN_SECOND))
    return;

  va_start (args, format);

  message = g_strdup_vprintf (format, args);

  va_end (args);

  if (! pika->console_messages &&
      pika->gui.wait           &&
      pika->gui.wait (pika, waitable, message))
    {
      return;
    }

  /* Translator:  This message is displayed while PIKA is waiting for
   * some operation to finish.  The %s argument is a message describing
   * the operation.
   */
  g_printerr (_("Please wait: %s\n"), message);

  pika_waitable_wait (waitable);

  g_free (message);
}

void
pika_help (Pika         *pika,
           PikaProgress *progress,
           const gchar  *help_domain,
           const gchar  *help_id)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));
  g_return_if_fail (progress == NULL || PIKA_IS_PROGRESS (progress));

  if (pika->gui.help)
    pika->gui.help (pika, progress, help_domain, help_id);
}

const gchar *
pika_get_program_class (Pika *pika)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

  if (pika->gui.get_program_class)
    return pika->gui.get_program_class (pika);

  return NULL;
}

gchar *
pika_get_display_name (Pika     *pika,
                       gint      display_id,
                       GObject **monitor,
                       gint     *monitor_number)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);
  g_return_val_if_fail (monitor != NULL, NULL);
  g_return_val_if_fail (monitor_number != NULL, NULL);

  if (pika->gui.get_display_name)
    return pika->gui.get_display_name (pika, display_id,
                                       monitor, monitor_number);

  *monitor = NULL;

  return NULL;
}

/**
 * pika_get_user_time:
 * @pika:
 *
 * Returns the timestamp of the last user interaction. The timestamp is
 * taken from events caused by user interaction such as key presses or
 * pointer movements. See gdk_x11_display_get_user_time().
 *
 * Returns: the timestamp of the last user interaction
 */
guint32
pika_get_user_time (Pika *pika)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), 0);

  if (pika->gui.get_user_time)
    return pika->gui.get_user_time (pika);

  return 0;
}

GFile *
pika_get_theme_dir (Pika *pika)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

  if (pika->gui.get_theme_dir)
    return pika->gui.get_theme_dir (pika);

  return NULL;
}

GFile *
pika_get_icon_theme_dir (Pika *pika)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

  if (pika->gui.get_icon_theme_dir)
    return pika->gui.get_icon_theme_dir (pika);

  return NULL;
}

PikaObject *
pika_get_window_strategy (Pika *pika)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

  if (pika->gui.get_window_strategy)
    return pika->gui.get_window_strategy (pika);

  return NULL;
}

PikaDisplay *
pika_get_empty_display (Pika *pika)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

  if (pika->gui.get_empty_display)
    return pika->gui.get_empty_display (pika);

  return NULL;
}

GBytes *
pika_get_display_window_id (Pika        *pika,
                            PikaDisplay *display)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);
  g_return_val_if_fail (PIKA_IS_DISPLAY (display), NULL);

  if (pika->gui.display_get_window_id)
    return pika->gui.display_get_window_id (display);

  return NULL;
}

PikaDisplay *
pika_create_display (Pika      *pika,
                     PikaImage *image,
                     PikaUnit   unit,
                     gdouble    scale,
                     GObject   *monitor)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);
  g_return_val_if_fail (image == NULL || PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (monitor == NULL || G_IS_OBJECT (monitor), NULL);

  if (pika->gui.display_create)
    return pika->gui.display_create (pika, image, unit, scale, monitor);

  return NULL;
}

void
pika_delete_display (Pika        *pika,
                     PikaDisplay *display)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));
  g_return_if_fail (PIKA_IS_DISPLAY (display));

  if (pika->gui.display_delete)
    pika->gui.display_delete (display);
}

void
pika_reconnect_displays (Pika      *pika,
                         PikaImage *old_image,
                         PikaImage *new_image)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));
  g_return_if_fail (PIKA_IS_IMAGE (old_image));
  g_return_if_fail (PIKA_IS_IMAGE (new_image));

  if (pika->gui.displays_reconnect)
    pika->gui.displays_reconnect (pika, old_image, new_image);
}

PikaProgress *
pika_new_progress (Pika        *pika,
                   PikaDisplay *display)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);
  g_return_val_if_fail (display == NULL || PIKA_IS_DISPLAY (display), NULL);

  if (pika->gui.progress_new)
    return pika->gui.progress_new (pika, display);

  return NULL;
}

void
pika_free_progress (Pika         *pika,
                    PikaProgress *progress)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));
  g_return_if_fail (PIKA_IS_PROGRESS (progress));

  if (pika->gui.progress_free)
    pika->gui.progress_free (pika, progress);
}

gboolean
pika_pdb_dialog_new (Pika          *pika,
                     PikaContext   *context,
                     PikaProgress  *progress,
                     GType          contents_type,
                     GBytes        *parent_handle,
                     const gchar   *title,
                     const gchar   *callback_name,
                     PikaObject    *object,
                     ...)
{
  gboolean retval = FALSE;

  g_return_val_if_fail (PIKA_IS_PIKA (pika), FALSE);
  g_return_val_if_fail (PIKA_IS_CONTEXT (context), FALSE);
  g_return_val_if_fail (progress == NULL || PIKA_IS_PROGRESS (progress), FALSE);
  g_return_val_if_fail (g_type_is_a (contents_type, PIKA_TYPE_RESOURCE) ||
                        g_type_is_a (contents_type, PIKA_TYPE_DRAWABLE), FALSE);
  g_return_val_if_fail (object == NULL ||
                        g_type_is_a (G_TYPE_FROM_INSTANCE (object), contents_type), FALSE);
  g_return_val_if_fail (title != NULL, FALSE);
  g_return_val_if_fail (callback_name != NULL, FALSE);

  if (pika->gui.pdb_dialog_new)
    {
      va_list args;

      va_start (args, object);

      retval = pika->gui.pdb_dialog_new (pika, context, progress,
                                         contents_type, parent_handle, title,
                                         callback_name, object, args);

      va_end (args);
    }

  return retval;
}

gboolean
pika_pdb_dialog_set (Pika        *pika,
                     GType        contents_type,
                     const gchar *callback_name,
                     PikaObject  *object,
                     ...)
{
  gboolean retval = FALSE;

  g_return_val_if_fail (PIKA_IS_PIKA (pika), FALSE);
  g_return_val_if_fail (g_type_is_a (contents_type, PIKA_TYPE_RESOURCE) ||
                        contents_type == PIKA_TYPE_DRAWABLE, FALSE);
  g_return_val_if_fail (callback_name != NULL, FALSE);
  g_return_val_if_fail (object == NULL || g_type_is_a (G_TYPE_FROM_INSTANCE (object), contents_type), FALSE);

  if (pika->gui.pdb_dialog_set)
    {
      va_list args;

      va_start (args, object);

      retval = pika->gui.pdb_dialog_set (pika, contents_type, callback_name,
                                         object, args);

      va_end (args);
    }

  return retval;
}

gboolean
pika_pdb_dialog_close (Pika          *pika,
                       GType          contents_type,
                       const gchar   *callback_name)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), FALSE);
  g_return_val_if_fail (g_type_is_a (contents_type, PIKA_TYPE_RESOURCE) ||
                        contents_type == PIKA_TYPE_DRAWABLE, FALSE);
  g_return_val_if_fail (callback_name != NULL, FALSE);

  if (pika->gui.pdb_dialog_close)
    return pika->gui.pdb_dialog_close (pika, contents_type, callback_name);

  return FALSE;
}

gboolean
pika_recent_list_add_file (Pika        *pika,
                           GFile       *file,
                           const gchar *mime_type)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), FALSE);
  g_return_val_if_fail (G_IS_FILE (file), FALSE);

  if (pika->gui.recent_list_add_file)
    return pika->gui.recent_list_add_file (pika, file, mime_type);

  return FALSE;
}

void
pika_recent_list_load (Pika *pika)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));

  if (pika->gui.recent_list_load)
    pika->gui.recent_list_load (pika);
}

GMountOperation *
pika_get_mount_operation (Pika         *pika,
                          PikaProgress *progress)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), FALSE);
  g_return_val_if_fail (progress == NULL || PIKA_IS_PROGRESS (progress), FALSE);

  if (pika->gui.get_mount_operation)
    return pika->gui.get_mount_operation (pika, progress);

  return g_mount_operation_new ();
}

PikaColorProfilePolicy
pika_query_profile_policy (Pika                      *pika,
                           PikaImage                 *image,
                           PikaContext               *context,
                           PikaColorProfile         **dest_profile,
                           PikaColorRenderingIntent  *intent,
                           gboolean                  *bpc,
                           gboolean                  *dont_ask)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), PIKA_COLOR_PROFILE_POLICY_KEEP);
  g_return_val_if_fail (PIKA_IS_IMAGE (image), PIKA_COLOR_PROFILE_POLICY_KEEP);
  g_return_val_if_fail (PIKA_IS_CONTEXT (context), PIKA_COLOR_PROFILE_POLICY_KEEP);
  g_return_val_if_fail (dest_profile != NULL, PIKA_COLOR_PROFILE_POLICY_KEEP);

  if (pika->gui.query_profile_policy)
    return pika->gui.query_profile_policy (pika, image, context,
                                           dest_profile,
                                           intent, bpc,
                                           dont_ask);

  return PIKA_COLOR_PROFILE_POLICY_KEEP;
}

PikaMetadataRotationPolicy
pika_query_rotation_policy (Pika        *pika,
                            PikaImage   *image,
                            PikaContext *context,
                            gboolean    *dont_ask)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), PIKA_METADATA_ROTATION_POLICY_ROTATE);
  g_return_val_if_fail (PIKA_IS_IMAGE (image), PIKA_METADATA_ROTATION_POLICY_ROTATE);
  g_return_val_if_fail (PIKA_IS_CONTEXT (context), PIKA_METADATA_ROTATION_POLICY_ROTATE);

  if (pika->gui.query_rotation_policy)
    return pika->gui.query_rotation_policy (pika, image, context, dont_ask);

  return PIKA_METADATA_ROTATION_POLICY_ROTATE;
}
