/*
 * 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_live_recording.h"
#include "roccat_osd_note.h"
#include "config.h"
#include <libnotify/notification.h>

struct _RoccatNotificationLiveRecording {
	RoccatNotificator *notificator;
	gpointer audio;
	gpointer osd;
	gchar *device_name;
	RoccatNotificationType type;
};

static gpointer roccat_notification_osd_live_recording_new(RoccatNotificator *notificator) {
	return notificator->osd_usable ? roccat_osd_note_new(notificator, NOTIFY_EXPIRES_DEFAULT) : NULL;
}

static void roccat_notification_osd_live_recording_free(gpointer data) {
	if (data)
		roccat_osd_note_free((RoccatOsdNote *)data);
}

static void roccat_notification_osd_live_recording_invoke(gpointer data, gchar const *format, ...) {
	va_list args;

	if (!data)
		return;

	va_start(args, format);
	roccat_osd_note_update_v((RoccatOsdNote *)data, format, args);
	va_end(args);
}

static void roccat_notification_osd_live_recording_cancel(gpointer data) {
	if (data)
		roccat_osd_note_close((RoccatOsdNote *)data);
}

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

static void roccat_notification_audio_live_recording_free(gpointer data) {}

static void roccat_notification_audio_live_recording_cancel(gpointer data) {
	gint retval;

	if (!data)
		return;

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

static void roccat_notification_audio_live_recording_invoke(gpointer data, gdouble volume, gchar const *filename) {
	gint retval;
	gchar *volume_string;

	if (!data)
		return;

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

	roccat_notification_audio_live_recording_cancel(data);
	retval = ca_context_play((ca_context *)data, ROCCAT_NOTIFICATION_LIVE_RECORDING_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);
}

static void roccat_notification_live_recording_invoke(RoccatNotificationLiveRecording *notification,
		RoccatNotificationType type, gdouble volume, gchar const *osd_string, gchar const *audio_filename) {
	notification->type = type;
	switch(type) {
	case ROCCAT_NOTIFICATION_TYPE_OFF:
		break;
	case ROCCAT_NOTIFICATION_TYPE_OSD:
		roccat_notification_osd_live_recording_invoke(notification->osd, "%s %s", notification->device_name, osd_string);
		break;
	case ROCCAT_NOTIFICATION_TYPE_AUDIO:
		roccat_notification_audio_live_recording_invoke(notification->audio, volume, audio_filename);
		break;
	default:
		g_error("Roccat notification: unknown type %i", notification->type);
	}
}

static void roccat_notification_live_recording_cancel(RoccatNotificationLiveRecording *notification) {
	switch(notification->type) {
	case ROCCAT_NOTIFICATION_TYPE_OFF:
		break;
	case ROCCAT_NOTIFICATION_TYPE_OSD:
		roccat_notification_osd_live_recording_cancel(notification->osd);
		break;
	case ROCCAT_NOTIFICATION_TYPE_AUDIO:
		roccat_notification_audio_live_recording_cancel(notification->audio);
		break;
	default:
		g_error("Roccat notification: unknown type %i", notification->type);
	}
}

RoccatNotificationLiveRecording *roccat_notification_live_recording_new(RoccatNotificator *notificator, gchar const *device_name) {
	RoccatNotificationLiveRecording *notification;
	notification = g_malloc0(sizeof(RoccatNotificationLiveRecording));
	notification->notificator = notificator;
	notification->osd = roccat_notification_osd_live_recording_new(notificator);
	notification->audio = roccat_notification_audio_live_recording_new(notificator);
	notification->device_name = g_strdup(device_name);

	return notification;
}

void roccat_notification_live_recording_free(RoccatNotificationLiveRecording *notification) {
	roccat_notification_live_recording_cancel(notification);
	roccat_notification_osd_live_recording_free(notification->osd);
	roccat_notification_audio_live_recording_free(notification->audio);
	g_free(notification->device_name);
	g_free(notification);
}

void roccat_notification_live_recording_select_macro_key(RoccatNotificationLiveRecording *notification, RoccatNotificationType type, gdouble volume) {
	roccat_notification_live_recording_invoke(notification, type, volume,
			"select macro key",
			INSTALL_PREFIX "/share/roccat/sounds/live_recording/select-macro-key.oga");
}

void roccat_notification_live_recording_record_macro(RoccatNotificationLiveRecording *notification, RoccatNotificationType type, gdouble volume) {
	roccat_notification_live_recording_invoke(notification, type, volume,
			"record macro and press record key again",
			INSTALL_PREFIX "/share/roccat/sounds/live_recording/record-macro.oga");
}

void roccat_notification_live_recording_end_success(RoccatNotificationLiveRecording *notification, RoccatNotificationType type, gdouble volume) {
	roccat_notification_live_recording_invoke(notification, type, volume,
			"macro successfully saved",
			INSTALL_PREFIX "/share/roccat/sounds/live_recording/end-success.oga");
}

void roccat_notification_live_recording_end_abort(RoccatNotificationLiveRecording *notification, RoccatNotificationType type, gdouble volume) {
	roccat_notification_live_recording_invoke(notification, type, volume,
			"macro recording aborted",
			INSTALL_PREFIX "/share/roccat/sounds/live_recording/end-abort.oga");
}

void roccat_notification_live_recording_invalid_key(RoccatNotificationLiveRecording *notification, RoccatNotificationType type, gdouble volume) {
	roccat_notification_live_recording_invoke(notification, type, volume,
			"invalid macro key",
			INSTALL_PREFIX "/share/roccat/sounds/live_recording/invalid-key.oga");
}
