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

#include "purplezulipconnection.h"
#include "purplezulipconstants.h"

struct _PurpleZulipProtocol {
	PurpleProtocol parent;
};

#define PURPLE_ZULIP_PROTOCOL_ERROR (g_quark_from_static_string("purple-zulip-protocol"))

/******************************************************************************
 * PurpleProtocol Implementation
 *****************************************************************************/
static PurpleAccountSettings *
purple_zulip_protocol_get_default_account_settings(G_GNUC_UNUSED PurpleProtocol *protocol)
{
	PurpleAccountSetting *setting = NULL;
	PurpleAccountSettings *settings = NULL;

	settings = purple_account_settings_new();

	setting = purple_account_setting_string_new(PURPLE_ZULIP_SETTING_SERVER_URI,
	                                            _("Server URI"),
	                                            NULL);
	purple_account_settings_add_setting(settings, setting);

	setting = purple_account_setting_string_new(PURPLE_ZULIP_SETTING_EMAIL,
	                                            _("E-Mail"),
	                                            NULL);
	purple_account_settings_add_setting(settings, setting);

	return settings;
}

static gboolean
purple_zulip_protocol_validate_account(G_GNUC_UNUSED PurpleProtocol *protocol,
                                       PurpleAccount *account,
                                       GError **error)
{
	PurpleAccountSettings *settings = NULL;
	const char *server_uri = NULL;

	settings = purple_account_get_settings(account);

	server_uri = purple_account_settings_get_string(settings,
	                                                PURPLE_ZULIP_SETTING_SERVER_URI,
	                                                NULL);
	if(birb_str_is_empty(server_uri)) {
		g_set_error_literal(error, PURPLE_ZULIP_PROTOCOL_ERROR, 0,
		                    _("server URI was not provided"));

		return FALSE;
	}

	return g_uri_is_valid(server_uri, G_URI_FLAGS_NONE, error);
}

static PurpleConnection *
purple_zulip_protocol_create_connection(G_GNUC_UNUSED PurpleProtocol *protocol,
                                        PurpleAccount *account,
                                        G_GNUC_UNUSED GError **error)
{
	g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), NULL);

	return g_object_new(
		PURPLE_ZULIP_TYPE_CONNECTION,
		"account", account,
		NULL);
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
G_DEFINE_DYNAMIC_TYPE_EXTENDED(
	PurpleZulipProtocol,
	purple_zulip_protocol,
	PURPLE_TYPE_PROTOCOL,
	G_TYPE_FLAG_FINAL,
	{})

static void
purple_zulip_protocol_init(G_GNUC_UNUSED PurpleZulipProtocol *protocol) {
}

static void
purple_zulip_protocol_class_finalize(G_GNUC_UNUSED PurpleZulipProtocolClass *klass)
{
}

static void
purple_zulip_protocol_class_init(PurpleZulipProtocolClass *klass) {
	PurpleProtocolClass *protocol_class = PURPLE_PROTOCOL_CLASS(klass);

	protocol_class->get_default_account_settings =
		purple_zulip_protocol_get_default_account_settings;
	protocol_class->validate_account = purple_zulip_protocol_validate_account;

	protocol_class->create_connection =
		purple_zulip_protocol_create_connection;
}

/******************************************************************************
 * Local Exports
 *****************************************************************************/
void
purple_zulip_protocol_register(GPluginNativePlugin *plugin) {
	purple_zulip_protocol_register_type(G_TYPE_MODULE(plugin));
}

PurpleProtocol *
purple_zulip_protocol_new(void) {
	PurpleProtocol *protocol = NULL;
	PurpleTags *tags = NULL;

	protocol = g_object_new(
		PURPLE_ZULIP_TYPE_PROTOCOL,
		"id", "prpl-zulip",
		"name", "ZULIP",
		"description", _("Zulip is an open source chat and collaborative "
		                 "software."),
		"icon-name", "im-purple-zulip",
		"icon-resource-path", "/im/pidgin/libpurple/protocols/zulip/icons",
		NULL);

	tags = purple_protocol_get_tags(protocol);
	purple_tags_add(tags, "in-development");

	return protocol;
}
