/*
 * 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 "savuconfig_profile_page.h"
#include "savuconfig_buttons_frame.h"
#include "savuconfig_polling_rate_selector.h"
#include "savuconfig_cpi_selector.h"
#include "savuconfig_sensitivity_selector.h"
#include "savuconfig_light_frame.h"
#include "roccat_text_dialog.h"
#include "roccat_gamefile_selector.h"
#include "roccat_cpi_fixed_selector.h"
#include "gtk_roccat_helper.h"

#define SAVUCONFIG_PROFILE_PAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), SAVUCONFIG_PROFILE_PAGE_TYPE, SavuconfigProfilePageClass))
#define IS_SAVUCONFIG_PROFILE_PAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), SAVUCONFIG_PROFILE_PAGE_TYPE))
#define SAVUCONFIG_PROFILE_PAGE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), SAVUCONFIG_PROFILE_PAGE_TYPE, SavuconfigProfilePagePrivate))

typedef struct _SavuconfigProfilePageClass SavuconfigProfilePageClass;
typedef struct _SavuconfigProfilePagePrivate SavuconfigProfilePagePrivate;

struct _SavuconfigProfilePage {
	GtkVBox parent;
	SavuconfigProfilePagePrivate *priv;
};

struct _SavuconfigProfilePageClass {
	GtkVBoxClass parent_class;
	void (*load_from_file)(SavuconfigProfilePage *profile_page);
	void (*save_to_file)(SavuconfigProfilePage *profile_page);
	void (*save_to_device)(SavuconfigProfilePage *profile_page);
};

struct _SavuconfigProfilePagePrivate {
	guint index;
	SavuRmp *rmp;
	gchar *profile_name;
	GtkLabel *tab_label;
	SavuconfigButtonsFrame *buttons;
	RoccatGamefileSelector *gamefiles;
	SavuconfigPollingRateSelector *polling_rate;
	SavuconfigCpiSelector *cpi;
	SavuconfigSensitivitySelector *sensitivity;
	SavuconfigLightFrame *light;
};

G_DEFINE_TYPE(SavuconfigProfilePage, savuconfig_profile_page, GTK_TYPE_VBOX);

enum {
	LOAD_FROM_FILE,
	SAVE_TO_FILE,
	SAVE_TO_DEVICE,
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

GtkWidget *savuconfig_profile_page_new(guint profile_index) {
	SavuconfigProfilePage *profile_page;

	profile_page = SAVUCONFIG_PROFILE_PAGE(g_object_new(SAVUCONFIG_PROFILE_PAGE_TYPE,
			"homogeneous", FALSE,
			"spacing", 0,
			NULL));

	profile_page->priv->index = profile_index;

	return GTK_WIDGET(profile_page);
}

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

	priv->profile_name = savu_rmp_get_profile_name(priv->rmp);
	update_label_text(profile_page);

	savuconfig_buttons_frame_set_from_rmp(priv->buttons, priv->rmp);
	savuconfig_sensitivity_selector_set_from_rmp(priv->sensitivity, priv->rmp);
	savuconfig_cpi_selector_set_from_rmp(priv->cpi, priv->rmp);
	savuconfig_light_frame_set_from_rmp(priv->light, priv->rmp);
	savuconfig_polling_rate_selector_set_value(priv->polling_rate, savu_rmp_get_polling_rate(priv->rmp));

	for (i = 0; i < SAVU_GAMEFILE_NUM; ++i)
		roccat_gamefile_selector_set_text(priv->gamefiles, i, savu_rmp_get_game_file(priv->rmp, i));
}

static void update_rmp(SavuconfigProfilePage *profile_page, SavuRmp *rmp) {
	SavuconfigProfilePagePrivate *priv = profile_page->priv;
	guint i;

	savu_rmp_set_profile_name(rmp, priv->profile_name);

	savuconfig_buttons_frame_update_rmp(priv->buttons, rmp);
	savuconfig_sensitivity_selector_update_rmp(priv->sensitivity, rmp);
	savuconfig_cpi_selector_update_rmp(priv->cpi, rmp);
	savuconfig_light_frame_update_rmp(priv->light, rmp);
	savu_rmp_set_polling_rate(rmp, savuconfig_polling_rate_selector_get_value(priv->polling_rate));

	for (i = 0; i < SAVU_GAMEFILE_NUM; ++i)
		savu_rmp_set_game_file(rmp, i, roccat_gamefile_selector_get_text(priv->gamefiles, i));
}

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

static void rename_cb(GtkButton *button, gpointer user_data) {
	SavuconfigProfilePage *profile_page = SAVUCONFIG_PROFILE_PAGE(user_data);
	SavuconfigProfilePagePrivate *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_rmp(SAVUCONFIG_PROFILE_PAGE(user_data));
}

static GtkWidget *buttons_new(SavuconfigProfilePage *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 mouse");
	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 savuconfig_profile_page_init(SavuconfigProfilePage *profile_page) {
	SavuconfigProfilePagePrivate *priv = SAVUCONFIG_PROFILE_PAGE_GET_PRIVATE(profile_page);
	GtkWidget *content, *buttons;
	GtkWidget *left;
	GtkWidget *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 = SAVUCONFIG_BUTTONS_FRAME(savuconfig_buttons_frame_new());
	priv->cpi = SAVUCONFIG_CPI_SELECTOR(savuconfig_cpi_selector_new());
	priv->gamefiles = ROCCAT_GAMEFILE_SELECTOR(roccat_gamefile_selector_new(SAVU_GAMEFILE_NUM));
	priv->polling_rate = SAVUCONFIG_POLLING_RATE_SELECTOR(savuconfig_polling_rate_selector_new());
	priv->sensitivity = SAVUCONFIG_SENSITIVITY_SELECTOR(savuconfig_sensitivity_selector_new());
	priv->light = SAVUCONFIG_LIGHT_FRAME(savuconfig_light_frame_new());

	gtk_box_pack_start(GTK_BOX(left), GTK_WIDGET(priv->light), TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(left), GTK_WIDGET(priv->cpi), TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(left), GTK_WIDGET(priv->polling_rate), TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(right), GTK_WIDGET(priv->buttons), TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(right), GTK_WIDGET(priv->sensitivity), TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(right), GTK_WIDGET(priv->gamefiles), TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(content), left, TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(content), right, TRUE, TRUE, 0);
	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) {
	SavuconfigProfilePagePrivate *priv = SAVUCONFIG_PROFILE_PAGE(object)->priv;
	G_OBJECT_CLASS(savuconfig_profile_page_parent_class)->finalize(object);
	g_free(priv->profile_name);
}

static void savuconfig_profile_page_class_init(SavuconfigProfilePageClass *klass) {
	GObjectClass *gobject_class;

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

	g_type_class_add_private(klass, sizeof(SavuconfigProfilePagePrivate));

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

void savuconfig_profile_page_set_rmp(SavuconfigProfilePage *profile_page, SavuRmp *rmp) {
	SavuconfigProfilePagePrivate *priv = profile_page->priv;
	if (priv->rmp)
		savu_rmp_free(priv->rmp);
	priv->rmp = savu_rmp_dup(rmp);
	set_from_rmp(profile_page);
}

SavuRmp *savuconfig_profile_page_get_rmp(SavuconfigProfilePage *profile_page) {
	SavuconfigProfilePagePrivate *priv = profile_page->priv;
	SavuRmp *rmp;
	rmp = savu_rmp_dup(priv->rmp);
	update_rmp(profile_page, rmp);
	return rmp;
}

guint savuconfig_profile_page_get_index(SavuconfigProfilePage *profile_page) {
	return profile_page->priv->index;
}

GtkWidget *savuconfig_profile_page_get_tab_label(SavuconfigProfilePage *profile_page) {
	return GTK_WIDGET(profile_page->priv->tab_label);
}
