/*
 * 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>

/******************************************************************************
 * Normal PurplePresenceManagerBackend Implementation
 *****************************************************************************/
#define TEST_PURPLE_TYPE_PRESENCE_MANAGER_BACKEND (test_purple_presence_manager_backend_get_type())

G_DECLARE_FINAL_TYPE(TestPurplePresenceManagerBackend,
                     test_purple_presence_manager_backend,
                     TEST_PURPLE,
                     PRESENCE_MANAGER_BACKEND,
                     PurplePresenceManagerBackend);

struct _TestPurplePresenceManagerBackend {
	PurplePresenceManagerBackend parent;

	guint delete_saved_presence_async;
	guint delete_saved_presence_finish;
	guint load_saved_presences_async;
	guint load_saved_presences_finish;
	guint save_saved_presence_async;
	guint save_saved_presence_finish;
};

G_DEFINE_FINAL_TYPE(TestPurplePresenceManagerBackend,
                    test_purple_presence_manager_backend,
                    PURPLE_TYPE_PRESENCE_MANAGER_BACKEND);

static void
test_purple_presence_manager_backend_delete_saved_presence_async(PurplePresenceManagerBackend *backend,
                                                                 PurpleSavedPresence *saved_presence,
                                                                 GCancellable *cancellable,
                                                                 GAsyncReadyCallback callback,
                                                                 gpointer data)
{
	TestPurplePresenceManagerBackend *test_backend = NULL;
	GTask *task = NULL;

	birb_assert_type(saved_presence, PURPLE_TYPE_SAVED_PRESENCE);

	test_backend = TEST_PURPLE_PRESENCE_MANAGER_BACKEND(backend);

	task = g_task_new(G_OBJECT(backend), cancellable, callback, data);
	g_task_return_boolean(task, TRUE);

	g_clear_object(&task);

	test_backend->delete_saved_presence_async += 1;
}

static gboolean
test_purple_presence_manager_backend_delete_saved_presence_finish(PurplePresenceManagerBackend *backend,
                                                                  GAsyncResult *result,
                                                                  GError **error)
{
	TestPurplePresenceManagerBackend *test_backend = NULL;

	test_backend = TEST_PURPLE_PRESENCE_MANAGER_BACKEND(backend);
	test_backend->delete_saved_presence_finish += 1;

	return g_task_propagate_boolean(G_TASK(result), error);
}

static void
test_purple_presence_manager_backend_load_saved_presences_async(PurplePresenceManagerBackend *backend,
                                                                GCancellable *cancellable,
                                                                GAsyncReadyCallback callback,
                                                                gpointer data)
{
	TestPurplePresenceManagerBackend *test_backend = NULL;
	GListStore *saved_presences = NULL;
	GTask *task = NULL;

	test_backend = TEST_PURPLE_PRESENCE_MANAGER_BACKEND(backend);

	task = g_task_new(G_OBJECT(backend), cancellable, callback, data);
	saved_presences = g_list_store_new(PURPLE_TYPE_SAVED_PRESENCE);
	g_task_return_pointer(task, saved_presences, g_object_unref);

	g_clear_object(&task);

	test_backend->load_saved_presences_async += 1;
}

static GListModel *
test_purple_presence_manager_backend_load_saved_presences_finish(PurplePresenceManagerBackend *backend,
                                                                 GAsyncResult *result,
                                                                 GError **error)
{
	TestPurplePresenceManagerBackend *test_backend = NULL;

	test_backend = TEST_PURPLE_PRESENCE_MANAGER_BACKEND(backend);
	test_backend->load_saved_presences_finish += 1;

	return g_task_propagate_pointer(G_TASK(result), error);
}

static void
test_purple_presence_manager_backend_save_saved_presence_async(PurplePresenceManagerBackend *backend,
                                                               PurpleSavedPresence *saved_presence,
                                                               GCancellable *cancellable,
                                                               GAsyncReadyCallback callback,
                                                               gpointer data)
{
	TestPurplePresenceManagerBackend *test_backend = NULL;
	GTask *task = NULL;

	birb_assert_type(saved_presence, PURPLE_TYPE_SAVED_PRESENCE);

	test_backend = TEST_PURPLE_PRESENCE_MANAGER_BACKEND(backend);

	task = g_task_new(G_OBJECT(backend), cancellable, callback, data);
	g_task_return_boolean(task, TRUE);

	g_clear_object(&task);

	test_backend->save_saved_presence_async += 1;
}

static gboolean
test_purple_presence_manager_backend_save_saved_presence_finish(PurplePresenceManagerBackend *backend,
                                                                GAsyncResult *result,
                                                                GError **error)
{
	TestPurplePresenceManagerBackend *test_backend = NULL;

	test_backend = TEST_PURPLE_PRESENCE_MANAGER_BACKEND(backend);
	test_backend->save_saved_presence_finish += 1;

	return g_task_propagate_boolean(G_TASK(result), error);
}

static void
test_purple_presence_manager_backend_init(TestPurplePresenceManagerBackend *backend)
{
	backend->delete_saved_presence_async = 0;
	backend->delete_saved_presence_finish = 0;
	backend->load_saved_presences_async = 0;
	backend->load_saved_presences_finish = 0;
	backend->save_saved_presence_async = 0;
	backend->save_saved_presence_finish = 0;
}

static void
test_purple_presence_manager_backend_class_init(TestPurplePresenceManagerBackendClass *klass)
{
	PurplePresenceManagerBackendClass *backend_class = NULL;

	backend_class = PURPLE_PRESENCE_MANAGER_BACKEND_CLASS(klass);
	backend_class->delete_saved_presence_async =
		test_purple_presence_manager_backend_delete_saved_presence_async;
	backend_class->delete_saved_presence_finish =
		test_purple_presence_manager_backend_delete_saved_presence_finish;
	backend_class->load_saved_presences_async =
		test_purple_presence_manager_backend_load_saved_presences_async;
	backend_class->load_saved_presences_finish =
		test_purple_presence_manager_backend_load_saved_presences_finish;
	backend_class->save_saved_presence_async =
		test_purple_presence_manager_backend_save_saved_presence_async;
	backend_class->save_saved_presence_finish =
		test_purple_presence_manager_backend_save_saved_presence_finish;
}

/******************************************************************************
 * Tests
 *****************************************************************************/
static void
test_purple_presence_manager_backend_normal_delete_saved_presence_cb(GObject *source,
                                                                     GAsyncResult *result,
                                                                     gpointer data)
{
	PurplePresenceManagerBackend *backend = NULL;
	GError *error = NULL;
	guint *counter = data;
	gboolean success = FALSE;

	birb_assert_type(source, TEST_PURPLE_TYPE_PRESENCE_MANAGER_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);

	*counter = *counter + 1;
}

static void
test_purple_presence_manager_backend_normal_delete_saved_presence(void) {
	TestPurplePresenceManagerBackend *test_backend = NULL;
	PurplePresenceManagerBackend *backend = NULL;
	PurpleSavedPresence *saved_presence = NULL;
	guint counter = 0;

	backend = g_object_new(TEST_PURPLE_TYPE_PRESENCE_MANAGER_BACKEND,
	                       NULL);
	test_backend = TEST_PURPLE_PRESENCE_MANAGER_BACKEND(backend);
	saved_presence = g_object_new(PURPLE_TYPE_SAVED_PRESENCE, NULL);

	purple_presence_manager_backend_delete_saved_presence_async(backend,
	                                                            saved_presence,
	                                                            NULL,
	                                                            test_purple_presence_manager_backend_normal_delete_saved_presence_cb,
	                                                            &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_cmpuint(test_backend->delete_saved_presence_async, ==, 1);
	g_assert_cmpuint(test_backend->delete_saved_presence_finish, ==, 1);
	g_assert_cmpuint(test_backend->load_saved_presences_async, ==, 0);
	g_assert_cmpuint(test_backend->load_saved_presences_finish, ==, 0);
	g_assert_cmpuint(test_backend->save_saved_presence_async, ==, 0);
	g_assert_cmpuint(test_backend->save_saved_presence_finish, ==, 0);

	g_assert_finalize_object(backend);
	g_assert_finalize_object(saved_presence);
}

static void
test_purple_presence_manager_backend_normal_load_saved_presences_cb(GObject *source,
                                                                    GAsyncResult *result,
                                                                    gpointer data)
{
	PurplePresenceManagerBackend *backend = NULL;
	GError *error = NULL;
	GListModel *saved_presences = NULL;
	GType type = G_TYPE_INVALID;
	guint *counter = data;

	birb_assert_type(source, TEST_PURPLE_TYPE_PRESENCE_MANAGER_BACKEND);

	backend = PURPLE_PRESENCE_MANAGER_BACKEND(source);
	saved_presences = purple_presence_manager_backend_load_saved_presences_finish(backend,
	                                                                              result,
	                                                                              &error);
	g_assert_no_error(error);

	birb_assert_type(saved_presences, G_TYPE_LIST_STORE);
	type = g_list_model_get_item_type(saved_presences);
	g_assert_cmpuint(type, ==, PURPLE_TYPE_SAVED_PRESENCE);
	g_clear_object(&saved_presences);

	*counter = *counter + 1;
}

static void
test_purple_presence_manager_backend_normal_load_saved_presences(void) {
	TestPurplePresenceManagerBackend *test_backend = NULL;
	PurplePresenceManagerBackend *backend = NULL;
	guint counter = 0;

	backend = g_object_new(TEST_PURPLE_TYPE_PRESENCE_MANAGER_BACKEND,
	                       NULL);
	test_backend = TEST_PURPLE_PRESENCE_MANAGER_BACKEND(backend);

	purple_presence_manager_backend_load_saved_presences_async(backend,
	                                                           NULL,
	                                                           test_purple_presence_manager_backend_normal_load_saved_presences_cb,
	                                                           &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_cmpuint(test_backend->delete_saved_presence_async, ==, 0);
	g_assert_cmpuint(test_backend->delete_saved_presence_finish, ==, 0);
	g_assert_cmpuint(test_backend->load_saved_presences_async, ==, 1);
	g_assert_cmpuint(test_backend->load_saved_presences_finish, ==, 1);
	g_assert_cmpuint(test_backend->save_saved_presence_async, ==, 0);
	g_assert_cmpuint(test_backend->save_saved_presence_finish, ==, 0);

	g_clear_object(&backend);
}

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

	birb_assert_type(source, TEST_PURPLE_TYPE_PRESENCE_MANAGER_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);

	*counter = *counter + 1;
}

static void
test_purple_presence_manager_backend_normal_save_saved_presence(void) {
	TestPurplePresenceManagerBackend *test_backend = NULL;
	PurplePresenceManagerBackend *backend = NULL;
	PurpleSavedPresence *saved_presence = NULL;
	guint counter = 0;

	backend = g_object_new(TEST_PURPLE_TYPE_PRESENCE_MANAGER_BACKEND,
	                       NULL);
	test_backend = TEST_PURPLE_PRESENCE_MANAGER_BACKEND(backend);

	saved_presence = g_object_new(PURPLE_TYPE_SAVED_PRESENCE, NULL);

	purple_presence_manager_backend_save_saved_presence_async(backend,
	                                                          saved_presence,
	                                                          NULL,
	                                                          test_purple_presence_manager_backend_normal_save_saved_presence_cb,
	                                                          &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_cmpuint(test_backend->delete_saved_presence_async, ==, 0);
	g_assert_cmpuint(test_backend->delete_saved_presence_finish, ==, 0);
	g_assert_cmpuint(test_backend->load_saved_presences_async, ==, 0);
	g_assert_cmpuint(test_backend->load_saved_presences_finish, ==, 0);
	g_assert_cmpuint(test_backend->save_saved_presence_async, ==, 1);
	g_assert_cmpuint(test_backend->save_saved_presence_finish, ==, 1);

	g_assert_finalize_object(backend);
	g_assert_finalize_object(saved_presence);
}

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

	g_test_add_func("/presence-manager-backend-normal/delete-saved-presence",
	                test_purple_presence_manager_backend_normal_delete_saved_presence);
	g_test_add_func("/presence-manager-backend-normal/load-saved-presences",
	                test_purple_presence_manager_backend_normal_load_saved_presences);
	g_test_add_func("/presence-manager-backend-normal/save-saved-presence",
	                test_purple_presence_manager_backend_normal_save_saved_presence);

	return g_test_run();
}
