/*
 * 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 PurpleContactManagerBackend Implementation
 *****************************************************************************/
#define TEST_PURPLE_TYPE_CONTACT_MANAGER_BACKEND (test_purple_contact_manager_backend_get_type())

G_DECLARE_FINAL_TYPE(TestPurpleContactManagerBackend,
                     test_purple_contact_manager_backend,
                     TEST_PURPLE,
                     CONTACT_MANAGER_BACKEND,
                     PurpleContactManagerBackend);

struct _TestPurpleContactManagerBackend {
	PurpleContactManagerBackend parent;

	guint delete_contact_async;
	guint delete_contact_finish;
	guint load_contacts_async;
	guint load_contacts_finish;
	guint save_contact_async;
	guint save_contact_finish;
};

G_DEFINE_FINAL_TYPE(TestPurpleContactManagerBackend,
                    test_purple_contact_manager_backend,
                    PURPLE_TYPE_CONTACT_MANAGER_BACKEND);

static void
test_purple_contact_manager_backend_delete_contact_async(PurpleContactManagerBackend *backend,
                                                         PurpleContact *contact,
                                                         GCancellable *cancellable,
                                                         GAsyncReadyCallback callback,
                                                         gpointer data)
{
	TestPurpleContactManagerBackend *test_backend = NULL;
	GTask *task = NULL;

	birb_assert_type(contact, PURPLE_TYPE_CONTACT);

	test_backend = TEST_PURPLE_CONTACT_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_contact_async += 1;
}

static gboolean
test_purple_contact_manager_backend_delete_contact_finish(PurpleContactManagerBackend *backend,
                                                          GAsyncResult *result,
                                                          GError **error)
{
	TestPurpleContactManagerBackend *test_backend = NULL;

	test_backend = TEST_PURPLE_CONTACT_MANAGER_BACKEND(backend);
	test_backend->delete_contact_finish += 1;

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

static void
test_purple_contact_manager_backend_load_contacts_async(PurpleContactManagerBackend *backend,
                                                        GCancellable *cancellable,
                                                        GAsyncReadyCallback callback,
                                                        gpointer data)
{
	TestPurpleContactManagerBackend *test_backend = NULL;
	PurpleContacts *contacts = NULL;
	GTask *task = NULL;

	test_backend = TEST_PURPLE_CONTACT_MANAGER_BACKEND(backend);

	task = g_task_new(G_OBJECT(backend), cancellable, callback, data);
	contacts = purple_contacts_new();
	g_task_return_pointer(task, contacts, g_object_unref);

	g_clear_object(&task);

	test_backend->load_contacts_async += 1;
}

static PurpleContacts *
test_purple_contact_manager_backend_load_contacts_finish(PurpleContactManagerBackend *backend,
                                                         GAsyncResult *result,
                                                         GError **error)
{
	TestPurpleContactManagerBackend *test_backend = NULL;

	test_backend = TEST_PURPLE_CONTACT_MANAGER_BACKEND(backend);
	test_backend->load_contacts_finish += 1;

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

static void
test_purple_contact_manager_backend_save_contact_async(PurpleContactManagerBackend *backend,
                                                       PurpleContact *contact,
                                                       GCancellable *cancellable,
                                                       GAsyncReadyCallback callback,
                                                       gpointer data)
{
	TestPurpleContactManagerBackend *test_backend = NULL;
	GTask *task = NULL;

	birb_assert_type(contact, PURPLE_TYPE_CONTACT);

	test_backend = TEST_PURPLE_CONTACT_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_contact_async += 1;
}

static gboolean
test_purple_contact_manager_backend_save_contact_finish(PurpleContactManagerBackend *backend,
                                                        GAsyncResult *result,
                                                        GError **error)
{
	TestPurpleContactManagerBackend *test_backend = NULL;

	test_backend = TEST_PURPLE_CONTACT_MANAGER_BACKEND(backend);
	test_backend->save_contact_finish += 1;

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

static void
test_purple_contact_manager_backend_init(TestPurpleContactManagerBackend *backend)
{
	backend->delete_contact_async = 0;
	backend->delete_contact_finish = 0;
	backend->load_contacts_async = 0;
	backend->load_contacts_finish = 0;
	backend->save_contact_async = 0;
	backend->save_contact_finish = 0;
}

static void
test_purple_contact_manager_backend_class_init(TestPurpleContactManagerBackendClass *klass)
{
	PurpleContactManagerBackendClass *backend_class = NULL;

	backend_class = PURPLE_CONTACT_MANAGER_BACKEND_CLASS(klass);
	backend_class->delete_contact_async =
		test_purple_contact_manager_backend_delete_contact_async;
	backend_class->delete_contact_finish =
		test_purple_contact_manager_backend_delete_contact_finish;
	backend_class->load_contacts_async =
		test_purple_contact_manager_backend_load_contacts_async;
	backend_class->load_contacts_finish =
		test_purple_contact_manager_backend_load_contacts_finish;
	backend_class->save_contact_async =
		test_purple_contact_manager_backend_save_contact_async;
	backend_class->save_contact_finish =
		test_purple_contact_manager_backend_save_contact_finish;
}

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

	birb_assert_type(source, TEST_PURPLE_TYPE_CONTACT_MANAGER_BACKEND);

	backend = PURPLE_CONTACT_MANAGER_BACKEND(source);
	success = purple_contact_manager_backend_delete_contact_finish(backend,
	                                                               result,
	                                                               &error);
	g_assert_no_error(error);

	g_assert_true(success);

	*counter = *counter + 1;
}

static void
test_purple_contact_manager_backend_normal_delete_contact(void) {
	TestPurpleContactManagerBackend *test_backend = NULL;
	PurpleAccount *account = NULL;
	PurpleContact *contact = NULL;
	PurpleContactManagerBackend *backend = NULL;
	guint counter = 0;

	account = purple_account_new("test", "test");
	backend = g_object_new(TEST_PURPLE_TYPE_CONTACT_MANAGER_BACKEND,
	                       NULL);
	test_backend = TEST_PURPLE_CONTACT_MANAGER_BACKEND(backend);
	contact = purple_contact_new(account, NULL);

	purple_contact_manager_backend_delete_contact_async(backend,
	                                                    contact,
	                                                    NULL,
	                                                    test_purple_contact_manager_backend_normal_delete_contact_cb,
	                                                    &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_cmpuint(test_backend->delete_contact_async, ==, 1);
	g_assert_cmpuint(test_backend->delete_contact_finish, ==, 1);
	g_assert_cmpuint(test_backend->load_contacts_async, ==, 0);
	g_assert_cmpuint(test_backend->load_contacts_finish, ==, 0);
	g_assert_cmpuint(test_backend->save_contact_async, ==, 0);
	g_assert_cmpuint(test_backend->save_contact_finish, ==, 0);

	g_assert_finalize_object(backend);
	g_assert_finalize_object(contact);
	g_assert_finalize_object(account);
}

static void
test_purple_contact_manager_backend_normal_load_contacts_cb(GObject *source,
                                                            GAsyncResult *result,
                                                            gpointer data)
{
	PurpleContactManagerBackend *backend = NULL;
	PurpleContacts *contacts = NULL;
	GError *error = NULL;
	guint *counter = data;

	birb_assert_type(source, TEST_PURPLE_TYPE_CONTACT_MANAGER_BACKEND);

	backend = PURPLE_CONTACT_MANAGER_BACKEND(source);
	contacts = purple_contact_manager_backend_load_contacts_finish(backend,
	                                                               result,
	                                                               &error);
	g_assert_no_error(error);

	birb_assert_type(contacts, PURPLE_TYPE_CONTACTS);
	g_clear_object(&contacts);

	*counter = *counter + 1;
}

static void
test_purple_contact_manager_backend_normal_load_contacts(void) {
	TestPurpleContactManagerBackend *test_backend = NULL;
	PurpleContactManagerBackend *backend = NULL;
	guint counter = 0;

	backend = g_object_new(TEST_PURPLE_TYPE_CONTACT_MANAGER_BACKEND,
	                       NULL);
	test_backend = TEST_PURPLE_CONTACT_MANAGER_BACKEND(backend);

	purple_contact_manager_backend_load_contacts_async(backend,
	                                                   NULL,
	                                                   test_purple_contact_manager_backend_normal_load_contacts_cb,
	                                                   &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_cmpuint(test_backend->delete_contact_async, ==, 0);
	g_assert_cmpuint(test_backend->delete_contact_finish, ==, 0);
	g_assert_cmpuint(test_backend->load_contacts_async, ==, 1);
	g_assert_cmpuint(test_backend->load_contacts_finish, ==, 1);
	g_assert_cmpuint(test_backend->save_contact_async, ==, 0);
	g_assert_cmpuint(test_backend->save_contact_finish, ==, 0);

	g_clear_object(&backend);
}

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

	birb_assert_type(source, TEST_PURPLE_TYPE_CONTACT_MANAGER_BACKEND);

	backend = PURPLE_CONTACT_MANAGER_BACKEND(source);
	success = purple_contact_manager_backend_save_contact_finish(backend,
	                                                             result,
	                                                             &error);
	g_assert_no_error(error);

	g_assert_true(success);

	*counter = *counter + 1;
}

static void
test_purple_contact_manager_backend_normal_save_contact(void) {
	TestPurpleContactManagerBackend *test_backend = NULL;
	PurpleAccount *account = NULL;
	PurpleContact *contact = NULL;
	PurpleContactManagerBackend *backend = NULL;
	guint counter = 0;

	account = purple_account_new("test", "test");
	backend = g_object_new(TEST_PURPLE_TYPE_CONTACT_MANAGER_BACKEND,
	                       NULL);
	test_backend = TEST_PURPLE_CONTACT_MANAGER_BACKEND(backend);

	contact = purple_contact_new(account, NULL);

	purple_contact_manager_backend_save_contact_async(backend,
	                                                  contact,
	                                                  NULL,
	                                                  test_purple_contact_manager_backend_normal_save_contact_cb,
	                                                  &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_cmpuint(test_backend->delete_contact_async, ==, 0);
	g_assert_cmpuint(test_backend->delete_contact_finish, ==, 0);
	g_assert_cmpuint(test_backend->load_contacts_async, ==, 0);
	g_assert_cmpuint(test_backend->load_contacts_finish, ==, 0);
	g_assert_cmpuint(test_backend->save_contact_async, ==, 1);
	g_assert_cmpuint(test_backend->save_contact_finish, ==, 1);

	g_assert_finalize_object(backend);
	g_assert_finalize_object(contact);
	g_assert_finalize_object(account);
}

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

	g_test_add_func("/contact-manager-backend-normal/delete-contact",
	                test_purple_contact_manager_backend_normal_delete_contact);
	g_test_add_func("/contact-manager-backend-normal/load-contacts",
	                test_purple_contact_manager_backend_normal_load_contacts);
	g_test_add_func("/contact-manager-backend-normal/save-contact",
	                test_purple_contact_manager_backend_normal_save_contact);

	return g_test_run();
}
