/*************************************************************************************************************
* This file contains buddies functions
* 2012/10 by Dlxko
* Version 4.0
**************************************************************************************************************/
#include "Buddyactions.h"

// Local statics variables
static bool handler_hidden_offlines_major;
static bool handler_hidden_offlines_minor;
static int buddy_number;
static gchar *file_avatar;
static GdkPixbuf *buddy_pixbuf;
static GtkTreeIter buddy_iter;

// Function to create new row with friend in buddy list
void additional_function_buddyactions_create_row_with_friend(int fx_buddy_number, char fx_nickname[DATA_ONE_SIZE+1], int fx_type)
{
	// Allocate error
	GError *px_error = NULL;

	// Get avatar or use default one
	file_avatar = g_strconcat("avatars/", fx_nickname, ".png", NULL);
	buddy_pixbuf = gdk_pixbuf_new_from_file_at_scale(file_avatar, AVATAR_IMAGE_SIZE, AVATAR_IMAGE_SIZE, 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, STATUS_COLUM, px_online, NICK_COLUM, fx_nickname, AVATAR_COLUM, buddy_pixbuf, STATUS_H_COLUM, "D", -1);
	else if (fx_type == 2)
		gtk_list_store_set(buddy_tree_view, &buddy_iter, STATUS_COLUM, px_offline, NICK_COLUM, fx_nickname, AVATAR_COLUM, buddy_pixbuf, STATUS_H_COLUM, "A", -1);
	else if (fx_type == 3)
		gtk_list_store_set(buddy_tree_view, &buddy_iter, STATUS_COLUM, px_away, NICK_COLUM, fx_nickname, AVATAR_COLUM, buddy_pixbuf, STATUS_H_COLUM, "B", -1);
	else if (fx_type == 4)
		gtk_list_store_set(buddy_tree_view, &buddy_iter, STATUS_COLUM, px_disturb, NICK_COLUM, fx_nickname, AVATAR_COLUM, buddy_pixbuf, STATUS_H_COLUM, "C", -1);

	// Sort list
	gtk_tree_sortable_set_sort_column_id(buddy_sortable_model, STATUS_H_COLUM, 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);

	// Check for problem
	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
	{
		// Refresh line
		gtk_list_store_remove(buddy_tree_view, buddy[buddy_number].tree_iter);
		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 make buddy away
int function_make_away(char nick_name[DATA_ONE_SIZE+1])
{
	// Get buddy number
	buddy_number = function_get_buddy_number(nick_name);

	// Check for problems
	if (buddy_number == (MAX_BUDDIES + 1))
	{
		function_show_warning(LANG_WRONG_BUDDY_NUMBER_WHEN_AWAYING);
		buddy[buddy_number].listed = false;
		return (MAX_BUDDIES + 1);
	}
	else
	{
		// Refresh line
		gtk_list_store_remove(buddy_tree_view, buddy[buddy_number].tree_iter);
		additional_function_buddyactions_create_row_with_friend(buddy_number, buddy[buddy_number].nick_name, 3);

		// 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 make buddy DND
int function_make_disturb(char nick_name[DATA_ONE_SIZE+1])
{
	// Get buddy number and check for problem
	buddy_number = function_get_buddy_number(nick_name);
	if (buddy_number == (MAX_BUDDIES + 1))
	{
		function_show_warning(LANG_WRONG_BUDDY_NUMBER_WHEN_AWAYING);
		buddy[buddy_number].listed = false;
		return (MAX_BUDDIES + 1);
	}

	// Refresh line
	gtk_list_store_remove(buddy_tree_view, buddy[buddy_number].tree_iter);
	additional_function_buddyactions_create_row_with_friend(buddy_number, buddy[buddy_number].nick_name, 4);

	// 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[DATA_ONE_SIZE+1])
{
	// If already existing buddy
	if (function_get_buddy_number(nick_name) != (MAX_BUDDIES + 1))
	{
		// Get buddy number and remove the row
		buddy_number = function_get_buddy_number(nick_name);
		gtk_list_store_remove(buddy_tree_view, buddy[buddy_number].tree_iter);

		// Create new row and update handlers if showing offlines
		if (handler_hidden_offlines_major == false)
		{
			additional_function_buddyactions_create_row_with_friend(buddy_number, buddy[buddy_number].nick_name, 2);
			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 again
		buddy[buddy_number].online = false;
		return true;
	}

	// Create next buddy 
	++current_buddy;

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

	// Create vbox for notebook
	buddy[current_buddy].vbox = gtk_vbox_new (FALSE, 0);

	// Create new row and update handlers if showing offlines
	if (handler_hidden_offlines_major == false)
	{
		additional_function_buddyactions_create_row_with_friend(current_buddy, nick_name, 2);
		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 again
	buddy[current_buddy].online = false;
	strcpy_s(buddy[current_buddy].nick_name, 10, nick_name);
	return true;
}

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

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

		// Else if hiddening isn't online and want to show and isn't in list
		else if (handler_hidden_offlines_major == true && buddy[buddy_number].listed == false && buddy[buddy_number].online == false && strlen(buddy[buddy_number].nick_name) && buddy[buddy_number].deleted == false)
		{
			// Create row, update handler and structure
			additional_function_buddyactions_create_row_with_friend(buddy_number, buddy[buddy_number].nick_name, 2);
			buddy[buddy_number].tree_iter = gtk_tree_iter_copy (&buddy_iter);
			buddy[buddy_number].listed = true;
			handler_hidden_offlines_minor = false;
		}
		++buddy_number;
	}
	while (buddy_number < MAX_BUDDIES);

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