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

struct _PidginMessage {
	GtkBox parent;

	PurpleMessage *message;
	gboolean show_formatting;
};

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

/******************************************************************************
 * Helpers
 *****************************************************************************/

/**
 * pidgin_message_set_tooltip_for_timestamp: (skip)
 * @tooltip: The tooltip to update.
 * @timestamp: The timestamp to set.
 *
 * Updates @tooltip to display @timestamp. This is meant to be called from
 * a GtkWidget::query-tooltip signal and its return value should be returned
 * from that handler.
 *
 * Returns: %TRUE if a tooltip was set, otherwise %FALSE.
 *
 * Since: 3.0
 */
static gboolean
pidgin_message_set_tooltip_for_timestamp(GtkTooltip *tooltip,
                                         GDateTime *timestamp)
{
	GDateTime *local = NULL;
	char *text = NULL;

	if(timestamp == NULL) {
		return FALSE;
	}

	local = g_date_time_to_local(timestamp);
	text = g_date_time_format(local, "%c");
	g_clear_pointer(&local, g_date_time_unref);

	gtk_tooltip_set_text(tooltip, text);
	g_clear_pointer(&text, g_free);

	return TRUE;
}

/******************************************************************************
 * Callbacks
 *****************************************************************************/
static PangoAttrList *
pidgin_message_get_message_attributes(G_GNUC_UNUSED GObject *self,
                                      PurpleMessage *message,
                                      gboolean show_formatting,
                                      G_GNUC_UNUSED gpointer data)
{
	PangoAttrList *attrs = NULL;

	if(!PURPLE_IS_MESSAGE(message)) {
		return NULL;
	}

	if(show_formatting) {
		PangoAttrList *message_attrs = NULL;

		message_attrs = purple_message_get_attributes(message);
		attrs = pango_attr_list_copy(message_attrs);
	} else {
		attrs = pango_attr_list_new();
	}

	if(purple_message_get_action(message)) {
		PangoAttribute *attr = NULL;

		attr = pango_attr_style_new(PANGO_STYLE_ITALIC);

		pango_attr_list_insert(attrs, attr);
	}

	if(purple_message_get_event(message)) {
		PangoAttribute *attr = NULL;

		attr = pango_attr_style_new(PANGO_STYLE_ITALIC);
		pango_attr_list_insert(attrs, attr);

		attr = pango_attr_weight_new(PANGO_WEIGHT_BOLD);
		pango_attr_list_insert(attrs, attr);
	}

	return attrs;
}

static char *
pidgin_message_get_timestamp_string(G_GNUC_UNUSED GObject *self,
                                    PurpleMessage *message,
                                    G_GNUC_UNUSED gpointer data)
{
	GDateTime *timestamp = NULL;

	if(!PURPLE_IS_MESSAGE(message)) {
		return NULL;
	}

	timestamp = purple_message_get_timestamp(message);
	if(timestamp != NULL) {
		GDateTime *local = NULL;
		char *ret = NULL;

		local = g_date_time_to_local(timestamp);
		ret = g_date_time_format(local, "%I:%M %p");
		g_date_time_unref(local);

		return ret;
	}

	return NULL;
}

static char *
pidgin_message_process_message_contents_cb(G_GNUC_UNUSED GObject *self,
                                           const char *contents,
                                           G_GNUC_UNUSED gpointer data)
{
	char *escaped = NULL;
	char *linkified = NULL;

	escaped = g_markup_escape_text(contents, -1);

	linkified = purple_markup_linkify(escaped);

	g_free(escaped);

	return linkified;
}

static gboolean
pidgin_message_query_tooltip_contents_cb(GtkWidget *self,
                                         G_GNUC_UNUSED gint x,
                                         G_GNUC_UNUSED gint y,
                                         G_GNUC_UNUSED gboolean keyboard_mode,
                                         GtkTooltip *tooltip,
                                         G_GNUC_UNUSED gpointer data)
{
	const char *uri = NULL;

	uri = gtk_label_get_current_uri(GTK_LABEL(self));
	if(birb_str_is_empty(uri)) {
		return FALSE;
	}

	gtk_tooltip_set_text(tooltip, uri);

	return TRUE;
}

static gboolean
pidgin_message_query_tooltip_edited_cb(G_GNUC_UNUSED GtkWidget *self,
                                       G_GNUC_UNUSED gint x,
                                       G_GNUC_UNUSED gint y,
                                       G_GNUC_UNUSED gboolean keyboard_mode,
                                       GtkTooltip *tooltip,
                                       gpointer data)
{
	PidginMessage *pidgin_message = data;
	PurpleMessage *purple_message = NULL;
	GDateTime *timestamp = NULL;

	if(!PIDGIN_IS_MESSAGE(pidgin_message)) {
		return FALSE;
	}

	purple_message = pidgin_message_get_message(pidgin_message);
	if(!PURPLE_IS_MESSAGE(purple_message)) {
		return FALSE;
	}

	timestamp = purple_message_get_edited_at(purple_message);

	return pidgin_message_set_tooltip_for_timestamp(tooltip, timestamp);
}

static gboolean
pidgin_message_query_tooltip_timestamp_cb(G_GNUC_UNUSED GtkWidget *self,
                                          G_GNUC_UNUSED gint x,
                                          G_GNUC_UNUSED gint y,
                                          G_GNUC_UNUSED gboolean keyboard_mode,
                                          GtkTooltip *tooltip,
                                          gpointer data)
{

	PidginMessage *message = data;
	GDateTime *timestamp = NULL;

	if(!PURPLE_IS_MESSAGE(message->message)) {
		return FALSE;
	}

	timestamp = purple_message_get_timestamp(message->message);

	return pidgin_message_set_tooltip_for_timestamp(tooltip, timestamp);
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
G_DEFINE_FINAL_TYPE(PidginMessage, pidgin_message, GTK_TYPE_BOX)

static void
pidgin_message_finalize(GObject *obj) {
	PidginMessage *message = PIDGIN_MESSAGE(obj);

	g_clear_object(&message->message);

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

static void
pidgin_message_get_property(GObject *obj, guint param_id, GValue *value,
                            GParamSpec *pspec)
{
	PidginMessage *message = PIDGIN_MESSAGE(obj);

	switch(param_id) {
	case PROP_MESSAGE:
		g_value_set_object(value, pidgin_message_get_message(message));
		break;
	case PROP_SHOW_FORMATTING:
		g_value_set_boolean(value,
		                    pidgin_message_get_show_formatting(message));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
pidgin_message_set_property(GObject *obj, guint param_id, const GValue *value,
                            GParamSpec *pspec)
{
	PidginMessage *message = PIDGIN_MESSAGE(obj);

	switch(param_id) {
	case PROP_MESSAGE:
		pidgin_message_set_message(message, g_value_get_object(value));
		break;
	case PROP_SHOW_FORMATTING:
		pidgin_message_set_show_formatting(message,
		                                   g_value_get_boolean(value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
pidgin_message_init(PidginMessage *message) {
	PurpleCore *core = NULL;
	GSettings *settings = NULL;

	gtk_widget_init_template(GTK_WIDGET(message));

	core = purple_core_get_default();
	settings = purple_core_new_settings(core, "im.pidgin.Pidgin3.Appearance");
	g_settings_bind(settings, "show-formatting",
	                G_OBJECT(message), "show-formatting",
	                G_SETTINGS_BIND_GET);
}

static void
pidgin_message_class_init(PidginMessageClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);

	obj_class->finalize = pidgin_message_finalize;
	obj_class->get_property = pidgin_message_get_property;
	obj_class->set_property = pidgin_message_set_property;

	/**
	 * PidginMessage:message:
	 *
	 * The [class@Purple.Message] to be displayed.
	 *
	 * Since: 3.0
	 */
	properties[PROP_MESSAGE] = g_param_spec_object(
		"message", NULL, NULL,
		PURPLE_TYPE_MESSAGE,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	/**
	 * PidginMessage:show-formatting:
	 *
	 * Whether or not formatting should be shown for this message.
	 *
	 * This will be bound to the show-formatting setting from
	 * im.pidgin.Pidgin3.Preferences.Appearance so any changes will affect
	 * all messages.
	 *
	 * Since: 3.0
	 */
	properties[PROP_SHOW_FORMATTING] = g_param_spec_boolean(
		"show-formatting", NULL, NULL,
		TRUE,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties(obj_class, N_PROPERTIES, properties);

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

	gtk_widget_class_bind_template_callback(widget_class,
	                                        pidgin_message_get_message_attributes);
	gtk_widget_class_bind_template_callback(widget_class,
	                                        pidgin_message_get_timestamp_string);
	gtk_widget_class_bind_template_callback(widget_class,
	                                        pidgin_message_query_tooltip_contents_cb);
	gtk_widget_class_bind_template_callback(widget_class,
	                                        pidgin_message_process_message_contents_cb);
	gtk_widget_class_bind_template_callback(widget_class,
	                                        pidgin_message_query_tooltip_edited_cb);
	gtk_widget_class_bind_template_callback(widget_class,
	                                        pidgin_message_query_tooltip_timestamp_cb);
}

/******************************************************************************
 * Public API
 *****************************************************************************/
PurpleMessage *
pidgin_message_get_message(PidginMessage *message) {
	g_return_val_if_fail(PIDGIN_IS_MESSAGE(message), NULL);

	return message->message;
}

gboolean
pidgin_message_get_show_formatting(PidginMessage *message) {
	g_return_val_if_fail(PIDGIN_IS_MESSAGE(message), FALSE);

	return message->show_formatting;
}

GtkWidget *
pidgin_message_new(PurpleMessage *message) {
	return g_object_new(
		PIDGIN_TYPE_MESSAGE,
		"message", message,
		NULL);
}

void
pidgin_message_set_message(PidginMessage *pidgin_message,
                           PurpleMessage *purple_message)
{
	g_return_if_fail(PIDGIN_IS_MESSAGE(pidgin_message));

	if(g_set_object(&pidgin_message->message, purple_message)) {
		g_object_notify_by_pspec(G_OBJECT(pidgin_message),
		                         properties[PROP_MESSAGE]);
	}
}

void
pidgin_message_set_show_formatting(PidginMessage *message,
                                   gboolean show_formatting)
{
	g_return_if_fail(PIDGIN_IS_MESSAGE(message));

	if(message->show_formatting != show_formatting) {
		GObject *obj = G_OBJECT(message);

		message->show_formatting = show_formatting;

		g_object_freeze_notify(obj);
		g_object_notify_by_pspec(obj, properties[PROP_MESSAGE]);
		g_object_notify_by_pspec(obj, properties[PROP_SHOW_FORMATTING]);
		g_object_thaw_notify(obj);
	}
}
