/* platform-model.c generated by valac 0.56.18-dirty, the Vala compiler
 * generated from platform-model.vala, do not modify */

/* This file is part of GNOME Games. License: GPL-3.0+.*/

#include <glib-object.h>
#include <gio/gio.h>
#include <glib.h>
#include "gnome-games.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

#define GAMES_TYPE_PLATFORM_MODEL (games_platform_model_get_type ())
#define GAMES_PLATFORM_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_PLATFORM_MODEL, GamesPlatformModel))
#define GAMES_PLATFORM_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_PLATFORM_MODEL, GamesPlatformModelClass))
#define GAMES_IS_PLATFORM_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_PLATFORM_MODEL))
#define GAMES_IS_PLATFORM_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_PLATFORM_MODEL))
#define GAMES_PLATFORM_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_PLATFORM_MODEL, GamesPlatformModelClass))

typedef struct _GamesPlatformModel GamesPlatformModel;
typedef struct _GamesPlatformModelClass GamesPlatformModelClass;
typedef struct _GamesPlatformModelPrivate GamesPlatformModelPrivate;

#define GAMES_TYPE_GAME_MODEL (games_game_model_get_type ())
#define GAMES_GAME_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_GAME_MODEL, GamesGameModel))
#define GAMES_GAME_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_GAME_MODEL, GamesGameModelClass))
#define GAMES_IS_GAME_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_GAME_MODEL))
#define GAMES_IS_GAME_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_GAME_MODEL))
#define GAMES_GAME_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_GAME_MODEL, GamesGameModelClass))

typedef struct _GamesGameModel GamesGameModel;
typedef struct _GamesGameModelClass GamesGameModelClass;
enum  {
	GAMES_PLATFORM_MODEL_0_PROPERTY,
	GAMES_PLATFORM_MODEL_NUM_PROPERTIES
};
static GParamSpec* games_platform_model_properties[GAMES_PLATFORM_MODEL_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_sequence_free0(var) ((var == NULL) ? NULL : (var = (g_sequence_free (var), NULL)))
#define _g_hash_table_unref0(var) ((var == NULL) ? NULL : (var = (g_hash_table_unref (var), NULL)))

struct _GamesPlatformModel {
	GObject parent_instance;
	GamesPlatformModelPrivate * priv;
};

struct _GamesPlatformModelClass {
	GObjectClass parent_class;
};

struct _GamesPlatformModelPrivate {
	GamesGameModel* game_model;
	GSequence* sequence;
	gint n_platforms;
	GHashTable* n_games;
};

static gint GamesPlatformModel_private_offset;
static gpointer games_platform_model_parent_class = NULL;
static GListModelInterface * games_platform_model_g_list_model_parent_iface = NULL;

VALA_EXTERN GType games_platform_model_get_type (void) G_GNUC_CONST ;
VALA_EXTERN GType games_game_model_get_type (void) G_GNUC_CONST ;
VALA_EXTERN GamesPlatformModel* games_platform_model_new (GamesGameModel* game_model);
VALA_EXTERN GamesPlatformModel* games_platform_model_construct (GType object_type,
                                                    GamesGameModel* game_model);
static void _g_object_unref0_ (gpointer var);
static guint _games_platform_hash_ghash_func (gconstpointer key);
static gboolean _games_platform_equal_gequal_func (gconstpointer a,
                                            gconstpointer b);
static gint _games_platform_compare_gcompare_data_func (gconstpointer a,
                                                 gconstpointer b,
                                                 gpointer self);
static void games_platform_model_game_added (GamesPlatformModel* self,
                                      GamesGame* game);
static void _games_platform_model_game_added_games_game_model_game_added (GamesGameModel* _sender,
                                                                   GamesGame* game,
                                                                   gpointer self);
static void games_platform_model_game_removed (GamesPlatformModel* self,
                                        GamesGame* game);
static void _games_platform_model_game_removed_games_game_model_game_removed (GamesGameModel* _sender,
                                                                       GamesGame* game,
                                                                       gpointer self);
static GObject* games_platform_model_real_get_item (GListModel* base,
                                             guint position);
static GType games_platform_model_real_get_item_type (GListModel* base);
static guint games_platform_model_real_get_n_items (GListModel* base);
static void games_platform_model_finalize (GObject * obj);
static GType games_platform_model_get_type_once (void);

static inline gpointer
games_platform_model_get_instance_private (GamesPlatformModel* self)
{
	return G_STRUCT_MEMBER_P (self, GamesPlatformModel_private_offset);
}

static gpointer
_g_object_ref0 (gpointer self)
{
	return self ? g_object_ref (self) : NULL;
}

static void
_g_object_unref0_ (gpointer var)
{
	(var == NULL) ? NULL : (var = (g_object_unref (var), NULL));
}

static guint
_games_platform_hash_ghash_func (gconstpointer key)
{
	guint result;
	result = games_platform_hash ((GamesPlatform*) key);
	return result;
}

static gboolean
_games_platform_equal_gequal_func (gconstpointer a,
                                   gconstpointer b)
{
	gboolean result;
	result = games_platform_equal ((GamesPlatform*) a, (GamesPlatform*) b);
	return result;
}

static gint
_games_platform_compare_gcompare_data_func (gconstpointer a,
                                            gconstpointer b,
                                            gpointer self)
{
	gint result;
	result = games_platform_compare ((GamesPlatform*) a, (GamesPlatform*) b);
	return result;
}

static void
_games_platform_model_game_added_games_game_model_game_added (GamesGameModel* _sender,
                                                              GamesGame* game,
                                                              gpointer self)
{
	games_platform_model_game_added ((GamesPlatformModel*) self, game);
}

static void
_games_platform_model_game_removed_games_game_model_game_removed (GamesGameModel* _sender,
                                                                  GamesGame* game,
                                                                  gpointer self)
{
	games_platform_model_game_removed ((GamesPlatformModel*) self, game);
}

GamesPlatformModel*
games_platform_model_construct (GType object_type,
                                GamesGameModel* game_model)
{
	GamesPlatformModel * self = NULL;
	GamesGameModel* _tmp0_;
	GSequence* _tmp1_;
	GHashTable* _tmp2_;
	guint n = 0U;
	GSequence* _tmp25_;
	g_return_val_if_fail (game_model != NULL, NULL);
	self = (GamesPlatformModel*) g_object_new (object_type, NULL);
	_tmp0_ = _g_object_ref0 (game_model);
	_g_object_unref0 (self->priv->game_model);
	self->priv->game_model = _tmp0_;
	_tmp1_ = g_sequence_new (_g_object_unref0_);
	_g_sequence_free0 (self->priv->sequence);
	self->priv->sequence = _tmp1_;
	self->priv->n_platforms = 0;
	_tmp2_ = g_hash_table_new_full (_games_platform_hash_ghash_func, _games_platform_equal_gequal_func, _g_object_unref0_, NULL);
	_g_hash_table_unref0 (self->priv->n_games);
	self->priv->n_games = _tmp2_;
	n = g_list_model_get_n_items ((GListModel*) game_model);
	{
		guint i = 0U;
		i = (guint) 0;
		{
			gboolean _tmp3_ = FALSE;
			_tmp3_ = TRUE;
			while (TRUE) {
				GamesGame* game = NULL;
				GObject* _tmp5_;
				GamesGame* _tmp6_;
				GamesPlatform* platform = NULL;
				GamesGame* _tmp7_;
				GamesPlatform* _tmp8_;
				GamesPlatform* _tmp9_;
				GamesPlatform* _tmp10_;
				GHashTable* _tmp11_;
				GamesPlatform* _tmp12_;
				gconstpointer _tmp13_;
				GHashTable* _tmp19_;
				GamesPlatform* _tmp20_;
				GamesPlatform* _tmp21_;
				GHashTable* _tmp22_;
				GamesPlatform* _tmp23_;
				gconstpointer _tmp24_;
				if (!_tmp3_) {
					guint _tmp4_;
					_tmp4_ = i;
					i = _tmp4_ + 1;
				}
				_tmp3_ = FALSE;
				if (!(i < n)) {
					break;
				}
				_tmp5_ = g_list_model_get_item ((GListModel*) game_model, i);
				_tmp6_ = GAMES_IS_GAME (_tmp5_) ? ((GamesGame*) _tmp5_) : NULL;
				if (_tmp6_ == NULL) {
					_g_object_unref0 (_tmp5_);
				}
				game = _tmp6_;
				_tmp7_ = game;
				_tmp8_ = games_game_get_platform (_tmp7_);
				_tmp9_ = _tmp8_;
				_tmp10_ = _g_object_ref0 (_tmp9_);
				platform = _tmp10_;
				_tmp11_ = self->priv->n_games;
				_tmp12_ = platform;
				_tmp13_ = g_hash_table_lookup (_tmp11_, _tmp12_);
				if (((guint) ((guintptr) _tmp13_)) == ((guint) 0)) {
					GSequence* _tmp14_;
					GamesPlatform* _tmp15_;
					GamesPlatform* _tmp16_;
					GSequenceIter* _tmp17_;
					gint _tmp18_;
					_tmp14_ = self->priv->sequence;
					_tmp15_ = platform;
					_tmp16_ = _g_object_ref0 (_tmp15_);
					_tmp17_ = g_sequence_append (_tmp14_, _tmp16_);
					_tmp18_ = self->priv->n_platforms;
					self->priv->n_platforms = _tmp18_ + 1;
				}
				_tmp19_ = self->priv->n_games;
				_tmp20_ = platform;
				_tmp21_ = _g_object_ref0 (_tmp20_);
				_tmp22_ = self->priv->n_games;
				_tmp23_ = platform;
				_tmp24_ = g_hash_table_lookup (_tmp22_, _tmp23_);
				g_hash_table_insert (_tmp19_, _tmp21_, (gpointer) ((guintptr) (((guint) ((guintptr) _tmp24_)) + 1)));
				_g_object_unref0 (platform);
				_g_object_unref0 (game);
			}
		}
	}
	_tmp25_ = self->priv->sequence;
	g_sequence_sort (_tmp25_, _games_platform_compare_gcompare_data_func, NULL);
	g_list_model_items_changed ((GListModel*) self, (guint) 0, g_list_model_get_n_items ((GListModel*) self), (guint) 0);
	g_signal_connect_object (game_model, "game-added", (GCallback) _games_platform_model_game_added_games_game_model_game_added, self, 0);
	g_signal_connect_object (game_model, "game-removed", (GCallback) _games_platform_model_game_removed_games_game_model_game_removed, self, 0);
	return self;
}

GamesPlatformModel*
games_platform_model_new (GamesGameModel* game_model)
{
	return games_platform_model_construct (GAMES_TYPE_PLATFORM_MODEL, game_model);
}

static GObject*
games_platform_model_real_get_item (GListModel* base,
                                    guint position)
{
	GamesPlatformModel * self;
	GSequenceIter* iter = NULL;
	GSequence* _tmp0_;
	GSequenceIter* _tmp1_;
	gconstpointer _tmp2_;
	GObject* _tmp3_;
	GObject* result;
	self = (GamesPlatformModel*) base;
	_tmp0_ = self->priv->sequence;
	_tmp1_ = g_sequence_get_iter_at_pos (_tmp0_, (gint) position);
	iter = _tmp1_;
	_tmp2_ = g_sequence_get (iter);
	_tmp3_ = _g_object_ref0 ((GObject*) ((GamesPlatform*) _tmp2_));
	result = _tmp3_;
	return result;
}

static GType
games_platform_model_real_get_item_type (GListModel* base)
{
	GamesPlatformModel * self;
	GType result;
	self = (GamesPlatformModel*) base;
	result = GAMES_TYPE_PLATFORM;
	return result;
}

static guint
games_platform_model_real_get_n_items (GListModel* base)
{
	GamesPlatformModel * self;
	guint result;
	self = (GamesPlatformModel*) base;
	result = (guint) self->priv->n_platforms;
	return result;
}

static void
games_platform_model_game_added (GamesPlatformModel* self,
                                 GamesGame* game)
{
	GamesPlatform* platform = NULL;
	GamesPlatform* _tmp0_;
	GamesPlatform* _tmp1_;
	GamesPlatform* _tmp2_;
	GHashTable* _tmp3_;
	GamesPlatform* _tmp4_;
	gconstpointer _tmp5_;
	GHashTable* _tmp11_;
	GamesPlatform* _tmp12_;
	GamesPlatform* _tmp13_;
	GHashTable* _tmp14_;
	GamesPlatform* _tmp15_;
	gconstpointer _tmp16_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (game != NULL);
	_tmp0_ = games_game_get_platform (game);
	_tmp1_ = _tmp0_;
	_tmp2_ = _g_object_ref0 (_tmp1_);
	platform = _tmp2_;
	_tmp3_ = self->priv->n_games;
	_tmp4_ = platform;
	_tmp5_ = g_hash_table_lookup (_tmp3_, _tmp4_);
	if (((guint) ((guintptr) _tmp5_)) == ((guint) 0)) {
		GSequenceIter* iter = NULL;
		GSequence* _tmp6_;
		GamesPlatform* _tmp7_;
		GamesPlatform* _tmp8_;
		GSequenceIter* _tmp9_;
		GSequenceIter* _tmp10_;
		_tmp6_ = self->priv->sequence;
		_tmp7_ = platform;
		_tmp8_ = _g_object_ref0 (_tmp7_);
		_tmp9_ = g_sequence_insert_sorted (_tmp6_, _tmp8_, _games_platform_compare_gcompare_data_func, NULL);
		iter = _tmp9_;
		_tmp10_ = iter;
		g_list_model_items_changed ((GListModel*) self, (guint) g_sequence_iter_get_position (_tmp10_), (guint) 0, (guint) 1);
	}
	_tmp11_ = self->priv->n_games;
	_tmp12_ = platform;
	_tmp13_ = _g_object_ref0 (_tmp12_);
	_tmp14_ = self->priv->n_games;
	_tmp15_ = platform;
	_tmp16_ = g_hash_table_lookup (_tmp14_, _tmp15_);
	g_hash_table_insert (_tmp11_, _tmp13_, (gpointer) ((guintptr) (((guint) ((guintptr) _tmp16_)) + 1)));
	_g_object_unref0 (platform);
}

static void
games_platform_model_game_removed (GamesPlatformModel* self,
                                   GamesGame* game)
{
	GamesPlatform* platform = NULL;
	GamesPlatform* _tmp0_;
	GamesPlatform* _tmp1_;
	GamesPlatform* _tmp2_;
	GHashTable* _tmp3_;
	GamesPlatform* _tmp4_;
	GamesPlatform* _tmp5_;
	GHashTable* _tmp6_;
	GamesPlatform* _tmp7_;
	gconstpointer _tmp8_;
	GHashTable* _tmp9_;
	GamesPlatform* _tmp10_;
	gconstpointer _tmp11_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (game != NULL);
	_tmp0_ = games_game_get_platform (game);
	_tmp1_ = _tmp0_;
	_tmp2_ = _g_object_ref0 (_tmp1_);
	platform = _tmp2_;
	_tmp3_ = self->priv->n_games;
	_tmp4_ = platform;
	_tmp5_ = _g_object_ref0 (_tmp4_);
	_tmp6_ = self->priv->n_games;
	_tmp7_ = platform;
	_tmp8_ = g_hash_table_lookup (_tmp6_, _tmp7_);
	g_hash_table_insert (_tmp3_, _tmp5_, (gpointer) ((guintptr) (((guint) ((guintptr) _tmp8_)) - 1)));
	_tmp9_ = self->priv->n_games;
	_tmp10_ = platform;
	_tmp11_ = g_hash_table_lookup (_tmp9_, _tmp10_);
	if (((guint) ((guintptr) _tmp11_)) == ((guint) 0)) {
		GSequenceIter* iter = NULL;
		GSequence* _tmp12_;
		GamesPlatform* _tmp13_;
		GSequenceIter* _tmp14_;
		gint pos = 0;
		GSequenceIter* _tmp15_;
		GSequenceIter* _tmp16_;
		_tmp12_ = self->priv->sequence;
		_tmp13_ = platform;
		_tmp14_ = g_sequence_lookup (_tmp12_, _tmp13_, _games_platform_compare_gcompare_data_func, NULL);
		iter = _tmp14_;
		_tmp15_ = iter;
		pos = g_sequence_iter_get_position (_tmp15_);
		_tmp16_ = iter;
		g_sequence_remove (_tmp16_);
		g_list_model_items_changed ((GListModel*) self, (guint) pos, (guint) 1, (guint) 0);
	}
	_g_object_unref0 (platform);
}

static void
games_platform_model_class_init (GamesPlatformModelClass * klass,
                                 gpointer klass_data)
{
	games_platform_model_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GamesPlatformModel_private_offset);
	G_OBJECT_CLASS (klass)->finalize = games_platform_model_finalize;
}

static void
games_platform_model_g_list_model_interface_init (GListModelInterface * iface,
                                                  gpointer iface_data)
{
	games_platform_model_g_list_model_parent_iface = g_type_interface_peek_parent (iface);
	iface->get_item = (GObject* (*) (GListModel*, guint)) games_platform_model_real_get_item;
	iface->get_item_type = (GType (*) (GListModel*)) games_platform_model_real_get_item_type;
	iface->get_n_items = (guint (*) (GListModel*)) games_platform_model_real_get_n_items;
}

static void
games_platform_model_instance_init (GamesPlatformModel * self,
                                    gpointer klass)
{
	self->priv = games_platform_model_get_instance_private (self);
}

static void
games_platform_model_finalize (GObject * obj)
{
	GamesPlatformModel * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GAMES_TYPE_PLATFORM_MODEL, GamesPlatformModel);
	_g_object_unref0 (self->priv->game_model);
	_g_sequence_free0 (self->priv->sequence);
	_g_hash_table_unref0 (self->priv->n_games);
	G_OBJECT_CLASS (games_platform_model_parent_class)->finalize (obj);
}

static GType
games_platform_model_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GamesPlatformModelClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) games_platform_model_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GamesPlatformModel), 0, (GInstanceInitFunc) games_platform_model_instance_init, NULL };
	static const GInterfaceInfo g_list_model_info = { (GInterfaceInitFunc) games_platform_model_g_list_model_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	GType games_platform_model_type_id;
	games_platform_model_type_id = g_type_register_static (G_TYPE_OBJECT, "GamesPlatformModel", &g_define_type_info, 0);
	g_type_add_interface_static (games_platform_model_type_id, g_list_model_get_type (), &g_list_model_info);
	GamesPlatformModel_private_offset = g_type_add_instance_private (games_platform_model_type_id, sizeof (GamesPlatformModelPrivate));
	return games_platform_model_type_id;
}

GType
games_platform_model_get_type (void)
{
	static volatile gsize games_platform_model_type_id__once = 0;
	if (g_once_init_enter (&games_platform_model_type_id__once)) {
		GType games_platform_model_type_id;
		games_platform_model_type_id = games_platform_model_get_type_once ();
		g_once_init_leave (&games_platform_model_type_id__once, games_platform_model_type_id);
	}
	return games_platform_model_type_id__once;
}

