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

#define PYRACONFIG_INFO_FRAME_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), PYRACONFIG_INFO_FRAME_TYPE, PyraconfigInfoFrameClass))
#define IS_PYRACONFIG_INFO_FRAME_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), PYRACONFIG_INFO_FRAME_TYPE))
#define PYRACONFIG_INFO_FRAME_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), PYRACONFIG_INFO_FRAME_TYPE, PyraconfigInfoFramePrivate))

struct _PyraconfigInfoFramePrivate {
	GtkLabel *firmware;
};

G_DEFINE_TYPE(PyraconfigInfoFrame, pyraconfig_info_frame, GTK_TYPE_FRAME);

GtkWidget *pyraconfig_info_frame_new(void) {
	PyraconfigInfoFrame *frame;

	frame = g_object_new(PYRACONFIG_INFO_FRAME_TYPE, NULL);

	return GTK_WIDGET(frame);
}

static void pyraconfig_info_frame_init(PyraconfigInfoFrame *frame) {
	PyraconfigInfoFramePrivate *priv = PYRACONFIG_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));

	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_frame_set_label(GTK_FRAME(frame), "Information");

	pyraconfig_info_frame_clear(frame);

	gtk_widget_show_all(GTK_WIDGET(table));
}

static void pyraconfig_info_frame_class_init(PyraconfigInfoFrameClass *klass) {
	g_type_class_add_private(klass, sizeof(PyraconfigInfoFramePrivate));
}

static void set_labels(PyraconfigInfoFrame *frame, gchar const *firmware) {
	PyraconfigInfoFramePrivate *priv = frame->priv;
	gtk_label_set_text(priv->firmware, firmware);
}

void pyraconfig_info_frame_clear(PyraconfigInfoFrame *frame) {
	set_labels(frame, "n/a");
}

void pyraconfig_info_frame_set(PyraconfigInfoFrame *frame, PyraInfo *info) {
	gchar *firmware = pyra_firmware_version_to_string(info->firmware_version);
	set_labels(frame, firmware);
	g_free(firmware);
}
