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

#include "libdino.h"
#include <glib-object.h>
#include <glib.h>
#include <gee.h>
#include "qlite.h"
#include "xmpp-vala.h"
#include <stdlib.h>
#include <string.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  {
	DINO_CALL_STORE_0_PROPERTY,
	DINO_CALL_STORE_ID_PROPERTY,
	DINO_CALL_STORE_NUM_PROPERTIES
};
static GParamSpec* dino_call_store_properties[DINO_CALL_STORE_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _qlite_database_unref0(var) ((var == NULL) ? NULL : (var = (qlite_database_unref (var), NULL)))
#define _qlite_statement_builder_unref0(var) ((var == NULL) ? NULL : (var = (qlite_statement_builder_unref (var), NULL)))
#define _qlite_row_option_unref0(var) ((var == NULL) ? NULL : (var = (qlite_row_option_unref (var), NULL)))
#define _xmpp_jid_unref0(var) ((var == NULL) ? NULL : (var = (xmpp_jid_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))

struct _DinoCallStorePrivate {
	DinoStreamInteractor* stream_interactor;
	DinoDatabase* db;
	WeakMap* calls_by_db_id;
};

static gint DinoCallStore_private_offset;
static gpointer dino_call_store_parent_class = NULL;
DinoModuleIdentity* dino_call_store_IDENTITY = NULL;
static DinoStreamInteractionModuleIface * dino_call_store_dino_stream_interaction_module_parent_iface = NULL;

static DinoCallStore* dino_call_store_new (DinoStreamInteractor* stream_interactor,
                                    DinoDatabase* db);
static DinoCallStore* dino_call_store_construct (GType object_type,
                                          DinoStreamInteractor* stream_interactor,
                                          DinoDatabase* db);
static void dino_call_store_cache_call (DinoCallStore* self,
                                 DinoEntitiesCall* call);
static DinoEntitiesCall* dino_call_store_create_call_from_row_opt (DinoCallStore* self,
                                                            QliteRowOption* row_opt,
                                                            DinoEntitiesConversation* conversation);
static void dino_call_store_finalize (GObject * obj);
static GType dino_call_store_get_type_once (void);
static void _vala_dino_call_store_get_property (GObject * object,
                                         guint property_id,
                                         GValue * value,
                                         GParamSpec * pspec);

static inline gpointer
dino_call_store_get_instance_private (DinoCallStore* self)
{
	return G_STRUCT_MEMBER_P (self, DinoCallStore_private_offset);
}

void
dino_call_store_start (DinoStreamInteractor* stream_interactor,
                       DinoDatabase* db)
{
	DinoCallStore* m = NULL;
	DinoCallStore* _tmp0_;
	g_return_if_fail (stream_interactor != NULL);
	g_return_if_fail (db != NULL);
	_tmp0_ = dino_call_store_new (stream_interactor, db);
	m = _tmp0_;
	dino_stream_interactor_add_module (stream_interactor, (DinoStreamInteractionModule*) m);
	_g_object_unref0 (m);
}

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

static gpointer
_qlite_database_ref0 (gpointer self)
{
	return self ? qlite_database_ref (self) : NULL;
}

static DinoCallStore*
dino_call_store_construct (GType object_type,
                           DinoStreamInteractor* stream_interactor,
                           DinoDatabase* db)
{
	DinoCallStore * self = NULL;
	DinoStreamInteractor* _tmp0_;
	DinoDatabase* _tmp1_;
	g_return_val_if_fail (stream_interactor != NULL, NULL);
	g_return_val_if_fail (db != NULL, NULL);
	self = (DinoCallStore*) g_object_new (object_type, NULL);
	_tmp0_ = _g_object_ref0 (stream_interactor);
	_g_object_unref0 (self->priv->stream_interactor);
	self->priv->stream_interactor = _tmp0_;
	_tmp1_ = _qlite_database_ref0 (db);
	_qlite_database_unref0 (self->priv->db);
	self->priv->db = _tmp1_;
	return self;
}

static DinoCallStore*
dino_call_store_new (DinoStreamInteractor* stream_interactor,
                     DinoDatabase* db)
{
	return dino_call_store_construct (DINO_TYPE_CALL_STORE, stream_interactor, db);
}

void
dino_call_store_add_call (DinoCallStore* self,
                          DinoEntitiesCall* call,
                          DinoEntitiesConversation* conversation)
{
	DinoDatabase* _tmp0_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (call != NULL);
	g_return_if_fail (conversation != NULL);
	_tmp0_ = self->priv->db;
	dino_entities_call_persist (call, _tmp0_);
	dino_call_store_cache_call (self, call);
}

DinoEntitiesCall*
dino_call_store_get_call_by_id (DinoCallStore* self,
                                gint id,
                                DinoEntitiesConversation* conversation)
{
	DinoEntitiesCall* call = NULL;
	WeakMap* _tmp0_;
	gpointer _tmp1_;
	DinoEntitiesCall* _tmp2_;
	QliteRowOption* row_option = NULL;
	DinoDatabase* _tmp3_;
	DinoDatabaseCallTable* _tmp4_;
	DinoDatabaseCallTable* _tmp5_;
	QliteQueryBuilder* _tmp6_;
	QliteQueryBuilder* _tmp7_;
	DinoDatabase* _tmp8_;
	DinoDatabaseCallTable* _tmp9_;
	DinoDatabaseCallTable* _tmp10_;
	QliteColumn* _tmp11_;
	QliteQueryBuilder* _tmp12_;
	QliteQueryBuilder* _tmp13_;
	QliteRowOption* _tmp14_;
	QliteRowOption* _tmp15_;
	QliteRowOption* _tmp16_;
	DinoEntitiesCall* _tmp17_;
	DinoEntitiesCall* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (conversation != NULL, NULL);
	_tmp0_ = self->priv->calls_by_db_id;
	_tmp1_ = gee_abstract_map_get ((GeeAbstractMap*) _tmp0_, (gpointer) ((gintptr) id));
	call = (DinoEntitiesCall*) _tmp1_;
	_tmp2_ = call;
	if (_tmp2_ != NULL) {
		result = call;
		return result;
	}
	_tmp3_ = self->priv->db;
	_tmp4_ = dino_database_get_call (_tmp3_);
	_tmp5_ = _tmp4_;
	_tmp6_ = qlite_table_select ((QliteTable*) _tmp5_, NULL, (gint) 0);
	_tmp7_ = _tmp6_;
	_tmp8_ = self->priv->db;
	_tmp9_ = dino_database_get_call (_tmp8_);
	_tmp10_ = _tmp9_;
	_tmp11_ = _tmp10_->id;
	_tmp12_ = qlite_query_builder_with (_tmp7_, G_TYPE_INT, NULL, NULL, _tmp11_, "=", (gpointer) ((gintptr) id));
	_tmp13_ = _tmp12_;
	_tmp14_ = qlite_query_builder_row (_tmp13_);
	_tmp15_ = _tmp14_;
	_qlite_statement_builder_unref0 (_tmp13_);
	_qlite_statement_builder_unref0 (_tmp7_);
	row_option = _tmp15_;
	_tmp16_ = row_option;
	_tmp17_ = dino_call_store_create_call_from_row_opt (self, _tmp16_, conversation);
	result = _tmp17_;
	_qlite_row_option_unref0 (row_option);
	_g_object_unref0 (call);
	return result;
}

static DinoEntitiesCall*
dino_call_store_create_call_from_row_opt (DinoCallStore* self,
                                          QliteRowOption* row_opt,
                                          DinoEntitiesConversation* conversation)
{
	GError* _inner_error0_ = NULL;
	DinoEntitiesCall* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (row_opt != NULL, NULL);
	g_return_val_if_fail (conversation != NULL, NULL);
	if (!qlite_row_option_is_present (row_opt)) {
		result = NULL;
		return result;
	}
	{
		DinoEntitiesCall* call = NULL;
		DinoDatabase* _tmp0_;
		QliteRow* _tmp1_;
		QliteRow* _tmp2_;
		DinoEntitiesCall* _tmp3_;
		DinoEntitiesConversationType _tmp4_;
		DinoEntitiesConversationType _tmp5_;
		DinoEntitiesCall* _tmp15_;
		_tmp0_ = self->priv->db;
		_tmp1_ = qlite_row_option_get_inner (row_opt);
		_tmp2_ = _tmp1_;
		_tmp3_ = dino_entities_call_new_from_row (_tmp0_, _tmp2_, &_inner_error0_);
		call = _tmp3_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if (_inner_error0_->domain == XMPP_INVALID_JID_ERROR) {
				goto __catch0_xmpp_invalid_jid_error;
			}
			g_critical ("file %s: line %d: unexpected 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 NULL;
		}
		_tmp4_ = dino_entities_conversation_get_type_ (conversation);
		_tmp5_ = _tmp4_;
		if (dino_entities_conversation_type_is_muc_semantic (_tmp5_)) {
			XmppJid* _tmp6_ = NULL;
			XmppJid* _tmp7_;
			XmppJid* _tmp8_;
			DinoEntitiesCall* _tmp9_;
			XmppJid* _tmp10_;
			XmppJid* _tmp11_;
			const gchar* _tmp12_;
			XmppJid* _tmp13_;
			DinoEntitiesCall* _tmp14_;
			_tmp7_ = dino_entities_conversation_get_counterpart (conversation);
			_tmp8_ = _tmp7_;
			_tmp9_ = call;
			_tmp10_ = dino_entities_call_get_ourpart (_tmp9_);
			_tmp11_ = _tmp10_;
			_tmp12_ = _tmp11_->resourcepart;
			_tmp13_ = xmpp_jid_with_resource (_tmp8_, _tmp12_, &_inner_error0_);
			_tmp6_ = _tmp13_;
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				_g_object_unref0 (call);
				if (_inner_error0_->domain == XMPP_INVALID_JID_ERROR) {
					goto __catch0_xmpp_invalid_jid_error;
				}
				g_critical ("file %s: line %d: unexpected 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 NULL;
			}
			_tmp14_ = call;
			dino_entities_call_set_ourpart (_tmp14_, _tmp6_);
			_xmpp_jid_unref0 (_tmp6_);
		}
		_tmp15_ = call;
		dino_call_store_cache_call (self, _tmp15_);
		result = call;
		return result;
	}
	goto __finally0;
	__catch0_xmpp_invalid_jid_error:
	{
		GError* e = NULL;
		GError* _tmp16_;
		const gchar* _tmp17_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp16_ = e;
		_tmp17_ = _tmp16_->message;
		g_warning ("call_store.vala:55: Got message with invalid Jid: %s", _tmp17_);
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		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 NULL;
	}
	result = NULL;
	return result;
}

static void
dino_call_store_cache_call (DinoCallStore* self,
                            DinoEntitiesCall* call)
{
	WeakMap* _tmp0_;
	gint _tmp1_;
	gint _tmp2_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (call != NULL);
	_tmp0_ = self->priv->calls_by_db_id;
	_tmp1_ = dino_entities_call_get_id (call);
	_tmp2_ = _tmp1_;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp0_, (gpointer) ((gintptr) _tmp2_), call);
}

static const gchar*
dino_call_store_real_get_id (DinoStreamInteractionModule* base)
{
	const gchar* result;
	DinoCallStore* self;
	DinoModuleIdentity* _tmp0_;
	const gchar* _tmp1_;
	const gchar* _tmp2_;
	self = (DinoCallStore*) base;
	_tmp0_ = dino_call_store_IDENTITY;
	_tmp1_ = dino_module_identity_get_id (_tmp0_);
	_tmp2_ = _tmp1_;
	result = _tmp2_;
	return result;
}

static void
dino_call_store_class_init (DinoCallStoreClass * klass,
                            gpointer klass_data)
{
	DinoModuleIdentity* _tmp0_;
	dino_call_store_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &DinoCallStore_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_dino_call_store_get_property;
	G_OBJECT_CLASS (klass)->finalize = dino_call_store_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), DINO_CALL_STORE_ID_PROPERTY, dino_call_store_properties[DINO_CALL_STORE_ID_PROPERTY] = g_param_spec_string ("id", "id", "id", NULL, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
	_tmp0_ = dino_module_identity_new (DINO_TYPE_CALL_STORE, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, "call_store");
	dino_call_store_IDENTITY = _tmp0_;
}

static void
dino_call_store_dino_stream_interaction_module_interface_init (DinoStreamInteractionModuleIface * iface,
                                                               gpointer iface_data)
{
	dino_call_store_dino_stream_interaction_module_parent_iface = g_type_interface_peek_parent (iface);
	iface->get_id = dino_call_store_real_get_id;
}

static void
dino_call_store_instance_init (DinoCallStore * self,
                               gpointer klass)
{
	WeakMap* _tmp0_;
	self->priv = dino_call_store_get_instance_private (self);
	_tmp0_ = weak_map_new (G_TYPE_INT, NULL, NULL, DINO_ENTITIES_TYPE_CALL, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
	self->priv->calls_by_db_id = _tmp0_;
}

static void
dino_call_store_finalize (GObject * obj)
{
	DinoCallStore * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, DINO_TYPE_CALL_STORE, DinoCallStore);
	_g_object_unref0 (self->priv->stream_interactor);
	_qlite_database_unref0 (self->priv->db);
	_g_object_unref0 (self->priv->calls_by_db_id);
	G_OBJECT_CLASS (dino_call_store_parent_class)->finalize (obj);
}

static GType
dino_call_store_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (DinoCallStoreClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) dino_call_store_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (DinoCallStore), 0, (GInstanceInitFunc) dino_call_store_instance_init, NULL };
	static const GInterfaceInfo dino_stream_interaction_module_info = { (GInterfaceInitFunc) dino_call_store_dino_stream_interaction_module_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	GType dino_call_store_type_id;
	dino_call_store_type_id = g_type_register_static (G_TYPE_OBJECT, "DinoCallStore", &g_define_type_info, 0);
	g_type_add_interface_static (dino_call_store_type_id, DINO_TYPE_STREAM_INTERACTION_MODULE, &dino_stream_interaction_module_info);
	DinoCallStore_private_offset = g_type_add_instance_private (dino_call_store_type_id, sizeof (DinoCallStorePrivate));
	return dino_call_store_type_id;
}

GType
dino_call_store_get_type (void)
{
	static volatile gsize dino_call_store_type_id__once = 0;
	if (g_once_init_enter (&dino_call_store_type_id__once)) {
		GType dino_call_store_type_id;
		dino_call_store_type_id = dino_call_store_get_type_once ();
		g_once_init_leave (&dino_call_store_type_id__once, dino_call_store_type_id);
	}
	return dino_call_store_type_id__once;
}

static void
_vala_dino_call_store_get_property (GObject * object,
                                    guint property_id,
                                    GValue * value,
                                    GParamSpec * pspec)
{
	DinoCallStore * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, DINO_TYPE_CALL_STORE, DinoCallStore);
	switch (property_id) {
		case DINO_CALL_STORE_ID_PROPERTY:
		g_value_set_string (value, dino_stream_interaction_module_get_id ((DinoStreamInteractionModule*) self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

