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

#include "test_ui.h"

#define PURPLE_GLOBAL_HEADER_INSIDE
#include "../purplehistoryadapterprivate.h"
#undef PURPLE_GLOBAL_HEADER_INSIDE

/******************************************************************************
 * TestPurpleHistoryAdapter Implementation
 *****************************************************************************/
#define TEST_PURPLE_TYPE_HISTORY_ADAPTER \
	(test_purple_history_adapter_get_type())
G_DECLARE_FINAL_TYPE(TestPurpleHistoryAdapter,
                     test_purple_history_adapter,
                     TEST_PURPLE, HISTORY_ADAPTER,
                     PurpleHistoryAdapter)

struct _TestPurpleHistoryAdapter {
	PurpleHistoryAdapter parent;

	gboolean activate_called;
	gboolean deactivate_called;
	gboolean query_called;
	gboolean remove_called;
	gboolean write_called;
};

G_DEFINE_FINAL_TYPE(TestPurpleHistoryAdapter,
                    test_purple_history_adapter,
                    PURPLE_TYPE_HISTORY_ADAPTER)

static gboolean
test_purple_history_adapter_activate(PurpleHistoryAdapter *a,
                                     G_GNUC_UNUSED GError **error)
{
	TestPurpleHistoryAdapter *ta = TEST_PURPLE_HISTORY_ADAPTER(a);

	ta->activate_called = TRUE;

	return TRUE;
}

static gboolean
test_purple_history_adapter_deactivate(PurpleHistoryAdapter *a,
                                       G_GNUC_UNUSED GError **error)
{
	TestPurpleHistoryAdapter *ta = TEST_PURPLE_HISTORY_ADAPTER(a);

	ta->deactivate_called = TRUE;

	return TRUE;
}

static GList *
test_purple_history_adapter_query(PurpleHistoryAdapter *a,
                                  G_GNUC_UNUSED const gchar *id,
                                  G_GNUC_UNUSED GError **error)
{
	TestPurpleHistoryAdapter *ta = TEST_PURPLE_HISTORY_ADAPTER(a);

	ta->query_called = TRUE;

	return NULL;
}

static gboolean
test_purple_history_adapter_remove(PurpleHistoryAdapter *a,
                                   G_GNUC_UNUSED const gchar *id,
                                   G_GNUC_UNUSED GError **error)
{
	TestPurpleHistoryAdapter *ta = TEST_PURPLE_HISTORY_ADAPTER(a);

	ta->remove_called = TRUE;

	return TRUE;
}

static gboolean
test_purple_history_adapter_write(PurpleHistoryAdapter *a,
                                  G_GNUC_UNUSED PurpleConversation *conversation,
                                  G_GNUC_UNUSED PurpleMessage *message,
                                  G_GNUC_UNUSED GError **error)
{
	TestPurpleHistoryAdapter *ta = TEST_PURPLE_HISTORY_ADAPTER(a);

	ta->write_called = TRUE;

	return TRUE;
}

static void
test_purple_history_adapter_init(G_GNUC_UNUSED TestPurpleHistoryAdapter *adapter)
{
}

static void
test_purple_history_adapter_class_init(TestPurpleHistoryAdapterClass *klass)
{
	PurpleHistoryAdapterClass *adapter_class = PURPLE_HISTORY_ADAPTER_CLASS(klass);

	adapter_class->activate = test_purple_history_adapter_activate;
	adapter_class->deactivate = test_purple_history_adapter_deactivate;
	adapter_class->query = test_purple_history_adapter_query;
	adapter_class->remove = test_purple_history_adapter_remove;
	adapter_class->write = test_purple_history_adapter_write;
}

static PurpleHistoryAdapter *
test_purple_history_adapter_new(void) {
	return g_object_new(
		TEST_PURPLE_TYPE_HISTORY_ADAPTER,
		"id", "test-adapter",
		"name", "Test Adapter",
		NULL);
}

/******************************************************************************
 * Callbacks
 *****************************************************************************/
static void
test_purple_history_manager_added_removed_cb(PurpleHistoryManager *manager,
                                             PurpleHistoryAdapter *adapter,
                                             gpointer data)
{
	guint *counter = data;

	g_assert_true(PURPLE_IS_HISTORY_MANAGER(manager));
	g_assert_true(PURPLE_IS_HISTORY_ADAPTER(adapter));

	*counter = *counter + 1;
}

/******************************************************************************
 * Add/Remove Tests
 *****************************************************************************/
static void
test_purple_history_manager_new(void) {
	PurpleHistoryManager *manager = NULL;

	manager = g_object_new(PURPLE_TYPE_HISTORY_MANAGER, NULL);

	g_assert_true(PURPLE_IS_HISTORY_MANAGER(manager));
	g_assert_true(G_IS_LIST_MODEL(manager));

	g_assert_finalize_object(manager);
}

static void
test_purple_history_manager_properties(void) {
	PurpleHistoryManager *manager = NULL;
	GType item_type = G_TYPE_INVALID;
	guint n_items = 0;

	manager = g_object_new(PURPLE_TYPE_HISTORY_MANAGER, NULL);

	g_object_get(
		G_OBJECT(manager),
		"item-type", &item_type,
		"n-items", &n_items,
		NULL);

	g_assert_true(item_type == PURPLE_TYPE_HISTORY_ADAPTER);

	/* While there aren't any items in the manager, this is still testing that
	 * the property is readable.
	 */
	g_assert_cmpuint(n_items, ==, 0);

	g_assert_finalize_object(manager);
}

static void
test_purple_history_manager_add_remove(void) {
	PurpleHistoryManager *manager = NULL;
	PurpleHistoryAdapter *adapter = NULL;
	GError *error = NULL;
	gboolean r = FALSE;
	guint added = 0;
	guint removed = 0;
	guint changed = 0;

	manager = g_object_new(PURPLE_TYPE_HISTORY_MANAGER, NULL);
	g_assert_true(PURPLE_IS_HISTORY_MANAGER(manager));

	g_signal_connect(manager, "added",
	                 G_CALLBACK(test_purple_history_manager_added_removed_cb),
	                 &added);
	g_signal_connect(manager, "removed",
	                 G_CALLBACK(test_purple_history_manager_added_removed_cb),
	                 &removed);
	birb_count_list_model_items_changed(G_LIST_MODEL(manager), &changed);

	adapter = test_purple_history_adapter_new();

	/* Add the first time cleanly. */
	added = 0;
	removed = 0;
	changed = 0;
	r = purple_history_manager_add(manager, adapter, &error);
	g_assert_no_error(error);
	g_assert_true(r);
	g_assert_cmpuint(added, ==, 1);
	g_assert_cmpuint(removed, ==, 0);
	g_assert_cmpuint(changed, ==, 1);

	/* Add again and verify the error. */
	added = 0;
	removed = 0;
	changed = 0;
	r = purple_history_manager_add(manager, adapter, &error);
	g_assert_false(r);
	g_assert_error(error, PURPLE_HISTORY_MANAGER_DOMAIN, 0);
	g_clear_error(&error);
	g_assert_cmpuint(added, ==, 0);
	g_assert_cmpuint(removed, ==, 0);
	g_assert_cmpuint(changed, ==, 0);

	/* Add the adapter. */
	added = 0;
	removed = 0;
	changed = 0;
	r = purple_history_manager_remove(manager, adapter, &error);
	g_assert_no_error(error);
	g_assert_true(r);
	g_assert_cmpuint(added, ==, 0);
	g_assert_cmpuint(removed, ==, 1);
	g_assert_cmpuint(changed, ==, 1);

	/* Remove the adapter again and verify the error. */
	added = 0;
	removed = 0;
	changed = 0;
	r = purple_history_manager_remove(manager, adapter, &error);
	g_assert_false(r);
	g_assert_error(error, PURPLE_HISTORY_MANAGER_DOMAIN, 0);
	g_clear_error(&error);
	g_assert_cmpuint(added, ==, 0);
	g_assert_cmpuint(removed, ==, 0);
	g_assert_cmpuint(changed, ==, 0);

	/* Final clean ups. */
	g_clear_object(&adapter);
	g_clear_object(&manager);
}

/******************************************************************************
 * Set Active Tests
 *****************************************************************************/
static void
test_purple_history_manager_set_active_null(void) {
	PurpleHistoryManager *manager = NULL;
	GError *error = NULL;
	gboolean ret = FALSE;

	manager = g_object_new(PURPLE_TYPE_HISTORY_MANAGER, NULL);
	ret = purple_history_manager_set_active(manager, NULL, &error);

	g_assert_no_error(error);
	g_assert_true(ret);

	g_clear_object(&manager);
}

static void
test_purple_history_manager_set_active_non_existent(void) {
	PurpleHistoryManager *manager = NULL;
	GError *error = NULL;
	gboolean ret = FALSE;

	manager = g_object_new(PURPLE_TYPE_HISTORY_MANAGER, NULL);
	ret = purple_history_manager_set_active(manager, "foo", &error);

	g_assert_error(error, PURPLE_HISTORY_MANAGER_DOMAIN, 0);
	g_assert_false(ret);
	g_clear_error(&error);

	g_clear_object(&manager);
}

static void
test_purple_history_manager_set_active_normal(void) {
	PurpleHistoryManager *manager = NULL;
	PurpleHistoryAdapter *adapter = NULL;
	GError *error = NULL;
	gboolean r = FALSE;
	TestPurpleHistoryAdapter *ta = NULL;

	manager = g_object_new(PURPLE_TYPE_HISTORY_MANAGER, NULL);

	/* Create the adapter and add it to the manager. */
	adapter = test_purple_history_adapter_new();
	ta = TEST_PURPLE_HISTORY_ADAPTER(adapter);
	r = purple_history_manager_add(manager, adapter, &error);
	g_assert_no_error(error);
	g_assert_true(r);

	/* Set the adapter as active and verify it was successful. */
	r = purple_history_manager_set_active(manager, "test-adapter",
	                                      &error);
	g_assert_no_error(error);
	g_assert_true(r);
	g_assert_true(ta->activate_called);

	/* Verify that removing the active adapter fails */
	r = purple_history_manager_remove(manager, adapter,
	                                      &error);
	g_assert_error(error, PURPLE_HISTORY_MANAGER_DOMAIN, 0);
	g_assert_false(r);
	g_clear_error(&error);

	/* Now unset the active adapter. */
	r = purple_history_manager_set_active(manager, NULL, &error);
	g_assert_no_error(error);
	g_assert_true(r);
	g_assert_true(ta->deactivate_called);

	/* Finally remove the adapter now that it's no longer active. */
	r = purple_history_manager_remove(manager, adapter, &error);
	g_assert_no_error(error);
	g_assert_true(r);

	/* And our final cleanup. */
	g_clear_object(&adapter);
	g_clear_object(&manager);
}

/******************************************************************************
 * No Adapter Tests
 *****************************************************************************/
static void
test_purple_history_manager_no_adapter_query(void) {
	PurpleHistoryManager *manager = NULL;
	GList *list = NULL;
	GError *error = NULL;

	manager = g_object_new(PURPLE_TYPE_HISTORY_MANAGER, NULL);
	list = purple_history_manager_query(manager, "", &error);

	g_assert_error(error, PURPLE_HISTORY_MANAGER_DOMAIN, 0);
	g_clear_error(&error);

	g_assert_null(list);

	g_clear_object(&manager);
}

static void
test_purple_history_manager_no_adapter_remove_query(void) {
	PurpleHistoryManager *manager = NULL;
	GError *error = NULL;
	gboolean result = FALSE;

	manager = g_object_new(PURPLE_TYPE_HISTORY_MANAGER, NULL);
	result = purple_history_manager_remove_query(manager, "", &error);

	g_assert_error(error, PURPLE_HISTORY_MANAGER_DOMAIN, 0);
	g_clear_error(&error);

	g_assert_false(result);

	g_clear_object(&manager);
}

static void
test_purple_history_manager_no_adapter_write(void) {
	PurpleAccount *account = NULL;
	PurpleConversation *conversation = NULL;
	PurpleHistoryManager *manager = NULL;
	PurpleMessage *message = NULL;
	GError *error = NULL;
	gboolean result = FALSE;

	manager = g_object_new(PURPLE_TYPE_HISTORY_MANAGER, NULL);

	message = g_object_new(PURPLE_TYPE_MESSAGE, NULL);
	account = purple_account_new("test", "test");
	conversation = g_object_new(PURPLE_TYPE_CONVERSATION,
	                            "account", account,
	                            "type", PURPLE_CONVERSATION_TYPE_DM,
	                            NULL);

	result = purple_history_manager_write(manager, conversation, message,
	                                      &error);

	g_assert_error(error, PURPLE_HISTORY_MANAGER_DOMAIN, 0);
	g_clear_error(&error);

	g_assert_false(result);

	g_clear_object(&message);
	g_clear_object(&conversation);
	g_clear_object(&account);
	g_clear_object(&manager);
}

/******************************************************************************
 * Manager Tests
 *****************************************************************************/
static void
test_purple_history_manager_adapter_query(void) {
	PurpleHistoryManager *manager = NULL;
	PurpleHistoryAdapter *adapter = NULL;
	TestPurpleHistoryAdapter *ta = NULL;
	GList *list = NULL;
	GError *error = NULL;
	gboolean result = FALSE;

	manager = g_object_new(PURPLE_TYPE_HISTORY_MANAGER, NULL);
	adapter = test_purple_history_adapter_new();
	ta = TEST_PURPLE_HISTORY_ADAPTER(adapter);

	result = purple_history_manager_add(manager, adapter, &error);
	g_assert_no_error(error);
	g_assert_true(result);

	result = purple_history_manager_set_active(manager, "test-adapter",
	                                           &error);
	g_assert_no_error(error);
	g_assert_true(result);

	list = purple_history_manager_query(manager, "", &error);
	g_assert_no_error(error);
	g_assert_null(list);
	g_assert_true(ta->query_called);

	result = purple_history_manager_set_active(manager, NULL, &error);
	g_assert_no_error(error);
	g_assert_true(result);

	result = purple_history_manager_remove(manager, adapter, &error);
	g_assert_no_error(error);
	g_assert_true(result);

	g_clear_object(&adapter);
	g_clear_object(&manager);
}

static void
test_purple_history_manager_adapter_remove_query(void) {
	PurpleHistoryManager *manager = NULL;
	PurpleHistoryAdapter *adapter = NULL;
	TestPurpleHistoryAdapter *ta = NULL;
	GError *error = NULL;
	gboolean result = FALSE;

	manager = g_object_new(PURPLE_TYPE_HISTORY_MANAGER, NULL);
	adapter = test_purple_history_adapter_new();
	ta = TEST_PURPLE_HISTORY_ADAPTER(adapter);

	result = purple_history_manager_add(manager, adapter, &error);
	g_assert_no_error(error);
	g_assert_true(result);

	result = purple_history_manager_set_active(manager, "test-adapter",
	                                           &error);
	g_assert_no_error(error);
	g_assert_true(result);

	result = purple_history_manager_remove_query(manager, "query", &error);
	g_assert_no_error(error);
	g_assert_true(result);
	g_assert_true(ta->remove_called);

	result = purple_history_manager_set_active(manager, NULL, &error);
	g_assert_no_error(error);
	g_assert_true(result);

	result = purple_history_manager_remove(manager, adapter, &error);
	g_assert_no_error(error);
	g_assert_true(result);

	g_clear_object(&adapter);
	g_clear_object(&manager);
}

static void
test_purple_history_manager_adapter_write(void) {
	PurpleAccount *account = NULL;
	PurpleConversation *conversation = NULL;
	PurpleHistoryManager *manager = NULL;
	PurpleHistoryAdapter *adapter = NULL;
	PurpleMessage *message = NULL;
	TestPurpleHistoryAdapter *ta = NULL;
	GError *error = NULL;
	gboolean result = FALSE;

	manager = g_object_new(PURPLE_TYPE_HISTORY_MANAGER, NULL);
	adapter = test_purple_history_adapter_new();
	ta = TEST_PURPLE_HISTORY_ADAPTER(adapter);

	result = purple_history_manager_add(manager, adapter, &error);
	g_assert_no_error(error);
	g_assert_true(result);

	result = purple_history_manager_set_active(manager, "test-adapter", &error);
	g_assert_no_error(error);
	g_assert_true(result);

	message = g_object_new(PURPLE_TYPE_MESSAGE, NULL);
	account = purple_account_new("test", "test");
	conversation = g_object_new(PURPLE_TYPE_CONVERSATION,
	                            "account", account,
	                            "type", PURPLE_CONVERSATION_TYPE_DM,
	                            NULL);
	result = purple_history_manager_write(manager, conversation, message,
	                                      &error);
	g_assert_no_error(error);
	g_assert_true(result);
	g_assert_true(ta->write_called);

	result = purple_history_manager_set_active(manager, NULL, &error);
	g_assert_no_error(error);
	g_assert_true(result);

	result = purple_history_manager_remove(manager, adapter, &error);
	g_assert_no_error(error);
	g_assert_true(result);

	g_clear_object(&adapter);
	g_clear_object(&message);
	g_clear_object(&conversation);
	g_clear_object(&account);
	g_clear_object(&manager);
}

/******************************************************************************
 * Main
 *****************************************************************************/
gint
main(gint argc, gchar *argv[]) {
	gint ret = 0;

	g_test_init(&argc, &argv, NULL);

	test_ui_purple_init();

	g_test_add_func("/history-manager/new",
	                test_purple_history_manager_new);
	g_test_add_func("/history-manager/properties",
	                test_purple_history_manager_properties);
	g_test_add_func("/history-manager/add-remove",
	                test_purple_history_manager_add_remove);
	g_test_add_func("/history-manager/set-active/null",
	                test_purple_history_manager_set_active_null);
	g_test_add_func("/history-manager/set-active/non-existent",
	                test_purple_history_manager_set_active_non_existent);
	g_test_add_func("/history-manager/set-active/normal",
	                test_purple_history_manager_set_active_normal);

	/* Tests for manager with an adapter */
	g_test_add_func("/history-manager/adapter/query",
	                test_purple_history_manager_adapter_query);
	g_test_add_func("/history-manager/adapter/remove-query",
	                test_purple_history_manager_adapter_remove_query);
	g_test_add_func("/history-manager/adapter/write",
	                test_purple_history_manager_adapter_write);

	/* Tests for manager with no adapter */
	g_test_add_func("/history-manager/no-adapter/query",
	                test_purple_history_manager_no_adapter_query);
	g_test_add_func("/history-manager/no-adapter/remove-query",
	                test_purple_history_manager_no_adapter_remove_query);
	g_test_add_func("/history-manager/no-adapter/write",
	                test_purple_history_manager_no_adapter_write);

	ret = g_test_run();

	test_ui_purple_uninit();

	return ret;
}
