/*************************************************************************************************************
* This file contains buddylists updates
* 2012/8 by Dlxko
* Version 3.0
**************************************************************************************************************/
#include "BuddiesUpdate.h"

// Local static variables
static int broad_buddy;
static MYSQL_ROW mysql_row;
static MYSQL_ROW mysql_row2;

// Function to process away status
gboolean function_process_away(int function_buddy_id)
{
	if (client[function_buddy_id].connected)
	{
		// Send status and set away handler only if dist status not activated
		if (!client[function_buddy_id].client_disturb_status == true)
			function_buddy_status_update(function_buddy_id, 3);

		// End timer
		client[function_buddy_id].client_away_timeout = 0;
		return false;
	}
	return false;
}

// Function to update status and broadcast it!
void function_buddy_status_update(int function_buddy_id, int function_type)
{
	// Count lenght and send data
	buffer_text_len = LENGHT_SIZE + OPCODE_SIZE + CODE_SIZE + DATA_ONE_SIZE;
	memset(buffer_text_len_char, 0, LENGHT_SIZE);
	sprintf_s(buffer_text_len_char, LENGHT_SIZE, "%d", buffer_text_len);

	// This will create BDL message
	memset(buffer_text,0,BUFFER_SIZE);
	memcpy(buffer_text,buffer_text_len_char, LENGHT_SIZE);
	memcpy(&buffer_text[LENGHT_SIZE], DX_OPCODE_BDL, OPCODE_SIZE);
	memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE], client[function_buddy_id].nick_name, strlen(client[function_buddy_id].nick_name));
	if (function_type == 1)
		memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE],DX_CODE_ONL, CODE_SIZE);
	else if (function_type == 2)
		memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE],DX_CODE_OFF, CODE_SIZE);
	else if (function_type == 3)
		memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE],DX_CODE_AWY, CODE_SIZE);
	else if (function_type == 4)
		memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE],DX_CODE_DND, CODE_SIZE);
	else if (function_type == 5)
		memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE],DX_CODE_ONL, CODE_SIZE);

	// This will update handlers
	if(function_type != 4)
		 client[function_buddy_id].client_disturb_status = false;
	else client[function_buddy_id].client_disturb_status = true;
	if(function_type != 3)
		 client[function_buddy_id].client_away_status = false;
	else client[function_buddy_id].client_away_status = true;

	// This will set timer after leaving DND
	if(function_type == 5)
		client[function_buddy_id].client_away_timeout = g_timeout_add_seconds_full(G_PRIORITY_DEFAULT, AWAY_PROCESS_TIME, (GSourceFunc)function_process_away, (gint*)function_buddy_id, NULL);

	// Send packet broadcast method
	function_broadcast_buddies(buffer_text, &client[function_buddy_id], buffer_text_len);
}

// Function to update buddies lists
void function_budies_update(int buddy)
{
	// Alloc mysql results
	MYSQL_RES *mysql_result = NULL;
	MYSQL_RES *mysql_result2 = NULL;

	// Get all avivable members from database
	memset(mysql_text, 0, MYSQL_TEXT_SIZE);
	sprintf_s(mysql_text, "SELECT * FROM  buddylist WHERE client_name  = '%s'", client[buddy].nick_name);
	mysql_result = function_mysql_result(mysql_text);

	// If nothing return
	if (!mysql_result)
		return;

	// Fetch rows from result
	while((mysql_row = mysql_fetch_row(mysql_result)))
	{
		// If friend found
		if (mysql_row[1])
		{
			// Look if selected client have this one in buddy list
			memset(mysql_text, 0, MYSQL_TEXT_SIZE);
			sprintf_s(mysql_text, "SELECT * FROM  buddylist WHERE client_name  = '%s' AND friend_name = '%s'", mysql_row[1], client[buddy].nick_name);
			mysql_return_value = function_mysql_returned(mysql_text);

			// If everything ok process
			if(mysql_return_value == MORE_ROWS_OR_SEARCHED)
			{
				// Prepare to get gravatar image
				memset(mysql_text, 0, MYSQL_TEXT_SIZE);
				sprintf_s(mysql_text, "SELECT * FROM accounts WHERE nickname = '%s'", mysql_row[1]);
				mysql_result2 = function_mysql_result(mysql_text);

				// If into accounts
				if (mysql_result2)
				{
					// Fetch received rows
					mysql_row2 = mysql_fetch_row(mysql_result2);

					// If gravatar found
					if (mysql_row[1] && mysql_row2[8])
					{
						// Count lenght and send data
						buffer_text_len = LENGHT_SIZE + OPCODE_SIZE + CODE_SIZE + DATA_ONE_SIZE + strlen(mysql_row2[8]);
						memset(buffer_text_len_char, 0, LENGHT_SIZE);
						sprintf_s(buffer_text_len_char, LENGHT_SIZE, "%d", buffer_text_len);

						// This will create BDL-OFFLINE message
						memset(buffer_text,0,BUFFER_SIZE);
						memcpy(buffer_text,buffer_text_len_char, LENGHT_SIZE);
						memcpy(&buffer_text[LENGHT_SIZE], DX_OPCODE_BDL, OPCODE_SIZE);
						memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE], mysql_row[1], strlen(mysql_row[1]));
						memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE],DX_CODE_OFF, CODE_SIZE);
						memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE+CODE_SIZE+DATA_ONE_SIZE], mysql_row2[8], strlen(mysql_row2[8]));

						// Send message and process the next row from DB
						function_send_data(&client[buddy], buffer_text, buffer_text_len);
					}
					else
					{
						// Show error message
						memset(warnings_tmp, 0, WARNING_TMP_SIZE);
						sprintf_s(warnings_tmp, WARNING_TMP_SIZE, "Missing gravatar for [%s] !!!", client[buddy].nick_name);
						function_show_info(warnings_tmp);
					}
				}
			}
		}
	}

	// Refresh counter
	broad_buddy = RESET;

	do // Now find onlines
	{
		// Check if client have online/offline status
		if (function_check_client(&client[broad_buddy]) && (client[broad_buddy].socket != client[buddy].socket) && function_check_friendstate(&client[broad_buddy], &client[buddy]) )
		{
			// Count lenght and send data
			buffer_text_len = LENGHT_SIZE + OPCODE_SIZE + CODE_SIZE + DATA_ONE_SIZE;
			memset(buffer_text_len_char, 0, LENGHT_SIZE);
			sprintf_s(buffer_text_len_char, LENGHT_SIZE, "%d", buffer_text_len);

			// This will create BDL messages
			memset(buffer_text,0,BUFFER_SIZE);
			memcpy(buffer_text,buffer_text_len_char, LENGHT_SIZE);
			memcpy(&buffer_text[LENGHT_SIZE], DX_OPCODE_BDL, OPCODE_SIZE);
			memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE], client[broad_buddy].nick_name, strlen(client[broad_buddy].nick_name));
			if ( (client[broad_buddy].client_away_status == false) && (client[broad_buddy].client_disturb_status == false) )
				memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE],DX_CODE_ONL, CODE_SIZE);
			else if ( (client[broad_buddy].client_away_status == true) && (client[broad_buddy].client_disturb_status == false) )
				memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE],DX_CODE_AWY, CODE_SIZE);
			else if ( (client[broad_buddy].client_away_status == false) && (client[broad_buddy].client_disturb_status == true) )
				memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE],DX_CODE_DND, CODE_SIZE);
			else 
				function_show_warning("Statushes mismash!");

			// Broadcast the buddy to client and process the next
			function_send_data(&client[buddy], buffer_text, buffer_text_len);
		}
		++broad_buddy;
	}
	while (broad_buddy < MAX_CLIENTS);

	// Free mysql result if exist
	if (mysql_result)
		mysql_free_result(mysql_result);
	if (mysql_result2)
		mysql_free_result(mysql_result2);
}