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

/******************************************************************************
 * Empty PurpleAccountManagerBackend Implementation
 *****************************************************************************/
#define TEST_PURPLE_TYPE_ACCOUNT_MANAGER_BACKEND (test_purple_account_manager_backend_get_type())

G_DECLARE_FINAL_TYPE(TestPurpleAccountManagerBackend,
                     test_purple_account_manager_backend,
                     TEST_PURPLE,
                     ACCOUNT_MANAGER_BACKEND,
                     PurpleAccountManagerBackend);

struct _TestPurpleAccountManagerBackend {
	PurpleAccountManagerBackend parent;

	guint delete_account_async;
	guint delete_account_finish;
	guint load_accounts_async;
	guint load_accounts_finish;
	guint save_account_async;
	guint save_account_finish;
};

G_DEFINE_FINAL_TYPE(TestPurpleAccountManagerBackend,
                    test_purple_account_manager_backend,
                    PURPLE_TYPE_ACCOUNT_MANAGER_BACKEND);

static void
test_purple_account_manager_backend_delete_account_async(PurpleAccountManagerBackend *backend,
                                                         PurpleAccount *account,
                                                         GCancellable *cancellable,
                                                         GAsyncReadyCallback callback,
                                                         gpointer data)
{
	TestPurpleAccountManagerBackend *test_backend = NULL;
	GTask *task = NULL;

	birb_assert_type(account, PURPLE_TYPE_ACCOUNT);

	test_backend = TEST_PURPLE_ACCOUNT_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_account_async += 1;
}

static gboolean
test_purple_account_manager_backend_delete_account_finish(PurpleAccountManagerBackend *backend,
                                                          GAsyncResult *result,
                                                          GError **error)
{
	TestPurpleAccountManagerBackend *test_backend = NULL;

	test_backend = TEST_PURPLE_ACCOUNT_MANAGER_BACKEND(backend);
	test_backend->delete_account_finish += 1;

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

static void
test_purple_account_manager_backend_load_accounts_async(PurpleAccountManagerBackend *backend,
                                                        GCancellable *cancellable,
                                                        GAsyncReadyCallback callback,
                                                        gpointer data)
{
	TestPurpleAccountManagerBackend *test_backend = NULL;
	GTask *task = NULL;
	GListStore *store = NULL;

	test_backend = TEST_PURPLE_ACCOUNT_MANAGER_BACKEND(backend);

	task = g_task_new(G_OBJECT(backend), cancellable, callback, data);
	store = g_list_store_new(PURPLE_TYPE_ACCOUNT);
	g_task_return_pointer(task, store, g_object_unref);

	g_clear_object(&task);

	test_backend->load_accounts_async += 1;
}

static GListModel *
test_purple_account_manager_backend_load_accounts_finish(PurpleAccountManagerBackend *backend,
                                                         GAsyncResult *result,
                                                         GError **error)
{
	TestPurpleAccountManagerBackend *test_backend = NULL;

	test_backend = TEST_PURPLE_ACCOUNT_MANAGER_BACKEND(backend);
	test_backend->load_accounts_finish += 1;

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

static void
test_purple_account_manager_backend_save_account_async(PurpleAccountManagerBackend *backend,
                                                       PurpleAccount *account,
                                                       GCancellable *cancellable,
                                                       GAsyncReadyCallback callback,
                                                       gpointer data)
{
	TestPurpleAccountManagerBackend *test_backend = NULL;
	GTask *task = NULL;

	birb_assert_type(account, PURPLE_TYPE_ACCOUNT);

	test_backend = TEST_PURPLE_ACCOUNT_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_account_async += 1;
}

static gboolean
test_purple_account_manager_backend_save_account_finish(PurpleAccountManagerBackend *backend,
                                                        GAsyncResult *result,
                                                        GError **error)
{
	TestPurpleAccountManagerBackend *test_backend = NULL;

	test_backend = TEST_PURPLE_ACCOUNT_MANAGER_BACKEND(backend);
	test_backend->save_account_finish += 1;

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

static void
test_purple_account_manager_backend_init(TestPurpleAccountManagerBackend *backend)
{
	backend->delete_account_async = 0;
	backend->delete_account_finish = 0;
	backend->load_accounts_async = 0;
	backend->load_accounts_finish = 0;
	backend->save_account_async = 0;
	backend->save_account_finish = 0;
}

static void
test_purple_account_manager_backend_class_init(TestPurpleAccountManagerBackendClass *klass)
{
	PurpleAccountManagerBackendClass *backend_class = NULL;

	backend_class = PURPLE_ACCOUNT_MANAGER_BACKEND_CLASS(klass);
	backend_class->delete_account_async =
		test_purple_account_manager_backend_delete_account_async;
	backend_class->delete_account_finish =
		test_purple_account_manager_backend_delete_account_finish;
	backend_class->load_accounts_async =
		test_purple_account_manager_backend_load_accounts_async;
	backend_class->load_accounts_finish =
		test_purple_account_manager_backend_load_accounts_finish;
	backend_class->save_account_async =
		test_purple_account_manager_backend_save_account_async;
	backend_class->save_account_finish =
		test_purple_account_manager_backend_save_account_finish;
}

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

	birb_assert_type(source, TEST_PURPLE_TYPE_ACCOUNT_MANAGER_BACKEND);

	backend = PURPLE_ACCOUNT_MANAGER_BACKEND(source);
	success = purple_account_manager_backend_delete_account_finish(backend,
	                                                               result,
	                                                               &error);
	g_assert_no_error(error);

	g_assert_true(success);

	*counter = *counter + 1;
}

static void
test_purple_account_manager_backend_normal_delete_account(void) {
	TestPurpleAccountManagerBackend *test_backend = NULL;
	PurpleAccount *account = NULL;
	PurpleAccountManagerBackend *backend = NULL;
	guint counter = 0;

	backend = g_object_new(TEST_PURPLE_TYPE_ACCOUNT_MANAGER_BACKEND, NULL);
	test_backend = TEST_PURPLE_ACCOUNT_MANAGER_BACKEND(backend);

	account = purple_account_new("test", "test");

	purple_account_manager_backend_delete_account_async(backend, account, NULL,
	                                                    test_purple_account_manager_backend_normal_delete_account_cb,
	                                                    &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_cmpuint(test_backend->delete_account_async, ==, 1);
	g_assert_cmpuint(test_backend->delete_account_finish, ==, 1);
	g_assert_cmpuint(test_backend->load_accounts_async, ==, 0);
	g_assert_cmpuint(test_backend->load_accounts_finish, ==, 0);
	g_assert_cmpuint(test_backend->save_account_async, ==, 0);
	g_assert_cmpuint(test_backend->save_account_finish, ==, 0);

	g_clear_object(&backend);
	g_clear_object(&account);
}

static void
test_purple_account_manager_backend_normal_load_accounts_cb(GObject *source,
                                                            GAsyncResult *result,
                                                            gpointer data)
{
	PurpleAccountManagerBackend *backend = NULL;
	GError *error = NULL;
	GListModel *accounts = NULL;
	guint *counter = data;

	birb_assert_type(source, TEST_PURPLE_TYPE_ACCOUNT_MANAGER_BACKEND);

	backend = PURPLE_ACCOUNT_MANAGER_BACKEND(source);
	accounts = purple_account_manager_backend_load_accounts_finish(backend,
	                                                               result,
	                                                               &error);
	g_assert_no_error(error);

	birb_assert_type(accounts, G_TYPE_LIST_STORE);
	g_clear_object(&accounts);

	*counter = *counter + 1;
}

static void
test_purple_account_manager_backend_normal_load_accounts(void) {
	TestPurpleAccountManagerBackend *test_backend = NULL;
	PurpleAccountManagerBackend *backend = NULL;
	guint counter = 0;

	backend = g_object_new(TEST_PURPLE_TYPE_ACCOUNT_MANAGER_BACKEND, NULL);
	test_backend = TEST_PURPLE_ACCOUNT_MANAGER_BACKEND(backend);

	purple_account_manager_backend_load_accounts_async(backend, NULL,
	                                                   test_purple_account_manager_backend_normal_load_accounts_cb,
	                                                   &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_cmpuint(test_backend->delete_account_async, ==, 0);
	g_assert_cmpuint(test_backend->delete_account_finish, ==, 0);
	g_assert_cmpuint(test_backend->load_accounts_async, ==, 1);
	g_assert_cmpuint(test_backend->load_accounts_finish, ==, 1);
	g_assert_cmpuint(test_backend->save_account_async, ==, 0);
	g_assert_cmpuint(test_backend->save_account_finish, ==, 0);

	g_clear_object(&backend);
}

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

	birb_assert_type(source, TEST_PURPLE_TYPE_ACCOUNT_MANAGER_BACKEND);

	backend = PURPLE_ACCOUNT_MANAGER_BACKEND(source);
	success = purple_account_manager_backend_save_account_finish(backend,
	                                                             result,
	                                                             &error);
	g_assert_no_error(error);

	g_assert_true(success);

	*counter = *counter + 1;
}

static void
test_purple_account_manager_backend_normal_save_account(void) {
	TestPurpleAccountManagerBackend *test_backend = NULL;
	PurpleAccount *account = NULL;
	PurpleAccountManagerBackend *backend = NULL;
	guint counter = 0;

	backend = g_object_new(TEST_PURPLE_TYPE_ACCOUNT_MANAGER_BACKEND, NULL);
	test_backend = TEST_PURPLE_ACCOUNT_MANAGER_BACKEND(backend);

	account = purple_account_new("test", "test");

	purple_account_manager_backend_save_account_async(backend, account, NULL,
	                                                  test_purple_account_manager_backend_normal_save_account_cb,
	                                                  &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_cmpuint(test_backend->delete_account_async, ==, 0);
	g_assert_cmpuint(test_backend->delete_account_finish, ==, 0);
	g_assert_cmpuint(test_backend->load_accounts_async, ==, 0);
	g_assert_cmpuint(test_backend->load_accounts_finish, ==, 0);
	g_assert_cmpuint(test_backend->save_account_async, ==, 1);
	g_assert_cmpuint(test_backend->save_account_finish, ==, 1);

	g_clear_object(&backend);
	g_clear_object(&account);
}

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

	g_test_add_func("/account-manager-backend-normal/delete-account",
	                test_purple_account_manager_backend_normal_delete_account);
	g_test_add_func("/account-manager-backend-normal/loads-accounts",
	                test_purple_account_manager_backend_normal_load_accounts);
	g_test_add_func("/account-manager-backend-normal/save-account",
	                test_purple_account_manager_backend_normal_save_account);

	return g_test_run();
}
