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

#define ROCCAT_TIMEOUT_BAR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), ROCCAT_TIMEOUT_BAR_TYPE, RoccatTimeoutBarClass))
#define IS_ROCCAT_TIMEOUT_BAR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), ROCCAT_TIMEOUT_BAR_TYPE))
#define ROCCAT_TIMEOUT_BAR_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), ROCCAT_TIMEOUT_BAR_TYPE, RoccatTimeoutBarPrivate))

typedef struct _RoccatTimeoutBarClass RoccatTimeoutBarClass;
typedef struct _RoccatTimeoutBarPrivate RoccatTimeoutBarPrivate;

struct _RoccatTimeoutBar {
	GtkProgressBar hbox;
	RoccatTimeoutBarPrivate *priv;
};

struct _RoccatTimeoutBarClass {
	GtkProgressBarClass parent_class;
	void(*timeout)(RoccatTimeoutBar *timeout_bar);
};

struct _RoccatTimeoutBarPrivate {
	guint timeout; /* secs */
	guint interval; /* msecs */
	guint source;
};

G_DEFINE_TYPE(RoccatTimeoutBar, roccat_timeout_bar, GTK_TYPE_PROGRESS_BAR);

static gboolean update_timeout_bar(RoccatTimeoutBar *timeout_bar) {
	RoccatTimeoutBarPrivate *priv = timeout_bar->priv;
	gdouble new_value, fraction;

	fraction = 1.0 / priv->timeout / 1000.0 * priv->interval;
	new_value = gtk_progress_bar_get_fraction(GTK_PROGRESS_BAR(timeout_bar)) + fraction;
	if (new_value > (1.0 - fraction)) {
		g_signal_emit_by_name((gpointer)timeout_bar, "timeout");
		return FALSE;
	} else {
		gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(timeout_bar), new_value);
		return TRUE;
	}
}

GtkWidget *roccat_timeout_bar_new(void) {
	return GTK_WIDGET(g_object_new(ROCCAT_TIMEOUT_BAR_TYPE, NULL));
}

void roccat_timeout_bar_start(RoccatTimeoutBar *timeout_bar, guint interval, guint timeout) {
	RoccatTimeoutBarPrivate *priv = timeout_bar->priv;

	if (priv->source)
		return;

	priv->interval = interval;
	priv->timeout = timeout;

	priv->source = g_timeout_add(interval, (GSourceFunc)update_timeout_bar, timeout_bar);
}

void roccat_timeout_bar_stop(RoccatTimeoutBar *timeout_bar) {
	RoccatTimeoutBarPrivate *priv = timeout_bar->priv;

	if (!priv->source)
		return;

	g_source_remove(priv->source);
	priv->source = 0;
}

static void roccat_timeout_bar_init(RoccatTimeoutBar *timeout_bar) {
	RoccatTimeoutBarPrivate *priv = ROCCAT_TIMEOUT_BAR_GET_PRIVATE(timeout_bar);

	timeout_bar->priv = priv;
}

static void roccat_timeout_bar_class_init(RoccatTimeoutBarClass *klass) {
	g_type_class_add_private(klass, sizeof(RoccatTimeoutBarPrivate));

	g_signal_new("timeout",
			G_TYPE_FROM_CLASS(klass),
			G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
			G_STRUCT_OFFSET(RoccatTimeoutBarClass, timeout),
			NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
}
