/*
 * Purple - Internet Messaging Library
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
 */

#include <glib.h>

#include <birb.h>

#include <purple.h>

/******************************************************************************
 * Add/Remove Test
 *****************************************************************************/
static void
test_purple_presence_manager_seagull_backend_load_empty_cb(GObject *source,
                                                           GAsyncResult *result,
                                                           G_GNUC_UNUSED gpointer data)
{
	PurplePresenceManagerBackend *backend = NULL;
	GError *error = NULL;
	GListModel *model = NULL;

	birb_assert_type(source, PURPLE_TYPE_PRESENCE_MANAGER_SEAGULL_BACKEND);
	backend = PURPLE_PRESENCE_MANAGER_BACKEND(source);

	model = purple_presence_manager_backend_load_saved_presences_finish(backend,
	                                                                    result,
	                                                                    &error);
	g_assert_no_error(error);

	g_assert_true(G_IS_LIST_MODEL(model));
	birb_assert_list_model_n_items(model, 0);

	g_clear_object(&model);
}

static void
test_purple_presence_manager_seagull_backend_load_single_cb(GObject *source,
                                                            GAsyncResult *result,
                                                            gpointer data)
{
	PurplePresenceManagerBackend *backend = NULL;
	PurpleSavedPresence *actual = NULL;
	PurpleSavedPresence *expected = data;
	GError *error = NULL;
	GListModel *model = NULL;

	birb_assert_type(source, PURPLE_TYPE_PRESENCE_MANAGER_SEAGULL_BACKEND);
	backend = PURPLE_PRESENCE_MANAGER_BACKEND(source);

	model = purple_presence_manager_backend_load_saved_presences_finish(backend,
	                                                                    result,
	                                                                    &error);
	g_assert_no_error(error);

	g_assert_true(G_IS_LIST_MODEL(model));
	birb_assert_list_model_n_items(model, 1);

	actual = g_list_model_get_item(model, 0);
	birb_assert_type(actual, PURPLE_TYPE_SAVED_PRESENCE);

	g_assert_true(purple_saved_presence_equal(actual, expected));
	g_clear_object(&actual);

	g_clear_object(&model);
}


static void
test_purple_presence_manager_seagull_backend_save_cb(GObject *source,
                                                     GAsyncResult *result,
                                                     gpointer data)
{
	PurplePresenceManagerBackend *backend = NULL;
	GError *error = NULL;
	gboolean success = FALSE;

	birb_assert_type(source, PURPLE_TYPE_PRESENCE_MANAGER_SEAGULL_BACKEND);
	backend = PURPLE_PRESENCE_MANAGER_BACKEND(source);

	success = purple_presence_manager_backend_save_saved_presence_finish(backend,
	                                                                     result,
	                                                                     &error);
	g_assert_no_error(error);
	g_assert_true(success);

	purple_presence_manager_backend_load_saved_presences_async(backend,
	                                                           NULL,
	                                                           test_purple_presence_manager_seagull_backend_load_single_cb,
	                                                           data);

	g_main_context_iteration(NULL, FALSE);
}

static void
test_purple_presence_manager_seagull_backend_delete_cb(GObject *source,
                                                       GAsyncResult *result,
                                                       G_GNUC_UNUSED gpointer data)
{
	PurplePresenceManagerBackend *backend = NULL;
	GError *error = NULL;
	gboolean success = FALSE;

	birb_assert_type(source, PURPLE_TYPE_PRESENCE_MANAGER_SEAGULL_BACKEND);
	backend = PURPLE_PRESENCE_MANAGER_BACKEND(source);

	success = purple_presence_manager_backend_delete_saved_presence_finish(backend,
	                                                                       result,
	                                                                       &error);
	g_assert_no_error(error);
	g_assert_true(success);

	purple_presence_manager_backend_load_saved_presences_async(backend,
	                                                           NULL,
	                                                           test_purple_presence_manager_seagull_backend_load_empty_cb,
	                                                           NULL);

	g_main_context_iteration(NULL, FALSE);
}

static void
test_purple_presence_manager_seagull_backend_add_remove(void) {
	PurplePresenceManagerBackend *backend = NULL;
	PurpleSavedPresence *saved_presence = NULL;
	GDateTime *dt = NULL;
	GError *error = NULL;

	backend = purple_presence_manager_seagull_backend_new(NULL, &error);
	g_assert_no_error(error);

	birb_assert_type(backend, PURPLE_TYPE_PRESENCE_MANAGER_SEAGULL_BACKEND);

	/* Create our saved presence for testing. */
	dt = g_date_time_new_from_iso8601("1999-02-19T00:00:00Z", NULL);
	saved_presence = g_object_new(
		PURPLE_TYPE_SAVED_PRESENCE,
		"emoji", "⎙",
		"id", "tt0151804",
		"last-used", dt,
		"message", "PC LOAD LETTER",
		"name", "Space Office",
		"primitive", PURPLE_PRESENCE_PRIMITIVE_STREAMING,
		NULL);
	g_clear_pointer(&dt, g_date_time_unref);

	/* Make sure that there's nothing to load. */
	purple_presence_manager_backend_load_saved_presences_async(backend, NULL,
	                                                           test_purple_presence_manager_seagull_backend_load_empty_cb,
	                                                           NULL);

	g_main_context_iteration(NULL, FALSE);

	/* Save the presence. */
	purple_presence_manager_backend_save_saved_presence_async(backend,
	                                                          saved_presence,
	                                                          NULL,
	                                                          test_purple_presence_manager_seagull_backend_save_cb,
	                                                          saved_presence);

	g_main_context_iteration(NULL, FALSE);

	/* Delete the presence. */
	purple_presence_manager_backend_delete_saved_presence_async(backend,
	                                                            saved_presence,
	                                                            NULL,
	                                                            test_purple_presence_manager_seagull_backend_delete_cb,
	                                                            NULL);

	g_main_context_iteration(NULL, FALSE);

	/* Clean up */
	g_assert_finalize_object(saved_presence);
	g_assert_finalize_object(backend);
}

/******************************************************************************
 * Main
 *****************************************************************************/
int
main(int argc, char *argv[]) {
	g_test_init(&argc, &argv, NULL);
	g_test_set_nonfatal_assertions();

	g_test_add_func("/presence-manager-seagull-backend/add-remove",
	                test_purple_presence_manager_seagull_backend_add_remove);

	return g_test_run();
}
