/*
 * 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 "ryosconfig_profile_page.h"
#include "roccat_gamefile_selector.h"
#include "roccat_text_dialog.h"
#include "ryosconfig_light_frame.h"
#include "gtk_roccat_helper.h"
#include "ryosconfig_keyboard_selector.h"
#include "ryosconfig_led_feedback_frame.h"
#include "ryosconfig_key_mask_selector.h"

#define RYOSCONFIG_PROFILE_PAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), RYOSCONFIG_PROFILE_PAGE_TYPE, RyosconfigProfilePageClass))
#define IS_RYOSCONFIG_PROFILE_PAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), RYOSCONFIG_PROFILE_PAGE_TYPE))
#define RYOSCONFIG_PROFILE_PAGE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), RYOSCONFIG_PROFILE_PAGE_TYPE, RyosconfigProfilePagePrivate))

typedef struct _RyosconfigProfilePageClass RyosconfigProfilePageClass;
typedef struct _RyosconfigProfilePagePrivate RyosconfigProfilePagePrivate;

struct _RyosconfigProfilePage {
	GtkVBox parent;
	RyosconfigProfilePagePrivate *priv;
};

struct _RyosconfigProfilePageClass {
	GtkVBoxClass parent_class;
	void (*load_from_file)(RyosconfigProfilePage *profile_page);
	void (*save_to_file)(RyosconfigProfilePage *profile_page);
	void (*save_to_device)(RyosconfigProfilePage *profile_page);
};

struct _RyosconfigProfilePagePrivate {
	guint index;
	RyosRkp *rkp;
	gchar *profile_name;
	GtkLabel *tab_label;
	GtkBox *misc_box;
	RyosconfigLightFrame *light_frame;
	RyosconfigKeyboardSelector *keyboard_selector;
	RoccatGamefileSelector *gamefiles;
	RyosconfigLedFeedbackFrame *feedback_frame;
	RyosconfigKeyMaskSelector *key_mask_selector;
};

G_DEFINE_TYPE(RyosconfigProfilePage, ryosconfig_profile_page, GTK_TYPE_VBOX);

enum {
	LOAD_FROM_FILE,
	SAVE_TO_FILE,
	SAVE_TO_DEVICE,
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

void ryosconfig_profile_page_set_device(RyosconfigProfilePage *profile_page, RoccatDevice const *device) {
	RyosconfigProfilePagePrivate *priv = profile_page->priv;
	ryosconfig_keyboard_selector_set_device_type(priv->keyboard_selector, device);
	ryosconfig_light_frame_set_device_type(priv->light_frame, device);
	ryosconfig_led_feedback_frame_set_device_type(priv->feedback_frame, device);
}

void ryosconfig_profile_page_update_brightness(RyosconfigProfilePage *profile_page, guint new_brightness) {
	RyosconfigProfilePagePrivate *priv = profile_page->priv;
	ryos_rkp_set_light_brightness_without_modified(priv->rkp, new_brightness);
	ryosconfig_light_frame_set_brightness(priv->light_frame, new_brightness);
}

void ryosconfig_profile_page_update_key(RyosconfigProfilePage *profile_page, guint macro_index, RyosMacro *macro) {
	RyosconfigProfilePagePrivate *priv = profile_page->priv;

	ryos_rkp_set_macro_without_modified(priv->rkp, macro_index, macro);
	ryos_rkp_set_key_to_macro_without_modified(priv->rkp, macro_index);

	ryosconfig_keyboard_selector_set_macro(priv->keyboard_selector, macro_index, macro);
}

GtkWidget *ryosconfig_profile_page_new(guint profile_index) {
	RyosconfigProfilePage *profile_page;

	profile_page = RYOSCONFIG_PROFILE_PAGE(g_object_new(RYOSCONFIG_PROFILE_PAGE_TYPE,
			"homogeneous", FALSE,
			"spacing", 0,
			NULL));

	profile_page->priv->index = profile_index;

	return GTK_WIDGET(profile_page);
}

static void update_label_text(RyosconfigProfilePage *profile_page) {
	RyosconfigProfilePagePrivate *priv = profile_page->priv;
	gchar *tab_title;
	tab_title = g_strdup_printf("Profile %u: %s", priv->index + 1, priv->profile_name);
	gtk_label_set_text(priv->tab_label, tab_title);
	g_free(tab_title);
}

static void set_from_rkp(RyosconfigProfilePage *profile_page) {
	RyosconfigProfilePagePrivate *priv = profile_page->priv;
	guint i;

	priv->profile_name = ryos_rkp_get_profile_name(priv->rkp);
	update_label_text(profile_page);

	ryosconfig_light_frame_set_from_rkp(priv->light_frame, priv->rkp);
	ryosconfig_led_feedback_frame_set_from_rkp(priv->feedback_frame, priv->rkp);
	ryosconfig_keyboard_selector_set_from_rkp(priv->keyboard_selector, priv->rkp);
	ryosconfig_key_mask_selector_set_from_rkp(priv->key_mask_selector, priv->rkp);

	for (i = 0; i < RYOS_RKP_PROFILE_GAMEFILE_NUM; ++i)
		roccat_gamefile_selector_set_text(priv->gamefiles, i, ryos_rkp_get_gamefile_name(priv->rkp, i));
}

static void update_rkp(RyosconfigProfilePage *profile_page, RyosRkp *rkp) {
	RyosconfigProfilePagePrivate *priv = profile_page->priv;
	guint i;

	ryos_rkp_set_profile_name(rkp, priv->profile_name);

	ryosconfig_light_frame_update_rkp(priv->light_frame, rkp);
	ryosconfig_led_feedback_frame_update_rkp(priv->feedback_frame, rkp);
	ryosconfig_keyboard_selector_update_rkp(priv->keyboard_selector, rkp);
	ryosconfig_key_mask_selector_update_rkp(priv->key_mask_selector, rkp);

	for (i = 0; i < RYOS_RKP_PROFILE_GAMEFILE_NUM; ++i)
		ryos_rkp_set_gamefile_name(rkp, i, roccat_gamefile_selector_get_text(priv->gamefiles, i));
}

static void load_from_file_cb(GtkButton *button, gpointer user_data) {
	RyosconfigProfilePage *profile_page = RYOSCONFIG_PROFILE_PAGE(user_data);
	g_signal_emit((gpointer)profile_page, signals[LOAD_FROM_FILE], 0);
}

static void save_to_file_cb(GtkButton *button, gpointer user_data) {
	RyosconfigProfilePage *profile_page = RYOSCONFIG_PROFILE_PAGE(user_data);
	g_signal_emit((gpointer)profile_page, signals[SAVE_TO_FILE], 0);
}

static void save_to_device_cb(GtkButton *button, gpointer user_data) {
	RyosconfigProfilePage *profile_page = RYOSCONFIG_PROFILE_PAGE(user_data);
	g_signal_emit((gpointer)profile_page, signals[SAVE_TO_DEVICE], 0);
}

static void rename_cb(GtkButton *button, gpointer user_data) {
	RyosconfigProfilePage *profile_page = RYOSCONFIG_PROFILE_PAGE(user_data);
	RyosconfigProfilePagePrivate *priv = profile_page->priv;
	gchar *new_name;

	new_name = roccat_text_dialog(gtk_roccat_widget_get_gtk_window(GTK_WIDGET(button)), "Enter new name", "Please enter new name", priv->profile_name);
	if (new_name) {
		/* skipping equality test and emitting every time */
		g_free(priv->profile_name);
		priv->profile_name = new_name;

		update_label_text(profile_page);
	}
}

static void reset_cb(GtkButton *button, gpointer user_data) {
	set_from_rkp(RYOSCONFIG_PROFILE_PAGE(user_data));
}

static GtkWidget *buttons_new(RyosconfigProfilePage *profile_page) {
	GtkWidget *hbox, *button;
	hbox = gtk_hbox_new(FALSE, 0);

	button = gtk_button_new_with_label("Read file");
	g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(load_from_file_cb), profile_page);
	gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);

	button = gtk_button_new_with_label("Save file");
	g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(save_to_file_cb), profile_page);
	gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);

	button = gtk_button_new_with_label("Store in keyboard");
	g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(save_to_device_cb), profile_page);
	gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);

	button = gtk_button_new_with_label("Rename");
	g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(rename_cb), profile_page);
	gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);

	button = gtk_button_new_with_label("Reset");
	g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(reset_cb), profile_page);
	gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);

	return hbox;
}

static void append_keys_page(RyosconfigProfilePage *profile_page, GtkNotebook *notebook) {
	RyosconfigProfilePagePrivate *priv = profile_page->priv;
	GtkWidget *vbox;

	vbox = gtk_vbox_new(FALSE, 0);
	priv->keyboard_selector = RYOSCONFIG_KEYBOARD_SELECTOR(ryosconfig_keyboard_selector_new());

	gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(priv->keyboard_selector), TRUE, TRUE, 0);
	gtk_notebook_append_page(notebook, vbox, gtk_label_new("Keys"));
}

static void append_misc_page(RyosconfigProfilePage *profile_page, GtkNotebook *notebook) {
	RyosconfigProfilePagePrivate *priv = profile_page->priv;
	GtkWidget *hbox;
	GtkWidget *vbox;

	hbox = gtk_hbox_new(FALSE, 0);
	vbox = gtk_vbox_new(FALSE, 0);
	priv->misc_box = GTK_BOX(gtk_vbox_new(FALSE, 0));
	priv->light_frame = RYOSCONFIG_LIGHT_FRAME(ryosconfig_light_frame_new());
	priv->feedback_frame = RYOSCONFIG_LED_FEEDBACK_FRAME(ryosconfig_led_feedback_frame_new());
	priv->gamefiles = ROCCAT_GAMEFILE_SELECTOR(roccat_gamefile_selector_new(RYOS_RKP_PROFILE_GAMEFILE_NUM));
	priv->key_mask_selector = RYOSCONFIG_KEY_MASK_SELECTOR(ryosconfig_key_mask_selector_new());

	gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(priv->light_frame), TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(priv->feedback_frame), TRUE, TRUE, 0);

	gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(priv->key_mask_selector), TRUE, TRUE, 0);

	gtk_box_pack_start(priv->misc_box, hbox, TRUE, TRUE, 0);
	gtk_box_pack_start(priv->misc_box, GTK_WIDGET(priv->gamefiles), TRUE, TRUE, 0);

	gtk_notebook_append_page(notebook, GTK_WIDGET(priv->misc_box), gtk_label_new("Misc"));
}

static void ryosconfig_profile_page_init(RyosconfigProfilePage *profile_page) {
	RyosconfigProfilePagePrivate *priv = RYOSCONFIG_PROFILE_PAGE_GET_PRIVATE(profile_page);
	GtkWidget *buttons;
	GtkWidget *notebook;

	profile_page->priv = priv;

	priv->index = 0;
	priv->profile_name = NULL;
	priv->rkp = NULL;

	buttons = buttons_new(profile_page);
	notebook = gtk_notebook_new();

	append_keys_page(profile_page, GTK_NOTEBOOK(notebook));
	append_misc_page(profile_page, GTK_NOTEBOOK(notebook));

	gtk_box_pack_start(GTK_BOX(profile_page), notebook, TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(profile_page), buttons, FALSE, FALSE, 0);

	gtk_widget_show_all(notebook);
	gtk_widget_show_all(buttons);

	priv->tab_label = GTK_LABEL(gtk_label_new(NULL));
}

static void finalize(GObject *object) {
	RyosconfigProfilePagePrivate *priv = RYOSCONFIG_PROFILE_PAGE(object)->priv;
	G_OBJECT_CLASS(ryosconfig_profile_page_parent_class)->finalize(object);
	g_free(priv->profile_name);
}

static void ryosconfig_profile_page_class_init(RyosconfigProfilePageClass *klass) {
	GObjectClass *gobject_class;

	gobject_class = (GObjectClass*)klass;
	gobject_class->finalize = finalize;

	g_type_class_add_private(klass, sizeof(RyosconfigProfilePagePrivate));

	signals[LOAD_FROM_FILE] = g_signal_new("load-from-file",
			G_TYPE_FROM_CLASS(klass),
			G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
			G_STRUCT_OFFSET(RyosconfigProfilePageClass, load_from_file),
			NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);

	signals[SAVE_TO_FILE] = g_signal_new("save-to-file",
			G_TYPE_FROM_CLASS(klass),
			G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
			G_STRUCT_OFFSET(RyosconfigProfilePageClass, save_to_file),
			NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);

	signals[SAVE_TO_DEVICE] = g_signal_new("save-to-device",
			G_TYPE_FROM_CLASS(klass),
			G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
			G_STRUCT_OFFSET(RyosconfigProfilePageClass, save_to_device),
			NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
}

void ryosconfig_profile_page_set_rkp(RyosconfigProfilePage *profile_page, RyosRkp const *rkp) {
	RyosconfigProfilePagePrivate *priv = profile_page->priv;
	if (priv->rkp)
		ryos_rkp_free(priv->rkp);
	priv->rkp = ryos_rkp_dup(rkp);
	set_from_rkp(profile_page);
}

RyosRkp *ryosconfig_profile_page_get_rkp(RyosconfigProfilePage *profile_page) {
	RyosconfigProfilePagePrivate *priv = profile_page->priv;
	RyosRkp *rkp;
	rkp = ryos_rkp_dup(priv->rkp);
	update_rkp(profile_page, rkp);
	return rkp;
}

guint ryosconfig_profile_page_get_index(RyosconfigProfilePage *profile_page) {
	return profile_page->priv->index;
}

GtkWidget *ryosconfig_profile_page_get_tab_label(RyosconfigProfilePage *profile_page) {
	return GTK_WIDGET(profile_page->priv->tab_label);
}
