/*
 * Pidgin - Internet Messenger
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * Pidgin 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 program 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 program 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 program; if not, see <https://www.gnu.org/licenses/>.
 */
#include <glib/gi18n-lib.h>

#include <gtk/gtk.h>

#include "pidginbadges.h"

struct _PidginBadges {
	AdwBin parent;

	PurpleBadges *badges;

	GtkSliceListModel *model;
};

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

/******************************************************************************
 * GObject implementation
 *****************************************************************************/
G_DEFINE_FINAL_TYPE(PidginBadges, pidgin_badges, ADW_TYPE_BIN)

static void
pidgin_badges_get_property(GObject *object, guint prop_id, GValue *value,
                           GParamSpec *pspec)
{
	PidginBadges *badges = PIDGIN_BADGES(object);

	switch (prop_id) {
	case PROP_BADGES:
		g_value_set_object(value, pidgin_badges_get_badges(badges));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
		break;
	}
}

static void
pidgin_badges_set_property(GObject *object, guint prop_id, const GValue *value,
                           GParamSpec *pspec)
{
	PidginBadges *badges = PIDGIN_BADGES(object);

	switch (prop_id) {
	case PROP_BADGES:
		pidgin_badges_set_badges(badges, g_value_get_object(value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
		break;
	}
}

static void
pidgin_badges_finalize(GObject *obj) {
	PidginBadges *badges = PIDGIN_BADGES(obj);

	g_clear_object(&badges->badges);

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

static void
pidgin_badges_class_init(PidginBadgesClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);

	/* Properties */
	obj_class->get_property = pidgin_badges_get_property;
	obj_class->set_property = pidgin_badges_set_property;
	obj_class->finalize = pidgin_badges_finalize;

	/**
	 * PidginBadges:badges:
	 *
	 * The badges that are being displayed.
	 *
	 * Since: 3.0
	 */
	properties[PROP_BADGES] = g_param_spec_object(
		"badges", NULL, NULL,
		PURPLE_TYPE_BADGES,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties(obj_class, N_PROPERTIES, properties);

	/* Widget template */
	gtk_widget_class_set_template_from_resource(widget_class,
	                                            "/im/pidgin/Pidgin3/badges.ui");

	gtk_widget_class_bind_template_child(widget_class, PidginBadges,
	                                     model);
}

static void
pidgin_badges_init(PidginBadges *badges) {
	gtk_widget_init_template(GTK_WIDGET(badges));
}

/******************************************************************************
 * Public API
 *****************************************************************************/
GtkWidget *
pidgin_badges_new(PurpleBadges *badges) {
	return g_object_new(PIDGIN_TYPE_BADGES,
	                    "badges", badges,
	                    NULL);
}

PurpleBadges *
pidgin_badges_get_badges(PidginBadges *badges) {
	g_return_val_if_fail(PIDGIN_IS_BADGES(badges), NULL);

	return badges->badges;
}

void
pidgin_badges_set_badges(PidginBadges *pidgin_badges,
                         PurpleBadges *purple_badges)
{
	g_return_if_fail(PIDGIN_IS_BADGES(pidgin_badges));

	if(g_set_object(&pidgin_badges->badges, purple_badges)) {
		gtk_slice_list_model_set_model(pidgin_badges->model,
		                               G_LIST_MODEL(purple_badges));

		g_object_notify_by_pspec(G_OBJECT(pidgin_badges),
		                         properties[PROP_BADGES]);
	}
}
