/*
 * Purple - Internet Messaging Library
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
 */

#include <glib/gi18n-lib.h>

#include "purpledemocommands.h"

#define PURPLE_DEMO_COMMAND_SOURCE N_("Demo Protocol")

/******************************************************************************
 * Command Implementations
 *****************************************************************************/
static void
purple_demo_command_edited_executed(G_GNUC_UNUSED PurpleCommand *command,
                                    PurpleConversation *conversation,
                                    GStrv params,
                                    G_GNUC_UNUSED gpointer data)
{
	PurpleAccount *account = NULL;
	PurpleContactInfo *info = NULL;
	PurpleConversationMember *member = NULL;
	PurpleConversationMembers *members = NULL;
	PurpleMessage *message = NULL;
	char *contents = NULL;

	members = purple_conversation_get_members(conversation);

	account = purple_conversation_get_account(conversation);
	info = purple_account_get_contact_info(account);
	member = purple_conversation_members_find_member(members, info);

	contents = g_strjoinv(" ", params);

	message = purple_message_new(member, contents);
	purple_message_set_edited(message, TRUE);

	g_clear_pointer(&contents, g_free);

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

/******************************************************************************
 * Public API
 *****************************************************************************/
void
purple_demo_commands_add(void) {
	PurpleCommand *command = NULL;
	PurpleCommandManager *manager = NULL;
	PurpleTags *tags = NULL;

	manager = purple_command_manager_get_default();

	command = purple_command_new("edited", PURPLE_DEMO_COMMAND_SOURCE, 1000);
	purple_command_set_summary(command,
	                           N_("Sends the contents as an edited message"));
	g_signal_connect(command, "executed",
	                 G_CALLBACK(purple_demo_command_edited_executed), NULL);
	tags = purple_command_get_tags(command);
	purple_tags_add(tags, "protocol-id:prpl-demo");
	purple_command_manager_add(manager, command);
}

void
purple_demo_commands_remove(void) {
	PurpleCommandManager *manager = purple_command_manager_get_default();

	purple_command_manager_remove_all_with_source(manager,
	                                              PURPLE_DEMO_COMMAND_SOURCE);
}