/* highscores.c generated by valac 0.56.19, the Vala compiler
 * generated from highscores.vala, do not modify */

/* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * Copyright © 2025 Johan GAY
 *
 * This file is part of GNOME Sudoku.
 *
 * GNOME Sudoku 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.
 *
 * GNOME Sudoku 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 GNOME Sudoku. If not, see <http://www.gnu.org/licenses/>.
 */

#include "libsudoku.h"
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gee.h>
#include <glib-object.h>
#include <float.h>
#include <math.h>
#include <json-glib/json-glib.h>

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

enum  {
	HIGHSCORES_0_PROPERTY,
	HIGHSCORES_NUM_PROPERTIES
};
static GParamSpec* highscores_properties[HIGHSCORES_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define __vala_JsonNode_free0(var) ((var == NULL) ? NULL : (var = (_vala_JsonNode_free (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))

struct _HighscoresPrivate {
	gchar* highscores_file;
	GeeHashMap* highscores;
};

static gint Highscores_private_offset;
static gpointer highscores_parent_class = NULL;

static gdouble* _double_dup (gdouble* self);
static void highscores_get_highscores (Highscores* self);
static JsonNode* _vala_JsonNode_copy (JsonNode* self);
static void highscores_read_difficulty (Highscores* self,
                                 JsonReader* reader,
                                 DifficultyCategory diff);
static void _vala_JsonNode_free (JsonNode* self);
static void highscores_save_highscores (Highscores* self);
static void highscores_finalize (GObject * obj);
static GType highscores_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
highscores_get_instance_private (Highscores* self)
{
	return G_STRUCT_MEMBER_P (self, Highscores_private_offset);
}

static gdouble*
_double_dup (gdouble* self)
{
	gdouble* dup;
	dup = g_new0 (gdouble, 1);
	memcpy (dup, self, sizeof (gdouble));
	return dup;
}

Highscores*
highscores_construct (GType object_type,
                      const gchar* path)
{
	Highscores * self = NULL;
	GeeHashMap* _tmp0_;
	gchar* _tmp1_;
	g_return_val_if_fail (path != NULL, NULL);
	self = (Highscores*) g_object_new (object_type, NULL);
	_tmp0_ = gee_hash_map_new (TYPE_DIFFICULTY_CATEGORY, NULL, NULL, G_TYPE_DOUBLE, (GBoxedCopyFunc) _double_dup, (GDestroyNotify) g_free, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
	_g_object_unref0 (self->priv->highscores);
	self->priv->highscores = _tmp0_;
	_tmp1_ = g_strdup (path);
	_g_free0 (self->priv->highscores_file);
	self->priv->highscores_file = _tmp1_;
	highscores_get_highscores (self);
	return self;
}

Highscores*
highscores_new (const gchar* path)
{
	return highscores_construct (TYPE_HIGHSCORES, path);
}

gdouble*
highscores_get_highscore (Highscores* self,
                          DifficultyCategory difficulty)
{
	GeeHashMap* _tmp0_;
	gpointer _tmp1_;
	gdouble* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->highscores;
	_tmp1_ = gee_abstract_map_get ((GeeAbstractMap*) _tmp0_, (gpointer) ((gintptr) difficulty));
	result = (gdouble*) _tmp1_;
	return result;
}

static JsonNode*
_vala_JsonNode_copy (JsonNode* self)
{
	return g_boxed_copy (json_node_get_type (), self);
}

static gpointer
__vala_JsonNode_copy0 (gpointer self)
{
	return self ? _vala_JsonNode_copy (self) : NULL;
}

static void
_vala_JsonNode_free (JsonNode* self)
{
	g_boxed_free (json_node_get_type (), self);
}

static void
highscores_get_highscores (Highscores* self)
{
	JsonParser* parser = NULL;
	JsonParser* _tmp0_;
	JsonNode* node = NULL;
	JsonParser* _tmp3_;
	JsonNode* _tmp4_;
	JsonNode* _tmp5_;
	JsonReader* reader = NULL;
	JsonNode* _tmp6_;
	JsonReader* _tmp7_;
	JsonReader* _tmp8_;
	JsonReader* _tmp9_;
	JsonReader* _tmp10_;
	JsonReader* _tmp11_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	_tmp0_ = json_parser_new ();
	parser = _tmp0_;
	{
		JsonParser* _tmp1_;
		const gchar* _tmp2_;
		_tmp1_ = parser;
		_tmp2_ = self->priv->highscores_file;
		json_parser_load_from_file (_tmp1_, _tmp2_, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		g_clear_error (&_inner_error0_);
		_g_object_unref0 (parser);
		return;
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_object_unref0 (parser);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
	_tmp3_ = parser;
	_tmp4_ = json_parser_get_root (_tmp3_);
	_tmp5_ = __vala_JsonNode_copy0 (_tmp4_);
	node = _tmp5_;
	_tmp6_ = node;
	_tmp7_ = json_reader_new (_tmp6_);
	reader = _tmp7_;
	_tmp8_ = reader;
	highscores_read_difficulty (self, _tmp8_, DIFFICULTY_CATEGORY_EASY);
	_tmp9_ = reader;
	highscores_read_difficulty (self, _tmp9_, DIFFICULTY_CATEGORY_MEDIUM);
	_tmp10_ = reader;
	highscores_read_difficulty (self, _tmp10_, DIFFICULTY_CATEGORY_HARD);
	_tmp11_ = reader;
	highscores_read_difficulty (self, _tmp11_, DIFFICULTY_CATEGORY_VERY_HARD);
	_g_object_unref0 (reader);
	__vala_JsonNode_free0 (node);
	_g_object_unref0 (parser);
}

static void
highscores_read_difficulty (Highscores* self,
                            JsonReader* reader,
                            DifficultyCategory diff)
{
	gchar* _tmp0_;
	gchar* _tmp1_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (reader != NULL);
	_tmp0_ = difficulty_category_to_untranslated_string (diff);
	_tmp1_ = _tmp0_;
	json_reader_read_member (reader, _tmp1_);
	_g_free0 (_tmp1_);
	if (json_reader_is_value (reader)) {
		GeeHashMap* _tmp2_;
		gdouble _tmp3_;
		_tmp2_ = self->priv->highscores;
		_tmp3_ = json_reader_get_double_value (reader);
		gee_abstract_map_set ((GeeAbstractMap*) _tmp2_, (gpointer) ((gintptr) diff), &_tmp3_);
	}
	json_reader_end_member (reader);
}

void
highscores_save_highscore (Highscores* self,
                           DifficultyCategory difficulty,
                           gdouble time_elapsed)
{
	GeeHashMap* _tmp0_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->highscores;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp0_, (gpointer) ((gintptr) difficulty), &time_elapsed);
	highscores_save_highscores (self);
}

static void
highscores_save_highscores (Highscores* self)
{
	JsonBuilder* builder = NULL;
	JsonBuilder* _tmp0_;
	JsonBuilder* _tmp1_;
	JsonBuilder* _tmp17_;
	JsonGenerator* generator = NULL;
	JsonGenerator* _tmp18_;
	JsonNode* root = NULL;
	JsonBuilder* _tmp19_;
	JsonNode* _tmp20_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	_tmp0_ = json_builder_new ();
	builder = _tmp0_;
	_tmp1_ = builder;
	json_builder_begin_object (_tmp1_);
	{
		GeeIterator* _highscore_it = NULL;
		GeeHashMap* _tmp2_;
		GeeIterator* _tmp3_;
		_tmp2_ = self->priv->highscores;
		_tmp3_ = gee_iterable_iterator ((GeeIterable*) _tmp2_);
		_highscore_it = _tmp3_;
		while (TRUE) {
			GeeIterator* _tmp4_;
			GeeMapEntry* highscore = NULL;
			GeeIterator* _tmp5_;
			gpointer _tmp6_;
			JsonBuilder* _tmp7_;
			GeeMapEntry* _tmp8_;
			gconstpointer _tmp9_;
			gconstpointer _tmp10_;
			gchar* _tmp11_;
			gchar* _tmp12_;
			JsonBuilder* _tmp13_;
			GeeMapEntry* _tmp14_;
			gconstpointer _tmp15_;
			gconstpointer _tmp16_;
			_tmp4_ = _highscore_it;
			if (!gee_iterator_next (_tmp4_)) {
				break;
			}
			_tmp5_ = _highscore_it;
			_tmp6_ = gee_iterator_get (_tmp5_);
			highscore = (GeeMapEntry*) _tmp6_;
			_tmp7_ = builder;
			_tmp8_ = highscore;
			_tmp9_ = gee_map_entry_get_key (_tmp8_);
			_tmp10_ = _tmp9_;
			_tmp11_ = difficulty_category_to_untranslated_string ((DifficultyCategory) ((gintptr) _tmp10_));
			_tmp12_ = _tmp11_;
			json_builder_set_member_name (_tmp7_, _tmp12_);
			_g_free0 (_tmp12_);
			_tmp13_ = builder;
			_tmp14_ = highscore;
			_tmp15_ = gee_map_entry_get_value (_tmp14_);
			_tmp16_ = _tmp15_;
			json_builder_add_double_value (_tmp13_, *((gdouble*) _tmp16_));
			_g_object_unref0 (highscore);
		}
		_g_object_unref0 (_highscore_it);
	}
	_tmp17_ = builder;
	json_builder_end_object (_tmp17_);
	_tmp18_ = json_generator_new ();
	generator = _tmp18_;
	json_generator_set_pretty (generator, TRUE);
	_tmp19_ = builder;
	_tmp20_ = json_builder_get_root (_tmp19_);
	root = _tmp20_;
	json_generator_set_root (generator, root);
	{
		const gchar* _tmp21_;
		_tmp21_ = self->priv->highscores_file;
		json_generator_to_file (generator, _tmp21_, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp22_;
		const gchar* _tmp23_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp22_ = e;
		_tmp23_ = _tmp22_->message;
		g_warning ("highscores.vala:98: %s", _tmp23_);
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		__vala_JsonNode_free0 (root);
		_g_object_unref0 (generator);
		_g_object_unref0 (builder);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
	__vala_JsonNode_free0 (root);
	_g_object_unref0 (generator);
	_g_object_unref0 (builder);
}

static void
highscores_class_init (HighscoresClass * klass,
                       gpointer klass_data)
{
	highscores_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &Highscores_private_offset);
	G_OBJECT_CLASS (klass)->finalize = highscores_finalize;
}

static void
highscores_instance_init (Highscores * self,
                          gpointer klass)
{
	self->priv = highscores_get_instance_private (self);
}

static void
highscores_finalize (GObject * obj)
{
	Highscores * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_HIGHSCORES, Highscores);
	_g_free0 (self->priv->highscores_file);
	_g_object_unref0 (self->priv->highscores);
	G_OBJECT_CLASS (highscores_parent_class)->finalize (obj);
}

static GType
highscores_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (HighscoresClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) highscores_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (Highscores), 0, (GInstanceInitFunc) highscores_instance_init, NULL };
	GType highscores_type_id;
	highscores_type_id = g_type_register_static (G_TYPE_OBJECT, "Highscores", &g_define_type_info, 0);
	Highscores_private_offset = g_type_add_instance_private (highscores_type_id, sizeof (HighscoresPrivate));
	return highscores_type_id;
}

GType
highscores_get_type (void)
{
	static volatile gsize highscores_type_id__once = 0;
	if (g_once_init_enter (&highscores_type_id__once)) {
		GType highscores_type_id;
		highscores_type_id = highscores_get_type_once ();
		g_once_init_leave (&highscores_type_id__once, highscores_type_id);
	}
	return highscores_type_id__once;
}

static inline gpointer
_vala_memdup2 (gconstpointer mem,
               gsize byte_size)
{
	gpointer new_mem;
	if (mem && byte_size != 0) {
		new_mem = g_malloc (byte_size);
		memcpy (new_mem, mem, byte_size);
	} else {
		new_mem = NULL;
	}
	return new_mem;
}

