/* 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 <gdk-pixbuf/gdk-pixbuf.h>

#include "libpikabase/pikabase.h"
#include "libpikamath/pikamath.h"

#include "core-types.h"

#include "config/pikaguiconfig.h"

#include "core/pika.h"
#include "core/pikacontainer.h"
#include "core/pikacontext.h"

#include "pikadisplay.h"

#include "pika-intl.h"

enum
{
  PROP_0,
  PROP_ID,
  PROP_PIKA
};


struct _PikaDisplayPrivate
{
  gint id; /* unique identifier for this display */
};


/*  local function prototypes  */

static void     pika_display_set_property           (GObject             *object,
                                                     guint                property_id,
                                                     const GValue        *value,
                                                     GParamSpec          *pspec);
static void     pika_display_get_property           (GObject             *object,
                                                     guint                property_id,
                                                     GValue              *value,
                                                     GParamSpec          *pspec);


G_DEFINE_TYPE_WITH_PRIVATE (PikaDisplay, pika_display, PIKA_TYPE_OBJECT)

#define parent_class pika_display_parent_class


static void
pika_display_class_init (PikaDisplayClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->set_property = pika_display_set_property;
  object_class->get_property = pika_display_get_property;

  g_object_class_install_property (object_class, PROP_ID,
                                   g_param_spec_int ("id",
                                                     NULL, NULL,
                                                     0, G_MAXINT, 0,
                                                     PIKA_PARAM_READABLE));

  g_object_class_install_property (object_class, PROP_PIKA,
                                   g_param_spec_object ("pika",
                                                        NULL, NULL,
                                                        PIKA_TYPE_PIKA,
                                                        PIKA_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY));
}

static void
pika_display_init (PikaDisplay *display)
{
  display->priv = pika_display_get_instance_private (display);
}

static void
pika_display_set_property (GObject      *object,
                           guint         property_id,
                           const GValue *value,
                           GParamSpec   *pspec)
{
  PikaDisplay        *display = PIKA_DISPLAY (object);
  PikaDisplayPrivate *private = display->priv;

  switch (property_id)
    {
    case PROP_PIKA:
      {
        gint id;

        display->pika = g_value_get_object (value); /* don't ref the pika */
        display->config = PIKA_DISPLAY_CONFIG (display->pika->config);

        do
          {
            id = display->pika->next_display_id++;

            if (display->pika->next_display_id == G_MAXINT)
              display->pika->next_display_id = 1;
          }
        while (pika_display_get_by_id (display->pika, id));

        private->id = id;
      }
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
pika_display_get_property (GObject    *object,
                           guint       property_id,
                           GValue     *value,
                           GParamSpec *pspec)
{
  PikaDisplay *display = PIKA_DISPLAY (object);

  switch (property_id)
    {
    case PROP_ID:
      g_value_set_int (value, display->priv->id);
      break;

    case PROP_PIKA:
      g_value_set_object (value, display->pika);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

/*  public functions  */

gint
pika_display_get_id (PikaDisplay *display)
{
  g_return_val_if_fail (PIKA_IS_DISPLAY (display), -1);

  return display->priv->id;
}

PikaDisplay *
pika_display_get_by_id (Pika *pika,
                        gint  id)
{
  GList *list;

  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

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

      if (pika_display_get_id (display) == id)
        return display;
    }

  return NULL;
}

gboolean
pika_display_present (PikaDisplay *display)
{
  g_return_val_if_fail (PIKA_IS_DISPLAY (display), FALSE);

  if (PIKA_DISPLAY_GET_CLASS (display)->present)
    return PIKA_DISPLAY_GET_CLASS (display)->present (display);

  return FALSE;
}

gboolean
pika_display_grab_focus (PikaDisplay *display)
{
  g_return_val_if_fail (PIKA_IS_DISPLAY (display), FALSE);

  if (PIKA_DISPLAY_GET_CLASS (display)->grab_focus)
    return PIKA_DISPLAY_GET_CLASS (display)->grab_focus (display);

  return FALSE;
}

Pika *
pika_display_get_pika (PikaDisplay *display)
{
  g_return_val_if_fail (PIKA_IS_DISPLAY (display), NULL);

  return display->pika;
}
