/*************************************************************************************************************
* This file contains sending offline msgs
* 2012/8 by Dlxko
* Version 3.0
**************************************************************************************************************/
#include "OfflineMessages.h"

// Local static variable
static MYSQL_ROW mysql_row;

// Function to send offline messages and requests
void function_send_offline_messages(_client *current_client)
{
	// Alloc mysql result
	MYSQL_RES *mysql_result = NULL;

	// Get result of searching
	memset(mysql_text, 0, MYSQL_TEXT_SIZE);
	sprintf_s(mysql_text, "SELECT * FROM  messages WHERE member_two = '%s' AND member_two_received = 0", current_client->nick_name);
	mysql_result = function_mysql_result(mysql_text);

	// If no result return;
	if(!mysql_result)
		return;

	// Do this while some rows
	while((mysql_row = mysql_fetch_row(mysql_result)))
	{
		// Check data
		if (mysql_row[0] && mysql_row[1] && mysql_row[3])
		{
			// Count lenght
			buffer_text_len = LENGHT_SIZE+DATA_ONE_SIZE+OPCODE_SIZE + strlen(mysql_row[3]);
			memset(buffer_text_len_char, 0, LENGHT_SIZE);
			sprintf_s(buffer_text_len_char,LENGHT_SIZE, "%d", buffer_text_len);

			// Update text for server with "DX_OPCODE_IMS" and name datas
			memset(buffer_text,0,BUFFER_SIZE);
			memcpy(buffer_text,buffer_text_len_char,LENGHT_SIZE);
			memcpy(&buffer_text[LENGHT_SIZE],DX_OPCODE_IMG,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], mysql_row[3], strlen(mysql_row[3]));

			// Send socket for sender and receiver and clean sock info
			function_send_data (current_client, buffer_text, buffer_text_len);

			// Mark as read
			memset(mysql_text, 0, MYSQL_TEXT_SIZE);
			sprintf_s(mysql_text, "UPDATE messages SET member_two_received = '1' WHERE id = '%s'", mysql_row[0]);
			mysql_return_value = function_mysql_returned(mysql_text);

			if (mysql_return_value != NO_RESULT_OR_SUCESS)
				function_show_warning("Can't update msg received state");
		}
	}

	// Get result of searching
	memset(mysql_text, 0, MYSQL_TEXT_SIZE);
	sprintf_s(mysql_text, "SELECT * FROM events WHERE req_client = '%s' AND opcode = 'NFR'", current_client->nick_name);
	mysql_result = function_mysql_result(mysql_text);

	// Do this while some rows
	while((mysql_row = mysql_fetch_row(mysql_result)))
	{
		// Check data
		if (mysql_row[0] && mysql_row[1])
		{
			// Count len and save it
			buffer_text_len = LENGHT_SIZE+DATA_ONE_SIZE+OPCODE_SIZE+AVATAR_SIZE;
			memset(buffer_text_len_char, 0, LENGHT_SIZE);
			sprintf_s(buffer_text_len_char,LENGHT_SIZE, "%d", buffer_text_len);

			// Send packet with new event to other side
			memset(buffer_text, 0, BUFFER_SIZE);
			memcpy(buffer_text, buffer_text_len_char, LENGHT_SIZE);
			memcpy(&buffer_text[LENGHT_SIZE], DX_OPCODE_NFF, 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], mysql_row[0], strlen(mysql_row[0]));
			function_send_data (current_client, buffer_text, buffer_text_len);
		}
	}

	// Dealloc result if exist
	if (mysql_result)
		mysql_free_result(mysql_result);
}