/*
 * 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 "koneplusconfig_profile_page.h"
#include "koneplus_buttons_frame.h"
#include "koneplus_polling_rate_selector.h"
#include "koneplus_sensitivity_selector.h"
#include "koneplus_light_effects_frame.h"
#include "koneplus_color_frame.h"
#include "koneplus_cpi_selector.h"
#include "roccat_gamefile_selector.h"
#include "roccat_text_dialog.h"
#include "gtk_roccat_helper.h"

#define KONEPLUSCONFIG_PROFILE_PAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), KONEPLUSCONFIG_PROFILE_PAGE_TYPE, KoneplusconfigProfilePageClass))
#define IS_KONEPLUSCONFIG_PROFILE_PAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), KONEPLUSCONFIG_PROFILE_PAGE_TYPE))
#define KONEPLUSCONFIG_PROFILE_PAGE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), KONEPLUSCONFIG_PROFILE_PAGE_TYPE, KoneplusconfigProfilePagePrivate))

typedef struct _KoneplusconfigProfilePageClass KoneplusconfigProfilePageClass;
typedef struct _KoneplusconfigProfilePagePrivate KoneplusconfigProfilePagePrivate;

struct _KoneplusconfigProfilePage {
	GtkVBox parent;
	KoneplusconfigProfilePagePrivate *priv;
};

struct _KoneplusconfigProfilePageClass {
	GtkVBoxClass parent_class;
	void (*load_from_file)(KoneplusconfigProfilePage *profile_page);
	void (*save_to_file)(KoneplusconfigProfilePage *profile_page);
	void (*save_to_device)(KoneplusconfigProfilePage *profile_page);
};

struct _KoneplusconfigProfilePagePrivate {
	guint index;
	KoneplusRmp *rmp;
	gchar *profile_name;
	GtkLabel *tab_label;
	KoneplusButtonsFrame *buttons;
	RoccatGamefileSelector *gamefiles;
	KoneplusPollingRateSelector *polling_rate;
	KoneplusCpiSelector *cpi;
	KoneplusSensitivitySelector *sensitivity;
	KoneplusLightEffectsFrame *light_effects;
	KoneplusColorFrame *colors;
};

G_DEFINE_TYPE(KoneplusconfigProfilePage, koneplusconfig_profile_page, GTK_TYPE_VBOX);

enum {
	LOAD_FROM_FILE,
	SAVE_TO_FILE,
	SAVE_TO_DEVICE,
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

GtkWidget *koneplusconfig_profile_page_new(guint profile_index) {
	KoneplusconfigProfilePage *profile_page;

	profile_page = KONEPLUSCONFIG_PROFILE_PAGE(g_object_new(KONEPLUSCONFIG_PROFILE_PAGE_TYPE,
			"homogeneous", FALSE,
			"spacing", 0,
			NULL));

	profile_page->priv->index = profile_index;

	return GTK_WIDGET(profile_page);
}

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

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

	koneplus_buttons_frame_set_from_rmp(priv->buttons, priv->rmp);
	koneplus_sensitivity_selector_set_from_rmp(priv->sensitivity, priv->rmp);
	koneplus_light_effects_frame_set_from_rmp(priv->light_effects, priv->rmp);
	koneplus_color_frame_set_from_rmp(priv->colors, priv->rmp);
	koneplus_cpi_selector_set_from_rmp(priv->cpi, priv->rmp);
	koneplus_polling_rate_selector_set_value(priv->polling_rate, koneplus_rmp_get_polling_rate(priv->rmp));

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

static void update_rmp(KoneplusconfigProfilePage *profile_page, KoneplusRmp *rmp) {
	KoneplusconfigProfilePagePrivate *priv = profile_page->priv;
	guint i;

	koneplus_rmp_set_profile_name(rmp, priv->profile_name);

	koneplus_buttons_frame_update_rmp(priv->buttons, rmp);
	koneplus_sensitivity_selector_update_rmp(priv->sensitivity, rmp);
	koneplus_light_effects_frame_update_rmp(priv->light_effects, rmp);
	koneplus_color_frame_update_rmp(priv->colors, rmp);
	koneplus_cpi_selector_update_rmp(priv->cpi, rmp);
	koneplus_rmp_set_polling_rate(rmp, koneplus_polling_rate_selector_get_value(priv->polling_rate));

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

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

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

static GtkWidget *buttons_new(KoneplusconfigProfilePage *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 koneplusconfig_profile_page_init(KoneplusconfigProfilePage *profile_page) {
	KoneplusconfigProfilePagePrivate *priv = KONEPLUSCONFIG_PROFILE_PAGE_GET_PRIVATE(profile_page);
	GtkWidget *content, *buttons;
	GtkWidget *left;
	GtkWidget *light;
	GtkWidget *right;

	profile_page->priv = priv;

	content = gtk_hbox_new(FALSE, 0);
	buttons = buttons_new(profile_page);
	left = gtk_vbox_new(FALSE, 0);
	light = gtk_hbox_new(FALSE, 0);
	right = gtk_vbox_new(FALSE, 0);
	priv->buttons = KONEPLUS_BUTTONS_FRAME(koneplus_buttons_frame_new());
	priv->gamefiles = ROCCAT_GAMEFILE_SELECTOR(roccat_gamefile_selector_new(KONEPLUS_GAMEFILE_NUM));
	priv->polling_rate = KONEPLUS_POLLING_RATE_SELECTOR(koneplus_polling_rate_selector_new());
	priv->cpi = KONEPLUS_CPI_SELECTOR(koneplus_cpi_selector_new());
	priv->sensitivity = KONEPLUS_SENSITIVITY_SELECTOR(koneplus_sensitivity_selector_new());
	priv->light_effects = KONEPLUS_LIGHT_EFFECTS_FRAME(koneplus_light_effects_frame_new());
	priv->colors = KONEPLUS_COLOR_FRAME(koneplus_color_frame_new());

	gtk_box_pack_start(GTK_BOX(light), GTK_WIDGET(priv->colors), TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(light), GTK_WIDGET(priv->light_effects), 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->polling_rate), TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(right), GTK_WIDGET(priv->gamefiles), TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(left), light, TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(left), GTK_WIDGET(priv->cpi), 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) {
	KoneplusconfigProfilePagePrivate *priv = KONEPLUSCONFIG_PROFILE_PAGE(object)->priv;
	G_OBJECT_CLASS(koneplusconfig_profile_page_parent_class)->finalize(object);
	g_free(priv->profile_name);
}

static void koneplusconfig_profile_page_class_init(KoneplusconfigProfilePageClass *klass) {
	GObjectClass *gobject_class;

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

	g_type_class_add_private(klass, sizeof(KoneplusconfigProfilePagePrivate));

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

void koneplusconfig_profile_page_set_rmp(KoneplusconfigProfilePage *profile_page, KoneplusRmp *rmp) {
	KoneplusconfigProfilePagePrivate *priv = profile_page->priv;
	if (priv->rmp)
		koneplus_rmp_free(priv->rmp);
	priv->rmp = koneplus_rmp_dup(rmp);
	set_from_rmp(profile_page);
}

KoneplusRmp *koneplusconfig_profile_page_get_rmp(KoneplusconfigProfilePage *profile_page) {
	KoneplusconfigProfilePagePrivate *priv = profile_page->priv;
	KoneplusRmp *rmp;
	rmp = koneplus_rmp_dup(priv->rmp);
	update_rmp(profile_page, rmp);
	return rmp;
}

guint koneplusconfig_profile_page_get_index(KoneplusconfigProfilePage *profile_page) {
	return profile_page->priv->index;
}

GtkWidget *koneplusconfig_profile_page_get_tab_label(KoneplusconfigProfilePage *profile_page) {
	return GTK_WIDGET(profile_page->priv->tab_label);
}
