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

struct _PidginInfoPane {
	GtkBox parent;

	PidginAvatar *avatar;
	GtkWidget *child;

	char *subtitle;
	char *title;
};

enum {
	PROP_0,
	PROP_AVATAR,
	PROP_CHILD,
	PROP_SUBTITLE,
	PROP_TITLE,
	N_PROPERTIES,
};
static GParamSpec *properties[N_PROPERTIES] = {NULL, };

G_DEFINE_FINAL_TYPE(PidginInfoPane, pidgin_info_pane, GTK_TYPE_BOX)

/******************************************************************************
 * Callbacks
 *****************************************************************************/
static char *
pidgin_info_pane_linkify_text_cb(GObject *self, const char *subtitle,
                                 G_GNUC_UNUSED gpointer data)
{
	GtkWidget *label = GTK_WIDGET(self);
	char *ret = NULL;

	ret = purple_markup_linkify(subtitle);

	gtk_widget_set_visible(label, !birb_str_is_empty(ret));

	return ret;
}

static void
pidgin_info_pane_subtitle_enter_cb(GtkEventControllerMotion *self,
                                   G_GNUC_UNUSED gdouble x,
                                   G_GNUC_UNUSED gdouble y,
                                   G_GNUC_UNUSED gpointer data)
{
	GtkWidget *label = NULL;

	label = gtk_event_controller_get_widget(GTK_EVENT_CONTROLLER(self));
	gtk_label_set_lines(GTK_LABEL(label), 10);
}

static void
pidgin_info_pane_subtitle_leave_cb(GtkEventControllerMotion *self,
                                   G_GNUC_UNUSED gdouble x,
                                   G_GNUC_UNUSED gdouble y,
                                   G_GNUC_UNUSED gpointer data)
{
	GtkWidget *label = NULL;

	label = gtk_event_controller_get_widget(GTK_EVENT_CONTROLLER(self));
	gtk_label_set_lines(GTK_LABEL(label), 2);
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
static void
pidgin_info_pane_get_property(GObject *obj, guint param_id, GValue *value,
                              GParamSpec *pspec)
{
	PidginInfoPane *pane = PIDGIN_INFO_PANE(obj);

	switch(param_id) {
	case PROP_AVATAR:
		g_value_set_object(value, pidgin_info_pane_get_avatar(pane));
		break;
	case PROP_CHILD:
		g_value_set_object(value, pidgin_info_pane_get_child(pane));
		break;
	case PROP_SUBTITLE:
		g_value_set_string(value, pidgin_info_pane_get_subtitle(pane));
		break;
	case PROP_TITLE:
		g_value_set_string(value, pidgin_info_pane_get_title(pane));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
pidgin_info_pane_set_property(GObject *obj, guint param_id, const GValue *value,
                              GParamSpec *pspec)
{
	PidginInfoPane *pane = PIDGIN_INFO_PANE(obj);

	switch(param_id) {
		case PROP_CHILD:
			pidgin_info_pane_set_child(pane, g_value_get_object(value));
			break;
		case PROP_SUBTITLE:
			pidgin_info_pane_set_subtitle(pane, g_value_get_string(value));
			break;
		case PROP_TITLE:
			pidgin_info_pane_set_title(pane, g_value_get_string(value));
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
			break;
	}
}

static void
pidgin_info_pane_finalize(GObject *obj) {
	PidginInfoPane *pane = PIDGIN_INFO_PANE(obj);

	g_clear_pointer(&pane->title, g_free);
	g_clear_pointer(&pane->subtitle, g_free);

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

static void
pidgin_info_pane_init(PidginInfoPane *pane) {
	gtk_widget_init_template(GTK_WIDGET(pane));
}

static void
pidgin_info_pane_class_init(PidginInfoPaneClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);

	obj_class->finalize = pidgin_info_pane_finalize;
	obj_class->get_property = pidgin_info_pane_get_property;
	obj_class->set_property = pidgin_info_pane_set_property;

	/**
	 * PidginInfoPane:avatar:
	 *
	 * The avatar widget the info pane.
	 *
	 * Since: 3.0
	 */
	properties[PROP_AVATAR] = g_param_spec_object(
		"avatar", NULL, NULL,
		PIDGIN_TYPE_AVATAR,
		G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

	/**
	 * PidginInfoPane:child:
	 *
	 * The child widget to display at the end of the info pane.
	 *
	 * Since: 3.0
	 */
	properties[PROP_CHILD] = g_param_spec_object(
		"child", NULL, NULL,
		GTK_TYPE_WIDGET,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

	/**
	 * PidginInfoPane:subtitle:
	 *
	 * The subtitle to display.
	 *
	 * Since: 3.0
	 */
	properties[PROP_SUBTITLE] = g_param_spec_string(
		"subtitle", NULL, NULL,
		NULL,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

	/**
	 * PidginInfoPane:title:
	 *
	 * The title to display.
	 *
	 * Since: 3.0
	 */
	properties[PROP_TITLE] = g_param_spec_string(
		"title", NULL, NULL,
		NULL,
		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/infopane.ui");

	gtk_widget_class_bind_template_child(widget_class, PidginInfoPane, avatar);

	gtk_widget_class_bind_template_callback(widget_class,
	                                        pidgin_info_pane_linkify_text_cb);
	gtk_widget_class_bind_template_callback(widget_class,
	                                        pidgin_info_pane_subtitle_enter_cb);
	gtk_widget_class_bind_template_callback(widget_class,
	                                        pidgin_info_pane_subtitle_leave_cb);
}

/******************************************************************************
 * API
 *****************************************************************************/
PidginAvatar *
pidgin_info_pane_get_avatar(PidginInfoPane *pane) {
	g_return_val_if_fail(PIDGIN_IS_INFO_PANE(pane), NULL);

	return pane->avatar;
}

GtkWidget *
pidgin_info_pane_get_child(PidginInfoPane *pane) {
	g_return_val_if_fail(PIDGIN_IS_INFO_PANE(pane), NULL);

	return pane->child;
}

const char *
pidgin_info_pane_get_subtitle(PidginInfoPane *pane) {
	g_return_val_if_fail(PIDGIN_IS_INFO_PANE(pane), NULL);

	return pane->subtitle;
}

const char *
pidgin_info_pane_get_title(PidginInfoPane *pane) {
	g_return_val_if_fail(PIDGIN_IS_INFO_PANE(pane), NULL);

	return pane->title;
}

GtkWidget *
pidgin_info_pane_new(void) {
	return g_object_new(PIDGIN_TYPE_INFO_PANE, NULL);
}

void
pidgin_info_pane_set_child(PidginInfoPane *pane, GtkWidget *widget) {
	g_return_if_fail(PIDGIN_IS_INFO_PANE(pane));
	g_return_if_fail(widget == NULL || gtk_widget_get_parent(widget) == NULL);

	if(pane->child == widget) {
		return;
	}

	g_clear_object(&pane->child);

	if(GTK_IS_WIDGET(widget)) {
		pane->child = g_object_ref_sink(widget);
		gtk_box_append(GTK_BOX(pane), pane->child);

		/* This might not be the best place for this, but this helps keep the
		 * user interface consistent and doesn't force users to do it.
		 */
		gtk_widget_set_margin_end(pane->child, 6);
	}

	g_object_notify_by_pspec(G_OBJECT(pane), properties[PROP_CHILD]);
}

void
pidgin_info_pane_set_subtitle(PidginInfoPane *pane, const char *subtitle) {
	g_return_if_fail(PIDGIN_IS_INFO_PANE(pane));

	if(g_set_str(&pane->subtitle, subtitle)) {
		g_object_notify_by_pspec(G_OBJECT(pane), properties[PROP_SUBTITLE]);
	}
}

void
pidgin_info_pane_set_title(PidginInfoPane *pane, const char *title) {
	g_return_if_fail(PIDGIN_IS_INFO_PANE(pane));

	if(g_set_str(&pane->title, title)) {
		g_object_notify_by_pspec(G_OBJECT(pane), properties[PROP_TITLE]);
	}
}
