/*
 * 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_key_sequence_frame.h"
#include "roccat_macro_editor_advanced.h"
#include "roccat_macro_editor_basic.h"

#define ROCCAT_MACRO_EDITOR_KEY_SEQUENCE_FRAME_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), ROCCAT_MACRO_EDITOR_KEY_SEQUENCE_FRAME_TYPE, RoccatMacroEditorKeySequenceFrameClass))
#define IS_ROCCAT_MACRO_EDITOR_KEY_SEQUENCE_FRAME_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), ROCCAT_MACRO_EDITOR_KEY_SEQUENCE_FRAME_TYPE))
#define ROCCAT_MACRO_EDITOR_KEY_SEQUENCE_FRAME_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), ROCCAT_MACRO_EDITOR_KEY_SEQUENCE_FRAME_TYPE, RoccatMacroEditorKeySequenceFramePrivate))

typedef struct _RoccatMacroEditorKeySequenceFrameClass RoccatMacroEditorKeySequenceFrameClass;
typedef struct _AbsoluteKeystroke AbsoluteKeystroke;

enum {
	NUM_VIEWS = 2
};

struct _RoccatMacroEditorKeySequenceFrameClass {
	GtkFrameClass parent_class;
	void (*modified)(RoccatMacroEditorKeySequenceFrame *key_sequence_frame);
};

struct _RoccatMacroEditorKeySequenceFramePrivate {
	GtkButton *clear_button;
	GtkComboBox *view_combo;
	GtkWidget *action_area;
	RoccatMacroEditorInterface *active_view;
	RoccatMacroEditorInterface *views[NUM_VIEWS];
	gulong handlers[NUM_VIEWS];
};

G_DEFINE_TYPE(RoccatMacroEditorKeySequenceFrame, roccat_macro_editor_key_sequence_frame, GTK_TYPE_FRAME);

enum {
	MODIFIED,
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

enum {
	VIEW_TITLE_COLUMN,
	VIEW_INDEX_COLUMN,
	VIEW_N_COLUMNS
};

static void set_clear_button_sensitive(RoccatMacroEditorKeySequenceFrame *key_sequence_frame) {
	RoccatMacroEditorKeySequenceFramePrivate *priv = key_sequence_frame->priv;
	gboolean empty = roccat_macro_editor_interface_empty(priv->active_view);
	gtk_widget_set_sensitive(GTK_WIDGET(priv->clear_button), !empty);
}

static void modified(RoccatMacroEditorKeySequenceFrame *key_sequence_frame) {
	g_signal_emit((gpointer)key_sequence_frame, signals[MODIFIED], 0);
}

static void modified_cb(RoccatMacroEditorInterface *view, gpointer user_data) {
	RoccatMacroEditorKeySequenceFrame *key_sequence_frame = ROCCAT_MACRO_EDITOR_KEY_SEQUENCE_FRAME(user_data);
	modified(key_sequence_frame);
}

static void empty_changed_cb(RoccatMacroEditorInterface *view, gpointer user_data) {
	RoccatMacroEditorKeySequenceFrame *key_sequence_frame = ROCCAT_MACRO_EDITOR_KEY_SEQUENCE_FRAME(user_data);
	set_clear_button_sensitive(key_sequence_frame);
}

static void clear_button_clicked_cb(GtkButton *button, gpointer user_data) {
	RoccatMacroEditorKeySequenceFrame *key_sequence_frame = ROCCAT_MACRO_EDITOR_KEY_SEQUENCE_FRAME(user_data);
	RoccatMacroEditorKeySequenceFramePrivate *priv = key_sequence_frame->priv;
	roccat_macro_editor_interface_clear(priv->active_view);
}

static RoccatMacroKeystrokes *deactivate_view(RoccatMacroEditorInterface *view) {
	gtk_widget_hide(GTK_WIDGET(view));
	return roccat_macro_editor_interface_get_keystrokes(view);
}

static void activate_view(RoccatMacroEditorKeySequenceFrame *key_sequence_frame, RoccatMacroKeystrokes *keystrokes) {
	RoccatMacroEditorKeySequenceFramePrivate *priv = key_sequence_frame->priv;
	GtkTreeModel *model;
	GtkTreeIter iter;
	guint index;

	if (!gtk_combo_box_get_active_iter(priv->view_combo, &iter)) {
		/* activate first iter if non active */
		model = gtk_combo_box_get_model(priv->view_combo);
		if (!gtk_tree_model_get_iter_first(model, &iter)) {
			priv->active_view = NULL;
			return;
		}
		gtk_combo_box_set_active_iter(priv->view_combo, &iter);
	}

	gtk_tree_model_get(gtk_combo_box_get_model(priv->view_combo), &iter, VIEW_INDEX_COLUMN, &index, -1);
	priv->active_view = priv->views[index];
	if (keystrokes)
		roccat_macro_editor_interface_set_keystrokes(priv->active_view, keystrokes);
	else
		roccat_macro_editor_interface_clear(priv->active_view);
	gtk_widget_show(GTK_WIDGET(priv->active_view));
}

static void block_handlers(RoccatMacroEditorKeySequenceFrame *key_sequence_frame) {
	RoccatMacroEditorKeySequenceFramePrivate *priv = key_sequence_frame->priv;
	guint i;
	for (i = 0; i < NUM_VIEWS; ++i)
		g_signal_handler_block(priv->views[i], priv->handlers[i]);
}

static void unblock_handlers(RoccatMacroEditorKeySequenceFrame *key_sequence_frame) {
	RoccatMacroEditorKeySequenceFramePrivate *priv = key_sequence_frame->priv;
	guint i;
	for (i = 0; i < NUM_VIEWS; ++i)
		g_signal_handler_unblock(priv->views[i], priv->handlers[i]);
}

static void combo_button_changed_cb(GtkComboBox *combo, gpointer user_data) {
	RoccatMacroEditorKeySequenceFrame *key_sequence_frame = ROCCAT_MACRO_EDITOR_KEY_SEQUENCE_FRAME(user_data);
	RoccatMacroEditorKeySequenceFramePrivate *priv = key_sequence_frame->priv;
	RoccatMacroKeystrokes *keystrokes;

	block_handlers(key_sequence_frame);

	if (priv->active_view)
		keystrokes = deactivate_view(priv->active_view);
	else
		keystrokes = NULL;

	activate_view(key_sequence_frame, keystrokes);

	unblock_handlers(key_sequence_frame);
}

static GtkWidget *view_combo_new(RoccatMacroEditorKeySequenceFrame *key_sequence_frame) {
	RoccatMacroEditorKeySequenceFramePrivate *priv = key_sequence_frame->priv;
	GtkWidget *combo;
	GtkListStore *store;
	GtkTreeIter iter;
	GtkCellRenderer *renderer;
	guint i;

	store = gtk_list_store_new(VIEW_N_COLUMNS, G_TYPE_STRING, G_TYPE_INT);

	for (i = 0; i < NUM_VIEWS; ++i) {
		gtk_list_store_append(store, &iter);
		gtk_list_store_set(store, &iter,
				VIEW_TITLE_COLUMN, roccat_macro_editor_interface_name(priv->views[i]),
				VIEW_INDEX_COLUMN, i,
				-1);
	}

	combo = gtk_combo_box_new_with_model(GTK_TREE_MODEL(store));
	g_object_unref(store);

	renderer = gtk_cell_renderer_text_new();
	gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), renderer, TRUE);
	gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), renderer, "text", VIEW_TITLE_COLUMN, NULL);

	return combo;
}

GtkWidget *roccat_macro_editor_key_sequence_frame_new(void) {
	RoccatMacroEditorKeySequenceFrame *key_sequence_frame;

	key_sequence_frame = g_object_new(ROCCAT_MACRO_EDITOR_KEY_SEQUENCE_FRAME_TYPE,
			"label", "Key sequence",
			NULL);

	set_clear_button_sensitive(key_sequence_frame);

	return GTK_WIDGET(key_sequence_frame);
}

static void roccat_macro_editor_key_sequence_frame_init(RoccatMacroEditorKeySequenceFrame *key_sequence_frame) {
	RoccatMacroEditorKeySequenceFramePrivate *priv = ROCCAT_MACRO_EDITOR_KEY_SEQUENCE_FRAME_GET_PRIVATE(key_sequence_frame);
	GtkWidget *vbox;
	guint i;

	key_sequence_frame->priv = priv;

	vbox = gtk_vbox_new(FALSE, 0);
	priv->action_area = gtk_hbox_new(FALSE, 0);

	priv->clear_button = GTK_BUTTON(gtk_button_new_with_label("Clear"));
	g_object_ref_sink(priv->clear_button);

	priv->views[0] = ROCCAT_MACRO_EDITOR_INTERFACE(roccat_macro_editor_advanced_new());
	g_object_ref_sink(priv->views[0]);

	priv->views[1] = ROCCAT_MACRO_EDITOR_INTERFACE(roccat_macro_editor_basic_new());
	g_object_ref_sink(priv->views[1]);

	priv->view_combo = GTK_COMBO_BOX(view_combo_new(key_sequence_frame));
	g_object_ref_sink(priv->view_combo);

	for (i = 0; i < NUM_VIEWS; ++i)
		gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(priv->views[i]), TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(vbox), priv->action_area, FALSE, FALSE, 0);

	gtk_box_pack_start(GTK_BOX(priv->action_area), GTK_WIDGET(priv->clear_button), FALSE, FALSE, 0);
	gtk_box_pack_start(GTK_BOX(priv->action_area), GTK_WIDGET(priv->view_combo), FALSE, FALSE, 0);

	gtk_container_add(GTK_CONTAINER(key_sequence_frame), vbox);

	for (i = 0; i < NUM_VIEWS; ++i)
		gtk_widget_hide(GTK_WIDGET(priv->views[i]));

	gtk_widget_show_all(priv->action_area);
	gtk_widget_show(vbox);
	activate_view(key_sequence_frame, NULL);

	for (i = 0; i < NUM_VIEWS; ++i) {
		priv->handlers[i] = g_signal_connect(G_OBJECT(priv->views[i]), "modified", G_CALLBACK(modified_cb), key_sequence_frame);
		g_signal_connect(G_OBJECT(priv->views[i]), "empty-changed", G_CALLBACK(empty_changed_cb), key_sequence_frame);
	}
	g_signal_connect(G_OBJECT(priv->clear_button), "clicked", G_CALLBACK(clear_button_clicked_cb), key_sequence_frame);
	g_signal_connect(G_OBJECT(priv->view_combo), "changed", G_CALLBACK(combo_button_changed_cb), key_sequence_frame);
}

static void roccat_macro_editor_key_sequence_frame_finalize(GObject *object) {
	RoccatMacroEditorKeySequenceFramePrivate *priv = ROCCAT_MACRO_EDITOR_KEY_SEQUENCE_FRAME(object)->priv;
	guint i;

	g_object_unref(priv->clear_button);
	g_object_unref(priv->view_combo);

	for (i = 0; i < NUM_VIEWS; ++i)
		g_object_unref(priv->views[i]);

	G_OBJECT_CLASS(roccat_macro_editor_key_sequence_frame_parent_class)->finalize(object);
}

static void roccat_macro_editor_key_sequence_frame_class_init(RoccatMacroEditorKeySequenceFrameClass *klass) {
	GObjectClass *gobject_class;

	gobject_class = G_OBJECT_CLASS(klass);

	gobject_class->finalize = roccat_macro_editor_key_sequence_frame_finalize;

	g_type_class_add_private(klass, sizeof(RoccatMacroEditorKeySequenceFramePrivate));

	signals[MODIFIED] = g_signal_new("modified",
			G_TYPE_FROM_CLASS(klass),
			G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
			G_STRUCT_OFFSET(RoccatMacroEditorKeySequenceFrameClass, modified),
			NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
}

void roccat_macro_editor_key_sequence_frame_set_keystrokes(RoccatMacroEditorKeySequenceFrame *key_sequence_frame, RoccatMacroKeystrokes *keystrokes) {
	block_handlers(key_sequence_frame);
	roccat_macro_editor_interface_set_keystrokes(key_sequence_frame->priv->active_view, keystrokes);
	unblock_handlers(key_sequence_frame);
}

RoccatMacroKeystrokes *roccat_macro_editor_key_sequence_frame_get_keystrokes(RoccatMacroEditorKeySequenceFrame *key_sequence_frame) {
	return roccat_macro_editor_interface_get_keystrokes(key_sequence_frame->priv->active_view);
}

void roccat_macro_editor_key_sequence_frame_add_keystroke(RoccatMacroEditorKeySequenceFrame *key_sequence_frame, guint key, guint action, glong abs_time, glong rel_time) {
	roccat_macro_editor_interface_add_keystroke(key_sequence_frame->priv->active_view, key, action, abs_time, rel_time);
}

void roccat_macro_editor_key_sequence_frame_clear(RoccatMacroEditorKeySequenceFrame *key_sequence_frame) {
	guint i;

	for (i = 0; i < NUM_VIEWS; ++i)
		roccat_macro_editor_interface_clear(key_sequence_frame->priv->views[i]);
}

glong roccat_macro_editor_key_sequence_frame_abs_time(RoccatMacroEditorKeySequenceFrame *key_sequence_frame) {
	return roccat_macro_editor_interface_abs_time(key_sequence_frame->priv->active_view);
}

gboolean roccat_macro_editor_key_sequence_frame_empty(RoccatMacroEditorKeySequenceFrame *key_sequence_frame) {
	return roccat_macro_editor_interface_empty(key_sequence_frame->priv->active_view);
}

GtkWidget *roccat_macro_editor_key_sequence_get_action_area(RoccatMacroEditorKeySequenceFrame *key_sequence_frame) {
	return key_sequence_frame->priv->action_area;
}
