/*
 * 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/gi18n-lib.h>

#include "purpledemoprotocoldirectory.h"

#include "purpledemoconnection.h"

/******************************************************************************
 * PurpleProtocolDirectory Implementation
 *****************************************************************************/
static void
purple_demo_protocol_directory_search_contacts_async(PurpleProtocolDirectory *protocol,
                                                     PurpleAccount *account,
                                                     const char *search_text,
                                                     GCancellable *cancellable,
                                                     GAsyncReadyCallback callback,
                                                     gpointer data)
{
	PurpleDemoConnection *demo_connection = NULL;
	PurpleConnection *connection = NULL;
	PurpleContacts *all_contacts = NULL;
	PurpleContacts *matched_contacts = NULL;
	GTask *task = NULL;
	guint n_items = 0;

	connection = purple_account_get_connection(account);
	demo_connection = PURPLE_DEMO_CONNECTION(connection);
	all_contacts = purple_demo_connection_get_contacts(demo_connection);

	task = g_task_new(protocol, cancellable, callback, data);

	/* If no search text was given, return everyone. */
	if(birb_str_is_empty(search_text)) {
		g_task_return_pointer(task, g_object_ref(all_contacts),
		                      g_object_unref);
		g_clear_object(&task);
		return;
	}

	/* We have a search text, so we need to filter the list. */
	matched_contacts = purple_contacts_new();
	n_items = g_list_model_get_n_items(G_LIST_MODEL(all_contacts));

	for(guint i = 0; i < n_items; i++) {
		PurpleContact *contact = NULL;
		PurpleContactInfo *info = NULL;

		contact = g_list_model_get_item(G_LIST_MODEL(all_contacts), i);
		info = PURPLE_CONTACT_INFO(contact);
		if(purple_contact_info_matches(info, search_text)) {
			purple_contacts_add(matched_contacts, contact);
		}
		g_clear_object(&contact);
	}

	g_task_return_pointer(task, matched_contacts, g_object_unref);
	g_clear_object(&task);
}

static PurpleContacts *
purple_demo_protocol_directory_search_contacts_finish(G_GNUC_UNUSED PurpleProtocolDirectory *protocol,
                                                      GAsyncResult *result,
                                                      GError **error)
{
	return g_task_propagate_pointer(G_TASK(result), error);
}

void
purple_demo_protocol_directory_init(PurpleProtocolDirectoryInterface *iface) {
	iface->search_contacts_async = purple_demo_protocol_directory_search_contacts_async;
	iface->search_contacts_finish = purple_demo_protocol_directory_search_contacts_finish;
}
