/*
 * 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 library 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 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 General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this library; if not, see <https://www.gnu.org/licenses/>.
 */

#include <birb.h>

#include "purplecontacts.h"

struct _PurpleContacts {
	GObject parent;

	GPtrArray *contacts;
};

enum {
	PROP_0,
	PROP_ITEM_TYPE,
	PROP_N_ITEMS,
	N_PROPERTIES,
};
static GParamSpec *properties[N_PROPERTIES] = {NULL, };

enum {
	SIG_ADDED,
	SIG_REMOVED,
	N_SIGNALS,
};
static guint signals[N_SIGNALS] = {0, };

/******************************************************************************
 * Helpers
 *****************************************************************************/
static void
purple_contacts_remove_internal(PurpleContacts *contacts,
                                PurpleContact *contact,
                                guint position)
{
	g_return_if_fail(PURPLE_IS_CONTACTS(contacts));
	g_return_if_fail(PURPLE_IS_CONTACT(contact));

	g_object_ref(G_OBJECT(contact));

	g_ptr_array_remove_index(contacts->contacts, position);

	g_list_model_items_changed(G_LIST_MODEL(contacts), position, 1, 0);

	g_signal_emit(G_OBJECT(contacts), signals[SIG_REMOVED], 0, contact);
	g_object_unref(contact);
}

/******************************************************************************
 * GListModel Implementation
 *****************************************************************************/
static GType
purple_contacts_get_item_type(G_GNUC_UNUSED GListModel *list) {
	return PURPLE_TYPE_CONTACT;
}

static guint
purple_contacts_get_n_items(GListModel *list) {
	PurpleContacts *contacts = PURPLE_CONTACTS(list);

	return contacts->contacts->len;
}

static gpointer
purple_contacts_get_item(GListModel *list, guint position) {
	PurpleContacts *contacts = PURPLE_CONTACTS(list);
	PurpleContact *contact = NULL;

	if(position < contacts->contacts->len) {
		contact = g_ptr_array_index(contacts->contacts, position);
		g_object_ref(contact);
	}

	return contact;
}

static void
purple_contacts_list_model_init(GListModelInterface *iface) {
	iface->get_item_type = purple_contacts_get_item_type;
	iface->get_n_items = purple_contacts_get_n_items;
	iface->get_item = purple_contacts_get_item;
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
G_DEFINE_FINAL_TYPE_WITH_CODE(
	PurpleContacts,
	purple_contacts,
	G_TYPE_OBJECT,
	G_IMPLEMENT_INTERFACE(G_TYPE_LIST_MODEL,
	                      purple_contacts_list_model_init))

static void
purple_contacts_finalize(GObject *obj) {
	PurpleContacts *contacts = PURPLE_CONTACTS(obj);

	g_clear_pointer(&contacts->contacts, g_ptr_array_unref);

	G_OBJECT_CLASS(purple_contacts_parent_class)->finalize(obj);
}

static void
purple_contacts_get_property(GObject *obj, guint param_id, GValue *value,
                             GParamSpec *pspec)
{
	GListModel *model = G_LIST_MODEL(obj);

	switch(param_id) {
	case PROP_ITEM_TYPE:
		g_value_set_gtype(value, g_list_model_get_item_type(model));
		break;
	case PROP_N_ITEMS:
		g_value_set_uint(value, g_list_model_get_n_items(model));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
purple_contacts_init(PurpleContacts *contacts) {
	contacts->contacts = g_ptr_array_new_full(1, g_object_unref);
}

static void
purple_contacts_class_init(PurpleContactsClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);

	obj_class->finalize = purple_contacts_finalize;
	obj_class->get_property = purple_contacts_get_property;

	/**
	 * PurpleContacts:item-type:
	 *
	 * The type of items. See [vfunc@Gio.ListModel.get_item_type].
	 *
	 * Since: 3.0
	 */
	properties[PROP_ITEM_TYPE] = g_param_spec_gtype(
		"item-type", NULL, NULL,
		G_TYPE_OBJECT,
		G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleContacts:n-items:
	 *
	 * The number of items. See [vfunc@Gio.ListModel.get_n_items].
	 *
	 * Since: 3.0
	 */
	properties[PROP_N_ITEMS] = g_param_spec_uint(
		"n-items", NULL, NULL,
		0, G_MAXUINT, 0,
		G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties(obj_class, N_PROPERTIES, properties);

	/**
	 * PurpleContacts::added:
	 * @contacts: the instance
	 * @contact: the contact that was added
	 *
	 * Emitted when a contact is added.
	 *
	 * Since: 3.0
	 */
	signals[SIG_ADDED] = g_signal_new_class_handler(
		"added",
		G_OBJECT_CLASS_TYPE(klass),
		G_SIGNAL_RUN_LAST,
		NULL,
		NULL,
		NULL,
		NULL,
		G_TYPE_NONE,
		1,
		PURPLE_TYPE_CONTACT);

	/**
	 * PurpleContacts::removed:
	 * @contacts: the instance
	 * @contact: the contact that was removed
	 *
	 * Emitted when a contact is removed.
	 *
	 * Since: 3.0
	 */
	signals[SIG_REMOVED] = g_signal_new_class_handler(
		"removed",
		G_OBJECT_CLASS_TYPE(klass),
		G_SIGNAL_RUN_LAST,
		NULL,
		NULL,
		NULL,
		NULL,
		G_TYPE_NONE,
		1,
		PURPLE_TYPE_CONTACT);
}

/******************************************************************************
 * Public API
 *****************************************************************************/
gboolean
purple_contacts_add(PurpleContacts *contacts, PurpleContact *contact) {
	g_return_val_if_fail(PURPLE_IS_CONTACTS(contacts), FALSE);
	g_return_val_if_fail(PURPLE_IS_CONTACT(contact), FALSE);

	for(guint i = 0; i < contacts->contacts->len; i++) {
		PurpleContact *candidate = g_ptr_array_index(contacts->contacts, i);

		if(purple_contact_equal(contact, candidate)) {
			return FALSE;
		}
	}

	g_ptr_array_add(contacts->contacts, g_object_ref(contact));

	g_list_model_items_changed(G_LIST_MODEL(contacts),
	                           contacts->contacts->len - 1, 0, 1);

	g_signal_emit(contacts, signals[SIG_ADDED], 0, contact);

	return TRUE;
}

PurpleContact *
purple_contacts_find_with_id(PurpleContacts *contacts, const char *id) {
	g_return_val_if_fail(PURPLE_IS_CONTACTS(contacts), NULL);

	for(guint i = 0; i < contacts->contacts->len; i++) {
		PurpleContact *candidate = NULL;
		PurpleContactInfo *info = NULL;
		const char *candidate_id = NULL;

		candidate = g_ptr_array_index(contacts->contacts, i);
		info = PURPLE_CONTACT_INFO(candidate);
		candidate_id = purple_contact_info_get_id(info);

		if(birb_str_equal(candidate_id, id)) {
			return candidate;
		}
	}

	return NULL;
}

PurpleContacts *
purple_contacts_new(void) {
	return g_object_new(PURPLE_TYPE_CONTACTS, NULL);
}

gboolean
purple_contacts_remove(PurpleContacts *contacts, PurpleContact *contact) {
	g_return_val_if_fail(PURPLE_IS_CONTACTS(contacts), FALSE);
	g_return_val_if_fail(PURPLE_IS_CONTACT(contact), FALSE);

	for(guint i = 0; i < contacts->contacts->len; i++) {
		PurpleContact *candidate = g_ptr_array_index(contacts->contacts, i);

		if(purple_contact_equal(contact, candidate)) {
			purple_contacts_remove_internal(contacts, candidate, i);

			return TRUE;
		}
	}

	return FALSE;
}

gboolean
purple_contacts_remove_with_id(PurpleContacts *contacts, const char *id) {
	g_return_val_if_fail(PURPLE_IS_CONTACTS(contacts), FALSE);

	for(guint i = 0; i < contacts->contacts->len; i++) {
		PurpleContact *candidate = NULL;
		PurpleContactInfo *info = NULL;
		const char *candidate_id = NULL;

		candidate = g_ptr_array_index(contacts->contacts, i);
		info = PURPLE_CONTACT_INFO(candidate);
		candidate_id = purple_contact_info_get_id(info);

		if(birb_str_equal(candidate_id, id)) {
			purple_contacts_remove_internal(contacts, candidate, i);

			return TRUE;
		}
	}

	return FALSE;
}
