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

#define ROCCAT_MACRO_EDITOR_RECORD_OPTIONS_FRAME_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), ROCCAT_MACRO_EDITOR_RECORD_OPTIONS_FRAME_TYPE, RoccatMacroEditorRecordOptionsFrameClass))
#define IS_ROCCAT_MACRO_EDITOR_RECORD_OPTIONS_FRAME_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), ROCCAT_MACRO_EDITOR_RECORD_OPTIONS_FRAME_TYPE))
#define ROCCAT_MACRO_EDITOR_RECORD_OPTIONS_FRAME_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), ROCCAT_MACRO_EDITOR_RECORD_OPTIONS_FRAME_TYPE, RoccatMacroEditorRecordOptionsFramePrivate))

typedef struct _RoccatMacroEditorRecordOptionsFrameClass RoccatMacroEditorRecordOptionsFrameClass;
typedef struct _AbsoluteKeystroke AbsoluteKeystroke;

struct _RoccatMacroEditorRecordOptionsFrameClass {
	GtkFrameClass parent_class;
	void (*started)(RoccatMacroEditorRecordOptionsFrame *record_options_frame);
	void (*paste)(RoccatMacroEditorRecordOptionsFrame *record_options_frame);
};

struct _RoccatMacroEditorRecordOptionsFramePrivate {
	GtkToggleButton *record_button;
	GtkToggleButton *delay_button;
	GtkButton *paste_button;
};

G_DEFINE_TYPE(RoccatMacroEditorRecordOptionsFrame, roccat_macro_editor_record_options_frame, GTK_TYPE_FRAME);

enum {
	STARTED,
	PASTE,
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

GtkWidget *roccat_macro_editor_record_options_frame_new(void) {
	RoccatMacroEditorRecordOptionsFrame *record_options_frame;

	record_options_frame = g_object_new(ROCCAT_MACRO_EDITOR_RECORD_OPTIONS_FRAME_TYPE,
			"label", "Record options",
			NULL);

	return GTK_WIDGET(record_options_frame);
}

static void started(RoccatMacroEditorRecordOptionsFrame *record_options_frame) {
	g_signal_emit((gpointer)record_options_frame, signals[STARTED], 0);
}

static void paste(RoccatMacroEditorRecordOptionsFrame *record_options_frame) {
	g_signal_emit((gpointer)record_options_frame, signals[PASTE], 0);
}

static void record_button_clicked_cb(GtkButton *button, gpointer user_data) {
	RoccatMacroEditorRecordOptionsFrame *record_options_frame = ROCCAT_MACRO_EDITOR_RECORD_OPTIONS_FRAME(user_data);
	if (roccat_macro_editor_record_options_frame_is_record_on(record_options_frame))
		started(record_options_frame);
}

static void paste_button_clicked_cb(GtkButton *button, gpointer user_data) {
	RoccatMacroEditorRecordOptionsFrame *record_options_frame = ROCCAT_MACRO_EDITOR_RECORD_OPTIONS_FRAME(user_data);
	paste(record_options_frame);
}

static void roccat_macro_editor_record_options_frame_init(RoccatMacroEditorRecordOptionsFrame *record_options_frame) {
	RoccatMacroEditorRecordOptionsFramePrivate *priv = ROCCAT_MACRO_EDITOR_RECORD_OPTIONS_FRAME_GET_PRIVATE(record_options_frame);
	GtkWidget *vbox;

	record_options_frame->priv = priv;

	vbox = gtk_vbox_new(FALSE, 0);
	gtk_container_add(GTK_CONTAINER(record_options_frame), vbox);

	priv->record_button = GTK_TOGGLE_BUTTON(gtk_toggle_button_new_with_label("Record"));
	g_object_ref_sink(priv->record_button);
	gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(priv->record_button), FALSE, FALSE, 0);
	g_signal_connect(G_OBJECT(priv->record_button), "clicked", G_CALLBACK(record_button_clicked_cb), record_options_frame);

	priv->delay_button = GTK_TOGGLE_BUTTON(gtk_check_button_new_with_label("Record delays"));
	g_object_ref_sink(priv->delay_button);
	gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(priv->delay_button), FALSE, FALSE, 0);

	priv->paste_button = GTK_BUTTON(gtk_button_new_with_label("Paste"));
	g_object_ref_sink(priv->paste_button);
	gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(priv->paste_button), FALSE, FALSE, 10);
	g_signal_connect(G_OBJECT(priv->paste_button), "clicked", G_CALLBACK(paste_button_clicked_cb), record_options_frame);

	gtk_widget_show_all(GTK_WIDGET(vbox));
}

static void roccat_macro_editor_record_options_frame_finalize(GObject *object) {
	RoccatMacroEditorRecordOptionsFramePrivate *priv = ROCCAT_MACRO_EDITOR_RECORD_OPTIONS_FRAME(object)->priv;
	g_object_unref(priv->record_button);
	g_object_unref(priv->delay_button);
	G_OBJECT_CLASS(roccat_macro_editor_record_options_frame_parent_class)->finalize(object);
}

static void roccat_macro_editor_record_options_frame_class_init(RoccatMacroEditorRecordOptionsFrameClass *klass) {
	GObjectClass *gobject_class;

	gobject_class = G_OBJECT_CLASS(klass);

	gobject_class->finalize = roccat_macro_editor_record_options_frame_finalize;

	g_type_class_add_private(klass, sizeof(RoccatMacroEditorRecordOptionsFramePrivate));

	signals[STARTED] = g_signal_new("started",
			G_TYPE_FROM_CLASS(klass),
			G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
			G_STRUCT_OFFSET(RoccatMacroEditorRecordOptionsFrameClass, started),
			NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);

	signals[PASTE] = g_signal_new("paste",
			G_TYPE_FROM_CLASS(klass),
			G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
			G_STRUCT_OFFSET(RoccatMacroEditorRecordOptionsFrameClass, paste),
			NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
}

gboolean roccat_macro_editor_record_options_frame_should_record_delays(RoccatMacroEditorRecordOptionsFrame *record_options_frame) {
	return gtk_toggle_button_get_active(record_options_frame->priv->delay_button);
}

gboolean roccat_macro_editor_record_options_frame_is_record_on(RoccatMacroEditorRecordOptionsFrame *record_options_frame) {
	return gtk_toggle_button_get_active(record_options_frame->priv->record_button);
}
