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

/*
* Copyright © 2025 Alain M. (https://github.com/alainm23/planify)
*
* 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, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authored by: Alain M. <alainmh23@gmail.com>
*/

#include <glib-object.h>
#include <glib.h>
#include <gio/gio.h>
#include "planify.h"
#include <json-glib/json-glib.h>
#include <libsoup/soup.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
#if !defined(VALA_EXTERN)
#if defined(_MSC_VER)
#define VALA_EXTERN __declspec(dllexport) extern
#elif __GNUC__ >= 4
#define VALA_EXTERN __attribute__((visibility("default"))) extern
#else
#define VALA_EXTERN extern
#endif
#endif

#define SERVICES_TYPE_API (services_api_get_type ())
#define SERVICES_API(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SERVICES_TYPE_API, ServicesApi))
#define SERVICES_API_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), SERVICES_TYPE_API, ServicesApiClass))
#define SERVICES_IS_API(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SERVICES_TYPE_API))
#define SERVICES_IS_API_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), SERVICES_TYPE_API))
#define SERVICES_API_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), SERVICES_TYPE_API, ServicesApiClass))

typedef struct _ServicesApi ServicesApi;
typedef struct _ServicesApiClass ServicesApiClass;
typedef struct _ServicesApiPrivate ServicesApiPrivate;
enum  {
	SERVICES_API_0_PROPERTY,
	SERVICES_API_NUM_PROPERTIES
};
static GParamSpec* services_api_properties[SERVICES_API_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _Block12Data Block12Data;
#define _json_object_unref0(var) ((var == NULL) ? NULL : (var = (json_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_bytes_unref0(var) ((var == NULL) ? NULL : (var = (g_bytes_unref (var), NULL)))
#define _json_array_unref0(var) ((var == NULL) ? NULL : (var = (json_array_unref (var), NULL)))
typedef struct _ServicesApiGetLatestReleaseData ServicesApiGetLatestReleaseData;

struct _ServicesApi {
	GObject parent_instance;
	ServicesApiPrivate * priv;
};

struct _ServicesApiClass {
	GObjectClass parent_class;
};

struct _Block12Data {
	int _ref_count_;
	ServicesApi* self;
	JsonObject* latest_release_json;
	gint64 latest_timestamp;
	gpointer _async_data_;
};

struct _ServicesApiGetLatestReleaseData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	ServicesApi* self;
	ObjectsRelease* result;
	Block12Data* _data12_;
	SoupSession* session;
	SoupSession* _tmp0_;
	SoupMessage* message;
	SoupMessage* _tmp1_;
	GBytes* response;
	SoupSession* _tmp2_;
	SoupMessage* _tmp3_;
	GBytes* _tmp4_;
	gchar* json_string;
	GBytes* _tmp5_;
	gsize _tmp6_;
	guint8* _tmp7_;
	gchar* _tmp8_;
	JsonParser* parser;
	JsonParser* _tmp9_;
	JsonParser* _tmp10_;
	const gchar* _tmp11_;
	JsonObject* root_object;
	JsonParser* _tmp12_;
	JsonNode* _tmp13_;
	JsonObject* _tmp14_;
	JsonObject* _tmp15_;
	JsonObject* _tmp16_;
	JsonArray* releases_array;
	JsonObject* _tmp17_;
	JsonArray* _tmp18_;
	JsonArray* _tmp19_;
	JsonArray* _tmp20_;
	JsonArray* _tmp21_;
	ObjectsRelease* _tmp22_;
	JsonObject* _tmp23_;
	JsonObject* _tmp24_;
	ObjectsRelease* _tmp25_;
	ObjectsRelease* _tmp26_;
	GError* _inner_error0_;
};

static gpointer services_api_parent_class = NULL;
static ServicesApi* services_api__instance;
static ServicesApi* services_api__instance = NULL;

VALA_EXTERN GType services_api_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ServicesApi, g_object_unref)
VALA_EXTERN ServicesApi* services_api_get_default (void);
VALA_EXTERN ServicesApi* services_api_new (void);
VALA_EXTERN ServicesApi* services_api_construct (GType object_type);
static void services_api_get_latest_release_data_free (gpointer _data);
VALA_EXTERN void services_api_get_latest_release (ServicesApi* self,
                                      GAsyncReadyCallback _callback_,
                                      gpointer _user_data_);
VALA_EXTERN ObjectsRelease* services_api_get_latest_release_finish (ServicesApi* self,
                                                        GAsyncResult* _res_,
                                                        GError** error);
static gboolean services_api_get_latest_release_co (ServicesApiGetLatestReleaseData* _data_);
static Block12Data* block12_data_ref (Block12Data* _data12_);
static void block12_data_unref (void * _userdata_);
static void services_api_get_latest_release_ready (GObject* source_object,
                                            GAsyncResult* _res_,
                                            gpointer _user_data_);
static void __lambda467_ (Block12Data* _data12_,
                   JsonArray* array,
                   guint index,
                   JsonNode* element);
static void ___lambda467__json_array_foreach (JsonArray* array,
                                       guint index_,
                                       JsonNode* element_node,
                                       gpointer self);
static void services_api_finalize (GObject * obj);
static GType services_api_get_type_once (void);

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

ServicesApi*
services_api_get_default (void)
{
	ServicesApi* _tmp0_;
	ServicesApi* _tmp2_;
	ServicesApi* _tmp3_;
	ServicesApi* result;
	_tmp0_ = services_api__instance;
	if (_tmp0_ == NULL) {
		ServicesApi* _tmp1_;
		_tmp1_ = services_api_new ();
		_g_object_unref0 (services_api__instance);
		services_api__instance = _tmp1_;
	}
	_tmp2_ = services_api__instance;
	_tmp3_ = _g_object_ref0 (_tmp2_);
	result = _tmp3_;
	return result;
}

static void
services_api_get_latest_release_data_free (gpointer _data)
{
	ServicesApiGetLatestReleaseData* _data_;
	_data_ = _data;
	_g_object_unref0 (_data_->result);
	_g_object_unref0 (_data_->self);
	g_slice_free (ServicesApiGetLatestReleaseData, _data_);
}

void
services_api_get_latest_release (ServicesApi* self,
                                 GAsyncReadyCallback _callback_,
                                 gpointer _user_data_)
{
	ServicesApiGetLatestReleaseData* _data_;
	ServicesApi* _tmp0_;
	g_return_if_fail (self != NULL);
	_data_ = g_slice_new0 (ServicesApiGetLatestReleaseData);
	_data_->_async_result = g_task_new (G_OBJECT (self), NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, services_api_get_latest_release_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	services_api_get_latest_release_co (_data_);
}

ObjectsRelease*
services_api_get_latest_release_finish (ServicesApi* self,
                                        GAsyncResult* _res_,
                                        GError** error)
{
	ObjectsRelease* result;
	ServicesApiGetLatestReleaseData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), error);
	if (NULL == _data_) {
		return NULL;
	}
	result = _data_->result;
	_data_->result = NULL;
	return result;
}

static Block12Data*
block12_data_ref (Block12Data* _data12_)
{
	g_atomic_int_inc (&_data12_->_ref_count_);
	return _data12_;
}

static void
block12_data_unref (void * _userdata_)
{
	Block12Data* _data12_;
	_data12_ = (Block12Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data12_->_ref_count_)) {
		ServicesApi* self;
		self = _data12_->self;
		_json_object_unref0 (_data12_->latest_release_json);
		_g_object_unref0 (self);
		g_slice_free (Block12Data, _data12_);
	}
}

static void
services_api_get_latest_release_ready (GObject* source_object,
                                       GAsyncResult* _res_,
                                       gpointer _user_data_)
{
	ServicesApiGetLatestReleaseData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	services_api_get_latest_release_co (_data_);
}

static gpointer
_json_object_ref0 (gpointer self)
{
	return self ? json_object_ref (self) : NULL;
}

static gpointer
_json_array_ref0 (gpointer self)
{
	return self ? json_array_ref (self) : NULL;
}

static gint64
int64_parse (const gchar* str,
             guint _base)
{
	gint64 result;
	g_return_val_if_fail (str != NULL, -1LL);
	result = g_ascii_strtoll (str, NULL, _base);
	return result;
}

static void
__lambda467_ (Block12Data* _data12_,
              JsonArray* array,
              guint index,
              JsonNode* element)
{
	ServicesApi* self;
	JsonObject* release_json = NULL;
	JsonObject* _tmp0_;
	JsonObject* _tmp1_;
	gint64 timestamp = 0LL;
	JsonObject* _tmp2_;
	const gchar* _tmp3_;
	self = _data12_->self;
	g_return_if_fail (array != NULL);
	g_return_if_fail (element != NULL);
	_tmp0_ = json_node_get_object (element);
	_tmp1_ = _json_object_ref0 (_tmp0_);
	release_json = _tmp1_;
	_tmp2_ = release_json;
	_tmp3_ = json_object_get_string_member (_tmp2_, "timestamp");
	timestamp = int64_parse (_tmp3_, (guint) 0);
	if (timestamp > _data12_->latest_timestamp) {
		JsonObject* _tmp4_;
		JsonObject* _tmp5_;
		_data12_->latest_timestamp = timestamp;
		_tmp4_ = release_json;
		_tmp5_ = _json_object_ref0 (_tmp4_);
		_json_object_unref0 (_data12_->latest_release_json);
		_data12_->latest_release_json = _tmp5_;
	}
	_json_object_unref0 (release_json);
}

static void
___lambda467__json_array_foreach (JsonArray* array,
                                  guint index_,
                                  JsonNode* element_node,
                                  gpointer self)
{
	__lambda467_ (self, array, index_, element_node);
}

static gboolean
services_api_get_latest_release_co (ServicesApiGetLatestReleaseData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_data12_ = g_slice_new0 (Block12Data);
	_data_->_data12_->_ref_count_ = 1;
	_data_->_data12_->self = g_object_ref (_data_->self);
	_data_->_data12_->_async_data_ = _data_;
	_data_->_tmp0_ = soup_session_new ();
	_data_->session = _data_->_tmp0_;
	_data_->_tmp1_ = soup_message_new ("GET", "https://flathub.org/api/v2/appstream/io.github.alainm23.planify?locale" \
"=en");
	_data_->message = _data_->_tmp1_;
	_data_->_tmp2_ = _data_->session;
	_data_->_tmp3_ = _data_->message;
	_data_->_state_ = 1;
	soup_session_send_and_read_async (_data_->_tmp2_, _data_->_tmp3_, G_PRIORITY_DEFAULT, NULL, services_api_get_latest_release_ready, _data_);
	return FALSE;
	_state_1:
	_data_->_tmp4_ = soup_session_send_and_read_finish (_data_->_tmp2_, _data_->_res_, &_data_->_inner_error0_);
	_data_->response = _data_->_tmp4_;
	if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
		g_task_return_error (_data_->_async_result, _data_->_inner_error0_);
		_g_object_unref0 (_data_->message);
		_g_object_unref0 (_data_->session);
		block12_data_unref (_data_->_data12_);
		_data_->_data12_ = NULL;
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_data_->_tmp5_ = _data_->response;
	_data_->_tmp6_ = 0;
	_data_->_tmp7_ = g_bytes_get_data (_data_->_tmp5_, &_data_->_tmp6_);
	_data_->_tmp8_ = g_strdup ((const gchar*) _data_->_tmp7_);
	_data_->json_string = _data_->_tmp8_;
	_data_->_tmp9_ = json_parser_new ();
	_data_->parser = _data_->_tmp9_;
	_data_->_tmp10_ = _data_->parser;
	_data_->_tmp11_ = _data_->json_string;
	json_parser_load_from_data (_data_->_tmp10_, _data_->_tmp11_, (gssize) -1, &_data_->_inner_error0_);
	if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
		g_task_return_error (_data_->_async_result, _data_->_inner_error0_);
		_g_object_unref0 (_data_->parser);
		_g_free0 (_data_->json_string);
		_g_bytes_unref0 (_data_->response);
		_g_object_unref0 (_data_->message);
		_g_object_unref0 (_data_->session);
		block12_data_unref (_data_->_data12_);
		_data_->_data12_ = NULL;
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_data_->_tmp12_ = _data_->parser;
	_data_->_tmp13_ = json_parser_get_root (_data_->_tmp12_);
	_data_->_tmp14_ = json_node_get_object (_data_->_tmp13_);
	_data_->_tmp15_ = _json_object_ref0 (_data_->_tmp14_);
	_data_->root_object = _data_->_tmp15_;
	_data_->_tmp16_ = _data_->root_object;
	if (!json_object_has_member (_data_->_tmp16_, "releases")) {
		_data_->result = NULL;
		_json_object_unref0 (_data_->root_object);
		_g_object_unref0 (_data_->parser);
		_g_free0 (_data_->json_string);
		_g_bytes_unref0 (_data_->response);
		_g_object_unref0 (_data_->message);
		_g_object_unref0 (_data_->session);
		block12_data_unref (_data_->_data12_);
		_data_->_data12_ = NULL;
		g_task_return_pointer (_data_->_async_result, _data_, NULL);
		if (_data_->_state_ != 0) {
			while (!g_task_get_completed (_data_->_async_result)) {
				g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
			}
		}
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_data_->_tmp17_ = _data_->root_object;
	_data_->_tmp18_ = json_object_get_array_member (_data_->_tmp17_, "releases");
	_data_->_tmp19_ = _json_array_ref0 (_data_->_tmp18_);
	_data_->releases_array = _data_->_tmp19_;
	_data_->_tmp20_ = _data_->releases_array;
	if (json_array_get_length (_data_->_tmp20_) == ((guint) 0)) {
		_data_->result = NULL;
		_json_array_unref0 (_data_->releases_array);
		_json_object_unref0 (_data_->root_object);
		_g_object_unref0 (_data_->parser);
		_g_free0 (_data_->json_string);
		_g_bytes_unref0 (_data_->response);
		_g_object_unref0 (_data_->message);
		_g_object_unref0 (_data_->session);
		block12_data_unref (_data_->_data12_);
		_data_->_data12_ = NULL;
		g_task_return_pointer (_data_->_async_result, _data_, NULL);
		if (_data_->_state_ != 0) {
			while (!g_task_get_completed (_data_->_async_result)) {
				g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
			}
		}
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_data_->_data12_->latest_release_json = NULL;
	_data_->_data12_->latest_timestamp = (gint64) 0;
	_data_->_tmp21_ = _data_->releases_array;
	json_array_foreach_element (_data_->_tmp21_, ___lambda467__json_array_foreach, _data_->_data12_);
	_data_->_tmp23_ = _data_->_data12_->latest_release_json;
	if (_data_->_tmp23_ != NULL) {
		_data_->_tmp24_ = _data_->_data12_->latest_release_json;
		_data_->_tmp25_ = objects_release_new_from_json (_data_->_tmp24_);
		_g_object_unref0 (_data_->_tmp22_);
		_data_->_tmp22_ = _data_->_tmp25_;
	} else {
		_g_object_unref0 (_data_->_tmp22_);
		_data_->_tmp22_ = NULL;
	}
	_data_->_tmp26_ = _data_->_tmp22_;
	_data_->_tmp22_ = NULL;
	_data_->result = _data_->_tmp26_;
	_g_object_unref0 (_data_->_tmp22_);
	_json_array_unref0 (_data_->releases_array);
	_json_object_unref0 (_data_->root_object);
	_g_object_unref0 (_data_->parser);
	_g_free0 (_data_->json_string);
	_g_bytes_unref0 (_data_->response);
	_g_object_unref0 (_data_->message);
	_g_object_unref0 (_data_->session);
	block12_data_unref (_data_->_data12_);
	_data_->_data12_ = NULL;
	g_task_return_pointer (_data_->_async_result, _data_, NULL);
	if (_data_->_state_ != 0) {
		while (!g_task_get_completed (_data_->_async_result)) {
			g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
		}
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}

ServicesApi*
services_api_construct (GType object_type)
{
	ServicesApi * self = NULL;
	self = (ServicesApi*) g_object_new (object_type, NULL);
	return self;
}

ServicesApi*
services_api_new (void)
{
	return services_api_construct (SERVICES_TYPE_API);
}

static void
services_api_class_init (ServicesApiClass * klass,
                         gpointer klass_data)
{
	services_api_parent_class = g_type_class_peek_parent (klass);
	G_OBJECT_CLASS (klass)->finalize = services_api_finalize;
}

static void
services_api_instance_init (ServicesApi * self,
                            gpointer klass)
{
}

static void
services_api_finalize (GObject * obj)
{
	ServicesApi * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, SERVICES_TYPE_API, ServicesApi);
	G_OBJECT_CLASS (services_api_parent_class)->finalize (obj);
}

static GType
services_api_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ServicesApiClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) services_api_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ServicesApi), 0, (GInstanceInitFunc) services_api_instance_init, NULL };
	GType services_api_type_id;
	services_api_type_id = g_type_register_static (G_TYPE_OBJECT, "ServicesApi", &g_define_type_info, 0);
	return services_api_type_id;
}

GType
services_api_get_type (void)
{
	static volatile gsize services_api_type_id__once = 0;
	if (g_once_init_enter (&services_api_type_id__once)) {
		GType services_api_type_id;
		services_api_type_id = services_api_get_type_once ();
		g_once_init_leave (&services_api_type_id__once, services_api_type_id);
	}
	return services_api_type_id__once;
}

