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

#include <gtk/gtk.h>

#include "pidginsavedpresencechooser.h"

#include "pidginiconname.h"

struct _PidginSavedPresenceChooser {
	GtkBox parent;

	GtkWidget *drop_down;
};

/******************************************************************************
 * Binding Closures
 *****************************************************************************/
static char *
pidgin_saved_presence_chooser_icon_name(G_GNUC_UNUSED GtkWidget *self,
                                        PurplePresencePrimitive primitive,
                                        G_GNUC_UNUSED gpointer data)
{
	const char *icon_name = NULL;

	icon_name = pidgin_icon_name_from_presence_primitive(primitive,
	                                                     "icon-missing");

	return g_strdup(icon_name);
}

static gboolean
purple_saved_presence_item_show_message(G_GNUC_UNUSED GtkWidget *self,
                                        const char *message,
                                        G_GNUC_UNUSED gpointer data)
{
	return !birb_str_is_empty(message);
}

/******************************************************************************
 * GObject implementation
 *****************************************************************************/
G_DEFINE_FINAL_TYPE(PidginSavedPresenceChooser,
                    pidgin_saved_presence_chooser,
                    GTK_TYPE_BOX)

static void
pidgin_saved_presence_chooser_init(PidginSavedPresenceChooser *chooser) {
	PurplePresenceManager *manager = NULL;

	gtk_widget_init_template(GTK_WIDGET(chooser));

	manager = purple_presence_manager_get_default();

	gtk_drop_down_set_model(GTK_DROP_DOWN(chooser->drop_down),
	                        G_LIST_MODEL(manager));
	g_object_bind_property(G_OBJECT(manager), "active-index",
	                       G_OBJECT(chooser->drop_down), "selected",
	                       G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
}

static void
pidgin_saved_presence_chooser_class_init(PidginSavedPresenceChooserClass *klass) {
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);

	gtk_widget_class_set_template_from_resource(widget_class,
	                                            "/im/pidgin/Pidgin3/savedpresencechooser.ui");

	gtk_widget_class_bind_template_child(widget_class,
	                                     PidginSavedPresenceChooser,
	                                     drop_down);

	gtk_widget_class_bind_template_callback(widget_class,
	                                        pidgin_saved_presence_chooser_icon_name);
	gtk_widget_class_bind_template_callback(widget_class,
	                                        purple_saved_presence_item_show_message);
}

/******************************************************************************
 * Public API
 *****************************************************************************/
GtkWidget *
pidgin_saved_presence_chooser_new(void) {
	return g_object_new(PIDGIN_TYPE_SAVED_PRESENCE_CHOOSER,
	                    NULL);
}
