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

#include <glib.h>
#include <glib/gi18n-lib.h>

#include <gplugin.h>
#include <gplugin-native.h>

#include <purple.h>

#define FLIP_COMMAND_SOURCE _("Coin Flip")

/******************************************************************************
 * Callbacks
 *****************************************************************************/
static void
flip_executed_cb(G_GNUC_UNUSED PurpleCommand *command,
                 PurpleConversation *conversation,
                 G_GNUC_UNUSED char **params,
                 G_GNUC_UNUSED gpointer data)
{
	PurpleMessage *message = NULL;
	const char *contents = NULL;
	gboolean heads = FALSE;

	heads = g_random_boolean();
	if(heads) {
		contents = _("flips a coin: HEADS");
	} else {
		contents = _("flips a coin: TAILS");
	}

	message = purple_conversation_create_message(conversation, contents);
	purple_message_set_action(message, TRUE);

	purple_conversation_send_message_async(conversation, message, NULL, NULL,
	                                       NULL);
	g_clear_object(&message);
}

/******************************************************************************
 * Plugin Exports
 *****************************************************************************/
static GPluginPluginInfo *
flip_query(G_GNUC_UNUSED GError **error) {
	const char * const authors[] = {
		"Pidgin Developers <devel@pidgin.im>",
		NULL
	};
	const char *description = N_("Sends a random coin flip to a "
	                             "conversation.");

	return purple_plugin_info_new(
		"id", "flip",
		"abi-version", PURPLE_ABI_VERSION,
		"name", N_("Coin Flip"),
		"version", DISPLAY_VERSION,
		"category", N_("Games"),
		"summary", N_("Send random coin flip messages"),
		"description", description,
		"authors", authors,
		"website", PURPLE_WEBSITE,
		NULL);
}

static gboolean
flip_load(GPluginPlugin *plugin, G_GNUC_UNUSED GError **error) {
	PurpleCommand *command = NULL;
	PurpleCommandManager *manager = NULL;

	manager = purple_core_get_command_manager(purple_core_get_default());

	command = purple_command_new("flip", FLIP_COMMAND_SOURCE, 0);
	g_signal_connect_object(command, "executed", G_CALLBACK(flip_executed_cb),
	                        plugin, G_CONNECT_DEFAULT);
	purple_command_manager_add(manager, command);

	return TRUE;
}

static gboolean
flip_unload(G_GNUC_UNUSED GPluginPlugin *plugin,
            G_GNUC_UNUSED gboolean shutdown,
            G_GNUC_UNUSED GError **error)
{
	PurpleCommandManager *manager = NULL;

	manager = purple_core_get_command_manager(purple_core_get_default());
	purple_command_manager_remove_all_with_source(manager, FLIP_COMMAND_SOURCE);

	return TRUE;
}

GPLUGIN_NATIVE_PLUGIN_DECLARE(flip);
