/*
 * 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 "arvoconfig_settings_page.h"
#include "arvoconfig_info_frame.h"
#include "roccat_actual_profile_selector.h"

#define ARVOCONFIG_SETTINGS_PAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), ARVOCONFIG_SETTINGS_PAGE_TYPE, ArvoconfigSettingsPageClass))
#define IS_ARVOCONFIG_SETTINGS_PAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), ARVOCONFIG_SETTINGS_PAGE_TYPE))
#define ARVOCONFIG_SETTINGS_PAGE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), ARVOCONFIG_SETTINGS_PAGE_TYPE, ArvoconfigSettingsPagePrivate))

typedef struct _ArvoconfigSettingsPageClass ArvoconfigSettingsPageClass;
typedef struct _ArvoconfigSettingsPagePrivate ArvoconfigSettingsPagePrivate;

struct _ArvoconfigSettingsPage {
	GtkVBox parent;
	ArvoconfigSettingsPagePrivate *priv;
};

struct _ArvoconfigSettingsPageClass {
	GtkVBoxClass parent_class;
	void (*actual_profile_changed)(ArvoconfigSettingsPage *settings_page);
};

struct _ArvoconfigSettingsPagePrivate {
	RoccatActualProfileSelector *actual_profile;
	ArvoconfigInfoFrame *info;
};

G_DEFINE_TYPE(ArvoconfigSettingsPage, arvoconfig_settings_page, GTK_TYPE_VBOX);

enum {
	ACTUAL_PROFILE_CHANGED,
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

guint arvoconfig_settings_page_get_actual_profile(ArvoconfigSettingsPage *settings_page) {
	return roccat_actual_profile_selector_get_value(settings_page->priv->actual_profile);
}

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

void arvoconfig_settings_page_clear_info(ArvoconfigSettingsPage *settings_page) {
	arvoconfig_info_frame_clear(settings_page->priv->info);
}

void arvoconfig_settings_page_set_info(ArvoconfigSettingsPage *settings_page, ArvoInfo *info) {
	arvoconfig_info_frame_set(settings_page->priv->info, info);
}

GtkWidget *arvoconfig_settings_page_new(void) {
	ArvoconfigSettingsPage *settings_page;

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

static void arvoconfig_settings_page_init(ArvoconfigSettingsPage *settings_page) {
	ArvoconfigSettingsPagePrivate *priv = ARVOCONFIG_SETTINGS_PAGE_GET_PRIVATE(settings_page);

	settings_page->priv = priv;

	priv->actual_profile = ROCCAT_ACTUAL_PROFILE_SELECTOR(roccat_actual_profile_selector_new(ARVO_PROFILE_NUM));
	priv->info = ARVOCONFIG_INFO_FRAME(arvoconfig_info_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);

	g_signal_connect(G_OBJECT(priv->actual_profile), "changed", G_CALLBACK(actual_profile_changed_cb), settings_page);

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

static void arvoconfig_settings_page_class_init(ArvoconfigSettingsPageClass *klass) {
	g_type_class_add_private(klass, sizeof(ArvoconfigSettingsPagePrivate));

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