/*
 * This file is part of roccat-tools.
 *
 * roccat-tools 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.
 *
 * roccat-tools 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 roccat-tools. If not, see <http://www.gnu.org/licenses/>.
 */

#include "roccat_notification_timer.h"
#include "config.h"

void roccat_notification_audio_timer_tick(gpointer data, gchar const *name, guint seconds) {
	gint retval;
	gchar *filename;

	if (!data)
		return;

	filename = g_strdup_printf(INSTALL_PREFIX "/share/roccat/sounds/timer/timer-%i.oga", seconds);

	roccat_notification_audio_timer_cancel(data);
	retval = ca_context_play((ca_context *)data, ROCCAT_NOTIFICATION_TIMER_ID,
			CA_PROP_MEDIA_FILENAME, filename,
			NULL);
	if (retval)
		g_warning("Roccat audio notification: %s", ca_strerror(retval));

	g_free(filename);
}

void roccat_notification_audio_timer_stop(gpointer data, gchar const *name, guint seconds) {
	gint retval;

	if (!data)
		return;

	roccat_notification_audio_timer_cancel(data);
	retval = ca_context_play((ca_context *)data, ROCCAT_NOTIFICATION_TIMER_ID,
			CA_PROP_MEDIA_FILENAME, INSTALL_PREFIX "/share/roccat/sounds/timer/timer-stopped.oga",
			NULL);
	if (retval)
		g_warning("Roccat audio notification: %s", ca_strerror(retval));
}

void roccat_notification_audio_timer_cancel(void *data) {
	gint retval;

	if (!data)
		return;

	retval = ca_context_cancel((ca_context *)data, ROCCAT_NOTIFICATION_TIMER_ID);
	if (retval != 0 && retval != CA_ERROR_STATE)
		g_warning("Roccat audio notification: %s", ca_strerror(retval));
}

void roccat_notification_audio_timer_start(gpointer data, gchar const *name, guint seconds) {
	gint retval;

	if (!data)
		return;

	roccat_notification_audio_timer_cancel(data);
	retval = ca_context_play((ca_context *)data, ROCCAT_NOTIFICATION_TIMER_ID,
			CA_PROP_MEDIA_FILENAME, INSTALL_PREFIX "/share/roccat/sounds/timer/timer-activated.oga",
			NULL);
	if (retval)
		g_warning("Roccat audio notification: %s", ca_strerror(retval));
}

gpointer roccat_notification_audio_timer_new(RoccatNotificator *notificator) {
	return notificator->audio_usable ? notificator->context : NULL;
}

void roccat_notification_audio_timer_free(gpointer data) {}
