/*
 * 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 "purplebadgemanager.h"

#include "util.h"

struct _PurpleBadgeManager {
	GObject parent;

	GHashTable *badges;
};

static PurpleBadgeManager *default_manager = NULL;

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
G_DEFINE_FINAL_TYPE(PurpleBadgeManager, purple_badge_manager, G_TYPE_OBJECT)

static void
purple_badge_manager_finalize(GObject *obj) {
	PurpleBadgeManager *manager = PURPLE_BADGE_MANAGER(obj);

	g_clear_pointer(&manager->badges, g_hash_table_unref);

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

static void
purple_badge_manager_init(PurpleBadgeManager *manager) {
	manager->badges = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
	                                        g_object_unref);
}

static void
purple_badge_manager_class_init(PurpleBadgeManagerClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);

	obj_class->finalize = purple_badge_manager_finalize;
}

/******************************************************************************
 * Public API
 *****************************************************************************/
gboolean
purple_badge_manager_add(PurpleBadgeManager *manager, PurpleBadge *badge) {
	const char *id = NULL;

	g_return_val_if_fail(PURPLE_IS_BADGE_MANAGER(manager), FALSE);
	g_return_val_if_fail(PURPLE_IS_BADGE(badge), FALSE);

	id = purple_badge_get_id(badge);
	if(purple_strempty(id)) {
		g_warning("attempted to add a badge with no id to a badge manager");

		return FALSE;
	}

	return g_hash_table_insert(manager->badges, g_strdup(id),
	                           g_object_ref(badge));
}

PurpleBadgeManager *
purple_badge_manager_new(void) {
	return g_object_new(PURPLE_TYPE_BADGE_MANAGER, NULL);
}

PurpleBadgeManager *
purple_badge_manager_get_default(void) {
	if(!PURPLE_IS_BADGE_MANAGER(default_manager)) {
		default_manager = purple_badge_manager_new();
		g_object_add_weak_pointer(G_OBJECT(default_manager),
		                          (gpointer *)&default_manager);
	}

	return default_manager;
}

PurpleBadge *
purple_badge_manager_find(PurpleBadgeManager *manager, const char *id) {
	g_return_val_if_fail(PURPLE_IS_BADGE_MANAGER(manager), NULL);
	g_return_val_if_fail(!purple_strempty(id), NULL);

	return g_hash_table_lookup(manager->badges, id);
}

gboolean
purple_badge_manager_remove(PurpleBadgeManager *manager, const char *id) {
	g_return_val_if_fail(PURPLE_IS_BADGE_MANAGER(manager), FALSE);
	g_return_val_if_fail(!purple_strempty(id), FALSE);

	return g_hash_table_remove(manager->badges, id);
}
