/* chess-clock.c generated by valac 0.27.1.15-b1df0, the Vala compiler
 * generated from chess-clock.vala, do not modify */

/* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * Copyright (C) 2010-2013 Robert Ancell
 * Copyright (C) 2013 Michael Catanzaro
 *
 * This program 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. See http://www.gnu.org/copyleft/gpl.html the full text of the
 * license.
 */

#include <glib.h>
#include <glib-object.h>


#define TYPE_CHESS_CLOCK (chess_clock_get_type ())
#define CHESS_CLOCK(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_CHESS_CLOCK, ChessClock))
#define CHESS_CLOCK_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_CHESS_CLOCK, ChessClockClass))
#define IS_CHESS_CLOCK(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_CHESS_CLOCK))
#define IS_CHESS_CLOCK_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_CHESS_CLOCK))
#define CHESS_CLOCK_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_CHESS_CLOCK, ChessClockClass))

typedef struct _ChessClock ChessClock;
typedef struct _ChessClockClass ChessClockClass;
typedef struct _ChessClockPrivate ChessClockPrivate;

#define TYPE_COLOR (color_get_type ())
#define _g_timer_destroy0(var) ((var == NULL) ? NULL : (var = (g_timer_destroy (var), NULL)))

struct _ChessClock {
	GObject parent_instance;
	ChessClockPrivate * priv;
};

struct _ChessClockClass {
	GObjectClass parent_class;
};

typedef enum  {
	COLOR_WHITE,
	COLOR_BLACK
} Color;

struct _ChessClockPrivate {
	gint _white_initial_seconds;
	gint _black_initial_seconds;
	gint _white_seconds_used;
	gint _black_seconds_used;
	Color _active_color;
	GTimer* timer;
	guint tick_timeout_id;
	gboolean is_active;
};


static gpointer chess_clock_parent_class = NULL;

GType chess_clock_get_type (void) G_GNUC_CONST;
GType color_get_type (void) G_GNUC_CONST;
#define CHESS_CLOCK_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_CHESS_CLOCK, ChessClockPrivate))
enum  {
	CHESS_CLOCK_DUMMY_PROPERTY,
	CHESS_CLOCK_WHITE_INITIAL_SECONDS,
	CHESS_CLOCK_BLACK_INITIAL_SECONDS,
	CHESS_CLOCK_WHITE_SECONDS_USED,
	CHESS_CLOCK_BLACK_SECONDS_USED,
	CHESS_CLOCK_ACTIVE_COLOR
};
ChessClock* chess_clock_new (gint white_initial_seconds, gint black_initial_seconds);
ChessClock* chess_clock_construct (GType object_type, gint white_initial_seconds, gint black_initial_seconds);
static void chess_clock_set_white_initial_seconds (ChessClock* self, gint value);
static void chess_clock_set_black_initial_seconds (ChessClock* self, gint value);
void chess_clock_start (ChessClock* self);
static void chess_clock_watch_timer (ChessClock* self);
static gboolean chess_clock_tick_cb (ChessClock* self);
Color chess_clock_get_active_color (ChessClock* self);
gint chess_clock_get_white_seconds_used (ChessClock* self);
static void chess_clock_set_white_seconds_used (ChessClock* self, gint value);
gint chess_clock_get_black_seconds_used (ChessClock* self);
static void chess_clock_set_black_seconds_used (ChessClock* self, gint value);
gint chess_clock_get_white_initial_seconds (ChessClock* self);
gint chess_clock_get_black_initial_seconds (ChessClock* self);
void chess_clock_stop (ChessClock* self);
static void chess_clock_stop_watching_timer (ChessClock* self);
void chess_clock_pause (ChessClock* self);
void chess_clock_unpause (ChessClock* self);
static gboolean _chess_clock_tick_cb_gsource_func (gpointer self);
void chess_clock_set_active_color (ChessClock* self, Color value);
static void chess_clock_finalize (GObject* obj);
static void _vala_chess_clock_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec);
static void _vala_chess_clock_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec);


ChessClock* chess_clock_construct (GType object_type, gint white_initial_seconds, gint black_initial_seconds) {
	ChessClock * self = NULL;
	gint _tmp0_ = 0;
	gint _tmp1_ = 0;
	self = (ChessClock*) g_object_new (object_type, NULL);
	_tmp0_ = white_initial_seconds;
	chess_clock_set_white_initial_seconds (self, _tmp0_);
	_tmp1_ = black_initial_seconds;
	chess_clock_set_black_initial_seconds (self, _tmp1_);
	return self;
}


ChessClock* chess_clock_new (gint white_initial_seconds, gint black_initial_seconds) {
	return chess_clock_construct (TYPE_CHESS_CLOCK, white_initial_seconds, black_initial_seconds);
}


void chess_clock_start (ChessClock* self) {
	gboolean _tmp0_ = FALSE;
	GTimer* _tmp1_ = NULL;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->is_active;
	if (_tmp0_) {
		return;
	}
	self->priv->is_active = TRUE;
	_tmp1_ = self->priv->timer;
	if (_tmp1_ == NULL) {
		GTimer* _tmp2_ = NULL;
		_tmp2_ = g_timer_new ();
		_g_timer_destroy0 (self->priv->timer);
		self->priv->timer = _tmp2_;
	} else {
		GTimer* _tmp3_ = NULL;
		_tmp3_ = self->priv->timer;
		g_timer_start (_tmp3_);
	}
	chess_clock_watch_timer (self);
}


static gboolean chess_clock_tick_cb (ChessClock* self) {
	gboolean result = FALSE;
	Color _tmp0_ = 0;
	Color _tmp1_ = 0;
	gboolean _tmp4_ = FALSE;
	gint _tmp5_ = 0;
	gint _tmp6_ = 0;
	g_return_val_if_fail (self != NULL, FALSE);
	_tmp0_ = chess_clock_get_active_color (self);
	_tmp1_ = _tmp0_;
	if (_tmp1_ == COLOR_WHITE) {
		gint _tmp2_ = 0;
		_tmp2_ = self->priv->_white_seconds_used;
		chess_clock_set_white_seconds_used (self, _tmp2_ + 1);
	} else {
		gint _tmp3_ = 0;
		_tmp3_ = self->priv->_black_seconds_used;
		chess_clock_set_black_seconds_used (self, _tmp3_ + 1);
	}
	g_signal_emit_by_name (self, "tick");
	_tmp5_ = self->priv->_white_seconds_used;
	_tmp6_ = self->priv->_white_initial_seconds;
	if (_tmp5_ >= _tmp6_) {
		_tmp4_ = TRUE;
	} else {
		gint _tmp7_ = 0;
		gint _tmp8_ = 0;
		_tmp7_ = self->priv->_black_seconds_used;
		_tmp8_ = self->priv->_black_initial_seconds;
		_tmp4_ = _tmp7_ >= _tmp8_;
	}
	if (_tmp4_) {
		chess_clock_stop (self);
		g_signal_emit_by_name (self, "expired");
	}
	result = TRUE;
	return result;
}


void chess_clock_stop (ChessClock* self) {
	gboolean _tmp0_ = FALSE;
	GTimer* _tmp1_ = NULL;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->is_active;
	if (!_tmp0_) {
		return;
	}
	_tmp1_ = self->priv->timer;
	g_timer_stop (_tmp1_);
	chess_clock_stop_watching_timer (self);
	self->priv->is_active = FALSE;
}


void chess_clock_pause (ChessClock* self) {
	gboolean _tmp0_ = FALSE;
	GTimer* _tmp1_ = NULL;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->is_active;
	if (!_tmp0_) {
		return;
	}
	_tmp1_ = self->priv->timer;
	g_timer_stop (_tmp1_);
	chess_clock_stop_watching_timer (self);
	self->priv->is_active = FALSE;
}


void chess_clock_unpause (ChessClock* self) {
	gboolean _tmp0_ = FALSE;
	GTimer* _tmp1_ = NULL;
	GTimer* _tmp3_ = NULL;
	g_return_if_fail (self != NULL);
	_tmp1_ = self->priv->timer;
	if (_tmp1_ == NULL) {
		_tmp0_ = TRUE;
	} else {
		gboolean _tmp2_ = FALSE;
		_tmp2_ = self->priv->is_active;
		_tmp0_ = _tmp2_;
	}
	if (_tmp0_) {
		return;
	}
	_tmp3_ = self->priv->timer;
	g_timer_continue (_tmp3_);
	chess_clock_watch_timer (self);
	self->priv->is_active = TRUE;
}


static gboolean _chess_clock_tick_cb_gsource_func (gpointer self) {
	gboolean result;
	result = chess_clock_tick_cb ((ChessClock*) self);
	return result;
}


static void chess_clock_watch_timer (ChessClock* self) {
	guint _tmp0_ = 0U;
	g_return_if_fail (self != NULL);
	_tmp0_ = g_timeout_add_full (G_PRIORITY_DEFAULT, (guint) 1000, _chess_clock_tick_cb_gsource_func, g_object_ref (self), g_object_unref);
	self->priv->tick_timeout_id = _tmp0_;
}


static void chess_clock_stop_watching_timer (ChessClock* self) {
	guint _tmp0_ = 0U;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->tick_timeout_id;
	g_source_remove (_tmp0_);
	self->priv->tick_timeout_id = (guint) 0;
}


gint chess_clock_get_white_initial_seconds (ChessClock* self) {
	gint result;
	gint _tmp0_ = 0;
	g_return_val_if_fail (self != NULL, 0);
	_tmp0_ = self->priv->_white_initial_seconds;
	result = _tmp0_;
	return result;
}


static void chess_clock_set_white_initial_seconds (ChessClock* self, gint value) {
	gint _tmp0_ = 0;
	g_return_if_fail (self != NULL);
	_tmp0_ = value;
	self->priv->_white_initial_seconds = _tmp0_;
	g_object_notify ((GObject *) self, "white-initial-seconds");
}


gint chess_clock_get_black_initial_seconds (ChessClock* self) {
	gint result;
	gint _tmp0_ = 0;
	g_return_val_if_fail (self != NULL, 0);
	_tmp0_ = self->priv->_black_initial_seconds;
	result = _tmp0_;
	return result;
}


static void chess_clock_set_black_initial_seconds (ChessClock* self, gint value) {
	gint _tmp0_ = 0;
	g_return_if_fail (self != NULL);
	_tmp0_ = value;
	self->priv->_black_initial_seconds = _tmp0_;
	g_object_notify ((GObject *) self, "black-initial-seconds");
}


gint chess_clock_get_white_seconds_used (ChessClock* self) {
	gint result;
	gint _tmp0_ = 0;
	g_return_val_if_fail (self != NULL, 0);
	_tmp0_ = self->priv->_white_seconds_used;
	result = _tmp0_;
	return result;
}


static void chess_clock_set_white_seconds_used (ChessClock* self, gint value) {
	gint _tmp0_ = 0;
	g_return_if_fail (self != NULL);
	_tmp0_ = value;
	self->priv->_white_seconds_used = _tmp0_;
	g_object_notify ((GObject *) self, "white-seconds-used");
}


gint chess_clock_get_black_seconds_used (ChessClock* self) {
	gint result;
	gint _tmp0_ = 0;
	g_return_val_if_fail (self != NULL, 0);
	_tmp0_ = self->priv->_black_seconds_used;
	result = _tmp0_;
	return result;
}


static void chess_clock_set_black_seconds_used (ChessClock* self, gint value) {
	gint _tmp0_ = 0;
	g_return_if_fail (self != NULL);
	_tmp0_ = value;
	self->priv->_black_seconds_used = _tmp0_;
	g_object_notify ((GObject *) self, "black-seconds-used");
}


Color chess_clock_get_active_color (ChessClock* self) {
	Color result;
	Color _tmp0_ = 0;
	g_return_val_if_fail (self != NULL, 0);
	_tmp0_ = self->priv->_active_color;
	result = _tmp0_;
	return result;
}


void chess_clock_set_active_color (ChessClock* self, Color value) {
	Color _tmp0_ = 0;
	Color _tmp1_ = 0;
	Color _tmp2_ = 0;
	Color _tmp3_ = 0;
	g_return_if_fail (self != NULL);
	_tmp0_ = value;
	_tmp1_ = chess_clock_get_active_color (self);
	_tmp2_ = _tmp1_;
	if (_tmp0_ == _tmp2_) {
		return;
	}
	chess_clock_stop (self);
	_tmp3_ = value;
	self->priv->_active_color = _tmp3_;
	chess_clock_start (self);
	g_object_notify ((GObject *) self, "active-color");
}


static void chess_clock_class_init (ChessClockClass * klass) {
	chess_clock_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (ChessClockPrivate));
	G_OBJECT_CLASS (klass)->get_property = _vala_chess_clock_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_chess_clock_set_property;
	G_OBJECT_CLASS (klass)->finalize = chess_clock_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), CHESS_CLOCK_WHITE_INITIAL_SECONDS, g_param_spec_int ("white-initial-seconds", "white-initial-seconds", "white-initial-seconds", G_MININT, G_MAXINT, 0, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), CHESS_CLOCK_BLACK_INITIAL_SECONDS, g_param_spec_int ("black-initial-seconds", "black-initial-seconds", "black-initial-seconds", G_MININT, G_MAXINT, 0, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), CHESS_CLOCK_WHITE_SECONDS_USED, g_param_spec_int ("white-seconds-used", "white-seconds-used", "white-seconds-used", G_MININT, G_MAXINT, 0, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), CHESS_CLOCK_BLACK_SECONDS_USED, g_param_spec_int ("black-seconds-used", "black-seconds-used", "black-seconds-used", G_MININT, G_MAXINT, 0, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), CHESS_CLOCK_ACTIVE_COLOR, g_param_spec_enum ("active-color", "active-color", "active-color", TYPE_COLOR, 0, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE | G_PARAM_WRITABLE));
	g_signal_new ("tick", TYPE_CHESS_CLOCK, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
	g_signal_new ("expired", TYPE_CHESS_CLOCK, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
}


static void chess_clock_instance_init (ChessClock * self) {
	self->priv = CHESS_CLOCK_GET_PRIVATE (self);
	self->priv->_white_seconds_used = 0;
	self->priv->_black_seconds_used = 0;
	self->priv->_active_color = COLOR_WHITE;
	self->priv->tick_timeout_id = (guint) 0;
	self->priv->is_active = FALSE;
}


static void chess_clock_finalize (GObject* obj) {
	ChessClock * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_CHESS_CLOCK, ChessClock);
	_g_timer_destroy0 (self->priv->timer);
	G_OBJECT_CLASS (chess_clock_parent_class)->finalize (obj);
}


GType chess_clock_get_type (void) {
	static volatile gsize chess_clock_type_id__volatile = 0;
	if (g_once_init_enter (&chess_clock_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (ChessClockClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) chess_clock_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ChessClock), 0, (GInstanceInitFunc) chess_clock_instance_init, NULL };
		GType chess_clock_type_id;
		chess_clock_type_id = g_type_register_static (G_TYPE_OBJECT, "ChessClock", &g_define_type_info, 0);
		g_once_init_leave (&chess_clock_type_id__volatile, chess_clock_type_id);
	}
	return chess_clock_type_id__volatile;
}


static void _vala_chess_clock_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec) {
	ChessClock * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, TYPE_CHESS_CLOCK, ChessClock);
	switch (property_id) {
		case CHESS_CLOCK_WHITE_INITIAL_SECONDS:
		g_value_set_int (value, chess_clock_get_white_initial_seconds (self));
		break;
		case CHESS_CLOCK_BLACK_INITIAL_SECONDS:
		g_value_set_int (value, chess_clock_get_black_initial_seconds (self));
		break;
		case CHESS_CLOCK_WHITE_SECONDS_USED:
		g_value_set_int (value, chess_clock_get_white_seconds_used (self));
		break;
		case CHESS_CLOCK_BLACK_SECONDS_USED:
		g_value_set_int (value, chess_clock_get_black_seconds_used (self));
		break;
		case CHESS_CLOCK_ACTIVE_COLOR:
		g_value_set_enum (value, chess_clock_get_active_color (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}


static void _vala_chess_clock_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec) {
	ChessClock * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, TYPE_CHESS_CLOCK, ChessClock);
	switch (property_id) {
		case CHESS_CLOCK_WHITE_INITIAL_SECONDS:
		chess_clock_set_white_initial_seconds (self, g_value_get_int (value));
		break;
		case CHESS_CLOCK_BLACK_INITIAL_SECONDS:
		chess_clock_set_black_initial_seconds (self, g_value_get_int (value));
		break;
		case CHESS_CLOCK_WHITE_SECONDS_USED:
		chess_clock_set_white_seconds_used (self, g_value_get_int (value));
		break;
		case CHESS_CLOCK_BLACK_SECONDS_USED:
		chess_clock_set_black_seconds_used (self, g_value_get_int (value));
		break;
		case CHESS_CLOCK_ACTIVE_COLOR:
		chess_clock_set_active_color (self, g_value_get_enum (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}



