/*
 * 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 "koneconfig_settings_page.h"
#include "koneconfig_info_frame.h"
#include "koneconfig_weight_frame.h"
#include "roccat_actual_profile_selector.h"

#define KONECONFIG_SETTINGS_PAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), KONECONFIG_SETTINGS_PAGE_TYPE, KoneconfigSettingsPageClass))
#define IS_KONECONFIG_SETTINGS_PAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), KONECONFIG_SETTINGS_PAGE_TYPE))
#define KONECONFIG_SETTINGS_PAGE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), KONECONFIG_SETTINGS_PAGE_TYPE, KoneconfigSettingsPagePrivate))

typedef struct _KoneconfigSettingsPageClass KoneconfigSettingsPageClass;
typedef struct _KoneconfigSettingsPagePrivate KoneconfigSettingsPagePrivate;

struct _KoneconfigSettingsPage {
	GtkVBox parent;
	KoneconfigSettingsPagePrivate *priv;
};

struct _KoneconfigSettingsPageClass {
	GtkVBoxClass parent_class;
	void (*actual_profile_changed)(KoneconfigSettingsPage *settings_page);
	void (*update_weight)(KoneconfigSettingsPage *settings_page);
};

struct _KoneconfigSettingsPagePrivate {
	RoccatActualProfileSelector *actual_profile;
	KoneconfigInfoFrame *info;
	KoneconfigWeightFrame *weight;
};

G_DEFINE_TYPE(KoneconfigSettingsPage, koneconfig_settings_page, GTK_TYPE_VBOX);

enum {
	ACTUAL_PROFILE_CHANGED,
	UPDATE_WEIGHT,
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

void koneconfig_settings_page_set_weight(KoneconfigSettingsPage *settings_page, guint weight) {
	koneconfig_weight_frame_set_value(settings_page->priv->weight, weight);
}

void koneconfig_settings_page_clear_weight(KoneconfigSettingsPage *settings_page) {
	koneconfig_weight_frame_unset_value(settings_page->priv->weight);
}

guint koneconfig_settings_page_get_actual_profile(KoneconfigSettingsPage *settings_page) {
	return roccat_actual_profile_selector_get_value(settings_page->priv->actual_profile);
}

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

void koneconfig_settings_page_clear_info(KoneconfigSettingsPage *settings_page) {
	koneconfig_info_frame_clear(settings_page->priv->info);
}

void koneconfig_settings_page_set_info(KoneconfigSettingsPage *settings_page, guint firmware_version) {
	koneconfig_info_frame_set(settings_page->priv->info, firmware_version);
}

GtkWidget *koneconfig_settings_page_new(void) {
	KoneconfigSettingsPage *settings_page;

	settings_page = KONECONFIG_SETTINGS_PAGE(g_object_new(KONECONFIG_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) {
	KoneconfigSettingsPage *settings_page = KONECONFIG_SETTINGS_PAGE(user_data);
	g_signal_emit((gpointer)settings_page, signals[ACTUAL_PROFILE_CHANGED], 0);
}

static void update_weight_cb(RoccatActualProfileSelector *actual_profile, gpointer user_data) {
	KoneconfigSettingsPage *settings_page = KONECONFIG_SETTINGS_PAGE(user_data);
	g_signal_emit((gpointer)settings_page, signals[UPDATE_WEIGHT], 0);
}

static void koneconfig_settings_page_init(KoneconfigSettingsPage *settings_page) {
	KoneconfigSettingsPagePrivate *priv = KONECONFIG_SETTINGS_PAGE_GET_PRIVATE(settings_page);

	settings_page->priv = priv;

	priv->actual_profile = ROCCAT_ACTUAL_PROFILE_SELECTOR(roccat_actual_profile_selector_new(KONE_PROFILE_NUM));
	priv->info = KONECONFIG_INFO_FRAME(koneconfig_info_frame_new());
	priv->weight = KONECONFIG_WEIGHT_FRAME(koneconfig_weight_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->weight), 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->weight), "update", G_CALLBACK(update_weight_cb), settings_page);

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

static void koneconfig_settings_page_class_init(KoneconfigSettingsPageClass *klass) {
	g_type_class_add_private(klass, sizeof(KoneconfigSettingsPagePrivate));

	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(KoneconfigSettingsPageClass, actual_profile_changed),
			NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);

	signals[UPDATE_WEIGHT] = g_signal_new("update-weight",
			G_TYPE_FROM_CLASS(klass),
			G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
			G_STRUCT_OFFSET(KoneconfigSettingsPageClass, update_weight),
			NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
}
