/*
 * Purple - Internet Messaging Library
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * Purple is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 */

#include <glib.h>

#include <birb.h>

#include <purple.h>

/******************************************************************************
 * Basic Connection Implementation
 *****************************************************************************/
G_DECLARE_FINAL_TYPE(TestPurpleConnection, test_purple_connection, TEST_PURPLE,
                     CONNECTION, PurpleConnection)

struct _TestPurpleConnection {
	PurpleConnection parent;

	guint get_action_menu;

	guint set_display_name_async;
	guint set_display_name_finish;
};

G_DEFINE_FINAL_TYPE(TestPurpleConnection, test_purple_connection,
                    PURPLE_TYPE_CONNECTION)

static BirbActionMenu *
test_purple_connection_get_action_menu(PurpleConnection *connection)
{
	TestPurpleConnection *test_connection = NULL;

	birb_assert_type(connection, test_purple_connection_get_type());

	test_connection = TEST_PURPLE_CONNECTION(connection);
	test_connection->get_action_menu += 1;

	return birb_action_menu_new();
}

static void
test_purple_connection_set_display_name_async(PurpleConnection *connection,
                                              const char *display_name,
                                              GCancellable *cancellable,
                                              GAsyncReadyCallback callback,
                                              gpointer data)
{
	TestPurpleConnection *test_connection = TEST_PURPLE_CONNECTION(connection);
	GTask *task = NULL;

	birb_assert_type(connection, test_purple_connection_get_type());

	g_assert_cmpstr(display_name, ==, "name-displayed");

	task = g_task_new(G_OBJECT(connection), cancellable, callback, data);
	g_task_return_boolean(task, TRUE);
	g_clear_object(&task);

	test_connection->set_display_name_async += 1;
}

static gboolean
test_purple_connection_set_display_name_finish(PurpleConnection *connection,
                                               GAsyncResult *result,
                                               GError **error)

{	TestPurpleConnection *test_connection = TEST_PURPLE_CONNECTION(connection);

	test_connection->set_display_name_finish += 1;

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

static void
test_purple_connection_init(TestPurpleConnection *connection) {
	connection->get_action_menu = 0;

	connection->set_display_name_async = 0;
	connection->set_display_name_finish = 0;
}

static void
test_purple_connection_class_init(TestPurpleConnectionClass *klass) {
	PurpleConnectionClass *connection_class = PURPLE_CONNECTION_CLASS(klass);

	connection_class->get_action_menu = test_purple_connection_get_action_menu;
	connection_class->set_display_name_async =
		test_purple_connection_set_display_name_async;
	connection_class->set_display_name_finish =
		test_purple_connection_set_display_name_finish;
}

/******************************************************************************
 * Tests
 *****************************************************************************/
static void
test_purple_connection_properties(void) {
	PurpleAccount *account = NULL;
	PurpleAccount *account1 = NULL;
	PurpleConnection *connection = NULL;
	GCancellable *cancellable = NULL;
	char *id = NULL;
	char *password = NULL;

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

	connection = g_object_new(
		PURPLE_TYPE_CONNECTION,
		"account", account,
		"id", "abc123",
		"password", "hunter2",
		NULL);

	birb_assert_type(connection, PURPLE_TYPE_CONNECTION);

	g_object_get(
		G_OBJECT(connection),
		"account", &account1,
		"cancellable", &cancellable,
		"id", &id,
		"password", &password,
		NULL);

	g_assert_true(account1 == account);
	g_clear_object(&account1);

	birb_assert_type(cancellable, G_TYPE_CANCELLABLE);
	g_clear_object(&cancellable);

	g_assert_cmpstr(id, ==, "abc123");
	g_clear_pointer(&id, g_free);

	g_assert_cmpstr(password, ==, "hunter2");
	g_clear_pointer(&password, g_free);

	g_assert_finalize_object(connection);
	g_assert_finalize_object(account);
}

static void
test_purple_connection_set_presence_default(void) {
	PurpleAccount *account = NULL;
	PurpleConnection *connection = NULL;
	PurpleContactInfo *info = NULL;
	PurplePresence *account_presence = NULL;
	PurplePresencePrimitive actual_primitive = PURPLE_PRESENCE_PRIMITIVE_OFFLINE;
	PurplePresencePrimitive expected_primitive = PURPLE_PRESENCE_PRIMITIVE_DO_NOT_DISTURB;
	PurpleSavedPresence *saved_presence = NULL;
	const char *actual_emoji = NULL;
	const char *actual_message = NULL;
	const char *expected_emoji = "👻";
	const char *expected_message = "Boo!";

	saved_presence = g_object_new(
		PURPLE_TYPE_SAVED_PRESENCE,
		"emoji", expected_emoji,
		"message", expected_message,
		"primitive", expected_primitive,
		NULL);

	/* Create the account and verify its default presence. */
	account = purple_account_new("test", "test");
	info = purple_account_get_contact_info(account);
	account_presence = purple_contact_info_get_presence(info);

	actual_emoji = purple_presence_get_emoji(account_presence);
	g_assert_cmpstr(actual_emoji, ==, NULL);

	actual_message = purple_presence_get_message(account_presence);
	g_assert_cmpstr(actual_message, ==, NULL);

	actual_primitive = purple_presence_get_primitive(account_presence);
	g_assert_cmpuint(actual_primitive, ==, PURPLE_PRESENCE_PRIMITIVE_OFFLINE);

	/* Create the connection and call set_presence on it. */
	connection = g_object_new(
		PURPLE_TYPE_CONNECTION,
		"account", account,
		NULL);

	purple_connection_set_presence(connection, saved_presence);

	/* Verify that the account presence was updated. */
	actual_emoji = purple_presence_get_emoji(account_presence);
	g_assert_cmpstr(actual_emoji, ==, expected_emoji);

	actual_message = purple_presence_get_message(account_presence);
	g_assert_cmpstr(actual_message, ==, expected_message);

	actual_primitive = purple_presence_get_primitive(account_presence);
	g_assert_cmpuint(actual_primitive, ==, expected_primitive);

	g_assert_finalize_object(saved_presence);
	g_assert_finalize_object(connection);
	g_assert_finalize_object(account);
}

static void
test_purple_connection_get_action_menu_valid(void) {
	TestPurpleConnection *test_connection = NULL;
	PurpleConnection *connection = NULL;
	BirbActionMenu *action_menu = NULL;

	connection = g_object_new(test_purple_connection_get_type(), NULL);
	test_connection = TEST_PURPLE_CONNECTION(connection);

	action_menu = purple_connection_get_action_menu(connection);
	g_assert_true(BIRB_IS_ACTION_MENU(action_menu));
	g_clear_object(&action_menu);
	g_assert_cmpuint(test_connection->get_action_menu, ==, 1);

	g_assert_finalize_object(connection);
}

/******************************************************************************
 * Set display name tests
 *****************************************************************************/
static void
test_purple_connection_implements_set_display_name(void) {
	PurpleConnection *connection = NULL;

	connection = g_object_new(test_purple_connection_get_type(), NULL);

	g_assert_true(purple_connection_implements_set_display_name(connection));

	g_assert_finalize_object(connection);
}

static void
test_purple_connection_set_display_name_cb(GObject *source, GAsyncResult *result,
                                         G_GNUC_UNUSED gpointer data)
{
	PurpleConnection *connection = PURPLE_CONNECTION(source);
	GError *error = NULL;
	gboolean success = FALSE;

	birb_assert_type(connection, test_purple_connection_get_type());

	success = purple_connection_set_display_name_finish(connection, result,
	                                                    &error);
	g_assert_no_error(error);
	g_assert_true(success);
}

static void
test_purple_connection_set_display_name(void) {
	TestPurpleConnection *test_connection = NULL;
	PurpleConnection *connection = NULL;

	connection = g_object_new(test_purple_connection_get_type(), NULL);
	test_connection = TEST_PURPLE_CONNECTION(connection);

	purple_connection_set_display_name_async(connection, "name-displayed",
	                                         NULL,
	                                         test_purple_connection_set_display_name_cb,
	                                         NULL);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(test_connection->set_display_name_async, ==, 1);
	g_assert_cmpuint(test_connection->set_display_name_finish, ==, 1);

	g_assert_finalize_object(connection);
}

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

	g_test_add_func("/connection/properties",
	                test_purple_connection_properties);

	g_test_add_func("/connection/set_presence_default",
	                test_purple_connection_set_presence_default);

	g_test_add_func("/connection/get-action-menu/valid",
	                test_purple_connection_get_action_menu_valid);

	g_test_add_func("/connection/implements-set-display-name",
	                test_purple_connection_implements_set_display_name);
	g_test_add_func("/connection/set-display-name",
	                test_purple_connection_set_display_name);

	return g_test_run();
}
