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

#if !defined(PURPLE_GLOBAL_HEADER_INSIDE) && !defined(PURPLE_COMPILATION)
# error "only <purple.h> may be included directly"
#endif

#ifndef PURPLE_SCHEDULER_H
#define PURPLE_SCHEDULER_H

#include <glib.h>
#include <glib-object.h>

#include "purplescheduledtask.h"
#include "purpleversion.h"

G_BEGIN_DECLS

#define PURPLE_SCHEDULER_ERROR purple_scheduler_error_quark()

/**
 * PurpleSchedulerError:
 * @PURPLE_SCHEDULER_ERROR_TASK_EXISTS: the task has already been added to the
 *  scheduler
 * @PURPLE_SCHEDULER_ERROR_FAILED_TO_SCHEDULE: the task failed to schedule for
 *  an unknown reason
 *
 * Error codes returned by the scheduler.
 *
 * Since: 3.0
 */
typedef enum {
	PURPLE_SCHEDULER_ERROR_TASK_EXISTS PURPLE_AVAILABLE_ENUMERATOR_IN_3_0,
	PURPLE_SCHEDULER_ERROR_FAILED_TO_SCHEDULE PURPLE_AVAILABLE_ENUMERATOR_IN_3_0,
} PurpleSchedulerError;

/**
 * purple_scheduler_error_quark:
 *
 * The error domain to identify errors with the scheduler.
 *
 * Returns: The error domain.
 *
 * Since: 3.0
 */
PURPLE_AVAILABLE_IN_3_0
GQuark purple_scheduler_error_quark(void);

/**
 * PurpleScheduler:
 *
 * An object that manages a collection of [class@ScheduledTask].
 *
 * Since: 3.0
 */

#define PURPLE_TYPE_SCHEDULER (purple_scheduler_get_type())

PURPLE_AVAILABLE_IN_3_0
G_DECLARE_FINAL_TYPE(PurpleScheduler, purple_scheduler, PURPLE, SCHEDULER,
                     GObject)

/**
 * purple_scheduler_add_task:
 * @task: (transfer none): the task to add
 * @execute_at: (transfer none): the date time for when the task should be
 *              executed.
 * @error: (out) (nullable): a return address for a #GError
 *
 * Adds a new task to the scheduler.
 *
 * If the task has already been added, false will be returned and @error will
 * be set.
 *
 * If @task is already scheduled, it will be rescheduled to run at @execute_at.
 *
 * Returns: true if the task was added successfully.
 *
 * Since: 3.0
 */
PURPLE_AVAILABLE_IN_3_0
gboolean purple_scheduler_add_task(PurpleScheduler *scheduler, PurpleScheduledTask *task, GDateTime *execute_at, GError **error);

/**
 * purple_scheduler_add_task_relative:
 * @task: (transfer none): the task to add
 * @when: the relative time from now to execute the task
 * @error: (out) (nullable): a return address for a #GError
 *
 * Adds a new task to the scheduler.
 *
 * If the task has already been added, false will be returned and @error will
 * be set.
 *
 * If @task is already scheduled, it will be rescheduled to run at @when.
 *
 * This is a wrapper around [method@Scheduler.add_task] that will add @when to
 * the current time for you.
 *
 * Returns: true if the task was added successfully.
 *
 * Since: 3.0
 */
PURPLE_AVAILABLE_IN_3_0
gboolean purple_scheduler_add_task_relative(PurpleScheduler *scheduler, PurpleScheduledTask *task, GTimeSpan when, GError **error);

/**
 * purple_scheduler_get_default:
 *
 * Gets the default scheduler.
 *
 * This can return %NULL if libpurple has not been initialized.
 *
 * Returns: (transfer none) (nullable): The default scheduler.
 *
 * Since: 3.0
 */
PURPLE_AVAILABLE_IN_3_0
PurpleScheduler *purple_scheduler_get_default(void);

/**
 * purple_scheduler_get_default_as_model:
 *
 * Gets the default scheduler cast to a [iface@Gio.ListModel].
 *
 * This can return %NULL if libpurple has not been initialized.
 *
 * Returns: (transfer none) (nullable): The default scheduler as a list model.
 *
 * Since: 3.0
 */
PURPLE_AVAILABLE_IN_3_0
GListModel *purple_scheduler_get_default_as_model(void);

/**
 * purple_scheduler_new:
 *
 * Creates a new scheduler.
 *
 * This is typically only used internally but may be useful to others.
 *
 * Returns: (transfer full): The new instance.
 *
 * Since: 3.0
 */
PURPLE_AVAILABLE_IN_3_0
PurpleScheduler *purple_scheduler_new(void);

/**
 * purple_scheduler_remove_task:
 * @id: the id of the task
 *
 * Removes a task from the scheduler.
 *
 * If the task is found and currently scheduled, it will be cancelled.
 *
 * Returns: true if the task was found and removed.
 *
 * Since: 3.0
 */
PURPLE_AVAILABLE_IN_3_0
gboolean purple_scheduler_remove_task(PurpleScheduler *scheduler, const char *id);

G_END_DECLS

#endif /* PURPLE_SCHEDULER_H */
