/*
 * 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 "isku_eventhandler.h"
#include "isku_eventhandler_channel.h"
#include "isku_dbus_server.h"
#include "roccat/isku.h"
#include "config.h"
#include "hid_uid.h"
#include <glib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#define ISKU_EVENTHANDLER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), ISKU_EVENTHANDLER_TYPE, IskuEventhandlerClass))
#define IS_ISKU_EVENTHANDLER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), ISKU_EVENTHANDLER_TYPE))
#define ISKU_EVENTHANDLER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), ISKU_EVENTHANDLER_TYPE, IskuEventhandlerPrivate))

typedef struct _IskuEventhandlerClass IskuEventhandlerClass;

struct _IskuEventhandlerClass {
	GObjectClass parent_class;
};

struct _IskuEventhandlerPrivate {
	RoccatEventhandlerHost *host;
	IskuDBusServer *dbus_server;
	RoccatDeviceScanner *device_scanner;
	RoccatDevice *device;
	gboolean device_set_up;
	RoccatKeyFile *config;
	guint actual_profile_index;
	IskuRkp *rkp[ISKU_PROFILE_NUM];
	IskuEventhandlerChannel *channel;
	
	RoccatNotificationProfile *profile_note;
	RoccatNotificationTimer *timer_note;
	RoccatNotificationLiveRecording *live_recording_note;
};

enum {
	PROP_0,
	PROP_HOST,
};

static void profile_changed(IskuEventhandler *eventhandler, guint profile_number) {
	IskuEventhandlerPrivate *priv = eventhandler->priv;
	guint profile_index = profile_number - 1;

	priv->actual_profile_index = profile_index;

	roccat_notification_profile_update(priv->profile_note, isku_configuration_get_profile_notification_type(priv->config),
			profile_number, isku_rkp_get_profile_name(priv->rkp[profile_index]));

	isku_dbus_server_emit_profile_changed(priv->dbus_server, profile_number);
}

static gboolean set_profile(IskuEventhandler *eventhandler, guint profile_number) {
	IskuEventhandlerPrivate *priv = eventhandler->priv;
	guint profile_index = profile_number - 1;
	GError *local_error = NULL;

	if (priv->actual_profile_index != profile_index) {
		if (!isku_actual_profile_write(priv->device, profile_index, &local_error)) {
			// FIXME error message
			g_clear_error(&local_error);
			return FALSE;
		}
		profile_changed(eventhandler, profile_number);
	}
	return TRUE;
}

static void window_changed_cb(RoccatEventhandlerHost *host, gchar const *title, gpointer user_data) {
	IskuEventhandler *eventhandler = ISKU_EVENTHANDLER(user_data);
	IskuEventhandlerPrivate *priv = eventhandler->priv;
	guint profile_index;
	guint game_file_index;
	gchar *pattern;

	for (profile_index = 0; profile_index < ISKU_PROFILE_NUM; ++profile_index) {
		for (game_file_index = 0; game_file_index < ISKU_GAMEFILE_NUM; ++game_file_index) {
			pattern = isku_rkp_get_game_file_name(priv->rkp[profile_index], game_file_index);
			if (strcmp(pattern, "") == 0) {
				g_free(pattern);
				continue;
			}

			if (g_regex_match_simple(pattern, title, 0, 0)) {
				set_profile(eventhandler, profile_index + 1);
				g_free(pattern);
				return;
			}
			g_free(pattern);
		}
	}

	set_profile(eventhandler, isku_configuration_get_default_profile_number(priv->config));
	return;
}

static gboolean talk_easyshift(IskuEventhandler *eventhandler, gboolean state) {
	IskuEventhandlerPrivate *priv = eventhandler->priv;
	GError *error = NULL;
	gboolean retval = FALSE;

	if (priv->device_set_up) {
		retval = isku_talk_easyshift(priv->device,
				(state != FALSE) ? ISKU_TALK_EASYSHIFT_ON : ISKU_TALK_EASYSHIFT_OFF,
				&error);
		if (error) {
			g_warning("Error issuing easyshift: %s", error->message);
			g_clear_error(&error);
		}
	}

	return retval;
}

static gboolean talk_easyshift_lock(IskuEventhandler *eventhandler, gboolean state) {
	IskuEventhandlerPrivate *priv = eventhandler->priv;
	GError *error = NULL;
	gboolean retval = FALSE;

	if (priv->device_set_up) {
		retval = isku_talk_easyshift_lock(priv->device,
				(state != FALSE) ? ISKU_TALK_EASYSHIFT_ON : ISKU_TALK_EASYSHIFT_OFF,
				&error);
		if (error) {
			g_warning("Error issuing easyshift_lock: %s", error->message);
			g_clear_error(&error);
		}
	}

	return retval;
}

static guint isku_eventhandler_get_talk_device(RoccatEventhandlerPlugin *self) {
	IskuEventhandlerPrivate *priv = ISKU_EVENTHANDLER(self)->priv;

	if (!priv->device_set_up)
		return 0;

	return roccat_device_get_product_id(priv->device);
}

static void isku_eventhandler_talk_easyshift(RoccatEventhandlerPlugin *self, gboolean state) {
	IskuEventhandler *eventhandler = ISKU_EVENTHANDLER(self);
	talk_easyshift(eventhandler, state);
}

static void isku_eventhandler_talk_easyshift_lock(RoccatEventhandlerPlugin *self, gboolean state) {
	IskuEventhandler *eventhandler = ISKU_EVENTHANDLER(self);
	talk_easyshift_lock(eventhandler, state);
}

static void isku_roccat_eventhandler_interface_init(RoccatEventhandlerPluginInterface *iface) {
	iface->get_talk_device = isku_eventhandler_get_talk_device;
	iface->talk_easyshift = isku_eventhandler_talk_easyshift;
	iface->talk_easyshift_lock = isku_eventhandler_talk_easyshift_lock;
	iface->talk_easyaim = NULL;
	iface->set_led_rgb = NULL;
	iface->restore_led_rgb = NULL;
}

G_DEFINE_TYPE_WITH_CODE(IskuEventhandler, isku_eventhandler, G_TYPE_OBJECT,
		G_IMPLEMENT_INTERFACE(ROCCAT_EVENTHANDLER_PLUGIN_TYPE, isku_roccat_eventhandler_interface_init));

GObject *isku_eventhandler_new(RoccatEventhandlerHost *host) {
	return g_object_new(ISKU_EVENTHANDLER_TYPE,
			"host", host,
			NULL);
}

static void talk_easyshift_cb(IskuDBusServer *server, guchar state, gpointer user_data) {
	IskuEventhandler *eventhandler = ISKU_EVENTHANDLER(user_data);
	talk_easyshift(eventhandler, state);
}

static void talk_easyshift_lock_cb(IskuDBusServer *server, guchar state, gpointer user_data) {
	IskuEventhandler *eventhandler = ISKU_EVENTHANDLER(user_data);
	talk_easyshift_lock(eventhandler, state);
}

static void open_gui_cb(gpointer source, gpointer user_data) {
	gchar *path;
	path = g_build_path("/", INSTALL_PREFIX, "bin", "iskuconfig", (gchar const *)NULL);
	roccat_double_fork(path);
	g_free(path);
}

static void profile_data_changed_outside_cb(IskuDBusServer *server, guchar number, gpointer user_data) {
	IskuEventhandler *eventhandler = ISKU_EVENTHANDLER(user_data);
	IskuEventhandlerPrivate *priv = eventhandler->priv;
	guint profile_index = number - 1;
	
	isku_rkp_free(priv->rkp[profile_index]);
	priv->rkp[profile_index] = isku_rkp_load_actual(profile_index);
}

static void configuration_reload(IskuEventhandler *eventhandler) {
	IskuEventhandlerPrivate *priv = eventhandler->priv;
	if (priv->config)
		isku_configuration_free(priv->config);
	priv->config = isku_configuration_load();
}

static void configuration_changed_outside_cb(IskuDBusServer *server, gpointer user_data) {
	IskuEventhandler *eventhandler = ISKU_EVENTHANDLER(user_data);
	configuration_reload(eventhandler);
}

static void profile_changed_cb(gpointer source, guchar profile_number, gpointer user_data) {
	IskuEventhandler *eventhandler = ISKU_EVENTHANDLER(user_data);
	profile_changed(eventhandler, profile_number);
}

static void quicklaunch_cb(IskuEventhandlerChannel *channel, guchar key_index, gpointer user_data) {
	IskuEventhandler *eventhandler = ISKU_EVENTHANDLER(user_data);
	IskuEventhandlerPrivate *priv = eventhandler->priv;
	IskuRkpMacroKeyInfo *macro_key_info;

	macro_key_info = isku_rkp_get_macro_key_info(priv->rkp[priv->actual_profile_index], key_index);
	isku_play_quicklaunch_forked(macro_key_info);
	isku_rkp_macro_key_info_free(macro_key_info);
}

static guint key_type_to_key_index(guint key_type) {
	if (key_type >= ISKU_KEY_TYPE_M1 && key_type <= ISKU_KEY_TYPE_M5)
		return key_type - ISKU_KEY_TYPE_M1 + ISKU_KEY_INDEX_M1;
	if (key_type >= ISKU_KEY_TYPE_T1 && key_type <= ISKU_KEY_TYPE_T3)
		return key_type - ISKU_KEY_TYPE_T1 + ISKU_KEY_INDEX_T1;
	if (key_type == HID_UID_KB_CAPSLOCK)
		return ISKU_KEY_INDEX_CAPSLOCK;
	g_warning("unsupported key_type 0x%x", key_type);
	return key_type;
}

static void talk_cb(IskuEventhandlerChannel *channel, guchar key_type, guchar event, gpointer user_data) {
	IskuEventhandler *eventhandler = ISKU_EVENTHANDLER(user_data);
	IskuEventhandlerPrivate *priv = eventhandler->priv;
	IskuRkpMacroKeyInfo *macro_key_info;
	static gboolean state = FALSE;

	macro_key_info = isku_rkp_get_macro_key_info(priv->rkp[priv->actual_profile_index], key_type_to_key_index(key_type));
	switch (macro_key_info->type) {
	case ISKU_KEY_TYPE_EASYSHIFT:
		break;
	case ISKU_KEY_TYPE_TALK_BOTH_EASYSHIFT:
		roccat_eventhandler_plugin_emit_talk_easyshift(ROCCAT_EVENTHANDLER_PLUGIN(eventhandler),
				ROCCAT_TALK_DEVICE_MOUSE,
				(event == ISKU_REPORT_BUTTON_EVENT_ACTION_PRESS) ? TRUE : FALSE);
		break;
	case ISKU_KEY_TYPE_TALK_OTHER_EASYSHIFT:
		roccat_eventhandler_plugin_emit_talk_easyshift(ROCCAT_EVENTHANDLER_PLUGIN(eventhandler),
				isku_rkp_macro_key_info_get_talk_device(macro_key_info),
				(event == ISKU_REPORT_BUTTON_EVENT_ACTION_PRESS) ? TRUE : FALSE);
		break;
	case ISKU_KEY_TYPE_TALK_OTHER_EASYSHIFT_LOCK:
		if (event == ISKU_REPORT_BUTTON_EVENT_ACTION_PRESS) {
			state = !state;
			roccat_eventhandler_plugin_emit_talk_easyshift_lock(ROCCAT_EVENTHANDLER_PLUGIN(eventhandler),
					isku_rkp_macro_key_info_get_talk_device(macro_key_info), state);
		}
		break;
	case ISKU_KEY_TYPE_TALK_OTHER_EASYAIM_1:
		roccat_eventhandler_plugin_emit_talk_easyaim(ROCCAT_EVENTHANDLER_PLUGIN(eventhandler),
				isku_rkp_macro_key_info_get_talk_device(macro_key_info),
				(event == ISKU_REPORT_BUTTON_EVENT_ACTION_PRESS) ? ROCCAT_TALK_EASYAIM_1 : ROCCAT_TALK_EASYAIM_OFF);
		break;
	case ISKU_KEY_TYPE_TALK_OTHER_EASYAIM_2:
		roccat_eventhandler_plugin_emit_talk_easyaim(ROCCAT_EVENTHANDLER_PLUGIN(eventhandler),
				isku_rkp_macro_key_info_get_talk_device(macro_key_info),
				(event == ISKU_REPORT_BUTTON_EVENT_ACTION_PRESS) ? ROCCAT_TALK_EASYAIM_2 : ROCCAT_TALK_EASYAIM_OFF);
		break;
	case ISKU_KEY_TYPE_TALK_OTHER_EASYAIM_3:
		roccat_eventhandler_plugin_emit_talk_easyaim(ROCCAT_EVENTHANDLER_PLUGIN(eventhandler),
				isku_rkp_macro_key_info_get_talk_device(macro_key_info),
				(event == ISKU_REPORT_BUTTON_EVENT_ACTION_PRESS) ? ROCCAT_TALK_EASYAIM_3 : ROCCAT_TALK_EASYAIM_OFF);
		break;
	case ISKU_KEY_TYPE_TALK_OTHER_EASYAIM_4:
		roccat_eventhandler_plugin_emit_talk_easyaim(ROCCAT_EVENTHANDLER_PLUGIN(eventhandler),
				isku_rkp_macro_key_info_get_talk_device(macro_key_info),
				(event == ISKU_REPORT_BUTTON_EVENT_ACTION_PRESS) ? ROCCAT_TALK_EASYAIM_4 : ROCCAT_TALK_EASYAIM_OFF);
		break;
	case ISKU_KEY_TYPE_TALK_OTHER_EASYAIM_5:
		roccat_eventhandler_plugin_emit_talk_easyaim(ROCCAT_EVENTHANDLER_PLUGIN(eventhandler),
				isku_rkp_macro_key_info_get_talk_device(macro_key_info),
				(event == ISKU_REPORT_BUTTON_EVENT_ACTION_PRESS) ? ROCCAT_TALK_EASYAIM_5 : ROCCAT_TALK_EASYAIM_OFF);
		break;
	default:
		g_debug("unknown talk event of type 0x%2x", macro_key_info->type);
	}
	isku_rkp_macro_key_info_free(macro_key_info);
}

static void light_changed_cb(IskuEventhandlerChannel *channel, guchar brightness_number, gpointer user_data) {
	IskuEventhandler *eventhandler = ISKU_EVENTHANDLER(user_data);
	IskuEventhandlerPrivate *priv = eventhandler->priv;
	guint profile_index;
	GError *error = NULL;

	profile_index = priv->actual_profile_index;

	isku_last_set_write(priv->device, &error);
	if (error) {
		g_clear_error(&error);
		return;
	}

	isku_rkp_set_light_brightness(priv->rkp[profile_index], brightness_number - 1);
	isku_rkp_save_actual(priv->rkp[profile_index], profile_index, &error);
	if (error) {
		g_clear_error(&error);
		return;
	}
	isku_rkp_set_unmodified(priv->rkp[profile_index]);

	isku_dbus_server_emit_brightness_changed(priv->dbus_server, profile_index + 1, brightness_number);
}

static gboolean recording_event_store_roccat_macro(IskuRkpMacroKeyInfo const *key_info, GError **error) {
	RoccatMacros *roccat_macros;
	RoccatMacro *roccat_macro;
	gboolean retval;

	roccat_macro = isku_rkp_macro_key_info_to_roccat_macro(key_info);

	/*
	 * Using device specific macroset name instead of default "MLR"
	 * because macro file is shared between devices.
	 * Macroset name stored in device is still "MLR" to reduce writes.
	 */
	roccat_macro_set_macro_set_name(roccat_macro, ISKU_DEVICE_NAME " MLR");

	roccat_macros = roccat_macros_load();
	roccat_macros_set(roccat_macros, roccat_macro);
	retval = roccat_macros_save(roccat_macros, error);
	roccat_macros_free(roccat_macros);
	roccat_macro_free(roccat_macro);

	return retval;
}

static IskuRkpMacroKeyInfo *recording_event_get_data(RoccatDevice *device, guint profile_index, guint key_index, GError **error) {
	IskuMacro *macro;
	IskuRkpMacroKeyInfo *key_info;

	macro = isku_macro_read(device, profile_index, key_index, error);
	if (!macro)
		return NULL;

	key_info = isku_macro_to_rkp_macro_key_info(macro);
	g_free(macro);

	return key_info;
}

static gboolean handle_recording_event(IskuEventhandler *eventhandler, guint profile_index, guint key_index) {
	IskuEventhandlerPrivate *priv = eventhandler->priv;
	IskuRkpMacroKeyInfo *key_info;
	GError *error = NULL;

	key_info = recording_event_get_data(priv->device, profile_index, key_index, &error);
	if (error) {
		g_clear_error(&error);
		return FALSE;
	}

	isku_last_set_write(priv->device, &error);
	if (error) {
		g_clear_error(&error);
		g_free(key_info);
		return FALSE;
	}

	isku_rkp_set_macro_key_info(priv->rkp[profile_index], key_index, key_info);
	isku_rkp_save_actual(priv->rkp[profile_index], profile_index, &error);
	if (error) {
		g_clear_error(&error);
		g_free(key_info);
		return FALSE;
	}
	isku_rkp_set_unmodified(priv->rkp[profile_index]);

	recording_event_store_roccat_macro(key_info, &error);
	if (error) {
		g_clear_error(&error);
		g_free(key_info);
		return FALSE;
	}

	/* rkp has to be stored before this */
	isku_dbus_server_emit_macro_changed(priv->dbus_server, profile_index + 1, key_index + 1, key_info);

	g_free(key_info);
	return TRUE;
}

static void live_recording_cb(IskuEventhandlerChannel *channel, guchar key_index, guchar event, gpointer user_data) {
	IskuEventhandler *eventhandler = ISKU_EVENTHANDLER(user_data);
	IskuEventhandlerPrivate *priv = eventhandler->priv;

	switch(event) {
	case ISKU_REPORT_BUTTON_LIVE_RECORDING_START:
		roccat_notification_live_recording_select_macro_key(priv->live_recording_note,
				isku_configuration_get_live_recording_notification_type(priv->config));
		break;
	case ISKU_REPORT_BUTTON_LIVE_RECORDING_MACRO_KEY_SELECTED:
		roccat_notification_live_recording_record_macro(priv->live_recording_note,
				isku_configuration_get_live_recording_notification_type(priv->config));
		break;
	case ISKU_REPORT_BUTTON_LIVE_RECORDING_END_SUCCESS:
		roccat_notification_live_recording_end_success(priv->live_recording_note,
				isku_configuration_get_live_recording_notification_type(priv->config));
		handle_recording_event(eventhandler, priv->actual_profile_index, key_index);
		break;
	case ISKU_REPORT_BUTTON_LIVE_RECORDING_END_ABORT:
		roccat_notification_live_recording_end_abort(priv->live_recording_note,
				isku_configuration_get_live_recording_notification_type(priv->config));
		break;
	case ISKU_REPORT_BUTTON_LIVE_RECORDING_INVALID_KEY:
		roccat_notification_live_recording_invalid_key(priv->live_recording_note,
				isku_configuration_get_live_recording_notification_type(priv->config));
		break;
	default:
		g_debug("unknown live recording event of type 0x%2x", event);
	}
}

static void timer_start_cb(IskuEventhandlerChannel *channel, guchar key_index, gpointer user_data) {
	IskuEventhandler *eventhandler = ISKU_EVENTHANDLER(user_data);
	IskuEventhandlerPrivate *priv = eventhandler->priv;
	IskuRkpMacroKeyInfo *macro_key_info;

	macro_key_info = isku_rkp_get_macro_key_info(priv->rkp[priv->actual_profile_index], key_index);
	roccat_notification_timer_start(priv->timer_note,
			isku_configuration_get_timer_notification_type(priv->config),
			(gchar const *)macro_key_info->timer_name,
			isku_rkp_macro_key_info_get_timer_length(macro_key_info));
	isku_rkp_macro_key_info_free(macro_key_info);
}

static void timer_stop_cb(IskuEventhandlerChannel *channel, gpointer user_data) {
	IskuEventhandler *eventhandler = ISKU_EVENTHANDLER(user_data);
	IskuEventhandlerPrivate *priv = eventhandler->priv;

	roccat_notification_timer_stop(priv->timer_note);
}

static gboolean profile_data_load(IskuEventhandler *eventhandler, GError **error) {
	IskuEventhandlerPrivate *priv = eventhandler->priv;
	guint i;

	for (i = 0; i < ISKU_PROFILE_NUM; ++i) {
		priv->rkp[i] = isku_rkp_load(priv->device, i, error);
		if (priv->rkp[i] == NULL)
			return FALSE;
	}
	return TRUE;
}

static gboolean add_cb(RoccatDevice *device, gpointer user_data) {
	IskuEventhandler *eventhandler = ISKU_EVENTHANDLER(user_data);
	IskuEventhandlerPrivate *priv = eventhandler->priv;
	GError *error = NULL;

	if (priv->device) {
		g_critical("Multiple devices present.");
		return TRUE;
	}

	priv->device = device;

	configuration_reload(eventhandler);

	/* Notificators gets initialized only once if a device is found.
	 * Get deinited once in finalizer.
	 */
	if (!priv->profile_note)
		priv->profile_note = roccat_notification_profile_new(roccat_eventhandler_host_get_notificator(priv->host), ISKU_DEVICE_NAME);
	if (!priv->timer_note)
		priv->timer_note = roccat_notification_timer_new(roccat_eventhandler_host_get_notificator(priv->host), ISKU_DEVICE_NAME);
	if (!priv->live_recording_note)
		priv->live_recording_note = roccat_notification_live_recording_new(roccat_eventhandler_host_get_notificator(priv->host), ISKU_DEVICE_NAME);
	
	priv->actual_profile_index = isku_actual_profile_read(priv->device, &error);
	if (error) {
		g_warning("Error reading actual profile: %s", error->message);
		g_clear_error(&error);
	}
	
	profile_data_load(eventhandler, &error);
	if (error) {
		g_warning("Error reading profile data: %s", error->message);
		g_clear_error(&error);
	}

	priv->dbus_server = isku_dbus_server_new();
	isku_dbus_server_connect(priv->dbus_server);
	g_signal_connect(G_OBJECT(priv->dbus_server), "easyshift", G_CALLBACK(talk_easyshift_cb), eventhandler);
	g_signal_connect(G_OBJECT(priv->dbus_server), "easyshift-lock", G_CALLBACK(talk_easyshift_lock_cb), eventhandler);
	g_signal_connect(G_OBJECT(priv->dbus_server), "open-gui", G_CALLBACK(open_gui_cb), eventhandler);
	g_signal_connect(G_OBJECT(priv->dbus_server), "profile-changed-outside", G_CALLBACK(profile_changed_cb), eventhandler);
	g_signal_connect(G_OBJECT(priv->dbus_server), "profile-data-changed-outside", G_CALLBACK(profile_data_changed_outside_cb), eventhandler);
	g_signal_connect(G_OBJECT(priv->dbus_server), "configuration-changed-outside", G_CALLBACK(configuration_changed_outside_cb), eventhandler);

	g_signal_connect(G_OBJECT(priv->host), "active-window-changed", G_CALLBACK(window_changed_cb), eventhandler);

	isku_eventhandler_channel_start(priv->channel, roccat_device_get_roccat_dev(priv->device), &error);
	if (error) {
		g_warning("Error starting chardev eventhandler: %s", error->message);
		g_clear_error(&error);
	}
	
	priv->device_set_up = TRUE;

	return FALSE;
}

static void stop(IskuEventhandler *eventhandler) {
	IskuEventhandlerPrivate *priv = eventhandler->priv;
	guint i;

	priv->device_set_up = FALSE;

	g_object_unref(priv->dbus_server);
	priv->dbus_server = NULL;

	isku_eventhandler_channel_stop(priv->channel);

	roccat_device_free(priv->device);
	priv->device = NULL;

	isku_configuration_free(priv->config);
	priv->config = NULL;

	for (i = 0; i < ISKU_PROFILE_NUM; ++i) {
		isku_rkp_free(priv->rkp[i]);
		priv->rkp[i] = NULL;
	}
}

static gboolean remove_cb(RoccatDevice *device, gpointer user_data) {
	IskuEventhandler *eventhandler = ISKU_EVENTHANDLER(user_data);
	IskuEventhandlerPrivate *priv = eventhandler->priv;
	if (priv->device && strcmp(roccat_device_get_identifier(device), roccat_device_get_identifier(priv->device)) == 0) {
		stop(eventhandler);
	}
	return TRUE;
}

static void isku_eventhandler_init(IskuEventhandler *eventhandler) {
	eventhandler->priv = ISKU_EVENTHANDLER_GET_PRIVATE(eventhandler);
}

static void finalize(GObject *object) {
	IskuEventhandler *eventhandler = ISKU_EVENTHANDLER(object);
	IskuEventhandlerPrivate *priv = eventhandler->priv;

	g_object_unref(priv->channel);
	priv->channel = NULL;

	roccat_device_scanner_free(priv->device_scanner);
	priv->device_scanner = NULL;

	stop(eventhandler);

	if (priv->live_recording_note) {
		roccat_notification_live_recording_free(priv->live_recording_note);
		priv->live_recording_note = NULL;
	}

	if (priv->timer_note) {
		roccat_notification_timer_free(priv->timer_note);
		priv->timer_note = NULL;
	}

	if (priv->profile_note) {
		roccat_notification_profile_free(priv->profile_note);
		priv->profile_note = NULL;
	}
	
	G_OBJECT_CLASS(isku_eventhandler_parent_class)->finalize(object);
}

static GObject *constructor(GType gtype, guint n_properties, GObjectConstructParam *properties) {
	IskuEventhandler *eventhandler;
	IskuEventhandlerPrivate *priv;
	GObject *obj;

	obj = G_OBJECT_CLASS(isku_eventhandler_parent_class)->constructor(gtype, n_properties, properties);
	eventhandler = ISKU_EVENTHANDLER(obj);
	priv = eventhandler->priv;

	priv->channel = isku_eventhandler_channel_new();
	g_signal_connect(G_OBJECT(priv->channel), "open-driver", G_CALLBACK(open_gui_cb), eventhandler);
	g_signal_connect(G_OBJECT(priv->channel), "profile-changed", G_CALLBACK(profile_changed_cb), eventhandler);
	g_signal_connect(G_OBJECT(priv->channel), "quicklaunch", G_CALLBACK(quicklaunch_cb), eventhandler);
	g_signal_connect(G_OBJECT(priv->channel), "talk", G_CALLBACK(talk_cb), eventhandler);
	g_signal_connect(G_OBJECT(priv->channel), "timer-start", G_CALLBACK(timer_start_cb), eventhandler);
	g_signal_connect(G_OBJECT(priv->channel), "timer-stop", G_CALLBACK(timer_stop_cb), eventhandler);
	g_signal_connect(G_OBJECT(priv->channel), "light-changed", G_CALLBACK(light_changed_cb), eventhandler);
	g_signal_connect(G_OBJECT(priv->channel), "live-recording", G_CALLBACK(live_recording_cb), eventhandler);

	priv->device_scanner = isku_device_scanner_new(add_cb, remove_cb, eventhandler);

	return obj;
}

static void set_property(GObject *object, guint prop_id, GValue const *value, GParamSpec *pspec) {
	IskuEventhandlerPrivate *priv = ISKU_EVENTHANDLER(object)->priv;

	switch(prop_id) {
	case PROP_HOST:
		priv->host = g_value_get_pointer(value);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
	}
}

static void isku_eventhandler_class_init(IskuEventhandlerClass *klass) {
	GObjectClass *gobject_class;

	gobject_class = G_OBJECT_CLASS(klass);

	gobject_class->constructor = constructor;
	gobject_class->finalize = finalize;
	gobject_class->set_property = set_property;

	g_type_class_add_private(klass, sizeof(IskuEventhandlerPrivate));

	g_object_class_install_property(gobject_class, PROP_HOST,
			g_param_spec_pointer("host",
					"Host",
					"Pointer to RoccatEventhandlerHost interface",
					G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
}

ROCCAT_EVENTHANDLER_PLUGIN_API RoccatEventhandlerPlugin *roccat_eventhandler_plugin_new(RoccatEventhandlerHost *host) {
	return ROCCAT_EVENTHANDLER_PLUGIN(isku_eventhandler_new(host));
}
