/* 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
 *
 * PikaEarlyRc: pre-parsing of pikarc suitable for use during early
 * initialization, when the pika singleton is not constructed yet
 *
 * Copyright (C) 2017  Jehan <jehan@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 <gio/gio.h>

#include "libpikabase/pikabase.h"
#include "libpikaconfig/pikaconfig.h"

#include "config-types.h"

#include "core/core-types.h"
#include "core/pika-utils.h"

#include "pikaearlyrc.h"

enum
{
  PROP_0,
  PROP_VERBOSE,
  PROP_SYSTEM_PIKARC,
  PROP_USER_PIKARC,
  PROP_LANGUAGE,
#ifdef G_OS_WIN32
  PROP_WIN32_POINTER_INPUT_API,
#endif
};


static void         pika_early_rc_constructed       (GObject      *object);
static void         pika_early_rc_finalize          (GObject      *object);
static void         pika_early_rc_set_property      (GObject      *object,
                                                     guint         property_id,
                                                     const GValue *value,
                                                     GParamSpec   *pspec);
static void         pika_early_rc_get_property      (GObject      *object,
                                                     guint         property_id,
                                                     GValue       *value,
                                                     GParamSpec   *pspec);


/* Just use PikaConfig interface's default implementation which will
 * fill the properties. */
G_DEFINE_TYPE_WITH_CODE (PikaEarlyRc, pika_early_rc, G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (PIKA_TYPE_CONFIG, NULL))

#define parent_class pika_early_rc_parent_class


static void
pika_early_rc_class_init (PikaEarlyRcClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->constructed  = pika_early_rc_constructed;
  object_class->finalize     = pika_early_rc_finalize;
  object_class->set_property = pika_early_rc_set_property;
  object_class->get_property = pika_early_rc_get_property;

  g_object_class_install_property (object_class, PROP_VERBOSE,
                                   g_param_spec_boolean ("verbose",
                                                         NULL, NULL,
                                                         FALSE,
                                                         G_PARAM_READWRITE |
                                                         G_PARAM_CONSTRUCT_ONLY));

  g_object_class_install_property (object_class, PROP_SYSTEM_PIKARC,
                                   g_param_spec_object ("system-pikarc",
                                                        NULL, NULL,
                                                        G_TYPE_FILE,
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY));

  g_object_class_install_property (object_class, PROP_USER_PIKARC,
                                   g_param_spec_object ("user-pikarc",
                                                        NULL, NULL,
                                                        G_TYPE_FILE,
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY));

  PIKA_CONFIG_PROP_STRING (object_class, PROP_LANGUAGE,
                           "language", NULL, NULL, NULL,
                           PIKA_PARAM_STATIC_STRINGS);

#ifdef G_OS_WIN32
  PIKA_CONFIG_PROP_ENUM (object_class, PROP_WIN32_POINTER_INPUT_API,
                         "win32-pointer-input-api", NULL, NULL,
                         PIKA_TYPE_WIN32_POINTER_INPUT_API,
                         PIKA_WIN32_POINTER_INPUT_API_WINDOWS_INK,
                         PIKA_PARAM_STATIC_STRINGS |
                         PIKA_CONFIG_PARAM_RESTART);
#endif
}

static void
pika_early_rc_init (PikaEarlyRc *rc)
{
}

static void
pika_early_rc_constructed (GObject *object)
{
  PikaEarlyRc *rc    = PIKA_EARLY_RC (object);
  GError      *error = NULL;

  if (rc->verbose)
    g_print ("Parsing '%s' for configuration data required during early initialization.\n",
             pika_file_get_utf8_name (rc->system_pikarc));

  if (! pika_config_deserialize_file (PIKA_CONFIG (rc),
                                      rc->system_pikarc, NULL, &error))
    {
      if (error->code != PIKA_CONFIG_ERROR_OPEN_ENOENT)
        g_message ("%s", error->message);

      g_clear_error (&error);
    }

  if (rc->verbose)
    g_print ("Parsing '%s' for configuration data required during early initialization.\n",
             pika_file_get_utf8_name (rc->user_pikarc));

  if (! pika_config_deserialize_file (PIKA_CONFIG (rc),
                                      rc->user_pikarc, NULL, &error))
    {
      if (error->code != PIKA_CONFIG_ERROR_OPEN_ENOENT)
        g_message ("%s", error->message);

      g_clear_error (&error);
    }

  if (rc->verbose)
    {
      if (rc->language)
        g_print ("Language property found: %s.\n", rc->language);
      else
        g_print ("No language property found.\n");
    }
}

static void
pika_early_rc_finalize (GObject *object)
{
  PikaEarlyRc *rc = PIKA_EARLY_RC (object);

  g_clear_object (&rc->system_pikarc);
  g_clear_object (&rc->user_pikarc);

  g_clear_pointer (&rc->language, g_free);

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

static void
pika_early_rc_set_property (GObject      *object,
                            guint         property_id,
                            const GValue *value,
                            GParamSpec   *pspec)
{
  PikaEarlyRc *rc = PIKA_EARLY_RC (object);

  switch (property_id)
    {
    case PROP_VERBOSE:
      rc->verbose = g_value_get_boolean (value);
      break;

    case PROP_SYSTEM_PIKARC:
      if (rc->system_pikarc)
        g_object_unref (rc->system_pikarc);

      if (g_value_get_object (value))
        rc->system_pikarc = g_value_dup_object (value);
      else
        rc->system_pikarc = pika_sysconf_directory_file ("pikarc", NULL);
      break;

    case PROP_USER_PIKARC:
      if (rc->user_pikarc)
        g_object_unref (rc->user_pikarc);

      if (g_value_get_object (value))
        rc->user_pikarc = g_value_dup_object (value);
      else
        rc->user_pikarc = pika_directory_file ("pikarc", NULL);
      break;

    case PROP_LANGUAGE:
      if (rc->language)
        g_free (rc->language);
      rc->language = g_value_dup_string (value);
      break;

#ifdef G_OS_WIN32
    case PROP_WIN32_POINTER_INPUT_API:
      {
        PikaWin32PointerInputAPI api = g_value_get_enum (value);
        gboolean have_wintab         = pika_win32_have_wintab ();
        gboolean have_windows_ink    = pika_win32_have_windows_ink ();
        gboolean api_is_wintab       = (api == PIKA_WIN32_POINTER_INPUT_API_WINTAB);
        gboolean api_is_windows_ink  = (api == PIKA_WIN32_POINTER_INPUT_API_WINDOWS_INK);

        if (api_is_wintab && !have_wintab && have_windows_ink)
          {
            rc->win32_pointer_input_api = PIKA_WIN32_POINTER_INPUT_API_WINDOWS_INK;
          }
        else if (api_is_windows_ink && !have_windows_ink && have_wintab)
          {
            rc->win32_pointer_input_api = PIKA_WIN32_POINTER_INPUT_API_WINTAB;
          }
        else
          {
            rc->win32_pointer_input_api = api;
          }
      }
      break;
#endif

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

static void
pika_early_rc_get_property (GObject    *object,
                            guint       property_id,
                            GValue     *value,
                            GParamSpec *pspec)
{
  PikaEarlyRc *rc = PIKA_EARLY_RC (object);

  switch (property_id)
    {
    case PROP_VERBOSE:
      g_value_set_boolean (value, rc->verbose);
      break;
    case PROP_SYSTEM_PIKARC:
      g_value_set_object (value, rc->system_pikarc);
      break;
    case PROP_USER_PIKARC:
      g_value_set_object (value, rc->user_pikarc);
      break;
    case PROP_LANGUAGE:
      g_value_set_string (value, rc->language);
      break;

#ifdef G_OS_WIN32
    case PROP_WIN32_POINTER_INPUT_API:
      g_value_set_enum (value, rc->win32_pointer_input_api);
      break;
#endif

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

/**
 * pika_early_rc_new:
 * @system_pikarc: the name of the system-wide pikarc file or %NULL to
 *                 use the standard location
 * @user_pikarc:   the name of the user pikarc file or %NULL to use the
 *                 standard location
 * @verbose:       enable console messages about loading the preferences
 *
 * Creates a new PikaEarlyRc object which looks for some configuration
 * data required during early initialization steps
 *
 * Returns: the new #PikaEarlyRc.
 */
PikaEarlyRc *
pika_early_rc_new (GFile    *system_pikarc,
                   GFile    *user_pikarc,
                   gboolean  verbose)
{
  PikaEarlyRc *rc;

  g_return_val_if_fail (system_pikarc == NULL || G_IS_FILE (system_pikarc),
                        NULL);
  g_return_val_if_fail (user_pikarc == NULL || G_IS_FILE (user_pikarc),
                        NULL);

  rc = g_object_new (PIKA_TYPE_EARLY_RC,
                     "verbose",       verbose,
                     "system-pikarc", system_pikarc,
                     "user-pikarc",   user_pikarc,
                     NULL);

  return rc;
}

/**
 * pika_early_rc_get_language:
 * @rc: a #PikaEarlyRc object.
 *
 * This function looks up the language set in `pikarc`.
 *
 * Returns: a newly allocated string representing the language or
 *               %NULL if the key couldn't be found.
 **/
gchar *
pika_early_rc_get_language (PikaEarlyRc *rc)
{
  return rc->language ? g_strdup (rc->language) : NULL;
}

#ifdef G_OS_WIN32

/**
 * pika_early_rc_get_win32_pointer_input_api:
 * @rc: a #PikaEarlyRc object.
 *
 * This function looks up the win32-specific pointer input API
 * set in `pikarc`.
 *
 * Returns: the selected win32-specific pointer input API
 **/
PikaWin32PointerInputAPI
pika_early_rc_get_win32_pointer_input_api (PikaEarlyRc *rc)
{
  return rc->win32_pointer_input_api;
}

#endif
