/*
 * 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 "savuconfig_info_frame.h"

#define SAVUCONFIG_INFO_FRAME_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), SAVUCONFIG_INFO_FRAME_TYPE, SavuconfigInfoFrameClass))
#define IS_SAVUCONFIG_INFO_FRAME_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), SAVUCONFIG_INFO_FRAME_TYPE))
#define SAVUCONFIG_INFO_FRAME_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), SAVUCONFIG_INFO_FRAME_TYPE, SavuconfigInfoFramePrivate))

struct _SavuconfigInfoFramePrivate {
	GtkLabel *firmware;
	GtkLabel *srom_id;
};

G_DEFINE_TYPE(SavuconfigInfoFrame, savuconfig_info_frame, GTK_TYPE_FRAME);

GtkWidget *savuconfig_info_frame_new(void) {
	SavuconfigInfoFrame *frame;

	frame = g_object_new(SAVUCONFIG_INFO_FRAME_TYPE, NULL);

	return GTK_WIDGET(frame);
}

static void savuconfig_info_frame_init(SavuconfigInfoFrame *frame) {
	SavuconfigInfoFramePrivate *priv = SAVUCONFIG_INFO_FRAME_GET_PRIVATE(frame);
	GtkTable *table;

	frame->priv = priv;

	table = GTK_TABLE(gtk_table_new(1, 2, FALSE));
	priv->firmware = GTK_LABEL(gtk_label_new(NULL));
	priv->srom_id = GTK_LABEL(gtk_label_new(NULL));

	gtk_container_add(GTK_CONTAINER(frame), GTK_WIDGET(table));
	gtk_table_attach(table, gtk_label_new("Firmware"), 0, 1, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
	gtk_table_attach(table, GTK_WIDGET(priv->firmware), 1, 2, 0, 1, GTK_EXPAND, GTK_SHRINK, 0, 0);
	gtk_table_attach(table, gtk_label_new("SROM ID"), 0, 1, 1, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
	gtk_table_attach(table, GTK_WIDGET(priv->srom_id), 1, 2, 1, 2, GTK_EXPAND, GTK_SHRINK, 0, 0);

	gtk_frame_set_label(GTK_FRAME(frame), "Information");

	savuconfig_info_frame_clear(frame);

	gtk_widget_show_all(GTK_WIDGET(table));
}

static void savuconfig_info_frame_class_init(SavuconfigInfoFrameClass *klass) {
	g_type_class_add_private(klass, sizeof(SavuconfigInfoFramePrivate));
}

static void set_labels(SavuconfigInfoFrame *frame, gchar const *firmware, gchar const *srom_id) {
	SavuconfigInfoFramePrivate *priv = frame->priv;
	gtk_label_set_text(priv->firmware, firmware);
	gtk_label_set_text(priv->srom_id, srom_id);
}

void savuconfig_info_frame_clear(SavuconfigInfoFrame *frame) {
	set_labels(frame, "n/a", "n/a");
}

void savuconfig_info_frame_set(SavuconfigInfoFrame *frame, SavuInfo *info) {
	gchar *firmware = savu_firmware_version_to_string(info->firmware_version);
	gchar *srom_id = g_strdup_printf("%02x", info->sensor_srom_id);
	set_labels(frame, firmware, srom_id);
	g_free(srom_id);
	g_free(firmware);
}
