/*
 * 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 "isku_settings_page.h"
#include "isku_info_frame.h"
#include "roccat_actual_profile_selector.h"
#include "roccat_device_reset_frame.h"

#define ISKU_SETTINGS_PAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), ISKU_SETTINGS_PAGE_TYPE, IskuSettingsPageClass))
#define IS_ISKU_SETTINGS_PAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), ISKU_SETTINGS_PAGE_TYPE))
#define ISKU_SETTINGS_PAGE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), ISKU_SETTINGS_PAGE_TYPE, IskuSettingsPagePrivate))

typedef struct _IskuSettingsPageClass IskuSettingsPageClass;
typedef struct _IskuSettingsPagePrivate IskuSettingsPagePrivate;

struct _IskuSettingsPage {
	GtkVBox parent;
	IskuSettingsPagePrivate *priv;
};

struct _IskuSettingsPageClass {
	GtkVBoxClass parent_class;
	void (*actual_profile_changed)(IskuSettingsPage *settings_page);
	void (*device_reset)(IskuSettingsPage *settings_page);
};

struct _IskuSettingsPagePrivate {
	RoccatActualProfileSelector *actual_profile;
	IskuInfoFrame *info;
	RoccatDeviceResetFrame *reset;
};

G_DEFINE_TYPE(IskuSettingsPage, isku_settings_page, GTK_TYPE_VBOX);

enum {
	ACTUAL_PROFILE_CHANGED,
	DEVICE_RESET,
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

guint isku_settings_page_get_actual_profile(IskuSettingsPage *settings_page) {
	return roccat_actual_profile_selector_get_value(settings_page->priv->actual_profile);
}

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

void isku_settings_page_clear_info(IskuSettingsPage *settings_page) {
	isku_info_frame_clear(settings_page->priv->info);
}

void isku_settings_page_set_info(IskuSettingsPage *settings_page, IskuInfo *info) {
	isku_info_frame_set(settings_page->priv->info, info);
}

GtkWidget *isku_settings_page_new(void) {
	IskuSettingsPage *settings_page;

	settings_page = ISKU_SETTINGS_PAGE(g_object_new(ISKU_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) {
	IskuSettingsPage *settings_page = ISKU_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) {
	IskuSettingsPage *settings_page = ISKU_SETTINGS_PAGE(user_data);
	g_signal_emit((gpointer)settings_page, signals[DEVICE_RESET], 0);
}

static void isku_settings_page_init(IskuSettingsPage *settings_page) {
	IskuSettingsPagePrivate *priv = ISKU_SETTINGS_PAGE_GET_PRIVATE(settings_page);

	settings_page->priv = priv;

	priv->actual_profile = ROCCAT_ACTUAL_PROFILE_SELECTOR(roccat_actual_profile_selector_new(ISKU_PROFILE_NUM));
	priv->info = ISKU_INFO_FRAME(isku_info_frame_new());
	priv->reset = ROCCAT_DEVICE_RESET_FRAME(roccat_device_reset_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);

	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);

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

static void isku_settings_page_class_init(IskuSettingsPageClass *klass) {
	g_type_class_add_private(klass, sizeof(IskuSettingsPagePrivate));

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