/*
 * 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 PurpleSchedulerBackend Implementation
 *****************************************************************************/
#define TEST_PURPLE_TYPE_SCHEDULER_BACKEND (test_purple_scheduler_backend_get_type())

G_DECLARE_FINAL_TYPE(TestPurpleSchedulerBackend,
                     test_purple_scheduler_backend,
                     TEST_PURPLE,
                     SCHEDULER_BACKEND,
                     PurpleSchedulerBackend);

struct _TestPurpleSchedulerBackend {
	PurpleSchedulerBackend parent;

	guint delete_task_async;
	guint delete_task_finish;
	guint load_tasks_async;
	guint load_tasks_finish;
	guint save_task_async;
	guint save_task_finish;
};

G_DEFINE_FINAL_TYPE(TestPurpleSchedulerBackend,
                    test_purple_scheduler_backend,
                    PURPLE_TYPE_SCHEDULER_BACKEND);

static void
test_purple_scheduler_backend_delete_task_async(PurpleSchedulerBackend *backend,
                                                PurpleScheduledTask *scheduled_task,
                                                GCancellable *cancellable,
                                                GAsyncReadyCallback callback,
                                                gpointer data)
{
	TestPurpleSchedulerBackend *test_backend = NULL;
	GTask *task = NULL;

	birb_assert_type(scheduled_task, PURPLE_TYPE_SCHEDULED_TASK);

	test_backend = TEST_PURPLE_SCHEDULER_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_task_async += 1;
}

static gboolean
test_purple_scheduler_backend_delete_task_finish(PurpleSchedulerBackend *backend,
                                                 GAsyncResult *result,
                                                 GError **error)
{
	TestPurpleSchedulerBackend *test_backend = NULL;

	test_backend = TEST_PURPLE_SCHEDULER_BACKEND(backend);
	test_backend->delete_task_finish += 1;

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

static void
test_purple_scheduler_backend_load_tasks_async(PurpleSchedulerBackend *backend,
                                               GCancellable *cancellable,
                                               GAsyncReadyCallback callback,
                                               gpointer data)
{
	TestPurpleSchedulerBackend *test_backend = NULL;
	GTask *task = NULL;
	GListStore *store = NULL;

	test_backend = TEST_PURPLE_SCHEDULER_BACKEND(backend);

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

	g_clear_object(&task);

	test_backend->load_tasks_async += 1;
}

static GListModel *
test_purple_scheduler_backend_load_tasks_finish(PurpleSchedulerBackend *backend,
                                                GAsyncResult *result,
                                                GError **error)
{
	TestPurpleSchedulerBackend *test_backend = NULL;

	test_backend = TEST_PURPLE_SCHEDULER_BACKEND(backend);
	test_backend->load_tasks_finish += 1;

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

static void
test_purple_scheduler_backend_save_task_async(PurpleSchedulerBackend *backend,
                                              PurpleScheduledTask *scheduled_task,
                                              GCancellable *cancellable,
                                              GAsyncReadyCallback callback,
                                              gpointer data)
{
	TestPurpleSchedulerBackend *test_backend = NULL;
	GTask *task = NULL;

	birb_assert_type(scheduled_task, PURPLE_TYPE_SCHEDULED_TASK);

	test_backend = TEST_PURPLE_SCHEDULER_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_task_async += 1;
}

static gboolean
test_purple_scheduler_backend_save_task_finish(PurpleSchedulerBackend *backend,
                                               GAsyncResult *result,
                                               GError **error)
{
	TestPurpleSchedulerBackend *test_backend = NULL;

	test_backend = TEST_PURPLE_SCHEDULER_BACKEND(backend);
	test_backend->save_task_finish += 1;

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

static void
test_purple_scheduler_backend_init(TestPurpleSchedulerBackend *backend)
{
	backend->delete_task_async = 0;
	backend->delete_task_finish = 0;
	backend->load_tasks_async = 0;
	backend->load_tasks_finish = 0;
	backend->save_task_async = 0;
	backend->save_task_finish = 0;
}

static void
test_purple_scheduler_backend_class_init(TestPurpleSchedulerBackendClass *klass)
{
	PurpleSchedulerBackendClass *backend_class = NULL;

	backend_class = PURPLE_SCHEDULER_BACKEND_CLASS(klass);
	backend_class->delete_task_async =
		test_purple_scheduler_backend_delete_task_async;
	backend_class->delete_task_finish =
		test_purple_scheduler_backend_delete_task_finish;
	backend_class->load_tasks_async =
		test_purple_scheduler_backend_load_tasks_async;
	backend_class->load_tasks_finish =
		test_purple_scheduler_backend_load_tasks_finish;
	backend_class->save_task_async =
		test_purple_scheduler_backend_save_task_async;
	backend_class->save_task_finish =
		test_purple_scheduler_backend_save_task_finish;
}

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

	birb_assert_type(source, TEST_PURPLE_TYPE_SCHEDULER_BACKEND);

	backend = PURPLE_SCHEDULER_BACKEND(source);
	success = purple_scheduler_backend_delete_task_finish(backend, result,
	                                                      &error);
	g_assert_no_error(error);

	g_assert_true(success);

	*counter = *counter + 1;
}

static void
test_purple_scheduler_backend_normal_delete_task(void) {
	TestPurpleSchedulerBackend *test_backend = NULL;
	PurpleScheduledTask *task = NULL;
	PurpleSchedulerBackend *backend = NULL;
	guint counter = 0;

	backend = g_object_new(TEST_PURPLE_TYPE_SCHEDULER_BACKEND, NULL);
	test_backend = TEST_PURPLE_SCHEDULER_BACKEND(backend);

	task = purple_scheduled_task_new("test-task", "Test", TRUE);

	purple_scheduler_backend_delete_task_async(backend, task, NULL,
	                                           test_purple_scheduler_backend_normal_delete_task_cb,
	                                           &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_cmpuint(test_backend->delete_task_async, ==, 1);
	g_assert_cmpuint(test_backend->delete_task_finish, ==, 1);
	g_assert_cmpuint(test_backend->load_tasks_async, ==, 0);
	g_assert_cmpuint(test_backend->load_tasks_finish, ==, 0);
	g_assert_cmpuint(test_backend->save_task_async, ==, 0);
	g_assert_cmpuint(test_backend->save_task_finish, ==, 0);

	g_clear_object(&backend);
	g_clear_object(&task);
}

static void
test_purple_scheduler_backend_normal_load_tasks_cb(GObject *source,
                                                   GAsyncResult *result,
                                                   gpointer data)
{
	PurpleSchedulerBackend *backend = NULL;
	GError *error = NULL;
	GListModel *tasks = NULL;
	guint *counter = data;

	birb_assert_type(source, TEST_PURPLE_TYPE_SCHEDULER_BACKEND);

	backend = PURPLE_SCHEDULER_BACKEND(source);
	tasks = purple_scheduler_backend_load_tasks_finish(backend, result,
	                                                   &error);
	g_assert_no_error(error);

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

	*counter = *counter + 1;
}

static void
test_purple_scheduler_backend_normal_load_tasks(void) {
	TestPurpleSchedulerBackend *test_backend = NULL;
	PurpleSchedulerBackend *backend = NULL;
	guint counter = 0;

	backend = g_object_new(TEST_PURPLE_TYPE_SCHEDULER_BACKEND, NULL);
	test_backend = TEST_PURPLE_SCHEDULER_BACKEND(backend);

	purple_scheduler_backend_load_tasks_async(backend, NULL,
	                                          test_purple_scheduler_backend_normal_load_tasks_cb,
	                                          &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_cmpuint(test_backend->delete_task_async, ==, 0);
	g_assert_cmpuint(test_backend->delete_task_finish, ==, 0);
	g_assert_cmpuint(test_backend->load_tasks_async, ==, 1);
	g_assert_cmpuint(test_backend->load_tasks_finish, ==, 1);
	g_assert_cmpuint(test_backend->save_task_async, ==, 0);
	g_assert_cmpuint(test_backend->save_task_finish, ==, 0);

	g_clear_object(&backend);
}

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

	birb_assert_type(source, TEST_PURPLE_TYPE_SCHEDULER_BACKEND);

	backend = PURPLE_SCHEDULER_BACKEND(source);
	success = purple_scheduler_backend_save_task_finish(backend, result,
	                                                    &error);
	g_assert_no_error(error);

	g_assert_true(success);

	*counter = *counter + 1;
}

static void
test_purple_scheduler_backend_normal_save_task(void) {
	TestPurpleSchedulerBackend *test_backend = NULL;
	PurpleScheduledTask *task = NULL;
	PurpleSchedulerBackend *backend = NULL;
	guint counter = 0;

	backend = g_object_new(TEST_PURPLE_TYPE_SCHEDULER_BACKEND, NULL);
	test_backend = TEST_PURPLE_SCHEDULER_BACKEND(backend);

	task = purple_scheduled_task_new("test-task", "Test", TRUE);

	purple_scheduler_backend_save_task_async(backend, task, NULL,
	                                         test_purple_scheduler_backend_normal_save_task_cb,
	                                         &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_cmpuint(test_backend->delete_task_async, ==, 0);
	g_assert_cmpuint(test_backend->delete_task_finish, ==, 0);
	g_assert_cmpuint(test_backend->load_tasks_async, ==, 0);
	g_assert_cmpuint(test_backend->load_tasks_finish, ==, 0);
	g_assert_cmpuint(test_backend->save_task_async, ==, 1);
	g_assert_cmpuint(test_backend->save_task_finish, ==, 1);

	g_clear_object(&backend);
	g_clear_object(&task);
}

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

	g_test_add_func("/scheduler-backend-normal/delete-task",
	                test_purple_scheduler_backend_normal_delete_task);
	g_test_add_func("/scheduler-backend-normal/loads-tasks",
	                test_purple_scheduler_backend_normal_load_tasks);
	g_test_add_func("/scheduler-backend-normal/save-task",
	                test_purple_scheduler_backend_normal_save_task);

	return g_test_run();
}
