/*
 * Purple Demo - Demo Protocol Plugin
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * Purple Demo 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 "purpledemoconnection.h"

#include "purpledemocontacts.h"
#include "purpledemoprotocolactions.h"

struct _PurpleDemoConnection {
	PurpleConnection parent;

	PurpleContacts *contacts;
};

G_DEFINE_DYNAMIC_TYPE_EXTENDED(PurpleDemoConnection, purple_demo_connection,
                               PURPLE_TYPE_CONNECTION, G_TYPE_FLAG_FINAL, {})

/******************************************************************************
 * PurpleConnection Implementation
 *****************************************************************************/
static gboolean
purple_demo_connection_connect(PurpleConnection *connection,
                               G_GNUC_UNUSED GError **error)
{
	PurpleAccount *account = NULL;
	PurpleContactInfo *info = NULL;
	PurplePresence *presence = NULL;
	PurplePresenceManager *manager = NULL;
	PurpleSavedPresence *saved_presence = NULL;
	const char *username = NULL;

	account = purple_connection_get_account(connection);
	info = purple_account_get_contact_info(account);
	presence = purple_contact_info_get_presence(info);

	username = purple_account_get_username(account);
	purple_contact_info_set_id(info, username);

	manager = purple_presence_manager_get_default();
	saved_presence = purple_presence_manager_get_active(manager);

	if(!PURPLE_IS_SAVED_PRESENCE(saved_presence)) {
		purple_presence_set_primitive(presence,
		                              PURPLE_PRESENCE_PRIMITIVE_AVAILABLE);
	} else {
		PurplePresencePrimitive primitive;
		const char *emoji = NULL;
		const char *message = NULL;

		emoji = purple_saved_presence_get_emoji(saved_presence);
		message = purple_saved_presence_get_message(saved_presence);
		primitive = purple_saved_presence_get_primitive(saved_presence);

		purple_presence_set_emoji(presence, emoji);
		purple_presence_set_message(presence, message);
		purple_presence_set_primitive(presence, primitive);
	}

	purple_account_ready(account);

	purple_demo_contacts_load(PURPLE_DEMO_CONNECTION(connection), account);

	return TRUE;
}

static gboolean
purple_demo_connection_disconnect(G_GNUC_UNUSED PurpleConnection *connection,
                                  G_GNUC_UNUSED const char *message,
                                  G_GNUC_UNUSED GError **error)
{
	return TRUE;
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
static void
purple_demo_connection_finalize(GObject *obj) {
	PurpleDemoConnection *connection = PURPLE_DEMO_CONNECTION(obj);

	g_clear_object(&connection->contacts);

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

static void
purple_demo_connection_init(PurpleDemoConnection *connection) {
	connection->contacts = purple_contacts_new();
}

static void
purple_demo_connection_class_finalize(G_GNUC_UNUSED PurpleDemoConnectionClass *klass) {
}

static void
purple_demo_connection_class_init(PurpleDemoConnectionClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);
	PurpleConnectionClass *connection_class = PURPLE_CONNECTION_CLASS(klass);

	obj_class->finalize = purple_demo_connection_finalize;

	connection_class->connect = purple_demo_connection_connect;
	connection_class->disconnect = purple_demo_connection_disconnect;
	connection_class->get_action_menu = purple_demo_protocol_get_action_menu;
}

/******************************************************************************
 * Internal API
 *****************************************************************************/
void
purple_demo_connection_register(GPluginNativePlugin *plugin) {
	purple_demo_connection_register_type(G_TYPE_MODULE(plugin));
}

PurpleContacts *
purple_demo_connection_get_contacts(PurpleDemoConnection *connection) {
	g_return_val_if_fail(PURPLE_DEMO_IS_CONNECTION(connection), NULL);

	return connection->contacts;
}
