/* score-dialog.c generated by valac 0.30.0, the Vala compiler
 * generated from score-dialog.vala, do not modify */

/*
 * Copyright (C) 2010-2013 Robert Ancell
 *
 * 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>
#include <gtk/gtk.h>
#include <glib/gi18n-lib.h>
#include <stdlib.h>
#include <string.h>
#include <pango/pango.h>
#include <float.h>
#include <math.h>


#define TYPE_SCORE_DIALOG (score_dialog_get_type ())
#define SCORE_DIALOG(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_SCORE_DIALOG, ScoreDialog))
#define SCORE_DIALOG_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_SCORE_DIALOG, ScoreDialogClass))
#define IS_SCORE_DIALOG(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_SCORE_DIALOG))
#define IS_SCORE_DIALOG_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_SCORE_DIALOG))
#define SCORE_DIALOG_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_SCORE_DIALOG, ScoreDialogClass))

typedef struct _ScoreDialog ScoreDialog;
typedef struct _ScoreDialogClass ScoreDialogClass;
typedef struct _ScoreDialogPrivate ScoreDialogPrivate;

#define TYPE_HISTORY (history_get_type ())
#define HISTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_HISTORY, History))
#define HISTORY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_HISTORY, HistoryClass))
#define IS_HISTORY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_HISTORY))
#define IS_HISTORY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_HISTORY))
#define HISTORY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_HISTORY, HistoryClass))

typedef struct _History History;
typedef struct _HistoryClass HistoryClass;

#define TYPE_HISTORY_ENTRY (history_entry_get_type ())
#define HISTORY_ENTRY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_HISTORY_ENTRY, HistoryEntry))
#define HISTORY_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_HISTORY_ENTRY, HistoryEntryClass))
#define IS_HISTORY_ENTRY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_HISTORY_ENTRY))
#define IS_HISTORY_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_HISTORY_ENTRY))
#define HISTORY_ENTRY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_HISTORY_ENTRY, HistoryEntryClass))

typedef struct _HistoryEntry HistoryEntry;
typedef struct _HistoryEntryClass HistoryEntryClass;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _HistoryPrivate HistoryPrivate;
#define _g_list_free0(var) ((var == NULL) ? NULL : (var = (g_list_free (var), NULL)))
typedef struct _HistoryEntryPrivate HistoryEntryPrivate;
#define _gtk_tree_path_free0(var) ((var == NULL) ? NULL : (var = (gtk_tree_path_free (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))

struct _ScoreDialog {
	GtkDialog parent_instance;
	ScoreDialogPrivate * priv;
};

struct _ScoreDialogClass {
	GtkDialogClass parent_class;
};

struct _ScoreDialogPrivate {
	History* history;
	HistoryEntry* selected_entry;
	GtkListStore* score_model;
	GtkTreeView* scores;
};

struct _History {
	GObject parent_instance;
	HistoryPrivate * priv;
	gchar* filename;
	GList* entries;
};

struct _HistoryClass {
	GObjectClass parent_class;
};

struct _HistoryEntry {
	GObject parent_instance;
	HistoryEntryPrivate * priv;
	GDateTime* date;
	gint score;
};

struct _HistoryEntryClass {
	GObjectClass parent_class;
};


static gpointer score_dialog_parent_class = NULL;

GType score_dialog_get_type (void) G_GNUC_CONST;
GType history_get_type (void) G_GNUC_CONST;
GType history_entry_get_type (void) G_GNUC_CONST;
#define SCORE_DIALOG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_SCORE_DIALOG, ScoreDialogPrivate))
enum  {
	SCORE_DIALOG_DUMMY_PROPERTY
};
ScoreDialog* score_dialog_new (History* history, HistoryEntry* selected_entry, gboolean show_close);
ScoreDialog* score_dialog_construct (GType object_type, History* history, HistoryEntry* selected_entry, gboolean show_close);
static void score_dialog_entry_added_cb (ScoreDialog* self, HistoryEntry* entry);
static void _score_dialog_entry_added_cb_history_entry_added (History* _sender, HistoryEntry* entry, gpointer self);
static gint score_dialog_compare_entries (HistoryEntry* a, HistoryEntry* b);
static gint _score_dialog_compare_entries_gcompare_func (gconstpointer a, gconstpointer b);
static void score_dialog_finalize (GObject* obj);


static gpointer _g_object_ref0 (gpointer self) {
	return self ? g_object_ref (self) : NULL;
}


static void _score_dialog_entry_added_cb_history_entry_added (History* _sender, HistoryEntry* entry, gpointer self) {
	score_dialog_entry_added_cb ((ScoreDialog*) self, entry);
}


static gint _score_dialog_compare_entries_gcompare_func (gconstpointer a, gconstpointer b) {
	gint result;
	result = score_dialog_compare_entries ((HistoryEntry*) a, (HistoryEntry*) b);
	return result;
}


ScoreDialog* score_dialog_construct (GType object_type, History* history, HistoryEntry* selected_entry, gboolean show_close) {
	ScoreDialog * self = NULL;
	History* _tmp0_ = NULL;
	History* _tmp1_ = NULL;
	History* _tmp2_ = NULL;
	HistoryEntry* _tmp3_ = NULL;
	HistoryEntry* _tmp4_ = NULL;
	gboolean _tmp5_ = FALSE;
	GtkBox* vbox = NULL;
	GtkBox* _tmp9_ = NULL;
	GtkBox* _tmp10_ = NULL;
	GtkBox* _tmp11_ = NULL;
	GtkBox* _tmp12_ = NULL;
	GtkBox* _tmp13_ = NULL;
	GtkScrolledWindow* scroll = NULL;
	GtkScrolledWindow* _tmp14_ = NULL;
	GtkScrolledWindow* _tmp15_ = NULL;
	GtkScrolledWindow* _tmp16_ = NULL;
	GtkScrolledWindow* _tmp17_ = NULL;
	GtkBox* _tmp18_ = NULL;
	GtkScrolledWindow* _tmp19_ = NULL;
	GtkListStore* _tmp20_ = NULL;
	GtkTreeView* _tmp21_ = NULL;
	GtkCellRendererText* renderer = NULL;
	GtkCellRendererText* _tmp22_ = NULL;
	GtkTreeView* _tmp23_ = NULL;
	const gchar* _tmp24_ = NULL;
	GtkCellRendererText* _tmp25_ = NULL;
	GtkCellRendererText* _tmp26_ = NULL;
	GtkCellRendererText* _tmp27_ = NULL;
	GtkTreeView* _tmp28_ = NULL;
	const gchar* _tmp29_ = NULL;
	GtkCellRendererText* _tmp30_ = NULL;
	GtkTreeView* _tmp31_ = NULL;
	GtkListStore* _tmp32_ = NULL;
	GtkTreeView* _tmp33_ = NULL;
	GtkScrolledWindow* _tmp34_ = NULL;
	GtkTreeView* _tmp35_ = NULL;
	GList* entries = NULL;
	History* _tmp36_ = NULL;
	GList* _tmp37_ = NULL;
	GList* _tmp38_ = NULL;
	GList* _tmp39_ = NULL;
	g_return_val_if_fail (history != NULL, NULL);
	self = (ScoreDialog*) g_object_new (object_type, NULL);
	_tmp0_ = history;
	_tmp1_ = _g_object_ref0 (_tmp0_);
	_g_object_unref0 (self->priv->history);
	self->priv->history = _tmp1_;
	_tmp2_ = history;
	g_signal_connect_object (_tmp2_, "entry-added", (GCallback) _score_dialog_entry_added_cb_history_entry_added, self, 0);
	_tmp3_ = selected_entry;
	_tmp4_ = _g_object_ref0 (_tmp3_);
	_g_object_unref0 (self->priv->selected_entry);
	self->priv->selected_entry = _tmp4_;
	_tmp5_ = show_close;
	if (_tmp5_) {
		const gchar* _tmp6_ = NULL;
		const gchar* _tmp7_ = NULL;
		_tmp6_ = _ ("_Close");
		gtk_dialog_add_button ((GtkDialog*) self, _tmp6_, (gint) GTK_RESPONSE_CLOSE);
		_tmp7_ = _ ("New Game");
		gtk_dialog_add_button ((GtkDialog*) self, _tmp7_, (gint) GTK_RESPONSE_OK);
	} else {
		const gchar* _tmp8_ = NULL;
		_tmp8_ = _ ("_OK");
		gtk_dialog_add_button ((GtkDialog*) self, _tmp8_, (gint) GTK_RESPONSE_DELETE_EVENT);
	}
	gtk_widget_set_size_request ((GtkWidget*) self, 200, 300);
	_tmp9_ = (GtkBox*) gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
	g_object_ref_sink (_tmp9_);
	vbox = _tmp9_;
	_tmp10_ = vbox;
	gtk_container_set_border_width ((GtkContainer*) _tmp10_, (guint) 6);
	_tmp11_ = vbox;
	gtk_widget_show ((GtkWidget*) _tmp11_);
	_tmp12_ = (GtkBox*) gtk_dialog_get_content_area ((GtkDialog*) self);
	_tmp13_ = vbox;
	gtk_box_pack_start (_tmp12_, (GtkWidget*) _tmp13_, TRUE, TRUE, (guint) 0);
	_tmp14_ = (GtkScrolledWindow*) gtk_scrolled_window_new (NULL, NULL);
	g_object_ref_sink (_tmp14_);
	scroll = _tmp14_;
	_tmp15_ = scroll;
	gtk_scrolled_window_set_shadow_type (_tmp15_, GTK_SHADOW_ETCHED_IN);
	_tmp16_ = scroll;
	gtk_scrolled_window_set_policy (_tmp16_, GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
	_tmp17_ = scroll;
	gtk_widget_show ((GtkWidget*) _tmp17_);
	_tmp18_ = vbox;
	_tmp19_ = scroll;
	gtk_box_pack_start (_tmp18_, (GtkWidget*) _tmp19_, TRUE, TRUE, (guint) 0);
	_tmp20_ = gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT, -1);
	_g_object_unref0 (self->priv->score_model);
	self->priv->score_model = _tmp20_;
	_tmp21_ = (GtkTreeView*) gtk_tree_view_new ();
	g_object_ref_sink (_tmp21_);
	_g_object_unref0 (self->priv->scores);
	self->priv->scores = _tmp21_;
	_tmp22_ = (GtkCellRendererText*) gtk_cell_renderer_text_new ();
	g_object_ref_sink (_tmp22_);
	renderer = _tmp22_;
	_tmp23_ = self->priv->scores;
	_tmp24_ = _ ("Date");
	_tmp25_ = renderer;
	gtk_tree_view_insert_column_with_attributes (_tmp23_, -1, _tmp24_, (GtkCellRenderer*) _tmp25_, "text", 0, "weight", 2, NULL);
	_tmp26_ = (GtkCellRendererText*) gtk_cell_renderer_text_new ();
	g_object_ref_sink (_tmp26_);
	_g_object_unref0 (renderer);
	renderer = _tmp26_;
	_tmp27_ = renderer;
	g_object_set ((GtkCellRenderer*) _tmp27_, "xalign", 1.0f, NULL);
	_tmp28_ = self->priv->scores;
	_tmp29_ = _ ("Score");
	_tmp30_ = renderer;
	gtk_tree_view_insert_column_with_attributes (_tmp28_, -1, _tmp29_, (GtkCellRenderer*) _tmp30_, "text", 1, "weight", 2, NULL);
	_tmp31_ = self->priv->scores;
	_tmp32_ = self->priv->score_model;
	gtk_tree_view_set_model (_tmp31_, (GtkTreeModel*) _tmp32_);
	_tmp33_ = self->priv->scores;
	gtk_widget_show ((GtkWidget*) _tmp33_);
	_tmp34_ = scroll;
	_tmp35_ = self->priv->scores;
	gtk_container_add ((GtkContainer*) _tmp34_, (GtkWidget*) _tmp35_);
	_tmp36_ = history;
	_tmp37_ = _tmp36_->entries;
	_tmp38_ = g_list_copy (_tmp37_);
	entries = _tmp38_;
	entries = g_list_sort (entries, _score_dialog_compare_entries_gcompare_func);
	_tmp39_ = entries;
	{
		GList* entry_collection = NULL;
		GList* entry_it = NULL;
		entry_collection = _tmp39_;
		for (entry_it = entry_collection; entry_it != NULL; entry_it = entry_it->next) {
			HistoryEntry* entry = NULL;
			entry = (HistoryEntry*) entry_it->data;
			{
				HistoryEntry* _tmp40_ = NULL;
				_tmp40_ = entry;
				score_dialog_entry_added_cb (self, _tmp40_);
			}
		}
	}
	_g_list_free0 (entries);
	_g_object_unref0 (renderer);
	_g_object_unref0 (scroll);
	_g_object_unref0 (vbox);
	return self;
}


ScoreDialog* score_dialog_new (History* history, HistoryEntry* selected_entry, gboolean show_close) {
	return score_dialog_construct (TYPE_SCORE_DIALOG, history, selected_entry, show_close);
}


static gint score_dialog_compare_entries (HistoryEntry* a, HistoryEntry* b) {
	gint result = 0;
	HistoryEntry* _tmp0_ = NULL;
	gint _tmp1_ = 0;
	HistoryEntry* _tmp2_ = NULL;
	gint _tmp3_ = 0;
	HistoryEntry* _tmp8_ = NULL;
	GDateTime* _tmp9_ = NULL;
	HistoryEntry* _tmp10_ = NULL;
	GDateTime* _tmp11_ = NULL;
	gint _tmp12_ = 0;
	g_return_val_if_fail (a != NULL, 0);
	g_return_val_if_fail (b != NULL, 0);
	_tmp0_ = a;
	_tmp1_ = _tmp0_->score;
	_tmp2_ = b;
	_tmp3_ = _tmp2_->score;
	if (_tmp1_ != _tmp3_) {
		HistoryEntry* _tmp4_ = NULL;
		gint _tmp5_ = 0;
		HistoryEntry* _tmp6_ = NULL;
		gint _tmp7_ = 0;
		_tmp4_ = b;
		_tmp5_ = _tmp4_->score;
		_tmp6_ = a;
		_tmp7_ = _tmp6_->score;
		result = _tmp5_ - _tmp7_;
		return result;
	}
	_tmp8_ = a;
	_tmp9_ = _tmp8_->date;
	_tmp10_ = b;
	_tmp11_ = _tmp10_->date;
	_tmp12_ = g_date_time_compare (_tmp9_, _tmp11_);
	result = _tmp12_;
	return result;
}


static void score_dialog_entry_added_cb (ScoreDialog* self, HistoryEntry* entry) {
	gchar* date_label = NULL;
	HistoryEntry* _tmp0_ = NULL;
	GDateTime* _tmp1_ = NULL;
	gchar* _tmp2_ = NULL;
	gchar* score_label = NULL;
	HistoryEntry* _tmp3_ = NULL;
	gint _tmp4_ = 0;
	gchar* _tmp5_ = NULL;
	gint weight = 0;
	HistoryEntry* _tmp6_ = NULL;
	HistoryEntry* _tmp7_ = NULL;
	GtkTreeIter iter = {0};
	GtkListStore* _tmp8_ = NULL;
	GtkTreeIter _tmp9_ = {0};
	GtkListStore* _tmp10_ = NULL;
	GtkTreeIter _tmp11_ = {0};
	const gchar* _tmp12_ = NULL;
	const gchar* _tmp13_ = NULL;
	gint _tmp14_ = 0;
	HistoryEntry* _tmp15_ = NULL;
	HistoryEntry* _tmp16_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (entry != NULL);
	_tmp0_ = entry;
	_tmp1_ = _tmp0_->date;
	_tmp2_ = g_date_time_format (_tmp1_, "%d/%m/%Y");
	date_label = _tmp2_;
	_tmp3_ = entry;
	_tmp4_ = _tmp3_->score;
	_tmp5_ = g_strdup_printf ("%i", _tmp4_);
	score_label = _tmp5_;
	weight = (gint) PANGO_WEIGHT_NORMAL;
	_tmp6_ = entry;
	_tmp7_ = self->priv->selected_entry;
	if (_tmp6_ == _tmp7_) {
		weight = (gint) PANGO_WEIGHT_BOLD;
	}
	_tmp8_ = self->priv->score_model;
	gtk_list_store_append (_tmp8_, &_tmp9_);
	iter = _tmp9_;
	_tmp10_ = self->priv->score_model;
	_tmp11_ = iter;
	_tmp12_ = date_label;
	_tmp13_ = score_label;
	_tmp14_ = weight;
	gtk_list_store_set (_tmp10_, &_tmp11_, 0, _tmp12_, 1, _tmp13_, 2, _tmp14_, -1);
	_tmp15_ = entry;
	_tmp16_ = self->priv->selected_entry;
	if (_tmp15_ == _tmp16_) {
		GtkTreeIter piter = {0};
		GtkTreeIter _tmp17_ = {0};
		GtkListStore* _tmp18_ = NULL;
		gboolean _tmp19_ = FALSE;
		GtkTreeView* _tmp25_ = NULL;
		GtkListStore* _tmp26_ = NULL;
		GtkTreeIter _tmp27_ = {0};
		GtkTreePath* _tmp28_ = NULL;
		GtkTreePath* _tmp29_ = NULL;
		_tmp17_ = iter;
		piter = _tmp17_;
		_tmp18_ = self->priv->score_model;
		_tmp19_ = gtk_tree_model_iter_previous ((GtkTreeModel*) _tmp18_, &piter);
		if (_tmp19_) {
			GtkTreeIter ppiter = {0};
			GtkTreeIter _tmp20_ = {0};
			GtkListStore* _tmp21_ = NULL;
			gboolean _tmp22_ = FALSE;
			_tmp20_ = piter;
			ppiter = _tmp20_;
			_tmp21_ = self->priv->score_model;
			_tmp22_ = gtk_tree_model_iter_previous ((GtkTreeModel*) _tmp21_, &ppiter);
			if (_tmp22_) {
				GtkTreeIter _tmp23_ = {0};
				_tmp23_ = ppiter;
				piter = _tmp23_;
			}
		} else {
			GtkTreeIter _tmp24_ = {0};
			_tmp24_ = iter;
			piter = _tmp24_;
		}
		_tmp25_ = self->priv->scores;
		_tmp26_ = self->priv->score_model;
		_tmp27_ = piter;
		_tmp28_ = gtk_tree_model_get_path ((GtkTreeModel*) _tmp26_, &_tmp27_);
		_tmp29_ = _tmp28_;
		gtk_tree_view_scroll_to_cell (_tmp25_, _tmp29_, NULL, FALSE, (gfloat) 0, (gfloat) 0);
		_gtk_tree_path_free0 (_tmp29_);
	}
	_g_free0 (score_label);
	_g_free0 (date_label);
}


static void score_dialog_class_init (ScoreDialogClass * klass) {
	score_dialog_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (ScoreDialogPrivate));
	G_OBJECT_CLASS (klass)->finalize = score_dialog_finalize;
}


static void score_dialog_instance_init (ScoreDialog * self) {
	self->priv = SCORE_DIALOG_GET_PRIVATE (self);
	self->priv->selected_entry = NULL;
}


static void score_dialog_finalize (GObject* obj) {
	ScoreDialog * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_SCORE_DIALOG, ScoreDialog);
	_g_object_unref0 (self->priv->history);
	_g_object_unref0 (self->priv->selected_entry);
	_g_object_unref0 (self->priv->score_model);
	_g_object_unref0 (self->priv->scores);
	G_OBJECT_CLASS (score_dialog_parent_class)->finalize (obj);
}


GType score_dialog_get_type (void) {
	static volatile gsize score_dialog_type_id__volatile = 0;
	if (g_once_init_enter (&score_dialog_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (ScoreDialogClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) score_dialog_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ScoreDialog), 0, (GInstanceInitFunc) score_dialog_instance_init, NULL };
		GType score_dialog_type_id;
		score_dialog_type_id = g_type_register_static (gtk_dialog_get_type (), "ScoreDialog", &g_define_type_info, 0);
		g_once_init_leave (&score_dialog_type_id__volatile, score_dialog_type_id);
	}
	return score_dialog_type_id__volatile;
}



