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

#include <glib/gi18n-lib.h>

#include "purplezulipconnection.h"

#include "purplezulipclient.h"
#include "purplezulipconstants.h"
#include "purplezulipeventqueue.h"

struct _PurpleZulipConnection {
	PurpleConnection parent;

	PurpleZulipClient *client;
	PurpleZulipEventQueue *event_queue;
};

#define PURPLE_ZULIP_CONNECTION_ERROR (g_quark_from_static_string("purple-zulip-connection"))

/******************************************************************************
 * PurpleConnection Implementation
 *****************************************************************************/
static void
purple_zulip_connection_login_cb(G_GNUC_UNUSED GObject *source,
                                 GAsyncResult *result,
                                 gpointer data)
{
	PurpleZulipConnection *connection = data;
	PurpleAccount *account = NULL;
	GError *error = NULL;
	gboolean success = FALSE;

	account = purple_connection_get_account(PURPLE_CONNECTION(connection));

	success = purple_zulip_client_login_finish(connection->client, result,
	                                           &error);
	if(error != NULL) {
		purple_account_disconnect_with_error(account, NULL, error);

		return;
	}

	if(!success) {
		purple_account_disconnect_with_new_error(account, NULL,
		                                         PURPLE_ZULIP_CONNECTION_ERROR,
		                                         0,
		                                         "unknown error");

		return;
	}

	purple_zulip_event_queue_connect(connection->event_queue);

	purple_account_ready(account);
}

static gboolean
purple_zulip_connection_connect(PurpleConnection *purple_connection,
                                G_GNUC_UNUSED GError **error)
{
	PurpleZulipConnection *connection = NULL;
	PurpleAccount *account = NULL;
	PurpleAccountSettings *settings = NULL;
	GCancellable *cancellable = NULL;
	const char *server_uri = NULL;

	connection = PURPLE_ZULIP_CONNECTION(purple_connection);

	account = purple_connection_get_account(purple_connection);
	cancellable = purple_connection_get_cancellable(purple_connection);
	settings = purple_account_get_settings(account);

	/* The settings should already have been validated by
	 * purple_account_connect.
	 */
	server_uri = purple_account_settings_get_string(settings,
	                                                PURPLE_ZULIP_SETTING_SERVER_URI,
	                                                NULL);

	connection->client = purple_zulip_client_new(server_uri, cancellable);
	connection->event_queue = purple_zulip_event_queue_new(connection,
	                                                       connection->client,
	                                                       cancellable);

	purple_zulip_client_login_async(connection->client, account,
	                                purple_zulip_connection_login_cb,
	                                connection);

	return TRUE;
}

static gboolean
purple_zulip_connection_disconnect(PurpleConnection *purple_connection,
                                   G_GNUC_UNUSED const char *message,
                                   G_GNUC_UNUSED GError **error)
{
	PurpleZulipConnection *connection = NULL;

	connection = PURPLE_ZULIP_CONNECTION(purple_connection);

	purple_zulip_event_queue_disconnect(connection->event_queue);

	g_clear_object(&connection->client);
	g_clear_object(&connection->event_queue);

	return TRUE;
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
G_DEFINE_DYNAMIC_TYPE_EXTENDED(
	PurpleZulipConnection,
	purple_zulip_connection,
	PURPLE_TYPE_CONNECTION,
	G_TYPE_FLAG_FINAL,
	{})

static void
purple_zulip_connection_finalize(GObject *obj) {
	PurpleZulipConnection *connection = PURPLE_ZULIP_CONNECTION(obj);

	g_clear_object(&connection->client);
	g_clear_object(&connection->event_queue);

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

static void
purple_zulip_connection_init(G_GNUC_UNUSED PurpleZulipConnection *connection) {
}

static void
purple_zulip_connection_class_finalize(G_GNUC_UNUSED PurpleZulipConnectionClass *klass)
{
}

static void
purple_zulip_connection_class_init(PurpleZulipConnectionClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);
	PurpleConnectionClass *connection_class = PURPLE_CONNECTION_CLASS(klass);

	obj_class->finalize = purple_zulip_connection_finalize;

	connection_class->connect = purple_zulip_connection_connect;
	connection_class->disconnect = purple_zulip_connection_disconnect;
}

/******************************************************************************
 * Local Exports
 *****************************************************************************/
void
purple_zulip_connection_register(GPluginNativePlugin *plugin) {
	purple_zulip_connection_register_type(G_TYPE_MODULE(plugin));
}
