/* 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
 *
 * pikaviewrendererdrawable.c
 * Copyright (C) 2003 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 "libpikabase/pikabase.h"
#include "libpikamath/pikamath.h"

#include "widgets-types.h"

#include "config/pikacoreconfig.h"

#include "core/pika.h"
#include "core/pikaasync.h"
#include "core/pikacancelable.h"
#include "core/pikadrawable.h"
#include "core/pikadrawable-preview.h"
#include "core/pikaimage.h"
#include "core/pikatempbuf.h"

#include "pikaviewrendererdrawable.h"


struct _PikaViewRendererDrawablePrivate
{
  PikaAsync *render_async;
  GtkWidget *render_widget;
  gint       render_buf_x;
  gint       render_buf_y;
  gboolean   render_update;

  gint       prev_width;
  gint       prev_height;
};


/*  local function prototypes  */

static void   pika_view_renderer_drawable_dispose       (GObject                  *object);

static void   pika_view_renderer_drawable_invalidate    (PikaViewRenderer         *renderer);
static void   pika_view_renderer_drawable_render        (PikaViewRenderer         *renderer,
                                                         GtkWidget                *widget);

static void   pika_view_renderer_drawable_cancel_render (PikaViewRendererDrawable *renderdrawable);


G_DEFINE_TYPE_WITH_PRIVATE (PikaViewRendererDrawable,
                            pika_view_renderer_drawable,
                            PIKA_TYPE_VIEW_RENDERER)

#define parent_class pika_view_renderer_drawable_parent_class


/*  private functions  */


static void
pika_view_renderer_drawable_class_init (PikaViewRendererDrawableClass *klass)
{
  GObjectClass          *object_class   = G_OBJECT_CLASS (klass);
  PikaViewRendererClass *renderer_class = PIKA_VIEW_RENDERER_CLASS (klass);

  object_class->dispose      = pika_view_renderer_drawable_dispose;

  renderer_class->invalidate = pika_view_renderer_drawable_invalidate;
  renderer_class->render     = pika_view_renderer_drawable_render;
}

static void
pika_view_renderer_drawable_init (PikaViewRendererDrawable *renderdrawable)
{
  renderdrawable->priv =
    pika_view_renderer_drawable_get_instance_private (renderdrawable);
}

static void
pika_view_renderer_drawable_dispose (GObject *object)
{
  PikaViewRendererDrawable *renderdrawable = PIKA_VIEW_RENDERER_DRAWABLE (object);

  pika_view_renderer_drawable_cancel_render (renderdrawable);

  G_OBJECT_CLASS (parent_class)->dispose (object);
}

static void
pika_view_renderer_drawable_invalidate (PikaViewRenderer *renderer)
{
  PikaViewRendererDrawable *renderdrawable = PIKA_VIEW_RENDERER_DRAWABLE (renderer);

  pika_view_renderer_drawable_cancel_render (renderdrawable);

  PIKA_VIEW_RENDERER_CLASS (parent_class)->invalidate (renderer);
}

static void
pika_view_renderer_drawable_render_async_callback (PikaAsync                *async,
                                                   PikaViewRendererDrawable *renderdrawable)
{
  GtkWidget *widget;

  /* rendering was canceled, and the view renderer is potentially dead (see
   * pika_view_renderer_drawable_cancel_render()).  bail.
   */
  if (pika_async_is_canceled (async))
    return;

  widget = renderdrawable->priv->render_widget;

  renderdrawable->priv->render_async  = NULL;
  renderdrawable->priv->render_widget = NULL;

  if (pika_async_is_finished (async))
    {
      PikaViewRenderer *renderer   = PIKA_VIEW_RENDERER (renderdrawable);
      PikaTempBuf      *render_buf = pika_async_get_result (async);

      pika_view_renderer_render_temp_buf (
        renderer,
        widget,
        render_buf,
        renderdrawable->priv->render_buf_x,
        renderdrawable->priv->render_buf_y,
        -1,
        PIKA_VIEW_BG_CHECKS,
        PIKA_VIEW_BG_CHECKS);

      if (renderdrawable->priv->render_update)
        pika_view_renderer_update (renderer);
    }

  g_object_unref (widget);
}

static void
pika_view_renderer_drawable_render (PikaViewRenderer *renderer,
                                    GtkWidget        *widget)
{
  PikaViewRendererDrawable *renderdrawable = PIKA_VIEW_RENDERER_DRAWABLE (renderer);
  PikaDrawable             *drawable;
  PikaItem                 *item;
  PikaImage                *image;
  const gchar              *icon_name;
  PikaAsync                *async;
  gint                      image_width;
  gint                      image_height;
  gint                      view_width;
  gint                      view_height;
  gint                      src_x;
  gint                      src_y;
  gint                      src_width;
  gint                      src_height;
  gint                      dst_x;
  gint                      dst_y;
  gint                      dst_width;
  gint                      dst_height;
  gdouble                   xres  = 1.0;
  gdouble                   yres  = 1.0;
  gboolean                  empty = FALSE;

  /* render is already in progress */
  if (renderdrawable->priv->render_async)
    return;

  drawable  = PIKA_DRAWABLE (renderer->viewable);
  item      = PIKA_ITEM (drawable);
  image     = pika_item_get_image (item);
  icon_name = pika_viewable_get_icon_name (renderer->viewable);

  if (image && ! image->pika->config->layer_previews)
    {
      renderdrawable->priv->prev_width  = 0;
      renderdrawable->priv->prev_height = 0;

      pika_view_renderer_render_icon (renderer, widget, icon_name);

      return;
    }

  if (image)
    pika_image_get_resolution (image, &xres, &yres);

  if (renderer->is_popup)
    image = NULL;

  if (image)
    {
      image_width  = pika_image_get_width  (image);
      image_height = pika_image_get_height (image);
    }
  else
    {
      image_width  = pika_item_get_width  (item);
      image_height = pika_item_get_height (item);
    }

  pika_viewable_calc_preview_size (image_width,
                                   image_height,
                                   renderer->width,
                                   renderer->height,
                                   renderer->dot_for_dot,
                                   xres,
                                   yres,
                                   &view_width,
                                   &view_height,
                                   NULL);

  src_x      = 0;
  src_y      = 0;
  src_width  = pika_item_get_width  (item);
  src_height = pika_item_get_height (item);

  if (image)
    {
      gint offset_x;
      gint offset_y;

      pika_item_get_offset (item, &offset_x, &offset_y);

      if (pika_rectangle_intersect (src_x, src_y,
                                    src_width, src_height,
                                    -offset_x, -offset_y,
                                    image_width, image_height,
                                    &src_x, &src_y,
                                    &src_width, &src_height))
        {
          offset_x += src_x;
          offset_y += src_y;

          dst_x      = ROUND (((gdouble) view_width  / image_width)  *
                              offset_x);
          dst_y      = ROUND (((gdouble) view_height / image_height) *
                              offset_y);
          dst_width  = ROUND (((gdouble) view_width  / image_width)  *
                              src_width);
          dst_height = ROUND (((gdouble) view_height / image_height) *
                              src_height);
        }
      else
        {
          dst_x      = 0;
          dst_y      = 0;
          dst_width  = 1;
          dst_height = 1;

          empty = TRUE;
        }
    }
  else
    {
      dst_x      = (renderer->width  - view_width)  / 2;
      dst_y      = (renderer->height - view_height) / 2;
      dst_width  = view_width;
      dst_height = view_height;
    }

  dst_width  = MAX (dst_width,  1);
  dst_height = MAX (dst_height, 1);

  if (! empty)
    {
      async = pika_drawable_get_sub_preview_async (drawable,
                                                   src_x, src_y,
                                                   src_width, src_height,
                                                   dst_width, dst_height);
    }
  else
    {
      const Babl  *format = pika_drawable_get_preview_format (drawable);
      PikaTempBuf *render_buf;

      async = pika_async_new ();

      render_buf = pika_temp_buf_new (dst_width, dst_height, format);
      pika_temp_buf_data_clear (render_buf);

      pika_async_finish_full (async,
                              render_buf,
                              (GDestroyNotify) pika_temp_buf_unref);
    }

  if (async)
    {
      renderdrawable->priv->render_async  = async;
      renderdrawable->priv->render_widget = g_object_ref (widget);
      renderdrawable->priv->render_buf_x  = dst_x;
      renderdrawable->priv->render_buf_y  = dst_y;
      renderdrawable->priv->render_update = FALSE;

      pika_async_add_callback_for_object (
        async,
        (PikaAsyncCallback) pika_view_renderer_drawable_render_async_callback,
        renderdrawable,
        renderdrawable);

      /* if rendering isn't done yet, update the render-view once it is, and
       * either keep the old drawable preview for now, or, if size changed (or
       * there's no old preview,) render an icon in the meantime.
       */
      if (renderdrawable->priv->render_async)
        {
          renderdrawable->priv->render_update = TRUE;

          if (renderer->width  != renderdrawable->priv->prev_width ||
              renderer->height != renderdrawable->priv->prev_height)
            {
              pika_view_renderer_render_icon (renderer, widget, icon_name);
            }
        }

      renderdrawable->priv->prev_width  = renderer->width;
      renderdrawable->priv->prev_height = renderer->height;

      g_object_unref (async);
    }
  else
    {
      renderdrawable->priv->prev_width  = 0;
      renderdrawable->priv->prev_height = 0;

      pika_view_renderer_render_icon (renderer, widget, icon_name);
    }
}

static void
pika_view_renderer_drawable_cancel_render (PikaViewRendererDrawable *renderdrawable)
{
  /* cancel the async render operation (if one is ongoing) without actually
   * waiting for it.  if the actual rendering hasn't started yet, it will be
   * immediately aborted; otherwise, it can't really be interrupted, so we just
   * let it go on without blocking the main thread.
   * pika_drawable_get_sub_preview_async() can continue rendering safely even
   * after the drawable had died, and our completion callback is prepared to
   * handle cancellation.
   */
  if (renderdrawable->priv->render_async)
    {
      pika_cancelable_cancel (
        PIKA_CANCELABLE (renderdrawable->priv->render_async));

      renderdrawable->priv->render_async = NULL;
    }

  g_clear_object (&renderdrawable->priv->render_widget);
}
