/*************************************************************************************************************
* This file contains functionality of history
* 2012/10 by Dlxko
* Version 4.0
**************************************************************************************************************/
#include "History.h"

// Function to search history
void signal_search_history(GtkWidget *unused_button, gint input_buddy_number)
{
	// Look for start iter
	if(buddy[input_buddy_number].start_iter_loaded == false)
	{
		gtk_text_buffer_get_start_iter(buddy[input_buddy_number].history_buffer, &buddy[input_buddy_number].beg_end_iter);
		buddy[input_buddy_number].start_iter_loaded = true;
	}

	// Check len of world to search or report
	if(strlen(gtk_entry_get_text(GTK_ENTRY (buddy[input_buddy_number].history_entry))) > 3)
	{
		// Search for the world, else get start iter again
		if(gtk_text_iter_forward_search(&buddy[input_buddy_number].beg_end_iter, gtk_entry_get_text(GTK_ENTRY (buddy[input_buddy_number].history_entry)), GTK_TEXT_SEARCH_TEXT_ONLY, &buddy[input_buddy_number].start_iter, &buddy[input_buddy_number].beg_end_iter, NULL))
		{
			// Select this world
			gtk_text_buffer_select_range(buddy[input_buddy_number].history_buffer, &buddy[input_buddy_number].start_iter, &buddy[input_buddy_number].beg_end_iter);

			// Scroll this world
			buddy[input_buddy_number].endmark = gtk_text_mark_new ("E1", 0);
			gtk_text_buffer_add_mark (buddy[input_buddy_number].history_buffer, buddy[input_buddy_number].endmark, &buddy[input_buddy_number].beg_end_iter);
			gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (buddy[input_buddy_number].history_view), buddy[input_buddy_number].endmark, 0.0, FALSE, 0, 0);
			gtk_text_buffer_delete_mark (buddy[input_buddy_number].history_buffer, buddy[input_buddy_number].endmark);
		}
		else gtk_text_buffer_get_start_iter(buddy[input_buddy_number].history_buffer, &buddy[input_buddy_number].beg_end_iter);
	}
	else function_show_warning(LANG_WORD_TO_SEARCH_IS_SMALL);
}

// Function to request data from server
void signal_request_history_data(GtkWidget *unused_button, gint input_buddy_number)
{
	// Check if entries contain datas
	if( (strlen(gtk_entry_get_text (GTK_ENTRY (buddy[input_buddy_number].history_day))) < 1) ||
		(strlen(gtk_entry_get_text (GTK_ENTRY (buddy[input_buddy_number].history_month))) < 1) ||
		(strlen(gtk_entry_get_text (GTK_ENTRY (buddy[input_buddy_number].history_year))) < 1) )
	{
		function_show_warning(LANG_NO_TEXT_HISTORY);
		return;
	}

	// Check buddy number
	if((input_buddy_number == MAX_BUDDIES+1) || (input_buddy_number > MAX_BUDDIES+1))
	{
		function_show_warning(LANG_WRONG_BUDDY_PASSED);
		return;
	}

	// Check buddy name
	if(strlen(buddy[input_buddy_number].nick_name) < 1)
	{
		function_show_warning(LANG_WRONG_BUDDY_PASSED_NAME);
		return;
	}

	// Get datas
	buddy[input_buddy_number].day_text = gtk_entry_get_text(GTK_ENTRY (buddy[input_buddy_number].history_day));
	buddy[input_buddy_number].month_text = gtk_entry_get_text(GTK_ENTRY (buddy[input_buddy_number].history_month));
	buddy[input_buddy_number].year_text = gtk_entry_get_text(GTK_ENTRY (buddy[input_buddy_number].history_year));

	// Count lenght
	sending_text_len = LENGHT_SIZE + OPCODE_SIZE + DATA_ONE_SIZE + 4 + 2 + 2;
	sprintf_s(sending_text_len_char, LENGHT_SIZE, "%d", sending_text_len);

	// Update text for server with "DX_OPCODE_NFR" and name datas
	memset(sending_text,0,BUF_SIZE);
	memcpy(sending_text, sending_text_len_char, LENGHT_SIZE);
	memcpy(&sending_text[LENGHT_SIZE], DX_OPCODE_HIS, OPCODE_SIZE);
	memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE], buddy[input_buddy_number].nick_name, strlen(buddy[input_buddy_number].nick_name));
	memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE], buddy[input_buddy_number].day_text, 2);
	memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE+2], buddy[input_buddy_number].month_text, 2);
	memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE+4], buddy[input_buddy_number].year_text, 4);

	// Send socket
	function_send_data(sending_text, sending_text_len);

	// Empty buffer of text view
	gtk_text_buffer_set_text (GTK_TEXT_BUFFER(buddy[input_buddy_number].history_buffer),"", 0);
}
