/*
 * 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_macro_editor_advanced.h"
#include "roccat_macro_editor_advanced_list_store.h"
#include "roccat_macro_editor_advanced_table.h"

#define ROCCAT_MACRO_EDITOR_ADVANCED_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), ROCCAT_MACRO_EDITOR_ADVANCED_TYPE, RoccatMacroEditorAdvancedClass))
#define IS_ROCCAT_MACRO_EDITOR_ADVANCED_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), ROCCAT_MACRO_EDITOR_ADVANCED_TYPE))
#define ROCCAT_MACRO_EDITOR_ADVANCED_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), ROCCAT_MACRO_EDITOR_ADVANCED_TYPE, RoccatMacroEditorAdvancedPrivate))

typedef struct _RoccatMacroEditorAdvancedClass RoccatMacroEditorAdvancedClass;

struct _RoccatMacroEditorAdvancedClass {
	GtkScrolledWindowClass parent_class;
};

struct _RoccatMacroEditorAdvancedPrivate {
	RoccatMacroEditorAdvancedListStore *list_store;
	RoccatMacroEditorAdvancedTable *table;
};

static gchar const *roccat_macro_editor_advanced_name(RoccatMacroEditorInterface *self) {
	return "Advanced";
}

static void roccat_macro_editor_advanced_set_keystrokes(RoccatMacroEditorInterface *self, RoccatMacroKeystrokes const *keystrokes) {
	RoccatMacroEditorAdvancedPrivate *priv = ROCCAT_MACRO_EDITOR_ADVANCED(self)->priv;
	roccat_macro_editor_advanced_list_store_set_keystrokes(priv->list_store, keystrokes);
	roccat_macro_editor_advanced_list_store_set_modified(priv->list_store, FALSE);
}

static RoccatMacroKeystrokes *roccat_macro_editor_advanced_get_keystrokes(RoccatMacroEditorInterface *self) {
	RoccatMacroEditorAdvancedPrivate *priv = ROCCAT_MACRO_EDITOR_ADVANCED(self)->priv;
	return roccat_macro_editor_advanced_list_store_get_keystrokes(priv->list_store);
}

static void roccat_macro_editor_advanced_clear(RoccatMacroEditorInterface *self) {
	RoccatMacroEditorAdvancedPrivate *priv = ROCCAT_MACRO_EDITOR_ADVANCED(self)->priv;
	roccat_macro_editor_advanced_list_store_clear(priv->list_store);
}

static gboolean roccat_macro_editor_advanced_empty(RoccatMacroEditorInterface *self) {
	RoccatMacroEditorAdvancedPrivate *priv = ROCCAT_MACRO_EDITOR_ADVANCED(self)->priv;
	return roccat_macro_editor_advanced_list_store_empty(priv->list_store);
}

static void roccat_macro_editor_advanced_add_keystroke(RoccatMacroEditorInterface *self, guint key, guint action, glong abs_time, glong rel_time) {
	RoccatMacroEditorAdvancedPrivate *priv = ROCCAT_MACRO_EDITOR_ADVANCED(self)->priv;
	roccat_macro_editor_advanced_list_store_add_keystroke(priv->list_store, key, action, abs_time);
}

static glong roccat_macro_editor_advanced_abs_time(RoccatMacroEditorInterface *self) {
	RoccatMacroEditorAdvancedPrivate *priv = ROCCAT_MACRO_EDITOR_ADVANCED(self)->priv;
	return roccat_macro_editor_advanced_list_store_get_abs_time(priv->list_store);
}

static gboolean roccat_macro_editor_advanced_get_modified(RoccatMacroEditorInterface *self) {
	RoccatMacroEditorAdvancedPrivate *priv = ROCCAT_MACRO_EDITOR_ADVANCED(self)->priv;
	return roccat_macro_editor_advanced_list_store_get_modified(priv->list_store);
}

static void roccat_macro_editor_advanced_set_modified(RoccatMacroEditorInterface *self, gboolean new_value) {
	RoccatMacroEditorAdvancedPrivate *priv = ROCCAT_MACRO_EDITOR_ADVANCED(self)->priv;
	return roccat_macro_editor_advanced_list_store_set_modified(priv->list_store, new_value);
}

static void roccat_macro_editor_advanced_interface_init(RoccatMacroEditorInterfaceInterface *iface) {
	iface->name = roccat_macro_editor_advanced_name;
	iface->set_keystrokes = roccat_macro_editor_advanced_set_keystrokes;
	iface->get_keystrokes = roccat_macro_editor_advanced_get_keystrokes;
	iface->clear = roccat_macro_editor_advanced_clear;
	iface->empty = roccat_macro_editor_advanced_empty;
	iface->add_keystroke = roccat_macro_editor_advanced_add_keystroke;
	iface->abs_time = roccat_macro_editor_advanced_abs_time;
	iface->get_modified = roccat_macro_editor_advanced_get_modified;
	iface->set_modified = roccat_macro_editor_advanced_set_modified;
}

G_DEFINE_TYPE_WITH_CODE(RoccatMacroEditorAdvanced, roccat_macro_editor_advanced, GTK_TYPE_SCROLLED_WINDOW,
		G_IMPLEMENT_INTERFACE(ROCCAT_MACRO_EDITOR_INTERFACE_TYPE, roccat_macro_editor_advanced_interface_init));


GtkWidget *roccat_macro_editor_advanced_new(void) {
	RoccatMacroEditorAdvanced *macro_editor_advanced;

	macro_editor_advanced = g_object_new(ROCCAT_MACRO_EDITOR_ADVANCED_TYPE,
			"hscrollbar-policy", GTK_POLICY_AUTOMATIC,
			"vscrollbar-policy", GTK_POLICY_AUTOMATIC,
			NULL);

	/* don't do this earlier or scrolled_window assertions GTK_IS_RANGE will fail */
	gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(macro_editor_advanced),
			GTK_WIDGET(macro_editor_advanced->priv->table));

	return GTK_WIDGET(macro_editor_advanced);
}

static void modified_cb(RoccatMacroEditorAdvancedListStore *store, gpointer user_data) {
	roccat_macro_editor_interface_modified(ROCCAT_MACRO_EDITOR_INTERFACE(user_data));
}

static void empty_changed_cb(RoccatMacroEditorAdvancedListStore *store, gpointer user_data) {
	roccat_macro_editor_interface_empty_changed(ROCCAT_MACRO_EDITOR_INTERFACE(user_data));
}

static void roccat_macro_editor_advanced_init(RoccatMacroEditorAdvanced *macro_editor_advanced) {
	RoccatMacroEditorAdvancedPrivate *priv = ROCCAT_MACRO_EDITOR_ADVANCED_GET_PRIVATE(macro_editor_advanced);

	macro_editor_advanced->priv = priv;

	priv->list_store = roccat_macro_editor_advanced_list_store_new();
	g_signal_connect(G_OBJECT(priv->list_store), "modified", G_CALLBACK(modified_cb), macro_editor_advanced);
	g_signal_connect(G_OBJECT(priv->list_store), "empty-changed", G_CALLBACK(empty_changed_cb), macro_editor_advanced);

	priv->table = ROCCAT_MACRO_EDITOR_ADVANCED_TABLE(roccat_macro_editor_advanced_table_new(priv->list_store));
	g_object_ref_sink(priv->table);

	gtk_widget_show(GTK_WIDGET(priv->table));
}

static void roccat_macro_editor_advanced_finalize(GObject *object) {
	RoccatMacroEditorAdvancedPrivate *priv = ROCCAT_MACRO_EDITOR_ADVANCED(object)->priv;
	g_object_unref(priv->list_store);
	g_object_unref(priv->table);
	G_OBJECT_CLASS(roccat_macro_editor_advanced_parent_class)->finalize(object);
}

static void roccat_macro_editor_advanced_class_init(RoccatMacroEditorAdvancedClass *klass) {
	GObjectClass *gobject_class;

	gobject_class = G_OBJECT_CLASS(klass);

	gobject_class->finalize = roccat_macro_editor_advanced_finalize;

	g_type_class_add_private(klass, sizeof(RoccatMacroEditorAdvancedPrivate));
}
