/*
 * 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 "purpleschedulerbackend.h"

G_DEFINE_QUARK(purple-scheduler-backend-error, purple_scheduler_backend_error)

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
G_DEFINE_ABSTRACT_TYPE(PurpleSchedulerBackend, purple_scheduler_backend,
                       G_TYPE_OBJECT)

static void
purple_scheduler_backend_init(G_GNUC_UNUSED PurpleSchedulerBackend *backend) {
}

static void
purple_scheduler_backend_class_init(G_GNUC_UNUSED PurpleSchedulerBackendClass *klass)
{
}

/******************************************************************************
 * Public API
 *****************************************************************************/
void
purple_scheduler_backend_delete_task_async(PurpleSchedulerBackend *backend,
                                           PurpleScheduledTask *task,
                                           GCancellable *cancellable,
                                           GAsyncReadyCallback callback,
                                           gpointer data)
{
	PurpleSchedulerBackendClass *klass = NULL;

	g_return_if_fail(PURPLE_IS_SCHEDULER_BACKEND(backend));
	g_return_if_fail(PURPLE_IS_SCHEDULED_TASK(task));

	klass = PURPLE_SCHEDULER_BACKEND_GET_CLASS(backend);
	if(klass != NULL && klass->delete_task_async != NULL) {
		klass->delete_task_async(backend, task, cancellable, callback, data);
	} else {
		g_task_report_new_error(G_OBJECT(backend), callback, data,
		                        purple_scheduler_backend_delete_task_async,
		                        PURPLE_SCHEDULER_BACKEND_ERROR,
		                        PURPLE_SCHEDULER_BACKEND_ERROR_VFUNC_NOT_IMPLEMENTED,
		                        "%s does not implement delete_task_async",
		                        klass != NULL ? G_OBJECT_TYPE_NAME(backend) : "Unknown");
	}
}

gboolean
purple_scheduler_backend_delete_task_finish(PurpleSchedulerBackend *backend,
                                            GAsyncResult *result,
                                            GError **error)
{
	PurpleSchedulerBackendClass *klass = NULL;

	g_return_val_if_fail(PURPLE_IS_SCHEDULER_BACKEND(backend), FALSE);
	g_return_val_if_fail(g_task_is_valid(result, backend), FALSE);

	if(g_async_result_is_tagged(result,
	                            purple_scheduler_backend_delete_task_async))
	{
		return g_task_propagate_boolean(G_TASK(result), error);
	}

	klass = PURPLE_SCHEDULER_BACKEND_GET_CLASS(backend);
	if(klass != NULL && klass->delete_task_finish != NULL) {
		return klass->delete_task_finish(backend, result, error);
	}

	g_set_error(error,
	            PURPLE_SCHEDULER_BACKEND_ERROR,
	            PURPLE_SCHEDULER_BACKEND_ERROR_VFUNC_NOT_IMPLEMENTED,
	            "%s does not implement delete_task_finish",
	            klass != NULL ? G_OBJECT_TYPE_NAME(backend) : "Unknown");


	return FALSE;
}

void
purple_scheduler_backend_load_tasks_async(PurpleSchedulerBackend *backend,
                                          GCancellable *cancellable,
                                          GAsyncReadyCallback callback,
                                          gpointer data)
{
	PurpleSchedulerBackendClass *klass = NULL;

	g_return_if_fail(PURPLE_IS_SCHEDULER_BACKEND(backend));

	klass = PURPLE_SCHEDULER_BACKEND_GET_CLASS(backend);
	if(klass != NULL && klass->load_tasks_async != NULL) {
		klass->load_tasks_async(backend, cancellable, callback, data);
	} else {
		g_task_report_new_error(G_OBJECT(backend), callback, data,
		                        purple_scheduler_backend_load_tasks_async,
		                        PURPLE_SCHEDULER_BACKEND_ERROR,
		                        PURPLE_SCHEDULER_BACKEND_ERROR_VFUNC_NOT_IMPLEMENTED,
		                        "%s does not implement load_tasks_async",
		                        klass != NULL ? G_OBJECT_TYPE_NAME(backend) : "Unknown");

	}
}

GListModel *
purple_scheduler_backend_load_tasks_finish(PurpleSchedulerBackend *backend,
                                           GAsyncResult *result,
                                           GError **error)
{
	PurpleSchedulerBackendClass *klass = NULL;

	g_return_val_if_fail(PURPLE_IS_SCHEDULER_BACKEND(backend), NULL);
	g_return_val_if_fail(g_task_is_valid(result, backend), NULL);

	if(g_async_result_is_tagged(result,
	                            purple_scheduler_backend_load_tasks_async))
	{
		return g_task_propagate_pointer(G_TASK(result), error);
	}

	klass = PURPLE_SCHEDULER_BACKEND_GET_CLASS(backend);
	if(klass != NULL && klass->load_tasks_finish != NULL) {
		return klass->load_tasks_finish(backend, result, error);
	}

	g_set_error(error,
	            PURPLE_SCHEDULER_BACKEND_ERROR,
	            PURPLE_SCHEDULER_BACKEND_ERROR_VFUNC_NOT_IMPLEMENTED,
	            "%s does not implement load_tasks_finish",
	            klass != NULL ? G_OBJECT_TYPE_NAME(backend) : "Unknown");

	return NULL;
}

void
purple_scheduler_backend_save_task_async(PurpleSchedulerBackend *backend,
                                         PurpleScheduledTask *task,
                                         GCancellable *cancellable,
                                         GAsyncReadyCallback callback,
                                         gpointer data)
{
	PurpleSchedulerBackendClass *klass = NULL;

	g_return_if_fail(PURPLE_IS_SCHEDULER_BACKEND(backend));
	g_return_if_fail(PURPLE_IS_SCHEDULED_TASK(task));

	klass = PURPLE_SCHEDULER_BACKEND_GET_CLASS(backend);
	if(klass != NULL && klass->save_task_async != NULL) {
		klass->save_task_async(backend, task, cancellable, callback, data);
	} else {
		g_task_report_new_error(G_OBJECT(backend), callback, data,
		                        purple_scheduler_backend_save_task_async,
		                        PURPLE_SCHEDULER_BACKEND_ERROR,
		                        PURPLE_SCHEDULER_BACKEND_ERROR_VFUNC_NOT_IMPLEMENTED,
		                        "%s does not implement save_task_async",
		                        klass != NULL ? G_OBJECT_TYPE_NAME(backend) : "Unknown");

	}
}

gboolean
purple_scheduler_backend_save_task_finish(PurpleSchedulerBackend *backend,
                                          GAsyncResult *result,
                                          GError **error)
{
	PurpleSchedulerBackendClass *klass = NULL;

	g_return_val_if_fail(PURPLE_IS_SCHEDULER_BACKEND(backend), FALSE);
	g_return_val_if_fail(g_task_is_valid(result, backend), FALSE);

	if(g_async_result_is_tagged(result,
	                            purple_scheduler_backend_save_task_async))
	{
		return g_task_propagate_boolean(G_TASK(result), error);
	}

	klass = PURPLE_SCHEDULER_BACKEND_GET_CLASS(backend);
	if(klass != NULL && klass->save_task_finish != NULL) {
		return klass->save_task_finish(backend, result, error);
	}

	g_set_error(error,
	            PURPLE_SCHEDULER_BACKEND_ERROR,
	            PURPLE_SCHEDULER_BACKEND_ERROR_VFUNC_NOT_IMPLEMENTED,
	            "%s does not implement save_task_finish",
	            klass != NULL ? G_OBJECT_TYPE_NAME(backend) : "Unknown");

	return FALSE;
}
