/*
 * 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 PurpleConversationManagerBackend Implementation
 *****************************************************************************/
#define TEST_PURPLE_TYPE_CONVERSATION_MANAGER_BACKEND (test_purple_conversation_manager_backend_get_type())

G_DECLARE_FINAL_TYPE(TestPurpleConversationManagerBackend,
                     test_purple_conversation_manager_backend,
                     TEST_PURPLE,
                     CONVERSATION_MANAGER_BACKEND,
                     PurpleConversationManagerBackend);

struct _TestPurpleConversationManagerBackend {
	PurpleConversationManagerBackend parent;

	guint delete_conversation_async;
	guint delete_conversation_finish;
	guint load_conversations_async;
	guint load_conversations_finish;
	guint save_conversation_async;
	guint save_conversation_finish;
};

G_DEFINE_FINAL_TYPE(TestPurpleConversationManagerBackend,
                    test_purple_conversation_manager_backend,
                    PURPLE_TYPE_CONVERSATION_MANAGER_BACKEND);

static void
test_purple_conversation_manager_backend_delete_conversation_async(PurpleConversationManagerBackend *backend,
                                                                   PurpleConversation *conversation,
                                                                   GCancellable *cancellable,
                                                                   GAsyncReadyCallback callback,
                                                                   gpointer data)
{
	TestPurpleConversationManagerBackend *test_backend = NULL;
	GTask *task = NULL;

	birb_assert_type(conversation, PURPLE_TYPE_CONVERSATION);

	test_backend = TEST_PURPLE_CONVERSATION_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_conversation_async += 1;
}

static gboolean
test_purple_conversation_manager_backend_delete_conversation_finish(PurpleConversationManagerBackend *backend,
                                                                    GAsyncResult *result,
                                                                    GError **error)
{
	TestPurpleConversationManagerBackend *test_backend = NULL;

	test_backend = TEST_PURPLE_CONVERSATION_MANAGER_BACKEND(backend);
	test_backend->delete_conversation_finish += 1;

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

static void
test_purple_conversation_manager_backend_load_conversations_async(PurpleConversationManagerBackend *backend,
                                                                  GCancellable *cancellable,
                                                                  GAsyncReadyCallback callback,
                                                                  gpointer data)
{
	TestPurpleConversationManagerBackend *test_backend = NULL;
	GTask *task = NULL;
	GListStore *store = NULL;

	test_backend = TEST_PURPLE_CONVERSATION_MANAGER_BACKEND(backend);

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

	g_clear_object(&task);

	test_backend->load_conversations_async += 1;
}

static GListModel *
test_purple_conversation_manager_backend_load_conversations_finish(PurpleConversationManagerBackend *backend,
                                                                   GAsyncResult *result,
                                                                   GError **error)
{
	TestPurpleConversationManagerBackend *test_backend = NULL;

	test_backend = TEST_PURPLE_CONVERSATION_MANAGER_BACKEND(backend);
	test_backend->load_conversations_finish += 1;

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

static void
test_purple_conversation_manager_backend_save_conversation_async(PurpleConversationManagerBackend *backend,
                                                                 PurpleConversation *conversation,
                                                                 GCancellable *cancellable,
                                                                 GAsyncReadyCallback callback,
                                                                 gpointer data)
{
	TestPurpleConversationManagerBackend *test_backend = NULL;
	GTask *task = NULL;

	birb_assert_type(conversation, PURPLE_TYPE_CONVERSATION);

	test_backend = TEST_PURPLE_CONVERSATION_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_conversation_async += 1;
}

static gboolean
test_purple_conversation_manager_backend_save_conversation_finish(PurpleConversationManagerBackend *backend,
                                                                  GAsyncResult *result,
                                                                  GError **error)
{
	TestPurpleConversationManagerBackend *test_backend = NULL;

	test_backend = TEST_PURPLE_CONVERSATION_MANAGER_BACKEND(backend);
	test_backend->save_conversation_finish += 1;

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

static void
test_purple_conversation_manager_backend_init(TestPurpleConversationManagerBackend *backend)
{
	backend->delete_conversation_async = 0;
	backend->delete_conversation_finish = 0;
	backend->load_conversations_async = 0;
	backend->load_conversations_finish = 0;
	backend->save_conversation_async = 0;
	backend->save_conversation_finish = 0;
}

static void
test_purple_conversation_manager_backend_class_init(TestPurpleConversationManagerBackendClass *klass)
{
	PurpleConversationManagerBackendClass *backend_class = NULL;

	backend_class = PURPLE_CONVERSATION_MANAGER_BACKEND_CLASS(klass);
	backend_class->delete_conversation_async =
		test_purple_conversation_manager_backend_delete_conversation_async;
	backend_class->delete_conversation_finish =
		test_purple_conversation_manager_backend_delete_conversation_finish;
	backend_class->load_conversations_async =
		test_purple_conversation_manager_backend_load_conversations_async;
	backend_class->load_conversations_finish =
		test_purple_conversation_manager_backend_load_conversations_finish;
	backend_class->save_conversation_async =
		test_purple_conversation_manager_backend_save_conversation_async;
	backend_class->save_conversation_finish =
		test_purple_conversation_manager_backend_save_conversation_finish;
}

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

	birb_assert_type(source, TEST_PURPLE_TYPE_CONVERSATION_MANAGER_BACKEND);

	backend = PURPLE_CONVERSATION_MANAGER_BACKEND(source);
	success = purple_conversation_manager_backend_delete_conversation_finish(backend, result,
	                                                                         &error);
	g_assert_no_error(error);

	g_assert_true(success);

	*counter = *counter + 1;
}

static void
test_purple_conversation_manager_backend_normal_delete_conversation(void) {
	TestPurpleConversationManagerBackend *test_backend = NULL;
	PurpleConversation *conversation = NULL;
	PurpleConversationManagerBackend *backend = NULL;
	guint counter = 0;

	backend = g_object_new(TEST_PURPLE_TYPE_CONVERSATION_MANAGER_BACKEND,
	                       NULL);
	test_backend = TEST_PURPLE_CONVERSATION_MANAGER_BACKEND(backend);

	conversation = g_object_new(PURPLE_TYPE_CONVERSATION, NULL);

	purple_conversation_manager_backend_delete_conversation_async(backend,
	                                                              conversation,
	                                                              NULL,
	                                                              test_purple_conversation_manager_backend_normal_delete_conversation_cb,
	                                                              &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_cmpuint(test_backend->delete_conversation_async, ==, 1);
	g_assert_cmpuint(test_backend->delete_conversation_finish, ==, 1);
	g_assert_cmpuint(test_backend->load_conversations_async, ==, 0);
	g_assert_cmpuint(test_backend->load_conversations_finish, ==, 0);
	g_assert_cmpuint(test_backend->save_conversation_async, ==, 0);
	g_assert_cmpuint(test_backend->save_conversation_finish, ==, 0);

	g_clear_object(&backend);
	g_clear_object(&conversation);
}

static void
test_purple_conversation_manager_backend_normal_load_conversations_cb(GObject *source,
                                                                      GAsyncResult *result,
                                                                      gpointer data)
{
	PurpleConversationManagerBackend *backend = NULL;
	GError *error = NULL;
	GListModel *conversations = NULL;
	guint *counter = data;

	birb_assert_type(source, TEST_PURPLE_TYPE_CONVERSATION_MANAGER_BACKEND);

	backend = PURPLE_CONVERSATION_MANAGER_BACKEND(source);
	conversations = purple_conversation_manager_backend_load_conversations_finish(backend,
	                                                                              result,
	                                                                              &error);
	g_assert_no_error(error);

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

	*counter = *counter + 1;
}

static void
test_purple_conversation_manager_backend_normal_load_conversations(void) {
	TestPurpleConversationManagerBackend *test_backend = NULL;
	PurpleConversationManagerBackend *backend = NULL;
	guint counter = 0;

	backend = g_object_new(TEST_PURPLE_TYPE_CONVERSATION_MANAGER_BACKEND,
	                       NULL);
	test_backend = TEST_PURPLE_CONVERSATION_MANAGER_BACKEND(backend);

	purple_conversation_manager_backend_load_conversations_async(backend,
	                                                             NULL,
	                                                             test_purple_conversation_manager_backend_normal_load_conversations_cb,
	                                                             &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_cmpuint(test_backend->delete_conversation_async, ==, 0);
	g_assert_cmpuint(test_backend->delete_conversation_finish, ==, 0);
	g_assert_cmpuint(test_backend->load_conversations_async, ==, 1);
	g_assert_cmpuint(test_backend->load_conversations_finish, ==, 1);
	g_assert_cmpuint(test_backend->save_conversation_async, ==, 0);
	g_assert_cmpuint(test_backend->save_conversation_finish, ==, 0);

	g_clear_object(&backend);
}

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

	birb_assert_type(source, TEST_PURPLE_TYPE_CONVERSATION_MANAGER_BACKEND);

	backend = PURPLE_CONVERSATION_MANAGER_BACKEND(source);
	success = purple_conversation_manager_backend_save_conversation_finish(backend,
	                                                                       result,
	                                                                       &error);
	g_assert_no_error(error);

	g_assert_true(success);

	*counter = *counter + 1;
}

static void
test_purple_conversation_manager_backend_normal_save_conversation(void) {
	TestPurpleConversationManagerBackend *test_backend = NULL;
	PurpleConversation *conversation = NULL;
	PurpleConversationManagerBackend *backend = NULL;
	guint counter = 0;

	backend = g_object_new(TEST_PURPLE_TYPE_CONVERSATION_MANAGER_BACKEND,
	                       NULL);
	test_backend = TEST_PURPLE_CONVERSATION_MANAGER_BACKEND(backend);

	conversation = g_object_new(PURPLE_TYPE_CONVERSATION, NULL);

	purple_conversation_manager_backend_save_conversation_async(backend,
	                                                            conversation,
	                                                            NULL,
	                                                            test_purple_conversation_manager_backend_normal_save_conversation_cb,
	                                                            &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_cmpuint(test_backend->delete_conversation_async, ==, 0);
	g_assert_cmpuint(test_backend->delete_conversation_finish, ==, 0);
	g_assert_cmpuint(test_backend->load_conversations_async, ==, 0);
	g_assert_cmpuint(test_backend->load_conversations_finish, ==, 0);
	g_assert_cmpuint(test_backend->save_conversation_async, ==, 1);
	g_assert_cmpuint(test_backend->save_conversation_finish, ==, 1);

	g_clear_object(&backend);
	g_clear_object(&conversation);
}

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

	g_test_add_func("/conversation-manager-backend-normal/delete-conversation",
	                test_purple_conversation_manager_backend_normal_delete_conversation);
	g_test_add_func("/conversation-manager-backend-normal/load-conversations",
	                test_purple_conversation_manager_backend_normal_load_conversations);
	g_test_add_func("/conversation-manager-backend-normal/save-conversation",
	                test_purple_conversation_manager_backend_normal_save_conversation);

	return g_test_run();
}
