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

#include "purpleprotocolmanager.h"
#include "purpleprotocolmanagerprivate.h"

#include "util.h"

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, };

struct _PurpleProtocolManager {
	GObject parent;

	GPtrArray *protocols;
};

static PurpleProtocolManager *default_manager = NULL;

/******************************************************************************
 * Helpers
 *****************************************************************************/
static PurpleProtocol *
purple_protocol_manager_find_with_id(PurpleProtocolManager *manager,
                                     const char *id, guint *position)
{
	g_return_val_if_fail(PURPLE_IS_PROTOCOL_MANAGER(manager), NULL);

	for(guint i = 0; i < manager->protocols->len; i++) {
		PurpleProtocol *protocol = NULL;
		const char *candidate_id = NULL;

		protocol = g_ptr_array_index(manager->protocols, i);
		candidate_id = purple_protocol_get_id(protocol);

		if(purple_strequal(candidate_id, id)) {
			if(position != NULL) {
				*position = i;
			}

			return protocol;
		}
	}

	return NULL;
}

/******************************************************************************
 * GListModel Implementation
 *****************************************************************************/
static GType
purple_protocol_manager_get_item_type(G_GNUC_UNUSED GListModel *list) {
	return PURPLE_TYPE_PROTOCOL;
}

static guint
purple_protocol_manager_get_n_items(GListModel *list) {
	PurpleProtocolManager *manager = PURPLE_PROTOCOL_MANAGER(list);

	return manager->protocols->len;
}

static gpointer
purple_protocol_manager_get_item(GListModel *list, guint position) {
	PurpleProtocolManager *manager = PURPLE_PROTOCOL_MANAGER(list);
	PurpleProtocol *protocol = NULL;

	if(position < manager->protocols->len) {
		protocol = g_object_ref(g_ptr_array_index(manager->protocols,
		                                          position));
	}

	return protocol;
}

static void
purple_protocol_manager_list_model_iface_init(GListModelInterface *iface) {
	iface->get_item_type = purple_protocol_manager_get_item_type;
	iface->get_n_items = purple_protocol_manager_get_n_items;
	iface->get_item = purple_protocol_manager_get_item;
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
G_DEFINE_FINAL_TYPE_WITH_CODE(PurpleProtocolManager, purple_protocol_manager,
                              G_TYPE_OBJECT,
                              G_IMPLEMENT_INTERFACE(G_TYPE_LIST_MODEL,
                                                    purple_protocol_manager_list_model_iface_init));

static void
purple_protocol_manager_finalize(GObject *obj) {
	PurpleProtocolManager *manager = NULL;

	manager = PURPLE_PROTOCOL_MANAGER(obj);

	g_clear_pointer(&manager->protocols, g_ptr_array_unref);

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

static void
purple_protocol_manager_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,
		                  purple_protocol_manager_get_item_type(model));
		break;
	case PROP_N_ITEMS:
		g_value_set_uint(value,
		                 purple_protocol_manager_get_n_items(model));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
purple_protocol_manager_init(PurpleProtocolManager *manager) {
	manager->protocols = g_ptr_array_new_full(10, g_object_unref);
}

static void
purple_protocol_manager_class_init(PurpleProtocolManagerClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);

	obj_class->finalize = purple_protocol_manager_finalize;
	obj_class->get_property = purple_protocol_manager_get_property;

	/**
	 * PurpleProtocolManager: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);

	/**
	 * PurpleProtocolManager: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);

	/**
	 * PurpleProtocolManager::added:
	 * @manager: The #PurpleProtocolManager instance.
	 * @protocol: The #PurpleProtocol that was added.
	 *
	 * Emitted after @protocol has been added to @manager.
	 *
	 * 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_PROTOCOL);

	/**
	 * PurpleProtocolManager::removed:
	 * @manager: The #PurpleProtocolManager instance.
	 * @protocol: The #PurpleProtocol that was removed.
	 *
	 * Emitted after @protocol has been removed from @manager.
	 *
	 * 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_PROTOCOL);
}

/******************************************************************************
 * Private API
 *****************************************************************************/
void
purple_protocol_manager_startup(void) {
	if(default_manager == NULL) {
		default_manager = g_object_new(PURPLE_TYPE_PROTOCOL_MANAGER, NULL);
		g_object_add_weak_pointer(G_OBJECT(default_manager),
		                          (gpointer)&default_manager);
	}
}

void
purple_protocol_manager_shutdown(void) {
	g_clear_object(&default_manager);
}

/******************************************************************************
 * Public API
 *****************************************************************************/
gboolean
purple_protocol_manager_add(PurpleProtocolManager *manager,
                            PurpleProtocol *protocol, GError **error)
{
	PurpleProtocol *candidate = NULL;
	const char *id = NULL;
	guint position = 0;

	g_return_val_if_fail(PURPLE_IS_PROTOCOL_MANAGER(manager), FALSE);
	g_return_val_if_fail(PURPLE_IS_PROTOCOL(protocol), FALSE);

	id = purple_protocol_get_id(protocol);
	candidate = purple_protocol_manager_find_with_id(manager, id, NULL);
	if(PURPLE_IS_PROTOCOL(candidate)) {
		g_set_error(error, PURPLE_PROTOCOL_MANAGER_DOMAIN, 0,
		            _("protocol %s has already been added"), id);

		return FALSE;
	}

	position = manager->protocols->len;
	g_ptr_array_add(manager->protocols, g_object_ref(protocol));

	g_signal_emit(G_OBJECT(manager), signals[SIG_ADDED], 0, protocol);
	g_list_model_items_changed(G_LIST_MODEL(manager), position, 0, 1);
	g_object_notify_by_pspec(G_OBJECT(manager), properties[PROP_N_ITEMS]);

	return TRUE;
}

PurpleProtocolManager *
purple_protocol_manager_get_default(void) {
	return default_manager;
}

PurpleProtocol *
purple_protocol_manager_find(PurpleProtocolManager *manager, const char *id) {
	g_return_val_if_fail(PURPLE_IS_PROTOCOL_MANAGER(manager), NULL);
	g_return_val_if_fail(!purple_strempty(id), NULL);

	return purple_protocol_manager_find_with_id(manager, id, NULL);
}

gboolean
purple_protocol_manager_remove(PurpleProtocolManager *manager,
                               PurpleProtocol *protocol, GError **error)
{
	PurpleProtocol *candidate = NULL;
	const char *id = NULL;
	gboolean ret = FALSE;
	guint position = 0;

	g_return_val_if_fail(PURPLE_IS_PROTOCOL_MANAGER(manager), FALSE);
	g_return_val_if_fail(PURPLE_IS_PROTOCOL(protocol), FALSE);

	/* We need to hold a reference on the protocol as typically we will be
	 * holding the only reference on the protocol when this is called and we
	 * will need to pass it to the signal emission after it's removed from the
	 * hash table that'll unref it.
	 */
	g_object_ref(protocol);

	id = purple_protocol_get_id(protocol);
	candidate = purple_protocol_manager_find_with_id(manager, id, &position);
	if(PURPLE_IS_PROTOCOL(candidate)) {
		g_ptr_array_remove_index(manager->protocols, position);

		g_signal_emit(G_OBJECT(manager), signals[SIG_REMOVED], 0, protocol);
		g_list_model_items_changed(G_LIST_MODEL(manager), position, 1, 0);
		g_object_notify_by_pspec(G_OBJECT(manager), properties[PROP_N_ITEMS]);

		ret = TRUE;
	} else {
		g_set_error(error, PURPLE_PROTOCOL_MANAGER_DOMAIN, 0,
		            _("protocol %s has not been added"), id);

		ret = FALSE;
	}

	g_object_unref(protocol);

	return ret;
}
