/* 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/pika.h"
#include "core/pikacontainer.h"
#include "core/pikacontext.h"
#include "core/pikaimage.h"
#include "core/pikalist.h"

#include "pikadisplay.h"
#include "pikadisplay-foreach.h"
#include "pikadisplayshell.h"
#include "pikadisplayshell-cursor.h"


gboolean
pika_displays_dirty (Pika *pika)
{
  GList *list;

  g_return_val_if_fail (PIKA_IS_PIKA (pika), FALSE);

  for (list = pika_get_display_iter (pika);
       list;
       list = g_list_next (list))
    {
      PikaDisplay *display = list->data;
      PikaImage   *image   = pika_display_get_image (display);

      if (image && pika_image_is_dirty (image))
        return TRUE;
    }

  return FALSE;
}

static void
pika_displays_image_dirty_callback (PikaImage     *image,
                                    PikaDirtyMask  dirty_mask,
                                    PikaContainer *container)
{
  if (pika_image_is_dirty (image)              &&
      pika_image_get_display_count (image) > 0 &&
      ! pika_container_have (container, PIKA_OBJECT (image)))
    pika_container_add (container, PIKA_OBJECT (image));
}

static void
pika_displays_dirty_images_disconnect (PikaContainer *dirty_container,
                                       PikaContainer *global_container)
{
  GQuark handler;

  handler = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (dirty_container),
                                                "clean-handler"));
  pika_container_remove_handler (global_container, handler);

  handler = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (dirty_container),
                                                "dirty-handler"));
  pika_container_remove_handler (global_container, handler);
}

static void
pika_displays_image_clean_callback (PikaImage     *image,
                                    PikaDirtyMask  dirty_mask,
                                    PikaContainer *container)
{
  if (! pika_image_is_dirty (image))
    pika_container_remove (container, PIKA_OBJECT (image));
}

PikaContainer *
pika_displays_get_dirty_images (Pika *pika)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

  if (pika_displays_dirty (pika))
    {
      PikaContainer *container = pika_list_new_weak (PIKA_TYPE_IMAGE, FALSE);
      GList         *list;
      GQuark         handler;

      handler =
        pika_container_add_handler (pika->images, "clean",
                                    G_CALLBACK (pika_displays_image_dirty_callback),
                                    container);
      g_object_set_data (G_OBJECT (container), "clean-handler",
                         GINT_TO_POINTER (handler));

      handler =
        pika_container_add_handler (pika->images, "dirty",
                                    G_CALLBACK (pika_displays_image_dirty_callback),
                                    container);
      g_object_set_data (G_OBJECT (container), "dirty-handler",
                         GINT_TO_POINTER (handler));

      g_signal_connect_object (container, "disconnect",
                               G_CALLBACK (pika_displays_dirty_images_disconnect),
                               G_OBJECT (pika->images), 0);

      pika_container_add_handler (container, "clean",
                                  G_CALLBACK (pika_displays_image_clean_callback),
                                  container);
      pika_container_add_handler (container, "dirty",
                                  G_CALLBACK (pika_displays_image_clean_callback),
                                  container);

      for (list = pika_get_image_iter (pika);
           list;
           list = g_list_next (list))
        {
          PikaImage *image = list->data;

          if (pika_image_is_dirty (image) &&
              pika_image_get_display_count (image) > 0)
            pika_container_add (container, PIKA_OBJECT (image));
        }

      return container;
    }

  return NULL;
}

/**
 * pika_displays_delete:
 * @pika:
 *
 * Calls pika_display_delete() an all displays in the display list.
 * This closes all displays, including the first one which is usually
 * kept open.
 */
void
pika_displays_delete (Pika *pika)
{
  /*  this removes the PikaDisplay from the list, so do a while loop
   *  "around" the first element to get them all
   */
  while (! pika_container_is_empty (pika->displays))
    {
      PikaDisplay *display = pika_get_display_iter (pika)->data;

      pika_display_delete (display);
    }
}

/**
 * pika_displays_close:
 * @pika:
 *
 * Calls pika_display_close() an all displays in the display list. The
 * first display will remain open without an image.
 */
void
pika_displays_close (Pika *pika)
{
  GList *list;
  GList *iter;

  g_return_if_fail (PIKA_IS_PIKA (pika));

  list = g_list_copy (pika_get_display_iter (pika));

  for (iter = list; iter; iter = g_list_next (iter))
    {
      PikaDisplay *display = iter->data;

      pika_display_close (display);
    }

  g_list_free (list);
}

void
pika_displays_reconnect (Pika      *pika,
                         PikaImage *old,
                         PikaImage *new)
{
  GList *contexts = NULL;
  GList *list;

  g_return_if_fail (PIKA_IS_PIKA (pika));
  g_return_if_fail (PIKA_IS_IMAGE (old));
  g_return_if_fail (PIKA_IS_IMAGE (new));

  /*  check which contexts refer to old_image  */
  for (list = pika->context_list; list; list = g_list_next (list))
    {
      PikaContext *context = list->data;

      if (pika_context_get_image (context) == old)
        contexts = g_list_prepend (contexts, list->data);
    }

  /*  set the new_image on the remembered contexts (in reverse order,
   *  since older contexts are usually the parents of newer
   *  ones). Also, update the contexts before the displays, or we
   *  might run into menu update functions that would see an
   *  inconsistent state (display = new, context = old), and thus
   *  inadvertently call actions as if the user had selected a menu
   *  item.
   */
  g_list_foreach (contexts, (GFunc) pika_context_set_image, new);
  g_list_free (contexts);

  for (list = pika_get_display_iter (pika);
       list;
       list = g_list_next (list))
    {
      PikaDisplay *display = list->data;

      if (pika_display_get_image (display) == old)
        pika_display_set_image (display, new);
    }
}

gint
pika_displays_get_num_visible (Pika *pika)
{
  GList *list;
  gint   visible = 0;

  g_return_val_if_fail (PIKA_IS_PIKA (pika), 0);

  for (list = pika_get_display_iter (pika);
       list;
       list = g_list_next (list))
    {
      PikaDisplay      *display = list->data;
      PikaDisplayShell *shell   = pika_display_get_shell (display);

      if (gtk_widget_is_drawable (GTK_WIDGET (shell)))
        {
          GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (shell));

          if (GTK_IS_WINDOW (toplevel))
            {
              GdkWindow      *window = gtk_widget_get_window (toplevel);
              GdkWindowState  state  = gdk_window_get_state (window);

              if ((state & (GDK_WINDOW_STATE_WITHDRAWN |
                            GDK_WINDOW_STATE_ICONIFIED)) == 0)
                {
                  visible++;
                }
            }
        }
    }

  return visible;
}

void
pika_displays_set_busy (Pika *pika)
{
  GList *list;

  g_return_if_fail (PIKA_IS_PIKA (pika));

  for (list = pika_get_display_iter (pika);
       list;
       list = g_list_next (list))
    {
      PikaDisplayShell *shell =
        pika_display_get_shell (PIKA_DISPLAY (list->data));

      pika_display_shell_set_override_cursor (shell, (PikaCursorType) GDK_WATCH);
    }
}

void
pika_displays_unset_busy (Pika *pika)
{
  GList *list;

  g_return_if_fail (PIKA_IS_PIKA (pika));

  for (list = pika_get_display_iter (pika);
       list;
       list = g_list_next (list))
    {
      PikaDisplayShell *shell =
        pika_display_get_shell (PIKA_DISPLAY (list->data));

      pika_display_shell_unset_override_cursor (shell);
    }
}
