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

// Local static variables
static MYSQL_ROW mysql_row;
int int_day;

// Function to send history messages
void function_send_history_messages(_client *current_client, char friend_name[DATA_ONE_SIZE], char day[2], char month[2], char year[4])
{
	// Alloc mysql result
	MYSQL_RES *mysql_result = NULL;

	// Convert day into int
	int_day = function_convert_packet_lenght(day);

	// Get result of searching
	memset(mysql_text, 0, MYSQL_TEXT_SIZE);
	sprintf_s(mysql_text, "SELECT * FROM messages WHERE (( member_one = '%s' AND member_two = '%s' ) OR ( member_one = '%s' AND member_two = '%s' )) AND ( time_stamp > '%s-%s-%d') AND ( time_stamp < '%s-%s-%d') ORDER BY time_stamp ASC;", current_client->nick_name, friend_name, friend_name, current_client->nick_name, year, month, int_day, year, month, (int_day+1));
	mysql_result = function_mysql_result(mysql_text);
	
	/// Say for the first nothing
	// Count lenght
	buffer_text_len = LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE+CODE_SIZE;
	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_IHM" and code NHI
	memset(buffer_text,0,BUFFER_SIZE);
	memcpy(buffer_text,buffer_text_len_char,LENGHT_SIZE);
	memcpy(&buffer_text[LENGHT_SIZE],DX_OPCODE_IMH,OPCODE_SIZE);
	memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE],friend_name,strlen(friend_name));
	memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE],DX_CODE_NHI,CODE_SIZE);

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

	// Send messages
	// Do this while some rows
	while((mysql_row = mysql_fetch_row(mysql_result)))
	{
		// Check data
		if (mysql_row[3] && mysql_row[1])
		{
			// Count lenght
			buffer_text_len = LENGHT_SIZE+DATA_ONE_SIZE+OPCODE_SIZE+DATA_ONE_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_IMH,OPCODE_SIZE);
			memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE],friend_name,strlen(friend_name));
			memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE],mysql_row[1],strlen(mysql_row[1]));
			memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_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);
		}
	}

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