/*
 * 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_macros_frame.h"
#include "roccat_macro_tree_view.h"

#define ROCCAT_MACRO_EDITOR_MACROS_FRAME_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), ROCCAT_MACRO_EDITOR_MACROS_FRAME_TYPE, RoccatMacroEditorMacrosFrameClass))
#define IS_ROCCAT_MACRO_EDITOR_MACROS_FRAME_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), ROCCAT_MACRO_EDITOR_MACROS_FRAME_TYPE))
#define ROCCAT_MACRO_EDITOR_MACROS_FRAME_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), ROCCAT_MACRO_EDITOR_MACROS_FRAME_TYPE, RoccatMacroEditorMacrosFramePrivate))

typedef struct _RoccatMacroEditorMacrosFrameClass RoccatMacroEditorMacrosFrameClass;
typedef struct _AbsoluteKeystroke AbsoluteKeystroke;

struct _RoccatMacroEditorMacrosFrameClass {
	GtkFrameClass parent_class;
	void(*removed)(RoccatMacroEditorMacrosFrame *frame);
	void(*selected)(RoccatMacroEditorMacrosFrame *frame, GtkTreeRowReference *reference);
};

struct _RoccatMacroEditorMacrosFramePrivate {
	RoccatMacroTreeView *tree_view;
};

G_DEFINE_TYPE(RoccatMacroEditorMacrosFrame, roccat_macro_editor_macros_frame, GTK_TYPE_FRAME);

enum {
	REMOVED,
	SELECTED,
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

GtkWidget *roccat_macro_editor_macros_frame_new(void) {
	RoccatMacroEditorMacrosFrame *macros_frame;

	macros_frame = g_object_new(ROCCAT_MACRO_EDITOR_MACROS_FRAME_TYPE,
			"label", "Macros",
			NULL);

	return GTK_WIDGET(macros_frame);
}

static void removed(RoccatMacroEditorMacrosFrame *macros_frame) {
	g_signal_emit((gpointer)macros_frame, signals[REMOVED], 0);
}

static void selected(RoccatMacroEditorMacrosFrame *macros_frame, GtkTreeRowReference *reference) {
	g_signal_emit((gpointer)macros_frame, signals[SELECTED], 0, reference);
}

static void macro_selected_cb(RoccatMacroTreeView *tree_view, GtkTreeRowReference *reference, gpointer user_data) {
	RoccatMacroEditorMacrosFrame *macros_frame = ROCCAT_MACRO_EDITOR_MACROS_FRAME(user_data);
	selected(macros_frame, reference);
}

static void macro_removed_cb(RoccatMacroTreeView *tree_view, gpointer user_data) {
	RoccatMacroEditorMacrosFrame *macros_frame = ROCCAT_MACRO_EDITOR_MACROS_FRAME(user_data);
	removed(macros_frame);
}

static void add_macroset_clicked_cb(GtkButton *button, gpointer user_data) {
	RoccatMacroEditorMacrosFramePrivate *priv = ROCCAT_MACRO_EDITOR_MACROS_FRAME(user_data)->priv;
	roccat_macro_tree_view_add_macroset(priv->tree_view);
}

static void roccat_macro_editor_macros_frame_init(RoccatMacroEditorMacrosFrame *macros_frame) {
	RoccatMacroEditorMacrosFramePrivate *priv = ROCCAT_MACRO_EDITOR_MACROS_FRAME_GET_PRIVATE(macros_frame);
	RoccatMacroTreeStore *store;
	GtkWidget *vbox;
	GtkWidget *scrolled_window;
	GtkWidget *hbox;
	GtkWidget *button;

	macros_frame->priv = priv;

	store = roccat_macro_tree_store_new();
	priv->tree_view = ROCCAT_MACRO_TREE_VIEW(roccat_macro_tree_view_new(store));
	g_object_ref_sink(priv->tree_view);
	g_object_unref(store);

	gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(priv->tree_view), FALSE);

	vbox = gtk_vbox_new(FALSE, 0);
	hbox = gtk_hbox_new(FALSE, 0);
	scrolled_window = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
	button = gtk_button_new_with_label("Add macroset");

	gtk_container_add(GTK_CONTAINER(scrolled_window), GTK_WIDGET(priv->tree_view));

	gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);

	gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);

	gtk_container_add(GTK_CONTAINER(macros_frame), vbox);

	g_signal_connect(G_OBJECT(priv->tree_view), "removed", G_CALLBACK(macro_removed_cb), macros_frame);
	g_signal_connect(G_OBJECT(priv->tree_view), "selected", G_CALLBACK(macro_selected_cb), macros_frame);
	g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(add_macroset_clicked_cb), macros_frame);

	gtk_widget_show_all(GTK_WIDGET(vbox));
}

static void roccat_macro_editor_macros_frame_finalize(GObject *object) {
	RoccatMacroEditorMacrosFramePrivate *priv = ROCCAT_MACRO_EDITOR_MACROS_FRAME(object)->priv;
	g_object_unref(priv->tree_view);
	G_OBJECT_CLASS(roccat_macro_editor_macros_frame_parent_class)->finalize(object);
}

static void roccat_macro_editor_macros_frame_class_init(RoccatMacroEditorMacrosFrameClass *klass) {
	GObjectClass *gobject_class;

	gobject_class = G_OBJECT_CLASS(klass);

	gobject_class->finalize = roccat_macro_editor_macros_frame_finalize;

	g_type_class_add_private(klass, sizeof(RoccatMacroEditorMacrosFramePrivate));

	signals[SELECTED] = g_signal_new("selected",
			G_TYPE_FROM_CLASS(klass),
			G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
			G_STRUCT_OFFSET(RoccatMacroEditorMacrosFrameClass, selected),
			NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE,
			1, GTK_TYPE_TREE_ROW_REFERENCE);


	signals[REMOVED] = g_signal_new("removed",
			G_TYPE_FROM_CLASS(klass),
			G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
			G_STRUCT_OFFSET(RoccatMacroEditorMacrosFrameClass, removed),
			NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
}

RoccatMacros *roccat_macro_editor_macros_frame_get_macros(RoccatMacroEditorMacrosFrame *macros_frame) {
	RoccatMacroTreeStore *store;

	store = roccat_macro_tree_view_get_store(macros_frame->priv->tree_view);
	return roccat_macro_tree_store_get_macros(store);
}

gboolean roccat_macro_editor_macros_frame_add_macros(RoccatMacroEditorMacrosFrame *macros_frame, RoccatMacros *roccat_macros) {
	RoccatMacroTreeStore *store;
	store = roccat_macro_tree_view_get_store(macros_frame->priv->tree_view);
	return roccat_macro_tree_store_add_macros(store, roccat_macros);
}

void roccat_macro_editor_macros_frame_clear(RoccatMacroEditorMacrosFrame *macros_frame) {
	RoccatMacroTreeStore *store;
	store = roccat_macro_tree_view_get_store(macros_frame->priv->tree_view);
	roccat_macro_tree_store_clear(store);
}
