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

#include "pidginconversationmember.h"

#include "pidgincolor.h"
#include "pidgincontactinfomenu.h"

struct _PidginConversationMember {
	GtkBox parent;

	PurpleConversationMember *conversation_member;

	gboolean show_presence;
};

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

/******************************************************************************
 * Callbacks
 *****************************************************************************/
static char *
pidgin_conversation_member_get_name_for_display(G_GNUC_UNUSED PidginConversationMember *self,
                                                PurpleConversationMember *conversation_member,
                                                G_GNUC_UNUSED gpointer data)
{
	const char *name_for_display = "";

	if(PURPLE_IS_CONVERSATION_MEMBER(conversation_member)) {
		name_for_display = purple_conversation_member_get_name_for_display(conversation_member);
	}

	return g_strdup(name_for_display);
}

static PangoAttrList *
pidgin_conversation_member_attributes(G_GNUC_UNUSED PidginConversationMember *self,
                                      PurpleConversationMember *conversation_member,
                                      G_GNUC_UNUSED gpointer data)
{
	GdkRGBA rgba;
	PangoAttribute *attr = NULL;
	PangoAttrList *attrs = NULL;
	gboolean color_valid = FALSE;
	const char *custom_color = NULL;

	if(!PURPLE_IS_CONVERSATION_MEMBER(conversation_member)) {
		return NULL;
	}

	custom_color = purple_conversation_member_get_color(conversation_member);

	if(!birb_str_is_empty(custom_color)) {
		color_valid = gdk_rgba_parse(&rgba, custom_color);
	}

	if(!color_valid) {
		const char *name_for_display = NULL;

		name_for_display = purple_conversation_member_get_name_for_display(conversation_member);

		pidgin_color_calculate_for_text(name_for_display, &rgba);
	}

	attrs = pango_attr_list_new();
	attr = pango_attr_foreground_new(0xFFFF * rgba.red,
	                                 0xFFFF * rgba.green,
	                                 0xFFFF * rgba.blue);
	pango_attr_list_insert(attrs, attr);

	return attrs;
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
G_DEFINE_FINAL_TYPE(PidginConversationMember, pidgin_conversation_member,
                    GTK_TYPE_BOX)

static void
pidgin_conversation_member_finalize(GObject *obj) {
	PidginConversationMember *member = PIDGIN_CONVERSATION_MEMBER(obj);

	g_clear_object(&member->conversation_member);

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

static void
pidgin_conversation_member_get_property(GObject *obj, guint param_id,
                                        GValue *value, GParamSpec *pspec)
{
	PidginConversationMember *member = PIDGIN_CONVERSATION_MEMBER(obj);

	switch(param_id) {
	case PROP_CONVERSATION_MEMBER:
		g_value_set_object(value,
		                   pidgin_conversation_member_get_conversation_member(member));
		break;
	case PROP_SHOW_PRESENCE:
		g_value_set_boolean(value,
		                    pidgin_conversation_member_get_show_presence(member));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
pidgin_conversation_member_set_property(GObject *obj, guint param_id,
                                        const GValue *value, GParamSpec *pspec)
{
	PidginConversationMember *member = PIDGIN_CONVERSATION_MEMBER(obj);

	switch(param_id) {
	case PROP_CONVERSATION_MEMBER:
		pidgin_conversation_member_set_conversation_member(member,
		                                                   g_value_get_object(value));
		break;
	case PROP_SHOW_PRESENCE:
		pidgin_conversation_member_set_show_presence(member,
		                                             g_value_get_boolean(value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
pidgin_conversation_member_init(PidginConversationMember *member) {
	member->show_presence = TRUE;

	gtk_widget_init_template(GTK_WIDGET(member));
}

static void
pidgin_conversation_member_class_init(PidginConversationMemberClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);

	obj_class->finalize = pidgin_conversation_member_finalize;
	obj_class->get_property = pidgin_conversation_member_get_property;
	obj_class->set_property = pidgin_conversation_member_set_property;

	/**
	 * PidginConversationMember:conversation-member:
	 *
	 * The conversation member which is being displayed.
	 *
	 * Since: 3.0
	 */
	properties[PROP_CONVERSATION_MEMBER] = g_param_spec_object(
		"conversation-member", NULL, NULL,
		PURPLE_TYPE_CONVERSATION_MEMBER,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	/**
	 * PidginConversationMember:show-presence:
	 *
	 * Whether or not the presence of the user should be shown.
	 *
	 * Since: 3.0
	 */
	properties[PROP_SHOW_PRESENCE] = g_param_spec_boolean(
		"show-presence", NULL, NULL,
		TRUE,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

	g_object_class_install_properties(obj_class, N_PROPERTIES, properties);

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

	gtk_widget_class_bind_template_callback(widget_class,
	                                        pidgin_conversation_member_get_name_for_display);
	gtk_widget_class_bind_template_callback(widget_class,
	                                        pidgin_conversation_member_attributes);
}

/******************************************************************************
 * Public API
 *****************************************************************************/
GtkWidget *
pidgin_conversation_member_new(PurpleConversationMember *conversation_member) {
	return g_object_new(
		PIDGIN_TYPE_CONVERSATION_MEMBER,
		"conversation-member", conversation_member,
		NULL);
}

PurpleConversationMember *
pidgin_conversation_member_get_conversation_member(PidginConversationMember *member)
{
	g_return_val_if_fail(PIDGIN_IS_CONVERSATION_MEMBER(member), NULL);

	return member->conversation_member;
}

gboolean
pidgin_conversation_member_get_show_presence(PidginConversationMember *member)
{
	g_return_val_if_fail(PIDGIN_IS_CONVERSATION_MEMBER(member), FALSE);

	return member->show_presence;
}

void
pidgin_conversation_member_set_conversation_member(PidginConversationMember *member,
                                                   PurpleConversationMember *conversation_member)
{
	g_return_if_fail(PIDGIN_IS_CONVERSATION_MEMBER(member));

	if(g_set_object(&member->conversation_member, conversation_member)) {
		g_object_notify_by_pspec(G_OBJECT(member),
		                         properties[PROP_CONVERSATION_MEMBER]);
	}
}

void
pidgin_conversation_member_set_show_presence(PidginConversationMember *member,
                                             gboolean show_presence)
{
	g_return_if_fail(PIDGIN_IS_CONVERSATION_MEMBER(member));

	if(member->show_presence != show_presence) {
		member->show_presence = show_presence;

		g_object_notify_by_pspec(G_OBJECT(member),
		                         properties[PROP_SHOW_PRESENCE]);
	}
}
