/*
 * 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 "koneplus_settings_page.h"
#include "koneplus_info_frame.h"
#include "koneplus_tcu_frame.h"
#include "koneplus_dcu_frame.h"
#include "roccat_actual_profile_selector.h"
#include "roccat_device_reset_frame.h"

#define KONEPLUS_SETTINGS_PAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), KONEPLUS_SETTINGS_PAGE_TYPE, KoneplusSettingsPageClass))
#define IS_KONEPLUS_SETTINGS_PAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), KONEPLUS_SETTINGS_PAGE_TYPE))
#define KONEPLUS_SETTINGS_PAGE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), KONEPLUS_SETTINGS_PAGE_TYPE, KoneplusSettingsPagePrivate))

typedef struct _KoneplusSettingsPageClass KoneplusSettingsPageClass;
typedef struct _KoneplusSettingsPagePrivate KoneplusSettingsPagePrivate;

struct _KoneplusSettingsPage {
	GtkVBox parent;
	KoneplusSettingsPagePrivate *priv;
};

struct _KoneplusSettingsPageClass {
	GtkVBoxClass parent_class;
	void (*actual_profile_changed)(KoneplusSettingsPage *settings_page);
	void (*device_reset)(KoneplusSettingsPage *settings_page);
	void (*tcu_changed)(KoneplusSettingsPage *settings_page);
	void (*dcu_changed)(KoneplusSettingsPage *settings_page);
};

struct _KoneplusSettingsPagePrivate {
	RoccatActualProfileSelector *actual_profile;
	KoneplusInfoFrame *info;
	KoneplusTcuFrame *tcu;
	KoneplusDcuFrame *dcu;
	RoccatDeviceResetFrame *reset;
};

G_DEFINE_TYPE(KoneplusSettingsPage, koneplus_settings_page, GTK_TYPE_VBOX);

enum {
	ACTUAL_PROFILE_CHANGED,
	DEVICE_RESET,
	TCU_CHANGED,
	DCU_CHANGED,
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

guint koneplus_settings_page_get_tcu_value(KoneplusSettingsPage *settings_page) {
	return koneplus_tcu_frame_get_value(settings_page->priv->tcu);
}

void koneplus_settings_page_set_tcu_value_blocked(KoneplusSettingsPage *settings_page, guint value) {
	koneplus_tcu_frame_set_value_blocked(settings_page->priv->tcu, value);
}

guint koneplus_settings_page_get_dcu_value(KoneplusSettingsPage *settings_page) {
	return koneplus_dcu_frame_get_value(settings_page->priv->dcu);
}

void koneplus_settings_page_set_dcu_value_blocked(KoneplusSettingsPage *settings_page, guint value) {
	koneplus_dcu_frame_set_value_blocked(settings_page->priv->dcu, value);
}

guint koneplus_settings_page_get_actual_profile(KoneplusSettingsPage *settings_page) {
	return roccat_actual_profile_selector_get_value(settings_page->priv->actual_profile);
}

void koneplus_settings_page_set_actual_profile(KoneplusSettingsPage *settings_page, guint profile_number) {
	roccat_actual_profile_selector_set_value(settings_page->priv->actual_profile, profile_number);
}

void koneplus_settings_page_clear_info(KoneplusSettingsPage *settings_page) {
	koneplus_info_frame_clear(settings_page->priv->info);
}

void koneplus_settings_page_set_info(KoneplusSettingsPage *settings_page, KoneplusInfo *info) {
	koneplus_info_frame_set(settings_page->priv->info, info);
}

GtkWidget *koneplus_settings_page_new(void) {
	KoneplusSettingsPage *settings_page;

	settings_page = KONEPLUS_SETTINGS_PAGE(g_object_new(KONEPLUS_SETTINGS_PAGE_TYPE,
			"homogeneous", FALSE,
			"spacing", 0,
			NULL));

	return GTK_WIDGET(settings_page);
}

static void actual_profile_changed_cb(RoccatActualProfileSelector *actual_profile, gpointer user_data) {
	KoneplusSettingsPage *settings_page = KONEPLUS_SETTINGS_PAGE(user_data);
	g_signal_emit((gpointer)settings_page, signals[ACTUAL_PROFILE_CHANGED], 0);
}

static void device_reset_cb(RoccatDeviceResetFrame *frame, gpointer user_data) {
	KoneplusSettingsPage *settings_page = KONEPLUS_SETTINGS_PAGE(user_data);
	g_signal_emit((gpointer)settings_page, signals[DEVICE_RESET], 0);
}

static void tcu_changed_cb(KoneplusTcuFrame *frame, gpointer user_data) {
	KoneplusSettingsPage *settings_page = KONEPLUS_SETTINGS_PAGE(user_data);
	g_signal_emit((gpointer)settings_page, signals[TCU_CHANGED], 0);
}

static void dcu_changed_cb(KoneplusDcuFrame *frame, gpointer user_data) {
	KoneplusSettingsPage *settings_page = KONEPLUS_SETTINGS_PAGE(user_data);
	g_signal_emit((gpointer)settings_page, signals[DCU_CHANGED], 0);
}

static void koneplus_settings_page_init(KoneplusSettingsPage *settings_page) {
	KoneplusSettingsPagePrivate *priv = KONEPLUS_SETTINGS_PAGE_GET_PRIVATE(settings_page);

	settings_page->priv = priv;

	priv->actual_profile = ROCCAT_ACTUAL_PROFILE_SELECTOR(roccat_actual_profile_selector_new(KONEPLUS_PROFILE_NUM));
	priv->info = KONEPLUS_INFO_FRAME(koneplus_info_frame_new());
	priv->reset = ROCCAT_DEVICE_RESET_FRAME(roccat_device_reset_frame_new());
	priv->tcu = KONEPLUS_TCU_FRAME(koneplus_tcu_frame_new());
	priv->dcu = KONEPLUS_DCU_FRAME(koneplus_dcu_frame_new());

	gtk_box_pack_start(GTK_BOX(settings_page), GTK_WIDGET(priv->info), FALSE, FALSE, 0);
	gtk_box_pack_start(GTK_BOX(settings_page), GTK_WIDGET(priv->actual_profile), FALSE, FALSE, 0);
	gtk_box_pack_start(GTK_BOX(settings_page), GTK_WIDGET(priv->reset), FALSE, FALSE, 0);
	gtk_box_pack_start(GTK_BOX(settings_page), GTK_WIDGET(priv->tcu), FALSE, FALSE, 0);
	gtk_box_pack_start(GTK_BOX(settings_page), GTK_WIDGET(priv->dcu), FALSE, FALSE, 0);

	g_signal_connect(G_OBJECT(priv->actual_profile), "changed", G_CALLBACK(actual_profile_changed_cb), settings_page);
	g_signal_connect(G_OBJECT(priv->reset), "clicked", G_CALLBACK(device_reset_cb), settings_page);
	g_signal_connect(G_OBJECT(priv->tcu), "changed", G_CALLBACK(tcu_changed_cb), settings_page);
	g_signal_connect(G_OBJECT(priv->dcu), "changed", G_CALLBACK(dcu_changed_cb), settings_page);

	gtk_widget_show(GTK_WIDGET(priv->info));
	gtk_widget_show(GTK_WIDGET(priv->actual_profile));
	gtk_widget_show(GTK_WIDGET(priv->reset));
	gtk_widget_show(GTK_WIDGET(priv->tcu));
	gtk_widget_show(GTK_WIDGET(priv->dcu));
}

static void koneplus_settings_page_class_init(KoneplusSettingsPageClass *klass) {
	g_type_class_add_private(klass, sizeof(KoneplusSettingsPagePrivate));

	signals[ACTUAL_PROFILE_CHANGED] = g_signal_new("actual-profile-changed",
			G_TYPE_FROM_CLASS(klass),
			G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
			G_STRUCT_OFFSET(KoneplusSettingsPageClass, actual_profile_changed),
			NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);

	signals[DEVICE_RESET] = g_signal_new("device-reset",
			G_TYPE_FROM_CLASS(klass),
			G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
			G_STRUCT_OFFSET(KoneplusSettingsPageClass, device_reset),
			NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);

	signals[TCU_CHANGED] = g_signal_new("tcu-changed",
			G_TYPE_FROM_CLASS(klass),
			G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
			G_STRUCT_OFFSET(KoneplusSettingsPageClass, tcu_changed),
			NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);

	signals[DCU_CHANGED] = g_signal_new("dcu-changed",
			G_TYPE_FROM_CLASS(klass),
			G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
			G_STRUCT_OFFSET(KoneplusSettingsPageClass, dcu_changed),
			NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
}
