/*************************************************************************************************************
* This file contains buddy functions
* 2012/8 by Dlxko
* Version 3.0
**************************************************************************************************************/
#include "Buddyactions.h"

// Local statics
static bool hidden_offlines;
static int buddy_number;
static bool handler;
static gchar *file_avatar;
static GdkPixbuf *buddy_pixbuf;
static GtkTreeIter buddy_iter;

void additional_function_buddyactions_create_row_with_friend(int fx_buddy_number, char fx_nickname[DATA_ONE_SIZE+1], int fx_type)
{
	// Alloc error
	GError *px_error = NULL;

	// Create new row with this avatar
	file_avatar = g_strconcat("avatars/", fx_nickname, ".png", NULL);
	buddy_pixbuf = gdk_pixbuf_new_from_file_at_scale(file_avatar, 25, 25, FALSE, &px_error);
	if (px_error)
	{
		function_show_warning(LANG_CANT_LOAD_AVATAR);
		buddy_pixbuf = default_buddy_pixbuf;
		g_error_free(px_error);
	}

	// Insert in the list
	gtk_list_store_append(buddy_tree_view, &buddy_iter);
	if (fx_type == 1)
		gtk_list_store_set(buddy_tree_view, &buddy_iter, PIXBUF_COL, buddy_pixbuf, TEXT_COL, fx_nickname, TEXT_COL2, LANG_STATUS_ONLINE, -1);
	else if (fx_type == 2)
		gtk_list_store_set(buddy_tree_view, &buddy_iter, PIXBUF_COL, buddy_pixbuf, TEXT_COL, fx_nickname, TEXT_COL2, LANG_STATUS_OFFLINE, -1);

	// Sort list
	gtk_tree_sortable_set_sort_column_id(buddy_sortable_model, TEXT_COL2, GTK_SORT_DESCENDING);
}

// Function to make buddy online
int function_make_online(char nick_name[DATA_ONE_SIZE+1])
{
	// Get buddy number
	buddy_number = function_get_buddy_number(nick_name);

	// If some problem while searching for buddy
	if (buddy_number == (MAX_BUDDIES + 1))
	{
		function_show_warning(LANG_WRONG_BUDDY_NUMBER_WHEN_ONLINING);
		buddy[buddy_number].listed = false;
		return (MAX_BUDDIES + 1);
	}
	else
	{
		// Destroy old widget
		gtk_list_store_remove(buddy_tree_view, buddy[buddy_number].tree_iter);

		// Create new row with friend
		additional_function_buddyactions_create_row_with_friend(buddy_number, buddy[buddy_number].nick_name, 1);

		// Update buddy struct info
		buddy[buddy_number].tree_iter = gtk_tree_iter_copy (&buddy_iter);
		buddy[buddy_number].online = true;
		buddy[buddy_number].listed = true;

		// Return its number
		return buddy_number;
	}
}

// Function to create or make buddy offline
bool function_make_offline(char nick_name[10])
{
	// Already existing buddy
	if (function_get_buddy_number(nick_name) != (MAX_BUDDIES + 1))
	{
		// Get buddy number
		buddy_number = function_get_buddy_number(nick_name);

		// Remove previous row
		gtk_list_store_remove(buddy_tree_view, buddy[buddy_number].tree_iter);

		// Create new row if showing offlines
		if (hidden_offlines == false)
		{
			// Create new row with friend
			additional_function_buddyactions_create_row_with_friend(buddy_number, buddy[buddy_number].nick_name, 2);

			// Update buddy struct info
			buddy[buddy_number].tree_iter = gtk_tree_iter_copy (&buddy_iter);
			buddy[buddy_number].listed = true;
		}
		else buddy[buddy_number].listed = false;

		// Update buddy struct info
		buddy[buddy_number].online = false;

		// Return its number
		return true;
	}

	// If new buddy comming set offline for default
	else if (function_get_buddy_number(nick_name) == (MAX_BUDDIES + 1))
	{
		// Next buddy
		++current_buddy;

		// If maximum buddies report
		if(current_buddy > MAX_BUDDIES)
		{
			function_show_warning(LANG_REACHED_BUDDY_LIMIT);
			return false;
		}

		// Create new row if showing offlines
		if (hidden_offlines == false)
		{
			// Create new row with friend
			additional_function_buddyactions_create_row_with_friend(current_buddy, nick_name, 2);

			// Update structure info
			buddy[current_buddy].tree_iter = gtk_tree_iter_copy (&buddy_iter);
			buddy[current_buddy].listed = true;
		}
		else buddy[current_buddy].listed = false;

		// Update buddy struct info
		buddy[current_buddy].online = false;
		strcpy_s(buddy[current_buddy].nick_name, 10, nick_name);
	}

	// Return true
	return true;
}

// Function to hide offlines
void function_buddy_hide()
{
	// Refresh counter
	buddy_number = 0;

	do
	{
		// Of hiddening isn't online and want to hide and is also in buddy list
		if (hidden_offlines == false && buddy[buddy_number].online == false && buddy[buddy_number].listed == true && strlen(buddy[buddy_number].nick_name) && buddy[buddy_number].deleted == false)
		{
			// Remove row, change listed to false and set handler
			gtk_list_store_remove(buddy_tree_view, buddy[buddy_number].tree_iter);
			buddy[buddy_number].listed = false;
			handler = true;
		}

		// Else if hiddening isn't online and want to show and isn't in list
		else if (hidden_offlines == true && buddy[buddy_number].listed == false && buddy[buddy_number].online == false && strlen(buddy[buddy_number].nick_name) && buddy[buddy_number].deleted == false)
		{
			// Create new row with friend
			additional_function_buddyactions_create_row_with_friend(buddy_number, buddy[buddy_number].nick_name, 2);

			// Update structure
			buddy[buddy_number].tree_iter = gtk_tree_iter_copy (&buddy_iter);
			buddy[buddy_number].listed = true;
			handler = false;
		}

		++buddy_number;
	}
	while (buddy_number < MAX_BUDDIES);

	// Set global handler from local
	if (handler == true)
		hidden_offlines = true;
	else
		hidden_offlines = false;
}