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

#include <birb.h>

#include "purplezuliptypes.h"

#define PURPLE_ZULIP_TYPE_COPY_STRING(sym) copy->sym = g_strdup(source->sym)
#define PURPLE_ZULIP_TYPE_COPY_VALUE(sym) copy->sym = source->sym

#define PURPLE_ZULIP_TYPE_GET_BOOLEAN(inst, sym) G_STMT_START { \
	inst->sym = json_object_get_boolean_member_with_default(object, \
	                                                        G_STRINGIFY_ARG(sym), \
	                                                        FALSE); \
} G_STMT_END

#define PURPLE_ZULIP_TYPE_GET_INT(inst, sym) G_STMT_START { \
	inst->sym = json_object_get_int_member_with_default(object, \
	                                                    G_STRINGIFY_ARG(sym), \
	                                                    G_GINT64_CONSTANT(0)); \
} G_STMT_END

#define PURPLE_ZULIP_TYPE_GET_STRING(inst, sym) G_STMT_START { \
	const char *value = NULL; \
	value = json_object_get_string_member_with_default(object, \
	                                                   G_STRINGIFY_ARG(sym), \
	                                                   NULL); \
	inst->sym = g_strdup(value); \
} G_STMT_END

/******************************************************************************
 * Message
 *****************************************************************************/
PurpleZulipMessage *
purple_zulip_message_copy(PurpleZulipMessage *source) {
	PurpleZulipMessage *copy = NULL;

	g_return_val_if_fail(source != NULL, NULL);

	copy = g_new0(PurpleZulipMessage, 1);

	PURPLE_ZULIP_TYPE_COPY_STRING(avatar_url);
	PURPLE_ZULIP_TYPE_COPY_STRING(client);
	PURPLE_ZULIP_TYPE_COPY_STRING(content);
	PURPLE_ZULIP_TYPE_COPY_STRING(content_type);
	PURPLE_ZULIP_TYPE_COPY_VALUE(id);
	PURPLE_ZULIP_TYPE_COPY_VALUE(is_me_message);
	PURPLE_ZULIP_TYPE_COPY_VALUE(recipient_id);
	PURPLE_ZULIP_TYPE_COPY_STRING(sender_email);
	PURPLE_ZULIP_TYPE_COPY_STRING(sender_full_name);
	PURPLE_ZULIP_TYPE_COPY_VALUE(sender_id);
	PURPLE_ZULIP_TYPE_COPY_STRING(sender_realm_str);
	PURPLE_ZULIP_TYPE_COPY_VALUE(stream_id);
	PURPLE_ZULIP_TYPE_COPY_STRING(subject);
	PURPLE_ZULIP_TYPE_COPY_VALUE(timestamp);
	PURPLE_ZULIP_TYPE_COPY_STRING(type);

	return copy;
}

void
purple_zulip_message_free(PurpleZulipMessage *message) {
	g_return_if_fail(message != NULL);

	g_free(message->avatar_url);
	g_free(message->client);
	g_free(message->content);
	g_free(message->content_type);
	g_free(message->sender_email);
	g_free(message->sender_full_name);
	g_free(message->sender_realm_str);
	g_free(message->subject);
	g_free(message->type);

	g_free(message);
}

static gpointer
purple_zulip_message_deserialize(JsonNode *node) {
	PurpleZulipMessage *message = NULL;
	JsonObject *object = NULL;

	message = g_new0(PurpleZulipMessage, 1);

	object = json_node_get_object(node);

	PURPLE_ZULIP_TYPE_GET_STRING(message, avatar_url);
	PURPLE_ZULIP_TYPE_GET_STRING(message, client);
	PURPLE_ZULIP_TYPE_GET_STRING(message, content);
	PURPLE_ZULIP_TYPE_GET_STRING(message, content_type);
	PURPLE_ZULIP_TYPE_GET_INT(message, id);
	PURPLE_ZULIP_TYPE_GET_BOOLEAN(message, is_me_message);
	PURPLE_ZULIP_TYPE_GET_INT(message, recipient_id);
	PURPLE_ZULIP_TYPE_GET_STRING(message, sender_email);
	PURPLE_ZULIP_TYPE_GET_STRING(message, sender_full_name);
	PURPLE_ZULIP_TYPE_GET_INT(message, sender_id);
	PURPLE_ZULIP_TYPE_GET_STRING(message, sender_realm_str);
	PURPLE_ZULIP_TYPE_GET_INT(message, stream_id);
	PURPLE_ZULIP_TYPE_GET_STRING(message, subject);
	PURPLE_ZULIP_TYPE_GET_INT(message, timestamp);
	PURPLE_ZULIP_TYPE_GET_STRING(message, type);

	return message;
}

G_DEFINE_BOXED_TYPE_WITH_CODE(PurpleZulipMessage,
                              purple_zulip_message,
                              purple_zulip_message_copy,
                              purple_zulip_message_free,
                              json_boxed_register_deserialize_func(g_define_type_id,
                                                                   JSON_NODE_OBJECT,
                                                                   purple_zulip_message_deserialize))

/******************************************************************************
 * User
 *****************************************************************************/
PurpleZulipUser *
purple_zulip_user_copy(PurpleZulipUser *source) {
	PurpleZulipUser *copy = NULL;

	g_return_val_if_fail(source != NULL, NULL);

	copy = g_new0(PurpleZulipUser, 1);

	PURPLE_ZULIP_TYPE_COPY_STRING(avatar_url);
	PURPLE_ZULIP_TYPE_COPY_VALUE(avatar_version);
	PURPLE_ZULIP_TYPE_COPY_VALUE(bot_type);
	PURPLE_ZULIP_TYPE_COPY_VALUE(bot_owner_id);
	PURPLE_ZULIP_TYPE_COPY_STRING(date_joined);
	PURPLE_ZULIP_TYPE_COPY_STRING(delivery_email);
	PURPLE_ZULIP_TYPE_COPY_STRING(email);
	PURPLE_ZULIP_TYPE_COPY_STRING(full_name);
	PURPLE_ZULIP_TYPE_COPY_VALUE(is_active);
	PURPLE_ZULIP_TYPE_COPY_VALUE(is_admin);
	PURPLE_ZULIP_TYPE_COPY_VALUE(is_bot);
	PURPLE_ZULIP_TYPE_COPY_VALUE(is_guest);
	PURPLE_ZULIP_TYPE_COPY_VALUE(is_imported_stub);
	PURPLE_ZULIP_TYPE_COPY_VALUE(is_owner);
	PURPLE_ZULIP_TYPE_COPY_VALUE(role);
	PURPLE_ZULIP_TYPE_COPY_STRING(timezone);
	PURPLE_ZULIP_TYPE_COPY_VALUE(user_id);

	return copy;
}

void
purple_zulip_user_free(PurpleZulipUser *user) {
	g_return_if_fail(user != NULL);

	g_free(user->avatar_url);
	g_free(user->date_joined);
	g_free(user->delivery_email);
	g_free(user->email);
	g_free(user->full_name);
	g_free(user->timezone);

	g_free(user);
}

static gpointer
purple_zulip_user_deserialize(JsonNode *node) {
	PurpleZulipUser *user = NULL;
	JsonObject *object = NULL;

	user = g_new(PurpleZulipUser, 1);

	object = json_node_get_object(node);

	PURPLE_ZULIP_TYPE_GET_STRING(user, avatar_url);
	PURPLE_ZULIP_TYPE_GET_INT(user, avatar_version);
	PURPLE_ZULIP_TYPE_GET_INT(user, bot_type);
	PURPLE_ZULIP_TYPE_GET_INT(user, bot_owner_id);
	PURPLE_ZULIP_TYPE_GET_STRING(user, date_joined);
	PURPLE_ZULIP_TYPE_GET_STRING(user, delivery_email);
	PURPLE_ZULIP_TYPE_GET_STRING(user, email);
	PURPLE_ZULIP_TYPE_GET_STRING(user, full_name);
	PURPLE_ZULIP_TYPE_GET_BOOLEAN(user, is_active);
	PURPLE_ZULIP_TYPE_GET_BOOLEAN(user, is_admin);
	PURPLE_ZULIP_TYPE_GET_BOOLEAN(user, is_bot);
	PURPLE_ZULIP_TYPE_GET_BOOLEAN(user, is_guest);
	PURPLE_ZULIP_TYPE_GET_BOOLEAN(user, is_imported_stub);
	PURPLE_ZULIP_TYPE_GET_BOOLEAN(user, is_owner);
	PURPLE_ZULIP_TYPE_GET_INT(user, role);
	PURPLE_ZULIP_TYPE_GET_STRING(user, timezone);
	PURPLE_ZULIP_TYPE_GET_INT(user, user_id);

	return user;
}

G_DEFINE_BOXED_TYPE_WITH_CODE(PurpleZulipUser,
                              purple_zulip_user,
                              purple_zulip_user_copy,
                              purple_zulip_user_free,
                              json_boxed_register_deserialize_func(g_define_type_id,
                                                                   JSON_NODE_OBJECT,
                                                                   purple_zulip_user_deserialize))
PurpleContact *
purple_zulip_user_to_contact(PurpleZulipUser *user, PurpleAccount *account) {
	PurpleContact *contact = NULL;
	PurpleContactInfo *info = NULL;
	char *id = NULL;

	g_return_val_if_fail(user != NULL, NULL);
	g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), NULL);

	id = g_strdup_printf("%d", user->user_id);
	contact = purple_contact_new(account, id);
	g_clear_pointer(&id, g_free);

	info = PURPLE_CONTACT_INFO(contact);
	purple_contact_info_set_display_name(info, user->full_name);
	purple_contact_info_set_email(info, user->email);

	if(!birb_str_is_empty(user->date_joined)) {
		GDateTime *created_time = NULL;

		created_time = g_date_time_new_from_iso8601(user->date_joined, NULL);
		purple_contact_info_set_created_time(info, created_time);
		g_clear_pointer(&created_time, g_date_time_unref);
	} else {
		purple_contact_info_set_created_time(info, NULL);
	}

	if(!birb_str_is_empty(user->timezone)) {
		GTimeZone *time_zone = NULL;

		time_zone = g_time_zone_new_identifier(user->timezone);
		purple_contact_info_set_time_zone(info, time_zone);
		g_clear_pointer(&time_zone, g_time_zone_unref);
	} else {
		purple_contact_info_set_time_zone(info, NULL);
	}

	return contact;
}
