/*
 * 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_basic.h"
#include "roccat_macro_editor_basic_list_store.h"
#include "roccat_macro_editor_basic_tree_view.h"

#define ROCCAT_MACRO_EDITOR_BASIC_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), ROCCAT_MACRO_EDITOR_BASIC_TYPE, RoccatMacroEditorBasicClass))
#define IS_ROCCAT_MACRO_EDITOR_BASIC_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), ROCCAT_MACRO_EDITOR_BASIC_TYPE))
#define ROCCAT_MACRO_EDITOR_BASIC_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), ROCCAT_MACRO_EDITOR_BASIC_TYPE, RoccatMacroEditorBasicPrivate))

typedef struct _RoccatMacroEditorBasicClass RoccatMacroEditorBasicClass;

struct _RoccatMacroEditorBasicClass {
	GtkScrolledWindowClass parent_class;
};

struct _RoccatMacroEditorBasicPrivate {
	RoccatMacroEditorBasicListStore *list_store;
	RoccatMacroEditorBasicTreeView *tree_view;
};

static gchar const *roccat_macro_editor_basic_name(RoccatMacroEditorInterface *self) {
	return "Basic";
}

static void roccat_macro_editor_basic_set_keystrokes(RoccatMacroEditorInterface *self, RoccatMacroKeystrokes const *keystrokes) {
	RoccatMacroEditorBasicPrivate *priv = ROCCAT_MACRO_EDITOR_BASIC(self)->priv;
	roccat_macro_editor_basic_list_store_set_keystrokes(priv->list_store, keystrokes);
	roccat_macro_editor_basic_list_store_set_modified(priv->list_store, FALSE);
}

static RoccatMacroKeystrokes *roccat_macro_editor_basic_get_keystrokes(RoccatMacroEditorInterface *self) {
	RoccatMacroEditorBasicPrivate *priv = ROCCAT_MACRO_EDITOR_BASIC(self)->priv;
	return roccat_macro_editor_basic_list_store_get_keystrokes(priv->list_store);
}

static void roccat_macro_editor_basic_clear(RoccatMacroEditorInterface *self) {
	RoccatMacroEditorBasicPrivate *priv = ROCCAT_MACRO_EDITOR_BASIC(self)->priv;
	roccat_macro_editor_basic_list_store_clear(priv->list_store);
}

static gboolean roccat_macro_editor_basic_empty(RoccatMacroEditorInterface *self) {
	RoccatMacroEditorBasicPrivate *priv = ROCCAT_MACRO_EDITOR_BASIC(self)->priv;
	return roccat_macro_editor_basic_list_store_empty(priv->list_store);
}

static void roccat_macro_editor_basic_add_keystroke(RoccatMacroEditorInterface *self, guint key, guint action, glong abs_time, glong rel_time) {
	RoccatMacroEditorBasicPrivate *priv = ROCCAT_MACRO_EDITOR_BASIC(self)->priv;
	roccat_macro_editor_basic_tree_view_add_keystroke(priv->tree_view, key, action, rel_time);
}

static glong roccat_macro_editor_basic_abs_time(RoccatMacroEditorInterface *self) {
	return 0L;
}

static gboolean roccat_macro_editor_basic_get_modified(RoccatMacroEditorInterface *self) {
	RoccatMacroEditorBasicPrivate *priv = ROCCAT_MACRO_EDITOR_BASIC(self)->priv;
	return roccat_macro_editor_basic_list_store_get_modified(priv->list_store);
}

static void roccat_macro_editor_basic_set_modified(RoccatMacroEditorInterface *self, gboolean new_value) {
	RoccatMacroEditorBasicPrivate *priv = ROCCAT_MACRO_EDITOR_BASIC(self)->priv;
	return roccat_macro_editor_basic_list_store_set_modified(priv->list_store, new_value);
}

static void roccat_macro_editor_basic_interface_init(RoccatMacroEditorInterfaceInterface *iface) {
	iface->name = roccat_macro_editor_basic_name;
	iface->set_keystrokes = roccat_macro_editor_basic_set_keystrokes;
	iface->get_keystrokes = roccat_macro_editor_basic_get_keystrokes;
	iface->clear = roccat_macro_editor_basic_clear;
	iface->empty = roccat_macro_editor_basic_empty;
	iface->add_keystroke = roccat_macro_editor_basic_add_keystroke;
	iface->abs_time = roccat_macro_editor_basic_abs_time;
	iface->get_modified = roccat_macro_editor_basic_get_modified;
	iface->set_modified = roccat_macro_editor_basic_set_modified;
}

G_DEFINE_TYPE_WITH_CODE(RoccatMacroEditorBasic, roccat_macro_editor_basic, GTK_TYPE_SCROLLED_WINDOW,
		G_IMPLEMENT_INTERFACE(ROCCAT_MACRO_EDITOR_INTERFACE_TYPE, roccat_macro_editor_basic_interface_init));

GtkWidget *roccat_macro_editor_basic_new(void) {
	RoccatMacroEditorBasic *macro_editor_basic;

	macro_editor_basic = g_object_new(ROCCAT_MACRO_EDITOR_BASIC_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_container_add(GTK_CONTAINER(macro_editor_basic), GTK_WIDGET(macro_editor_basic->priv->tree_view));

	return GTK_WIDGET(macro_editor_basic);
}

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

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

static void roccat_macro_editor_basic_init(RoccatMacroEditorBasic *macro_editor_basic) {
	RoccatMacroEditorBasicPrivate *priv = ROCCAT_MACRO_EDITOR_BASIC_GET_PRIVATE(macro_editor_basic);

	macro_editor_basic->priv = priv;

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

	priv->tree_view = ROCCAT_MACRO_EDITOR_BASIC_TREE_VIEW(roccat_macro_editor_basic_tree_view_new(priv->list_store));
	gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(priv->tree_view), FALSE);

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

static void roccat_macro_editor_basic_finalize(GObject *object) {
	RoccatMacroEditorBasicPrivate *priv = ROCCAT_MACRO_EDITOR_BASIC(object)->priv;
	g_object_unref(priv->list_store);
	G_OBJECT_CLASS(roccat_macro_editor_basic_parent_class)->finalize(object);
}

static void roccat_macro_editor_basic_class_init(RoccatMacroEditorBasicClass *klass) {
	GObjectClass *gobject_class;

	gobject_class = G_OBJECT_CLASS(klass);

	gobject_class->finalize = roccat_macro_editor_basic_finalize;

	g_type_class_add_private(klass, sizeof(RoccatMacroEditorBasicPrivate));
}
