
#include <ocarina/gtk.h>
#include <libsaria/library.h>
#include <libsaria/print.h>

#include "songlist.h"

SongList::SongList()
{
}

SongList::~SongList()
{
}

string columns[7] = { "Id", "#", "Title", "Length", "Artist", "Album", "Year" };
int col_width[7]  = {    2,  20,     300,       60,      125,     125,     50 };

static void songlist_row_activated(GtkTreeView *treeview, GtkTreePath *path,
				   GtkTreeViewColumn *column, gpointer data)
{
	GtkTreeIter iter;
	ino_t inode;
	GtkTreeModel *model = gtk_tree_view_get_model(treeview);
	gtk_tree_model_get_iter(model, &iter, path);
	gtk_tree_model_get(model, &iter, 0, &inode, -1);
	print("Double click! ");
	println(inode);
	libsaria_play_id(inode);
}

void SongList::add_column(unsigned int index)
{
	GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes(
					columns[index].c_str(), text_cell,
					"text", index, NULL);
	gtk_tree_view_column_set_resizable(column, TRUE);
	gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
	gtk_tree_view_column_set_min_width(column, 2);
	gtk_tree_view_column_set_max_width(column, 700);
	gtk_tree_view_column_set_fixed_width(column, col_width[index]);
	gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
}

/*
 * I need an init() function to set up the scrolled window AFTER
 * the gtk_init() function has been called.
 */
void SongList::init(string text)
{
	window = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(window),
				       GTK_POLICY_AUTOMATIC,
				       GTK_POLICY_AUTOMATIC);

	treeview  = gtk_tree_view_new();
	text_cell = gtk_cell_renderer_text_new();
	gtk_cell_renderer_text_set_fixed_height_from_font(
					GTK_CELL_RENDERER_TEXT(text_cell), 1);
	GTK_CONNECT(treeview, "row-activated", songlist_row_activated, NULL);

	liststore = gtk_list_store_new(7, G_TYPE_LONG, G_TYPE_INT, G_TYPE_STRING,
				       G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
				       G_TYPE_INT);
	for (unsigned int i = 0; i < 7; i++)
		add_column(i);

	gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(treeview), TRUE);
	gtk_tree_view_set_model(GTK_TREE_VIEW(treeview), GTK_TREE_MODEL(liststore));
	container_add(window, treeview);
	gtk_widget_show_all(window);

	label = gtk_label_new(text.c_str());
	gtk_label_set_angle(GTK_LABEL(label), 90);
	gtk_widget_show(label);
}

GtkWidget *SongList::get_window()
{
	return window;
}

GtkWidget *SongList::get_label()
{
	return label;
}

void SongList::freeze()
{
	gtk_widget_freeze_child_notify(treeview);
	gtk_tree_view_set_model(GTK_TREE_VIEW(treeview), NULL);
}

void SongList::thaw()
{
	gtk_widget_thaw_child_notify(treeview);
	gtk_tree_view_set_model(GTK_TREE_VIEW(treeview), GTK_TREE_MODEL(liststore));
}

void SongList::clear()
{
	freeze();
	gtk_list_store_clear(liststore);
	thaw();
}

void SongList::insert(list<Track> &track_list)
{
	int ins_next = 0;
	Track *track;
	list<Track>::iterator it;

	freeze();
	for (it = track_list.begin(); it != track_list.end(); it++) {
		track = &(*it);
		gtk_list_store_insert_with_values(liststore, NULL, ins_next,
				0, track->get_inode(),
				1, track->get_track(),
				2, track->get_title().c_str(),
				3, track->get_lenstr().c_str(),
				4, track->get_artist().c_str(),
				5, track->get_album().c_str(),
				6, track->get_year(),
				-1);
		ins_next++;
	}
	thaw();
}
