/*
 * 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/>.
 */

#ifdef G_LOG_DOMAIN
# undef G_LOG_DOMAIN
#endif
#define G_LOG_DOMAIN "PurpleScheduler"

#include <birb.h>

#include "purplescheduler.h"
#include "purpleschedulerprivate.h"

struct _PurpleScheduler {
	GObject parent;

	GPtrArray *tasks;
};

enum {
	PROP_0,
	PROP_ITEM_TYPE,
	PROP_N_ITEMS,
	N_PROPERTIES,
};
static GParamSpec *properties[N_PROPERTIES] = {NULL, };

enum {
	SIG_EXECUTE_TASK,
	N_SIGNALS,
};
static guint signals[N_SIGNALS] = {0, };

G_DEFINE_QUARK(purple-scheduler-error, purple_scheduler_error)

/******************************************************************************
 * Helpers
 *****************************************************************************/
/**
 * purple_scheduler_find_task_with_id: (skip)
 * @id: the id of the task to search for
 * @position: (out) (nullable): a return address for the position of the item
 *
 * Looks for a task with the given id.
 *
 * If the task is found it will be returned as well as it's position.
 *
 * Returns: (transfer none) (nullable): The task if found.
 *
 * Since: 3.0
 */
static PurpleScheduledTask *
purple_scheduler_find_task_with_id(PurpleScheduler *scheduler,
                                   const char *id,
                                   guint *position)
{
	g_return_val_if_fail(PURPLE_IS_SCHEDULER(scheduler), NULL);

	for(guint i = 0; i < scheduler->tasks->len; i++) {
		PurpleScheduledTask *task = NULL;
		const char *task_id = NULL;

		task = g_ptr_array_index(scheduler->tasks, i);
		task_id = purple_scheduled_task_get_id(task);

		if(birb_str_equal(id, task_id)) {
			if(position != NULL) {
				*position = i;
			}

			return task;
		}
	}

	return NULL;
}

/**
 * purple_scheduler_unref_task: (skip)
 * @task: the task to cancel and unref
 *
 * Cancels a task if necessary before unreferencing it.
 *
 * Since: 3.0
 */
static void
purple_scheduler_unref_task(PurpleScheduledTask *task) {
	PurpleScheduledTaskState state = PURPLE_SCHEDULED_TASK_STATE_UNSCHEDULED;

	g_return_if_fail(PURPLE_IS_SCHEDULED_TASK(task));

	state = purple_scheduled_task_get_state(task);
	if(state == PURPLE_SCHEDULED_TASK_STATE_SCHEDULED) {
		purple_scheduled_task_cancel(task);
	}

	g_object_unref(task);
}

/******************************************************************************
 * Callbacks
 *****************************************************************************/
static void
purple_scheduler_task_execute_cb(PurpleScheduledTask *task, gpointer data) {
	PurpleScheduler *scheduler = data;
	const char *task_type = NULL;

	task_type = purple_scheduled_task_get_task_type(task);
	g_signal_emit(scheduler,
	              signals[SIG_EXECUTE_TASK],
	              g_quark_from_string(task_type),
	              task,
	              task_type);
}

static void
purple_scheduler_task_notify_state_cb(GObject *obj,
                                      G_GNUC_UNUSED GParamSpec *pspec,
                                      gpointer data)
{
	PurpleScheduledTask *task = PURPLE_SCHEDULED_TASK(obj);
	PurpleScheduledTaskState state = PURPLE_SCHEDULED_TASK_STATE_UNSCHEDULED;
	PurpleScheduler *scheduler = data;

	state = purple_scheduled_task_get_state(task);
	if(state == PURPLE_SCHEDULED_TASK_STATE_EXECUTED) {
		purple_scheduler_remove_task(scheduler,
		                             purple_scheduled_task_get_id(task));
	}
}

/******************************************************************************
 * GListModel Implementation
 *****************************************************************************/
static GType
purple_scheduler_get_item_type(G_GNUC_UNUSED GListModel *model) {
	return PURPLE_TYPE_SCHEDULED_TASK;
}

static guint
purple_scheduler_get_n_items(GListModel *list) {
	PurpleScheduler *scheduler = PURPLE_SCHEDULER(list);

	return scheduler->tasks->len;
}

static gpointer
purple_scheduler_get_item(GListModel *list, guint position) {
	PurpleScheduler *scheduler = PURPLE_SCHEDULER(list);
	PurpleScheduledTask *task = NULL;

	if(position < scheduler->tasks->len) {
		task = g_ptr_array_index(scheduler->tasks, position);
		g_object_ref(task);
	}

	return task;
}

static void
purple_scheduler_list_model_init(GListModelInterface *iface) {
	iface->get_item_type = purple_scheduler_get_item_type;
	iface->get_n_items = purple_scheduler_get_n_items;
	iface->get_item = purple_scheduler_get_item;
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
G_DEFINE_FINAL_TYPE_WITH_CODE(PurpleScheduler, purple_scheduler, G_TYPE_OBJECT,
                              G_IMPLEMENT_INTERFACE(G_TYPE_LIST_MODEL,
                                                    purple_scheduler_list_model_init));

static void
purple_scheduler_finalize(GObject *obj) {
	PurpleScheduler *scheduler = PURPLE_SCHEDULER(obj);

	g_clear_pointer(&scheduler->tasks, g_ptr_array_unref);

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

static void
purple_scheduler_get_property(GObject *obj, guint param_id, GValue *value,
                              GParamSpec *pspec)
{
	GListModel *model = G_LIST_MODEL(obj);

	switch(param_id) {
	case PROP_ITEM_TYPE:
		g_value_set_gtype(value, g_list_model_get_item_type(model));
		break;
	case PROP_N_ITEMS:
		g_value_set_uint(value, g_list_model_get_n_items(model));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
purple_scheduler_init(PurpleScheduler *scheduler) {
	scheduler->tasks = g_ptr_array_new_full(10,
	                                        (GDestroyNotify)purple_scheduler_unref_task);
}

static void
purple_scheduler_class_init(PurpleSchedulerClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);

	obj_class->finalize = purple_scheduler_finalize;
	obj_class->get_property = purple_scheduler_get_property;

	/**
	 * PurpleScheduler:item-type:
	 *
	 * The type of items. See [vfunc@Gio.ListModel.get_item_type].
	 *
	 * Since: 3.0
	 */
	properties[PROP_ITEM_TYPE] = g_param_spec_gtype(
		"item-type", NULL, NULL,
		G_TYPE_OBJECT,
		G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleScheduler:n-items:
	 *
	 * The number of items. See [vfunc@Gio.ListModel.get_n_items].
	 *
	 * Since: 3.0
	 */
	properties[PROP_N_ITEMS] = g_param_spec_uint(
		"n-items", NULL, NULL,
		0, G_MAXUINT, 0,
		G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties(obj_class, N_PROPERTIES, properties);

	/**
	 * PurpleScheduler::execute-task:
	 * @scheduler: the instance
	 * @task: the task being executed
	 * @task_type: the task type
	 *
	 * Emitted when a task is being executed.
	 *
	 * This signal supports details on [property@ScheduledTask:task-type] to
	 * make it easier to listen for specific task types being executed.
	 *
	 * Since: 3.0
	 */
	signals[SIG_EXECUTE_TASK] = g_signal_new_class_handler(
		"execute-task",
		G_OBJECT_CLASS_TYPE(klass),
		G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
		NULL,
		NULL,
		NULL,
		NULL,
		G_TYPE_NONE,
		2,
		PURPLE_TYPE_SCHEDULED_TASK,
		G_TYPE_STRING);
}

/******************************************************************************
 * Private API
 *****************************************************************************/
PurpleScheduler *
purple_scheduler_startup(G_GNUC_UNUSED PurpleCore *core,
                         G_GNUC_UNUSED GError **error)
{
	return purple_scheduler_new();
}

/******************************************************************************
 * Public API
 *****************************************************************************/
gboolean
purple_scheduler_add_task(PurpleScheduler *scheduler,
                          PurpleScheduledTask *task,
                          GDateTime *execute_at,
                          GError **error)
{
	PurpleScheduledTask *existing = NULL;
	GError *local_error = NULL;
	const char *id = NULL;

	g_return_val_if_fail(PURPLE_IS_SCHEDULER(scheduler), FALSE);
	g_return_val_if_fail(PURPLE_IS_SCHEDULED_TASK(task), FALSE);
	g_return_val_if_fail(execute_at != NULL, FALSE);

	id = purple_scheduled_task_get_id(task);
	existing = purple_scheduler_find_task_with_id(scheduler, id, NULL);
	if(PURPLE_IS_SCHEDULED_TASK(existing)) {
		g_set_error(error,
		            PURPLE_SCHEDULER_ERROR,
		            PURPLE_SCHEDULER_ERROR_TASK_EXISTS,
		            "a task with id %s already exists",
		            id);
		return FALSE;
	}

	if(!purple_scheduled_task_schedule(task, execute_at, &local_error)) {
		if(local_error != NULL) {
			g_propagate_error(error, local_error);
		} else {
			g_set_error_literal(error,
			                    PURPLE_SCHEDULER_ERROR,
			                    PURPLE_SCHEDULER_ERROR_FAILED_TO_SCHEDULE,
			                    "the task failed to schedule for an unknown "
			                    "reason");
		}

		return FALSE;
	}

	/* Connect to the execute signal so we can propagate the signal. */
	g_signal_connect_object(task,
	                        "execute",
	                        G_CALLBACK(purple_scheduler_task_execute_cb),
	                        scheduler,
	                        G_CONNECT_DEFAULT);

	/* Add a handler to remove the task after it's been executed. */
	g_signal_connect_object(task,
	                        "notify::state",
	                        G_CALLBACK(purple_scheduler_task_notify_state_cb),
	                        scheduler,
	                        G_CONNECT_DEFAULT);

	/* Finally add the item and emit the items-changed signal. */
	g_ptr_array_add(scheduler->tasks, g_object_ref(task));
	g_list_model_items_changed(G_LIST_MODEL(scheduler),
	                           scheduler->tasks->len - 1, 0, 1);

	return TRUE;
}

gboolean
purple_scheduler_add_task_relative(PurpleScheduler *scheduler,
                                   PurpleScheduledTask *task,
                                   GTimeSpan when,
                                   GError **error)
{
	GDateTime *now = NULL;
	GDateTime *execute_at = NULL;
	gboolean ret = FALSE;

	g_return_val_if_fail(PURPLE_IS_SCHEDULER(scheduler), FALSE);
	g_return_val_if_fail(PURPLE_IS_SCHEDULED_TASK(task), FALSE);

	now = g_date_time_new_now_local();
	execute_at = g_date_time_add(now, when);
	g_date_time_unref(now);

	ret = purple_scheduler_add_task(scheduler, task, execute_at, error);

	g_date_time_unref(execute_at);

	return ret;
}

PurpleScheduler *
purple_scheduler_get_default(void) {
	return purple_core_get_scheduler(purple_core_get_default());
}

GListModel *
purple_scheduler_get_default_as_model(void) {
	PurpleScheduler *scheduler = NULL;

	scheduler = purple_core_get_scheduler(purple_core_get_default());
	if(PURPLE_IS_SCHEDULER(scheduler)) {
		return G_LIST_MODEL(scheduler);
	}

	return NULL;
}

PurpleScheduler *
purple_scheduler_new(void) {
	return g_object_new(PURPLE_TYPE_SCHEDULER, NULL);
}

gboolean
purple_scheduler_remove_task(PurpleScheduler *scheduler, const char *id) {
	PurpleScheduledTask *task = NULL;
	PurpleScheduledTaskState state = PURPLE_SCHEDULED_TASK_STATE_UNSCHEDULED;
	guint position = 0;

	g_return_val_if_fail(PURPLE_IS_SCHEDULER(scheduler), FALSE);
	g_return_val_if_fail(!birb_str_is_empty(id), FALSE);

	task = purple_scheduler_find_task_with_id(scheduler, id, &position);
	if(!PURPLE_IS_SCHEDULED_TASK(task)) {
		return FALSE;
	}

	state = purple_scheduled_task_get_state(task);
	if(state == PURPLE_SCHEDULED_TASK_STATE_SCHEDULED) {
		purple_scheduled_task_cancel(task);
	}

	g_ptr_array_remove_index(scheduler->tasks, position);

	g_list_model_items_changed(G_LIST_MODEL(scheduler), position, 1, 0);

	return TRUE;
}
