/*
 * 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_tcu_frame.h"
#include "roccat/koneplus.h"

#define KONEPLUS_TCU_FRAME_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), KONEPLUS_TCU_FRAME_TYPE, KoneplusTcuFrameClass))
#define IS_KONEPLUS_TCU_FRAME_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), KONEPLUS_TCU_FRAME_TYPE))
#define KONEPLUS_TCU_FRAME_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), KONEPLUS_TCU_FRAME_TYPE, KoneplusTcuFramePrivate))

typedef struct _KoneplusTcuFrameClass KoneplusTcuFrameClass;
typedef struct _KoneplusTcuFramePrivate KoneplusTcuFramePrivate;

struct _KoneplusTcuFrame {
	GtkFrame parent;
	KoneplusTcuFramePrivate *priv;
};

struct _KoneplusTcuFrameClass {
	GtkFrameClass parent_class;
	void (*changed)(KoneplusTcuFrame *frame);
};

struct _KoneplusTcuFramePrivate {
	GtkToggleButton *active_button;
	GtkButton *cal_button;
	gulong cal_handler;
	gulong active_handler;
};

G_DEFINE_TYPE(KoneplusTcuFrame, koneplus_tcu_frame, GTK_TYPE_FRAME);

enum {
	CHANGED,
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

guint koneplus_tcu_frame_get_value(KoneplusTcuFrame *frame) {
	return (gtk_toggle_button_get_active(frame->priv->active_button) == TRUE) ? KONEPLUS_SENSOR_TCU_ON : KONEPLUS_SENSOR_TCU_OFF;
}

void koneplus_tcu_frame_set_value_blocked(KoneplusTcuFrame *frame, guint value) {
	KoneplusTcuFramePrivate *priv = frame->priv;
	g_signal_handler_block(G_OBJECT(priv->active_button), priv->active_handler);
	gtk_toggle_button_set_active(frame->priv->active_button, (value == KONEPLUS_SENSOR_TCU_ON) ? TRUE : FALSE);
	g_signal_handler_unblock(G_OBJECT(priv->active_button), priv->active_handler);
}

GtkWidget *koneplus_tcu_frame_new(void) {
	KoneplusTcuFrame *koneplus_tcu_frame;

	koneplus_tcu_frame = KONEPLUS_TCU_FRAME(g_object_new(KONEPLUS_TCU_FRAME_TYPE,
			NULL));

	return GTK_WIDGET(koneplus_tcu_frame);
}

static void active_toggled_cb(GtkToggleButton *button, gpointer user_data) {
	KoneplusTcuFrame *frame = (KoneplusTcuFrame *)user_data;
	g_signal_emit((gpointer)frame, signals[CHANGED], 0);
}

static void cal_clicked_cb(GtkButton *button, gpointer user_data) {
	KoneplusTcuFrame *frame = (KoneplusTcuFrame *)user_data;
	koneplus_tcu_frame_set_value_blocked(frame, KONEPLUS_SENSOR_TCU_ON);
	g_signal_emit((gpointer)frame, signals[CHANGED], 0);
}

static void koneplus_tcu_frame_init(KoneplusTcuFrame *frame) {
	KoneplusTcuFramePrivate *priv = KONEPLUS_TCU_FRAME_GET_PRIVATE(frame);
	GtkWidget *box;

	frame->priv = priv;

	box = gtk_hbox_new(FALSE, 0);
	priv->active_button = GTK_TOGGLE_BUTTON(gtk_check_button_new_with_label("Active"));
	priv->cal_button = GTK_BUTTON(gtk_button_new_with_label("(Re)calibrate"));

	gtk_container_add(GTK_CONTAINER(frame), box);
	gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(priv->active_button), FALSE, FALSE, 0);
	gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(priv->cal_button), FALSE, FALSE, 0);

	priv->active_handler = g_signal_connect(G_OBJECT(priv->active_button), "toggled", G_CALLBACK(active_toggled_cb), frame);
	priv->cal_handler = g_signal_connect(G_OBJECT(priv->cal_button), "clicked", G_CALLBACK(cal_clicked_cb), frame);

	gtk_frame_set_label(GTK_FRAME(frame), "Tracking control unit");

	gtk_widget_show_all(box);
}

static void koneplus_tcu_frame_class_init(KoneplusTcuFrameClass *klass) {
	g_type_class_add_private(klass, sizeof(KoneplusTcuFramePrivate));

	signals[CHANGED] = g_signal_new("changed",
			G_TYPE_FROM_CLASS(klass),
			G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
			G_STRUCT_OFFSET(KoneplusTcuFrameClass, changed),
			NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
}
