/*
 * 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 "arvoconfig_profile_page.h"
#include "arvoconfig_buttons_frame.h"
#include "roccat_gamefile_selector.h"
#include "arvoconfig_mode_selector.h"
#include "arvoconfig_key_mask_selector.h"
#include "roccat_text_dialog.h"
#include "gtk_roccat_helper.h"

#define ARVOCONFIG_PROFILE_PAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), ARVOCONFIG_PROFILE_PAGE_TYPE, ArvoconfigProfilePageClass))
#define IS_ARVOCONFIG_PROFILE_PAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), ARVOCONFIG_PROFILE_PAGE_TYPE))
#define ARVOCONFIG_PROFILE_PAGE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), ARVOCONFIG_PROFILE_PAGE_TYPE, ArvoconfigProfilePagePrivate))

typedef struct _ArvoconfigProfilePageClass ArvoconfigProfilePageClass;
typedef struct _ArvoconfigProfilePagePrivate ArvoconfigProfilePagePrivate;

struct _ArvoconfigProfilePage {
	GtkVBox parent;
	ArvoconfigProfilePagePrivate *priv;
};

struct _ArvoconfigProfilePageClass {
	GtkVBoxClass parent_class;
	void (*load_from_file)(ArvoconfigProfilePage *profile_page);
	void (*save_to_file)(ArvoconfigProfilePage *profile_page);
	void (*save_to_device)(ArvoconfigProfilePage *profile_page);
};

struct _ArvoconfigProfilePagePrivate {
	guint index;
	ArvoRkp *rkp;
	gchar *profile_name;
	GtkLabel *tab_label;
	ArvoconfigButtonsFrame *buttons;
	RoccatGamefileSelector *gamefiles;
	ArvoconfigModeSelector *mode;
	ArvoconfigKeyMaskSelector *key_mask;
};

G_DEFINE_TYPE(ArvoconfigProfilePage, arvoconfig_profile_page, GTK_TYPE_VBOX);

enum {
	LOAD_FROM_FILE,
	SAVE_TO_FILE,
	SAVE_TO_DEVICE,
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

GtkWidget *arvoconfig_profile_page_new(guint profile_index) {
	ArvoconfigProfilePage *profile_page;

	profile_page = ARVOCONFIG_PROFILE_PAGE(g_object_new(ARVOCONFIG_PROFILE_PAGE_TYPE,
			"homogeneous", FALSE,
			"spacing", 0,
			NULL));

	profile_page->priv->index = profile_index;

	return GTK_WIDGET(profile_page);
}

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

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

	arvoconfig_buttons_frame_set_from_rkp(priv->buttons, priv->rkp);
	arvoconfig_key_mask_selector_set_from_rkp(priv->key_mask, priv->rkp);
	arvoconfig_mode_selector_set_from_rkp(priv->mode, priv->rkp);

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

static void update_rkp(ArvoconfigProfilePage *profile_page, ArvoRkp *rkp) {
	ArvoconfigProfilePagePrivate *priv = profile_page->priv;
	guint i;

	arvo_rkp_set_profile_name(rkp, priv->profile_name);
	arvoconfig_buttons_frame_update_rkp(priv->buttons, rkp);
	arvoconfig_key_mask_selector_update_rkp(priv->key_mask, rkp);
	arvoconfig_mode_selector_update_rkp(priv->mode, rkp);

	for (i = 0; i < ARVO_GAMEFILE_NUM; ++i)
		arvo_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) {
	ArvoconfigProfilePage *profile_page = ARVOCONFIG_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) {
	ArvoconfigProfilePage *profile_page = ARVOCONFIG_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) {
	ArvoconfigProfilePage *profile_page = ARVOCONFIG_PROFILE_PAGE(user_data);
	g_signal_emit((gpointer)profile_page, signals[SAVE_TO_DEVICE], 0);
}

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

static GtkWidget *buttons_new(ArvoconfigProfilePage *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 arvoconfig_profile_page_init(ArvoconfigProfilePage *profile_page) {
	ArvoconfigProfilePagePrivate *priv = ARVOCONFIG_PROFILE_PAGE_GET_PRIVATE(profile_page);
	GtkWidget *content, *buttons, *left, *right;

	profile_page->priv = priv;

	content = gtk_hbox_new(FALSE, 0);
	buttons = buttons_new(profile_page);

	left = gtk_vbox_new(FALSE, 0);
	right = gtk_vbox_new(FALSE, 0);

	priv->buttons = ARVOCONFIG_BUTTONS_FRAME(arvoconfig_buttons_frame_new());
	priv->gamefiles = ROCCAT_GAMEFILE_SELECTOR(roccat_gamefile_selector_new(ARVO_GAMEFILE_NUM));
	priv->mode = ARVOCONFIG_MODE_SELECTOR(arvoconfig_mode_selector_new());
	priv->key_mask = ARVOCONFIG_KEY_MASK_SELECTOR(arvoconfig_key_mask_selector_new());

	gtk_box_pack_start(GTK_BOX(left), GTK_WIDGET(priv->buttons), TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(left), GTK_WIDGET(priv->gamefiles), TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(right), GTK_WIDGET(priv->mode), TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(right), GTK_WIDGET(priv->key_mask), TRUE, TRUE, 0);

	gtk_container_add(GTK_CONTAINER(content), left);
	gtk_container_add(GTK_CONTAINER(content), right);

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

	gtk_widget_show_all(content);
	gtk_widget_show_all(buttons);

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

static void finalize(GObject *object) {
	ArvoconfigProfilePagePrivate *priv = ARVOCONFIG_PROFILE_PAGE(object)->priv;
	G_OBJECT_CLASS(arvoconfig_profile_page_parent_class)->finalize(object);
	g_free(priv->profile_name);
}

static void arvoconfig_profile_page_class_init(ArvoconfigProfilePageClass *klass) {
	GObjectClass *gobject_class;

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

	g_type_class_add_private(klass, sizeof(ArvoconfigProfilePagePrivate));

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

void arvoconfig_profile_page_set_rkp(ArvoconfigProfilePage *profile_page, ArvoRkp *rkp) {
	ArvoconfigProfilePagePrivate *priv = profile_page->priv;
	if (priv->rkp)
		arvo_rkp_free(priv->rkp);
	priv->rkp = arvo_rkp_dup(rkp);
	set_from_rkp(profile_page);
}

ArvoRkp *arvoconfig_profile_page_get_rkp(ArvoconfigProfilePage *profile_page) {
	ArvoconfigProfilePagePrivate *priv = profile_page->priv;
	ArvoRkp *rkp;
	rkp = arvo_rkp_dup(priv->rkp);
	update_rkp(profile_page, rkp);
	return rkp;
}

guint arvoconfig_profile_page_get_index(ArvoconfigProfilePage *profile_page) {
	return profile_page->priv->index;
}

GtkWidget *arvoconfig_profile_page_get_tab_label(ArvoconfigProfilePage *profile_page) {
	return GTK_WIDGET(profile_page->priv->tab_label);
}
