/*
 * 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 "iskuconfig_profile_page.h"
#include "isku_capslock_frame.h"
#include "isku_easyzone_frame.h"
#include "isku_function_frame.h"
#include "isku_macro_frame.h"
#include "isku_media_frame.h"
#include "isku_thumbster_frame.h"
#include "isku_key_mask_selector.h"
#include "roccat_light_selector.h"
#include "roccat_gamefile_selector.h"
#include "roccat_text_dialog.h"
#include "gtk_roccat_helper.h"

#define ISKUCONFIG_PROFILE_PAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), ISKUCONFIG_PROFILE_PAGE_TYPE, IskuconfigProfilePageClass))
#define IS_ISKUCONFIG_PROFILE_PAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), ISKUCONFIG_PROFILE_PAGE_TYPE))
#define ISKUCONFIG_PROFILE_PAGE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), ISKUCONFIG_PROFILE_PAGE_TYPE, IskuconfigProfilePagePrivate))

typedef struct _IskuconfigProfilePageClass IskuconfigProfilePageClass;
typedef struct _IskuconfigProfilePagePrivate IskuconfigProfilePagePrivate;

struct _IskuconfigProfilePage {
	GtkVBox parent;
	IskuconfigProfilePagePrivate *priv;
};

struct _IskuconfigProfilePageClass {
	GtkVBoxClass parent_class;
	void (*load_from_file)(IskuconfigProfilePage *profile_page);
	void (*save_to_file)(IskuconfigProfilePage *profile_page);
	void (*save_to_device)(IskuconfigProfilePage *profile_page);
};

struct _IskuconfigProfilePagePrivate {
	guint index;
	IskuRkp *rkp;
	gchar *profile_name;
	GtkLabel *tab_label;
	IskuCapslockFrame *capslock;
	IskuEasyzoneFrame *easyzone;
	IskuFunctionFrame *function;
	IskuMacroFrame *macro;
	IskuMediaFrame *media;
	IskuThumbsterFrame *thumbster;
	RoccatGamefileSelector *gamefiles;
	IskuKeyMaskSelector *key_mask;
	RoccatLightSelector *light;
};

G_DEFINE_TYPE(IskuconfigProfilePage, iskuconfig_profile_page, GTK_TYPE_VBOX);

enum {
	LOAD_FROM_FILE,
	SAVE_TO_FILE,
	SAVE_TO_DEVICE,
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

void iskuconfig_profile_page_update_brightness(IskuconfigProfilePage *profile_page, guint new_brightness) {
	IskuconfigProfilePagePrivate *priv = profile_page->priv;
	roccat_light_selector_set_brightness(priv->light, new_brightness);
	isku_rkp_set_light_brightness_without_modified(priv->rkp, new_brightness);
}

void iskuconfig_profile_page_update_key(IskuconfigProfilePage *profile_page, guint key_index, IskuRkpMacroKeyInfo *key_info) {
	IskuconfigProfilePagePrivate *priv = profile_page->priv;

	isku_capslock_frame_set_key(priv->capslock, key_index, key_info);
	isku_easyzone_frame_set_key(priv->easyzone, key_index, key_info);
	isku_function_frame_set_key(priv->function, key_index, key_info);
	isku_macro_frame_set_key(priv->macro, key_index, key_info);
	isku_media_frame_set_key(priv->media, key_index, key_info);
	isku_thumbster_frame_set_key(priv->thumbster, key_index, key_info);

	isku_rkp_set_macro_key_info_without_modified(priv->rkp, key_index, key_info);
}

GtkWidget *iskuconfig_profile_page_new(guint profile_index) {
	IskuconfigProfilePage *profile_page;

	profile_page = ISKUCONFIG_PROFILE_PAGE(g_object_new(ISKUCONFIG_PROFILE_PAGE_TYPE,
			"homogeneous", FALSE,
			"spacing", 0,
			NULL));

	profile_page->priv->index = profile_index;

	return GTK_WIDGET(profile_page);
}

static void update_label_text(IskuconfigProfilePage *profile_page) {
	IskuconfigProfilePagePrivate *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(IskuconfigProfilePage *profile_page) {
	IskuconfigProfilePagePrivate *priv = profile_page->priv;
	guint i;

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

	isku_capslock_frame_set_from_rkp(priv->capslock, priv->rkp);
	isku_easyzone_frame_set_from_rkp(priv->easyzone, priv->rkp);
	isku_function_frame_set_from_rkp(priv->function, priv->rkp);
	isku_macro_frame_set_from_rkp(priv->macro, priv->rkp);
	isku_media_frame_set_from_rkp(priv->media, priv->rkp);
	isku_thumbster_frame_set_from_rkp(priv->thumbster, priv->rkp);
	isku_key_mask_selector_set_from_rkp(priv->key_mask, priv->rkp);

	roccat_light_selector_set_brightness(priv->light, isku_rkp_get_light_brightness(priv->rkp));
	roccat_light_selector_set_dimness(priv->light, isku_rkp_get_light_dimness(priv->rkp));
	roccat_light_selector_set_timeout(priv->light, isku_rkp_get_light_timeout(priv->rkp));

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

static void update_rkp(IskuconfigProfilePage *profile_page, IskuRkp *rkp) {
	IskuconfigProfilePagePrivate *priv = profile_page->priv;
	guint i;

	isku_rkp_set_profile_name(rkp, priv->profile_name);
	isku_capslock_frame_update_rkp(priv->capslock, rkp);
	isku_easyzone_frame_update_rkp(priv->easyzone, rkp);
	isku_function_frame_update_rkp(priv->function, rkp);
	isku_macro_frame_update_rkp(priv->macro, rkp);
	isku_media_frame_update_rkp(priv->media, rkp);
	isku_thumbster_frame_update_rkp(priv->thumbster, rkp);
	isku_key_mask_selector_update_rkp(priv->key_mask, rkp);

	isku_rkp_set_light_brightness(rkp, roccat_light_selector_get_brightness(priv->light));
	isku_rkp_set_light_dimness(rkp, roccat_light_selector_get_dimness(priv->light));
	isku_rkp_set_light_timeout(rkp, roccat_light_selector_get_timeout(priv->light));

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

static void load_from_file_cb(GtkButton *button, gpointer user_data) {
	IskuconfigProfilePage *profile_page = ISKUCONFIG_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) {
	IskuconfigProfilePage *profile_page = ISKUCONFIG_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) {
	IskuconfigProfilePage *profile_page = ISKUCONFIG_PROFILE_PAGE(user_data);
	g_signal_emit((gpointer)profile_page, signals[SAVE_TO_DEVICE], 0);
}

static void rename_cb(GtkButton *button, gpointer user_data) {
	IskuconfigProfilePage *profile_page = ISKUCONFIG_PROFILE_PAGE(user_data);
	IskuconfigProfilePagePrivate *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(ISKUCONFIG_PROFILE_PAGE(user_data));
}

static GtkWidget *buttons_new(IskuconfigProfilePage *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 iskuconfig_profile_page_init(IskuconfigProfilePage *profile_page) {
	IskuconfigProfilePagePrivate *priv = ISKUCONFIG_PROFILE_PAGE_GET_PRIVATE(profile_page);
	GtkWidget *buttons;
	GtkWidget *notebook;
	GtkWidget *page1;
	GtkWidget *page2;
	GtkWidget *page3;
	GtkWidget *hbox;
	GtkWidget *vbox;
	GtkWidget *light_frame;

	profile_page->priv = priv;

	buttons = buttons_new(profile_page);
	notebook = gtk_notebook_new();
	hbox = gtk_hbox_new(FALSE, 0);
	vbox = gtk_vbox_new(FALSE, 0);
	page1 = gtk_vbox_new(FALSE, 0);
	page2 = gtk_vbox_new(FALSE, 0);
	page3 = gtk_hbox_new(FALSE, 0);
	light_frame = gtk_frame_new("Light");
	priv->easyzone = ISKU_EASYZONE_FRAME(isku_easyzone_frame_new());
	priv->function = ISKU_FUNCTION_FRAME(isku_function_frame_new());
	priv->media = ISKU_MEDIA_FRAME(isku_media_frame_new());
	priv->capslock = ISKU_CAPSLOCK_FRAME(isku_capslock_frame_new());
	priv->macro = ISKU_MACRO_FRAME(isku_macro_frame_new());
	priv->thumbster = ISKU_THUMBSTER_FRAME(isku_thumbster_frame_new());
	priv->gamefiles = ROCCAT_GAMEFILE_SELECTOR(roccat_gamefile_selector_new(ISKU_GAMEFILE_NUM));
	priv->key_mask = ISKU_KEY_MASK_SELECTOR(isku_key_mask_selector_new());
	priv->light = ROCCAT_LIGHT_SELECTOR(roccat_light_selector_new_with_range(ISKU_LIGHT_BRIGHTNESS_MIN, ISKU_LIGHT_BRIGHTNESS_MAX, 1));

	gtk_container_add(GTK_CONTAINER(light_frame), GTK_WIDGET(priv->light));

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

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

	gtk_box_pack_start(GTK_BOX(page1), hbox, TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(page1), GTK_WIDGET(priv->macro), TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(page1), GTK_WIDGET(priv->thumbster), TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(page1), GTK_WIDGET(priv->gamefiles), TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(page2), GTK_WIDGET(priv->easyzone), TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(page3), GTK_WIDGET(priv->function), TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(page3), GTK_WIDGET(priv->media), TRUE, TRUE, 0);

	gtk_notebook_append_page(GTK_NOTEBOOK(notebook), page1, gtk_label_new("Main"));
	gtk_notebook_append_page(GTK_NOTEBOOK(notebook), page2, gtk_label_new("Easyzone"));
	gtk_notebook_append_page(GTK_NOTEBOOK(notebook), page3, gtk_label_new("Media/F keys"));

	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) {
	IskuconfigProfilePagePrivate *priv = ISKUCONFIG_PROFILE_PAGE(object)->priv;
	G_OBJECT_CLASS(iskuconfig_profile_page_parent_class)->finalize(object);
	g_free(priv->profile_name);
}

static void iskuconfig_profile_page_class_init(IskuconfigProfilePageClass *klass) {
	GObjectClass *gobject_class;

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

	g_type_class_add_private(klass, sizeof(IskuconfigProfilePagePrivate));

	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(IskuconfigProfilePageClass, 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(IskuconfigProfilePageClass, 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(IskuconfigProfilePageClass, save_to_device),
			NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
}

void iskuconfig_profile_page_set_rkp(IskuconfigProfilePage *profile_page, IskuRkp *rkp) {
	IskuconfigProfilePagePrivate *priv = profile_page->priv;
	if (priv->rkp)
		isku_rkp_free(priv->rkp);
	priv->rkp = isku_rkp_dup(rkp);
	set_from_rkp(profile_page);
}

IskuRkp *iskuconfig_profile_page_get_rkp(IskuconfigProfilePage *profile_page) {
	IskuconfigProfilePagePrivate *priv = profile_page->priv;
	IskuRkp *rkp;
	rkp = isku_rkp_dup(priv->rkp);
	update_rkp(profile_page, rkp);
	return rkp;
}

guint iskuconfig_profile_page_get_index(IskuconfigProfilePage *profile_page) {
	return profile_page->priv->index;
}

GtkWidget *iskuconfig_profile_page_get_tab_label(IskuconfigProfilePage *profile_page) {
	return GTK_WIDGET(profile_page->priv->tab_label);
}
