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

/******************************************************************************
 * Tests
 *****************************************************************************/
static void
test_purple_contacts_add_removed_counter_cb(PurpleContacts *contacts,
                                            PurpleContact *contact,
                                            gpointer data)
{
	guint *counter = data;

	birb_assert_type(contacts, PURPLE_TYPE_CONTACTS);
	birb_assert_type(contact, PURPLE_TYPE_CONTACT);

	*counter = *counter + 1;
}

/******************************************************************************
 * Tests
 *****************************************************************************/
static void
test_purple_contacts_new(void) {
	PurpleContacts *contacts = NULL;

	contacts = purple_contacts_new();

	birb_assert_type(contacts, PURPLE_TYPE_CONTACTS);
	g_assert_true(G_IS_LIST_MODEL(contacts));

	g_assert_finalize_object(contacts);
}

static void
test_purple_contacts_properties(void) {
	PurpleContacts *contacts = NULL;
	GType item_type = G_TYPE_INVALID;
	guint n_items = 0;

	contacts = g_object_new(
		PURPLE_TYPE_CONTACTS,
		NULL);

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

	g_assert_cmpuint(item_type, ==, PURPLE_TYPE_CONTACT);

	g_assert_cmpuint(n_items, ==, 0);

	g_assert_finalize_object(contacts);
}

static void
test_purple_contacts_add_remove(void) {
	PurpleAccount *account = NULL;
	PurpleContact *candidate = NULL;
	PurpleContact *contact1 = NULL;
	PurpleContact *contact2 = NULL;
	PurpleContacts *contacts = NULL;
	guint added = 0;
	guint counter = 0;
	guint removed = 0;
	gboolean result = FALSE;

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

	contacts = purple_contacts_new();
	birb_count_list_model_items_changed(G_LIST_MODEL(contacts), &counter);
	g_signal_connect(contacts, "added",
	                 G_CALLBACK(test_purple_contacts_add_removed_counter_cb),
	                 &added);
	g_signal_connect(contacts, "removed",
	                 G_CALLBACK(test_purple_contacts_add_removed_counter_cb),
	                 &removed);

	birb_assert_list_model_n_items(contacts, 0);

	/* Add the first contact. */
	added = 0;
	counter = 0;
	removed = 0;
	contact1 = purple_contact_new(account, "contact1");
	result = purple_contacts_add(contacts, contact1);
	g_assert_true(result);
	birb_assert_list_model_n_items(contacts, 1);
	g_assert_cmpuint(added, ==, 1);
	g_assert_cmpuint(counter, ==, 1);
	g_assert_cmpuint(removed, ==, 0);

	/* Add the second contact. */
	added = 0;
	counter = 0;
	removed = 0;
	contact2 = purple_contact_new(account, "contact2");
	result = purple_contacts_add(contacts, contact2);
	g_assert_true(result);
	birb_assert_list_model_n_items(contacts, 2);
	g_assert_cmpuint(added, ==, 1);
	g_assert_cmpuint(counter, ==, 1);
	g_assert_cmpuint(removed, ==, 0);

	/* Attempt to add the first contact again. */
	added = 0;
	counter = 0;
	removed = 0;
	result = purple_contacts_add(contacts, contact1);
	g_assert_false(result);
	birb_assert_list_model_n_items(contacts, 2);
	g_assert_cmpuint(added, ==, 0);
	g_assert_cmpuint(counter, ==, 0);
	g_assert_cmpuint(removed, ==, 0);

	/* Now make sure we can find the first contact. */
	candidate = purple_contacts_find_with_id(contacts, "contact1");
	g_assert_true(purple_contact_equal(candidate, contact1));

	/* Find the second contact. */
	candidate = purple_contacts_find_with_id(contacts, "contact2");
	g_assert_true(purple_contact_equal(candidate, contact2));

	/* Remove the first contact. */
	added = 0;
	counter = 0;
	removed = 0;
	result = purple_contacts_remove(contacts, contact1);
	g_assert_true(result);
	birb_assert_list_model_n_items(contacts, 1);
	g_assert_cmpuint(added, ==, 0);
	g_assert_cmpuint(counter, ==, 1);
	g_assert_cmpuint(removed, ==, 1);

	/* Attempt to remove the first contact again. */
	added = 0;
	counter = 0;
	removed = 0;
	result = purple_contacts_remove(contacts, contact1);
	g_assert_false(result);
	birb_assert_list_model_n_items(contacts, 1);
	g_assert_cmpuint(added, ==, 0);
	g_assert_cmpuint(counter, ==, 0);
	g_assert_cmpuint(removed, ==, 0);

	/* Remove the second contact. */
	added = 0;
	counter = 0;
	removed = 0;
	result = purple_contacts_remove(contacts, contact2);
	g_assert_true(result);
	birb_assert_list_model_n_items(contacts, 0);
	g_assert_cmpuint(added, ==, 0);
	g_assert_cmpuint(counter, ==, 1);
	g_assert_cmpuint(removed, ==, 1);

	/* Unref the contacts object first so we can assert on the individual
	 * contacts. Account is last because the contacts all reference it.
	 */
	g_assert_finalize_object(contacts);
	g_assert_finalize_object(contact1);
	g_assert_finalize_object(contact2);
	g_assert_finalize_object(account);
}

static void
test_purple_contacts_remove_with_id(void) {
	PurpleAccount *account = NULL;
	PurpleContact *candidate = NULL;
	PurpleContact *contact = NULL;
	PurpleContacts *contacts = NULL;
	guint added = 0;
	guint counter = 0;
	guint removed = 0;
	gboolean result = 0;

	account = purple_account_new("test", "test");
	contact = purple_contact_new(account, "contact1");

	contacts = purple_contacts_new();
	g_signal_connect(contacts, "added",
	                 G_CALLBACK(test_purple_contacts_add_removed_counter_cb),
	                 &added);
	g_signal_connect(contacts, "removed",
	                 G_CALLBACK(test_purple_contacts_add_removed_counter_cb),
	                 &removed);
	birb_count_list_model_items_changed(G_LIST_MODEL(contacts), &counter);

	/* Add the contact. */
	added = 0;
	counter = 0;
	removed = 0;
	result = purple_contacts_add(contacts, contact);
	g_assert_true(result);
	birb_assert_list_model_n_items(contacts, 1);
	g_assert_cmpuint(added, ==, 1);
	g_assert_cmpuint(counter, ==, 1);
	g_assert_cmpuint(removed, ==, 0);

	/* Make sure we can find the contact. */
	candidate = purple_contacts_find_with_id(contacts, "contact1");
	g_assert_true(purple_contact_equal(candidate, contact));

	/* Remove the contact. */
	added = 0;
	counter = 0;
	removed = 0;
	result = purple_contacts_remove_with_id(contacts, "contact1");
	g_assert_true(result);
	birb_assert_list_model_n_items(contacts, 0);
	g_assert_cmpuint(added, ==, 0);
	g_assert_cmpuint(counter, ==, 1);
	g_assert_cmpuint(removed, ==, 1);

	/* Attempt to remove again. */
	counter = 0;
	result = purple_contacts_remove_with_id(contacts, "contact1");
	g_assert_false(result);
	g_assert_cmpuint(counter, ==, 0);
	birb_assert_list_model_n_items(contacts, 0);

	g_assert_finalize_object(contacts);
	g_assert_finalize_object(contact);
	g_assert_finalize_object(account);
}

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

	g_test_add_func("/contacts/new", test_purple_contacts_new);
	g_test_add_func("/contacts/properties", test_purple_contacts_properties);

	g_test_add_func("/contacts/add-remove", test_purple_contacts_add_remove);
	g_test_add_func("/contacts/remove-with-id",
	                test_purple_contacts_remove_with_id);

	return g_test_run();
}
