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

#define KONEPLUS_INFO_FRAME_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), KONEPLUS_INFO_FRAME_TYPE, KoneplusInfoFrameClass))
#define IS_KONEPLUS_INFO_FRAME_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), KONEPLUS_INFO_FRAME_TYPE))
#define KONEPLUS_INFO_FRAME_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), KONEPLUS_INFO_FRAME_TYPE, KoneplusInfoFramePrivate))

struct _KoneplusInfoFramePrivate {
	GtkLabel *firmware;
};

G_DEFINE_TYPE(KoneplusInfoFrame, koneplus_info_frame, GTK_TYPE_FRAME);

GtkWidget *koneplus_info_frame_new(void) {
	KoneplusInfoFrame *frame;

	frame = g_object_new(KONEPLUS_INFO_FRAME_TYPE, NULL);

	return GTK_WIDGET(frame);
}

static void koneplus_info_frame_init(KoneplusInfoFrame *frame) {
	KoneplusInfoFramePrivate *priv = KONEPLUS_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");

	koneplus_info_frame_clear(frame);

	gtk_widget_show_all(GTK_WIDGET(table));
}

static void koneplus_info_frame_class_init(KoneplusInfoFrameClass *klass) {
	g_type_class_add_private(klass, sizeof(KoneplusInfoFramePrivate));
}

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

void koneplus_info_frame_clear(KoneplusInfoFrame *frame) {
	set_labels(frame, "n/a");
}

void koneplus_info_frame_set(KoneplusInfoFrame *frame, KoneplusInfo *info) {
	gchar *firmware = koneplus_firmware_version_to_string(info->firmware_version);
	set_labels(frame, firmware);
	g_free(firmware);
}
