/*************************************************************************************************************
* This file contains buddy compare row function, creating the list, and signal when activated
* 2012/8 by Dlxko
* Version 3.0
**************************************************************************************************************/
#include "BuddyList.h"

//For signal when clicked
static GtkTreeModel *model;
static GtkTreeIter iter;
static gint buddy_number;
static gint return_value;

// For creating buddy list
static GtkWidget *buddy_scroll;
static GtkCellRenderer *buddy_renderer;
static GtkWidget *buddy_treeView;
static GtkTreeViewColumn *buddy_column;
static GtkWidget *buddy_vbox;
static GtkTreeSelection *buddy_selection;
static GtkToolItem *buddy_addremo;
static GtkToolItem *buddy_showhide;

// My own shorting function which is used by three model
gint function_compare_rows (GtkTreeModel *input_treemodel, GtkTreeIter  *input_first_iter, GtkTreeIter  *input_second_iter, gpointer userdata)
{
	// Empty returning value
	return_value = 0;

	// Create pointers for names
	gchar *first_name, *second_name;

	// Get the names for future use
	gtk_tree_model_get(input_treemodel, input_first_iter, TEXT_COL2, &first_name, -1);
	gtk_tree_model_get(input_treemodel, input_second_iter, TEXT_COL2, &second_name, -1);

	// If some name is empty
	if (first_name == NULL || second_name == NULL)
	{
		// If both are empty return zero, else -1 or 1
		if (first_name == NULL && second_name == NULL)
			return_value = 0;
		else
			return_value = (first_name == NULL) ? -1 : 1;
	}
	// Get return value if non-empty by collate
	else return_value = g_utf8_collate(first_name,second_name);

	// Free variables
	g_free(first_name);
	g_free(second_name);

	// Return value
	return return_value;
}

// Function after clicking to some buddy in buddy list ( double click )
void signal_list_row_activated(GtkTreeView *input_treeview,  GtkTreePath *input_tree_path, GtkTreeViewColumn *input_colum, gpointer userdata)
{
	// Create pointer to memory
	gchar *clicked_name;

	// Get model or report with warning
	model = gtk_tree_view_get_model(input_treeview);

	// Get iter or show warning
	if (gtk_tree_model_get_iter(model, &iter, input_tree_path))
	{
		// Get datas from colum
		gtk_tree_model_get(model, &iter, TEXT_COL, &clicked_name, -1);

		// Get current buddy
		buddy_number = function_get_buddy_number(clicked_name);
		if (buddy_number == MAX_BUDDIES + 1)
			function_show_warning(LANG_XBUDDY_XCHAT);

		// Open chat
		function_create_chat(buddy_number);

		// Free memory
		g_free(clicked_name);
	}
	else function_show_warning(LANG_XITER_BUDDYLIST);
}

// Create new buddy list
void create_buddylist()
{
	// Create buddy window
	buddy_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(GTK_WINDOW(buddy_window), LANG_CONTACTS);
	gtk_window_set_default_size(GTK_WINDOW(buddy_window), 180, 400);
	gtk_window_set_icon(GTK_WINDOW(buddy_window), default_icon);

	// Create scroll, set options, and pack into vbox
	buddy_scroll = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(buddy_scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

	// Create tree view
	buddy_tree_view = gtk_list_store_new(3, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_STRING);

	// Create shortable list
	buddy_sortable_model = GTK_TREE_SORTABLE(buddy_tree_view);
	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 and renderer
	buddy_treeView = gtk_tree_view_new_with_model(GTK_TREE_MODEL(buddy_tree_view));
	buddy_renderer = gtk_cell_renderer_pixbuf_new();

	// Create new avatar colum and renderer
	buddy_column = gtk_tree_view_column_new_with_attributes(LANG_AVATAR, buddy_renderer, "pixbuf", PIXBUF_COL, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW (buddy_treeView), buddy_column);
	buddy_renderer = gtk_cell_renderer_text_new();

	// Create new nickname colum and renderer
	buddy_column = gtk_tree_view_column_new_with_attributes(LANG_NICKNAME, buddy_renderer, "text", TEXT_COL, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW (buddy_treeView), buddy_column);
	buddy_renderer = gtk_cell_renderer_text_new();

	// Create new colum status
	buddy_column = gtk_tree_view_column_new_with_attributes(LANG_STATUS, buddy_renderer, "text", TEXT_COL2, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW (buddy_treeView), buddy_column);

	// Implement header
	buddy_vbox = gtk_vbox_new(FALSE, 0);

	// Create toolbar
	buddy_toolbar = gtk_toolbar_new();
	gtk_toolbar_set_style(GTK_TOOLBAR(buddy_toolbar), GTK_TOOLBAR_ICONS);
	gtk_container_set_border_width(GTK_CONTAINER(buddy_toolbar), 2);

	//Create buttons
	buddy_addremo = gtk_tool_button_new_from_stock(GTK_STOCK_ADD);
	gtk_toolbar_insert(GTK_TOOLBAR(buddy_toolbar), buddy_addremo, -1);
	buddy_showhide = gtk_tool_button_new_from_stock(GTK_STOCK_CLEAR);
	gtk_toolbar_insert(GTK_TOOLBAR(buddy_toolbar), buddy_showhide, -1);
	buddy_events = gtk_tool_button_new_from_stock(GTK_STOCK_NO);
	gtk_toolbar_insert(GTK_TOOLBAR(buddy_toolbar), buddy_events, -1);

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

	// Create selection
	buddy_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(buddy_treeView));

	// Handle signals
	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);
	g_signal_connect(G_OBJECT(buddy_addremo), "clicked", G_CALLBACK(signal_create_buddy_adder), NULL);
	g_signal_connect(G_OBJECT(buddy_showhide), "clicked", G_CALLBACK(function_buddy_hide), NULL);
}