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

static int broad_buddy;

/********************* BUDDY LIST UPDATE *********************/
void function_budies_update(int type, int buddy)
{
	// This type send all avivable contact to buddy list of selected buddy
	if (type == 1)
	{
		// 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);

		// Fetch received rows
		while((mysql_row = mysql_fetch_row(mysql_result)))
		{
			// Prevent crash
			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 (mysql_result2)
					{
						// Fetch received rows
						mysql_row2 = mysql_fetch_row(mysql_result2);

						// Prevent crash
						if (mysql_row2[8])
						{
							// Count lenght and send data
							buffer_text_len = LENGHT_SIZE + OPCODE_SIZE + CODE_SIZE + DATA_ONE_SIZE + strlen(mysql_row2[8]);
							sprintf_s(buffer_text_len_char, LENGHT_SIZE, "%d", buffer_text_len);

							// This will create BDL-ONLINE 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],DX_CODE_OFF, CODE_SIZE);
							memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE+CODE_SIZE], mysql_row[1], strlen(mysql_row[1]));
							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
					{
						std::cout << color(FOREGROUND_RED|FOREGROUND_BLUE) << "Missing gravatar for ["
							<< color(FOREGROUND_GREEN|FOREGROUND_RED) << client[buddy].nick_name
							<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "]"
							<< color(FOREGROUND_RED|FOREGROUND_RED) << " !!!"
							<< white << std::endl;
					}
				}
			}
		}

		// Refresh counter
		broad_buddy = 0;

		// Now we need to check server avivables for online clients
		do
		{
			// 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 + strlen(client[broad_buddy].nick_name);
				sprintf_s(buffer_text_len_char, LENGHT_SIZE, "%d", buffer_text_len);

				// This will create BDL-ONLINE 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],DX_CODE_ONL, CODE_SIZE);
				memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE+CODE_SIZE], client[broad_buddy].nick_name, strlen(client[broad_buddy].nick_name));

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

	// Status change to online
	else if (type == 2)
	{
		// Check if client have online/offline status
		if (function_check_client(&client[buddy]) )
		{
			// Count lenght and send data
			buffer_text_len = LENGHT_SIZE + OPCODE_SIZE + CODE_SIZE + strlen(client[buddy].nick_name);
			sprintf_s(buffer_text_len_char, LENGHT_SIZE, "%d", buffer_text_len);

			// This will create BDL-ONLINE 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],DX_CODE_ONL, CODE_SIZE);
			memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE+CODE_SIZE], client[buddy].nick_name, strlen(client[buddy].nick_name));

			// Broadcast status change to all of the connected clients and enter next client
			function_broadcast_buddies(buffer_text, &client[buddy], buffer_text_len);
		}
	}

	// Status change if client go offline
	else if (type == 3)
	{
		// Count lenght and send data
		buffer_text_len = LENGHT_SIZE + OPCODE_SIZE + CODE_SIZE + strlen(client[buddy].nick_name);
		sprintf_s(buffer_text_len_char, LENGHT_SIZE, "%d", buffer_text_len);

		// This will create BDL-ONLINE 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],DX_CODE_OFF, CODE_SIZE);
		memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE+CODE_SIZE], client[buddy].nick_name, strlen(client[buddy].nick_name));

		// Broadcast that someone go offline
		function_broadcast_buddies(buffer_text, &client[buddy], buffer_text_len);
	}
}