/*************************************************************************************************************
This file contains buddy functions
2012/6 by Dlxko
**************************************************************************************************************/
#include "BuddyList.h"

/******************************** My own shorting function ***********************************/
gint function_compare_rows (GtkTreeModel *model, GtkTreeIter  *a, GtkTreeIter  *b, gpointer userdata)
{
	// Definitions
	gchar *name1, *name2;
	gint ret;

	// Get variables
	gtk_tree_model_get(model, a, TEXT_COL2, &name1, -1);
	gtk_tree_model_get(model, b, TEXT_COL2, &name2, -1);

	// If something is empty
	if (name1 == NULL || name2 == NULL)
	{
		if (name1 == NULL && name2 == NULL)
			ret = 0;
		else
			ret = (name1 == NULL) ? -1 : 1;
	}
	else
	{
		// Get return value if non-empty
		ret = g_utf8_collate(name1,name2);
	}

	// Free variables
	g_free(name1);
	g_free(name2);

	// Return value
	return ret;
}

/******************************** After clicking on selection ***********************************/
void signal_list_row_activated(GtkTreeView *treeview,  GtkTreePath *path, GtkTreeViewColumn *col, gpointer userdata)
{
	// Gtk variables
	GtkTreeModel *model;
	GtkTreeIter   iter;
	gint buddy_number;
	gchar *name;

	// Get model
	model = gtk_tree_view_get_model(treeview);
	if (!model) function_show_warning("Can't create chat becouse of can't creating model!");

	// Get itter
	if (gtk_tree_model_get_iter(model, &iter, path))
	{
		// Get datas from colum
		gtk_tree_model_get(model, &iter, TEXT_COL, &name, -1);

		// Get current buddy
		buddy_number = function_get_buddy_number(name);
		if (buddy_number == MAX_BUDDIES + 1)
			function_show_warning("Selected buddy doesn't exit - can't open!");

		// Open chat and free name
		function_create_chat(buddy_number);
		g_free(name);
	}
	else function_show_warning("Can't get iter from buddylist!");
}

/******************************** Create new buddylist ***********************************/
bool create_buddylist()
{
	// Create buddy window
	buddy_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	if (!buddy_window) return false;
	gtk_window_set_title(GTK_WINDOW(buddy_window), "Contacts");
	gtk_window_set_default_size(GTK_WINDOW(buddy_window), 180, 400);
	gtk_window_set_icon(GTK_WINDOW(buddy_window), default_icon);

	// Create tree view
	buddy_tree_view = gtk_list_store_new(3, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_STRING);
	if (!buddy_tree_view) return false;

	// Create shortable list
	buddy_sortable_model = GTK_TREE_SORTABLE(buddy_tree_view);
	if (!buddy_sortable_model) return false;
	gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(buddy_sortable_model), TEXT_COL2, function_compare_rows, GINT_TO_POINTER(TEXT_COL2), NULL);
	gtk_tree_sortable_set_sort_column_id(buddy_sortable_model, TEXT_COL2, GTK_SORT_DESCENDING);

	// Create buddy treeView
	buddy_treeView = gtk_tree_view_new_with_model(GTK_TREE_MODEL(buddy_tree_view));
	if (!buddy_treeView) return false;

	// Create new renderer
	buddy_renderer = gtk_cell_renderer_pixbuf_new();
	if (!buddy_renderer) return false;

	// Create new avatar colum
	buddy_column = gtk_tree_view_column_new_with_attributes( "Avatar", buddy_renderer, "pixbuf", PIXBUF_COL, NULL);
	if (!buddy_column) return false;
	gtk_tree_view_append_column(GTK_TREE_VIEW (buddy_treeView), buddy_column);

	// Create new renderer
	buddy_renderer = gtk_cell_renderer_text_new();
	if (!buddy_renderer) return false;

	// Create new nickname colum
	buddy_column = gtk_tree_view_column_new_with_attributes("Nick", buddy_renderer, "text", TEXT_COL, NULL);
	if (!buddy_column) return false;
	gtk_tree_view_append_column(GTK_TREE_VIEW (buddy_treeView), buddy_column);

	// Create new renderer
	buddy_renderer = gtk_cell_renderer_text_new();
	if (!buddy_renderer) return false;

	// Create new colum status
	buddy_column = gtk_tree_view_column_new_with_attributes("Status", buddy_renderer, "text", TEXT_COL2, NULL);
	if (!buddy_column) return false;
	gtk_tree_view_append_column(GTK_TREE_VIEW (buddy_treeView), buddy_column);

	// Implement footer
	buddy_vbox = gtk_vbox_new(FALSE, 0);
	buddy_footer = gtk_vbox_new(FALSE, 0);
	if (!buddy_vbox || !buddy_footer)
		return false;

	// Pack them into container
	gtk_box_pack_start(GTK_BOX(buddy_vbox), buddy_treeView, TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(buddy_vbox), buddy_footer, FALSE, FALSE, 0);
	gtk_container_add(GTK_CONTAINER(buddy_window), buddy_vbox);

	// Handle signals
	buddy_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(buddy_treeView));
	if (!buddy_selection) return false;
	g_signal_connect(buddy_treeView, "row-activated", (GCallback) signal_list_row_activated, NULL);
	g_signal_connect(GTK_OBJECT(buddy_window), "destroy", G_CALLBACK(signal_go_offline), NULL);

	// Create horizontal footer
	buddy_hfooter = gtk_hbox_new(FALSE, 0);
	if (!buddy_hfooter) return false;
	gtk_box_pack_start(GTK_BOX(buddy_footer), buddy_hfooter, FALSE, FALSE, 0);

	// Add friend button
	buddy_add = gtk_button_new_with_label("+");
	if (!buddy_add) return false;
	g_signal_connect (G_OBJECT(buddy_add), "clicked", G_CALLBACK (signal_create_buddy_adder), NULL);
	gtk_widget_set_size_request(buddy_add, 35, 26);

	// Hide offlines button
	buddy_hide = gtk_button_new_with_label("V");
	if (!buddy_hide) return false;
	gtk_widget_set_size_request(buddy_hide, 35, 26);
	g_signal_connect (G_OBJECT(buddy_hide), "clicked", G_CALLBACK (function_buddy_hide), NULL);

	// Show requests
	buddy_requests = gtk_button_new_with_label("X");
	if (!buddy_requests) return false;
	gtk_widget_set_size_request(buddy_requests, 35, 26);

	// Pack them into footer
	gtk_box_pack_start(GTK_BOX(buddy_hfooter), buddy_add, FALSE, FALSE, 0);
	gtk_box_pack_start(GTK_BOX(buddy_hfooter), buddy_hide, FALSE, FALSE, 0);
	gtk_box_pack_start(GTK_BOX(buddy_hfooter), buddy_requests, FALSE, FALSE, 0);

	return true;
}

/************* Create new requests window *******************/
bool crate_requests()
{
	// Create window
	requests_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	if (!requests_window) return false;
	gtk_window_set_title(GTK_WINDOW(requests_window), "Friend requests");
	gtk_window_set_default_size(GTK_WINDOW(requests_window), 380, 300);
	gtk_widget_hide_on_delete(requests_window);
	g_signal_connect_swapped (requests_window, "delete-event", G_CALLBACK (signal_close_requests), NULL);
	gtk_window_set_icon(GTK_WINDOW(requests_window), default_icon);

	// Create new vbox for everything to add in
	requests_vbox = gtk_vbox_new(FALSE, 0);
	if (!requests_vbox) return false;
	gtk_container_add (GTK_CONTAINER (requests_window), requests_vbox);

	// Show box
	gtk_widget_show(requests_vbox);

	return true;
}