/*
 * Purple - Internet Messaging Library
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * Purple 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 library 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 library 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 library; if not, see <https://www.gnu.org/licenses/>.
 */

#include <glib/gi18n-lib.h>

#include "purplenotificationconnectionerror.h"

struct _PurpleNotificationConnectionError {
	PurpleNotification parent;
};

enum {
	PROP_0,
	N_PROPERTIES,
	/* Overrides */
	PROP_ACCOUNT,
};

static void
purple_notification_connection_error_error_notify_cb(GObject *self,
                                                     GParamSpec *pspec,
                                                     gpointer data);

/******************************************************************************
 * Helpers
 *****************************************************************************/
static void
purple_notification_connection_error_update(PurpleNotification *notification) {
	PurpleAccount *account = NULL;
	PurpleProtocol *protocol = NULL;
	GError *error = NULL;
	GObject *obj = G_OBJECT(notification);
	char *title = NULL;
	const char *icon_name = "network-error-symbolic";
	const char *subtitle = NULL;

	account = purple_notification_get_account(notification);
	error = purple_account_get_error(account);

	protocol = purple_account_get_protocol(account);
	if(PURPLE_IS_PROTOCOL(protocol)) {
		icon_name = purple_protocol_get_icon_name(protocol);
	}

	if(error == NULL) {
		title = g_strdup_printf(_("%s has no connection error"),
		                        purple_account_get_name(account));
	} else {
		title = g_strdup_printf(_("%s disconnected"),
		                        purple_account_get_name(account));
		subtitle = error->message;
	}

	g_object_freeze_notify(obj);
	purple_notification_set_icon_name(notification, icon_name);
	purple_notification_set_subtitle(notification, subtitle);
	purple_notification_set_title(notification, title);
	g_object_thaw_notify(obj);

	g_clear_pointer(&title, g_free);
}

static void
purple_notification_connection_error_set_account(PurpleNotificationConnectionError *error,
                                                 PurpleAccount *account)
{
	PurpleAccount *old = NULL;
	PurpleNotification *notification = PURPLE_NOTIFICATION(error);

	old = purple_notification_get_account(notification);
	if(old == account) {
		return;
	}

	if(PURPLE_IS_ACCOUNT(old)) {
		g_signal_handlers_disconnect_by_func(old,
		                                     purple_notification_connection_error_error_notify_cb,
		                                     notification);
	}

	purple_notification_set_account(notification, account);

	g_signal_connect_object(account, "notify::error",
	                        G_CALLBACK(purple_notification_connection_error_error_notify_cb),
	                        notification, G_CONNECT_DEFAULT);

	purple_notification_connection_error_update(notification);
}

/******************************************************************************
 * Callbacks
 *****************************************************************************/
static void
purple_notification_connection_error_error_notify_cb(G_GNUC_UNUSED GObject *self,
                                                     G_GNUC_UNUSED GParamSpec *pspec,
                                                     gpointer data)
{
	purple_notification_connection_error_update(data);
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
G_DEFINE_FINAL_TYPE(PurpleNotificationConnectionError,
                    purple_notification_connection_error,
                    PURPLE_TYPE_NOTIFICATION)

static void
purple_notification_connection_error_get_property(GObject *obj, guint param_id,
                                                  GValue *value,
                                                  GParamSpec *pspec)
{
	PurpleNotification *notification = PURPLE_NOTIFICATION(obj);

	switch(param_id) {
	/* Overrides */
	case PROP_ACCOUNT:
		g_value_set_object(value,
		                   purple_notification_get_account(notification));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
purple_notification_connection_error_set_property(GObject *obj, guint param_id,
                                                  const GValue *value,
                                                  GParamSpec *pspec)
{
	PurpleNotificationConnectionError *error = NULL;

	error = PURPLE_NOTIFICATION_CONNECTION_ERROR(obj);

	switch(param_id) {
	/* Overrides */
	case PROP_ACCOUNT:
		purple_notification_connection_error_set_account(error,
		                                                 g_value_get_object(value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
purple_notification_connection_error_init(G_GNUC_UNUSED PurpleNotificationConnectionError *notification)
{
}

static void
purple_notification_connection_error_class_init(PurpleNotificationConnectionErrorClass *klass)
{
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);

	obj_class->get_property = purple_notification_connection_error_get_property;
	obj_class->set_property = purple_notification_connection_error_set_property;

	g_object_class_override_property(obj_class, PROP_ACCOUNT, "account");
}

/******************************************************************************
 * Public API
 *****************************************************************************/
PurpleNotification *
purple_notification_connection_error_new(const char *id,
                                         PurpleAccount *account)
{
	g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), NULL);

	return g_object_new(
		PURPLE_TYPE_NOTIFICATION_CONNECTION_ERROR,
		"id", id,
		"account", account,
		NULL);
}
