/*
 * 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_audio.h"
#include "config.h"

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

	if (!data)
		return;

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

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

	g_free(volume_string);
	g_free(filename);
}

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

	if (!data)
		return;

	volume_string = g_strdup_printf("%f", volume);

	roccat_notification_audio_timer_cancel(data);
	retval = ca_context_play((ca_context *)data, ROCCAT_NOTIFICATION_TIMER_ID,
			CA_PROP_CANBERRA_VOLUME, volume_string,
			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));

	g_free(volume_string);
}

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, gdouble volume, gchar const *name, guint seconds) {
	gint retval;
	gchar *volume_string;

	if (!data)
		return;

	volume_string = g_strdup_printf("%f", volume);

	roccat_notification_audio_timer_cancel(data);
	retval = ca_context_play((ca_context *)data, ROCCAT_NOTIFICATION_TIMER_ID,
			CA_PROP_CANBERRA_VOLUME, volume_string,
			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));

	g_free(volume_string);
}

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

void roccat_notification_audio_timer_free(gpointer data) {}
