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

#include "pidgin/pidginnotificationconnectionerror.h"

struct _PidginNotificationConnectionError {
	PidginNotification parent;

	GtkWidget *reconnect;
	GtkWidget *modify;
};

G_DEFINE_FINAL_TYPE(PidginNotificationConnectionError,
                    pidgin_notification_connection_error, PIDGIN_TYPE_NOTIFICATION)

/******************************************************************************
 * Helpers
 *****************************************************************************/
static void
pidgin_notification_connection_error_update_targets(PidginNotificationConnectionError *error)
{
	PurpleAccount *account = NULL;
	PidginNotification *pidgin_notification = PIDGIN_NOTIFICATION(error);
	PurpleNotification *purple_notification = NULL;
	const char *id = NULL;

	purple_notification = pidgin_notification_get_notification(pidgin_notification);
	account = purple_notification_get_account(purple_notification);
	id = purple_account_get_id(account);

	gtk_actionable_set_action_target(GTK_ACTIONABLE(error->reconnect), "s",
	                                 id);
	gtk_actionable_set_action_target(GTK_ACTIONABLE(error->modify), "s", id);
}

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

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
static void
pidgin_notification_connection_error_init(PidginNotificationConnectionError *notification)
{
	gtk_widget_init_template(GTK_WIDGET(notification));
}

static void
pidgin_notification_connection_error_constructed(GObject *obj) {
	PidginNotification *pidgin_notification = PIDGIN_NOTIFICATION(obj);
	PurpleNotification *purple_notification = NULL;

	G_OBJECT_CLASS(pidgin_notification_connection_error_parent_class)->constructed(obj);

	purple_notification = pidgin_notification_get_notification(pidgin_notification);
	g_signal_connect_object(purple_notification, "notify::account",
	                        G_CALLBACK(pidgin_notification_connection_error_account_notify_cb),
	                        obj, G_CONNECT_DEFAULT);

	pidgin_notification_connection_error_update_targets(PIDGIN_NOTIFICATION_CONNECTION_ERROR(obj));
}

static void
pidgin_notification_connection_error_class_init(PidginNotificationConnectionErrorClass *klass)
{
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);

	obj_class->constructed = pidgin_notification_connection_error_constructed;

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

	gtk_widget_class_bind_template_child(widget_class,
	                                     PidginNotificationConnectionError,
	                                     reconnect);
	gtk_widget_class_bind_template_child(widget_class,
	                                     PidginNotificationConnectionError,
	                                     modify);
}

/******************************************************************************
 * API
 *****************************************************************************/
GtkWidget *
pidgin_notification_connection_error_new(PurpleNotificationConnectionError *notification)
{
	g_return_val_if_fail(PURPLE_IS_NOTIFICATION_CONNECTION_ERROR(notification),
	                     NULL);

	return g_object_new(
		PIDGIN_TYPE_NOTIFICATION_CONNECTION_ERROR,
		"notification", notification,
		NULL);
}
