/*************************************************************************************************************
* This file contains send msg action
* 2012/8 by Dlxko
* Version 3.0
**************************************************************************************************************/
#include "Sendaction.h"

// Local static variables
static GtkTextIter end_iter;
static GtkTextIter start_iter;
static gint line_count;
static GtkTextMark *endmark;
static int character_count;
static char wait_counter_char[20];

// Function to send message
void signal_send_message(GtkWidget *entry, gint buddy_numb)
{
	// Message buffer
	static gchar *message;

	// Create new non-responded state
	// Just look for empty chat slot
	int wait_counter = 0;
	do
	{
		if(messages[wait_counter].waiting == true)
			++wait_counter;
		else
			break;
	}
	while (wait_counter < MAX_MESSAGES);

	// If more then maximum messages in front
	if (wait_counter == MAX_MESSAGES)
		function_show_warning(LANG_REACHED_MAX_LIMIT_NON_RECV_MSGS);

	// Convert msg number to chars
	sprintf_s(wait_counter_char, "%d", wait_counter);

	// Get time and inset into string, create char, copy from string to char
	std::string time = function_get_std_time();
	gchar *timemsg = new char[time.size()+1];
	timemsg[time.size()]=0;
	memcpy(timemsg, time.c_str(), time.size());

	// Get text from text_entry if possible
	if (buddy[buddy_numb].entry)
	{
		if (strlen(gtk_entry_get_text (GTK_ENTRY (buddy[buddy_numb].entry))) > 0)
			entry_text = gtk_entry_get_text (GTK_ENTRY (buddy[buddy_numb].entry));
		else function_show_warning(LANG_SEND_NON_ENTERED_MSG);
	}
	else function_show_warning(LANG_ERROR_GET_ENRY_DATA);

	// Count sending text lenght
	sending_text_len = LENGHT_SIZE + strlen(entry_text) + OPCODE_SIZE + DATA_ONE_SIZE + MSG_NUMB_SIZE;
	sprintf_s(sending_text_len_char, LENGHT_SIZE, "%d", sending_text_len);

	// Update text for server with "DX_OPCODE_MSG" and friend datas
	if ((strlen(buddy[buddy_numb].nick_name) > 0) || (strlen(wait_counter_char) > 0) || (strlen(entry_text) > 0))
	{
		memset(sending_text,0,BUF_SIZE);
		memcpy(sending_text,sending_text_len_char,LENGHT_SIZE);
		memcpy(&sending_text[LENGHT_SIZE],DX_OPCODE_MSG,OPCODE_SIZE);
		memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE], buddy[buddy_numb].nick_name, strlen(buddy[buddy_numb].nick_name));
		memcpy(&sending_text[LENGHT_SIZE+DATA_ONE_SIZE+OPCODE_SIZE], wait_counter_char, strlen(wait_counter_char));
		memcpy(&sending_text[LENGHT_SIZE+DATA_ONE_SIZE+OPCODE_SIZE+MSG_NUMB_SIZE], entry_text, strlen(entry_text));

		// Create message with time if its necessary
		if (buddy[buddy_numb].who_sender == 0 || buddy[buddy_numb].who_sender == 3)
		{
			// Get message of time
			message = g_strconcat ("\n", timemsg, LANG_ME, NULL);

			// Check this message and insert
			if (strlen(message) > 0)
			{
				gtk_text_buffer_get_end_iter (buddy[buddy_numb].buffer, &end_iter);
				gtk_text_buffer_insert (buddy[buddy_numb].buffer, &end_iter, message, -1);

				// Check line count and make it bold
				line_count = gtk_text_buffer_get_line_count (buddy[buddy_numb].buffer);
				if (line_count > 0)
				{
					gtk_text_buffer_get_iter_at_line(buddy[buddy_numb].buffer, &start_iter, line_count);
					gtk_text_buffer_get_end_iter (buddy[buddy_numb].buffer, &end_iter);
					gtk_text_buffer_apply_tag_by_name (buddy[buddy_numb].buffer, "bold", &start_iter, &end_iter);
				}
				else function_show_warning(LANG_XGET_NUMBER_OF_LINES);
			}
			else function_show_warning(LANG_WRONG_MESSAGE_CREATED);
		}

		// Print other datas
		message = g_strconcat ("\n", entry_text, " ", NULL);
		if (strlen(message) > 0)
		{
			gtk_text_buffer_get_end_iter (buddy[buddy_numb].buffer, &end_iter);
			gtk_text_buffer_insert (buddy[buddy_numb].buffer, &end_iter, message, -1);
		}
		else function_show_warning(LANG_WRONG_MESSAGE_CREATED2);

		// Send packet
		function_send_data(sending_text, sending_text_len);

		// Erase GTK text entry
		if(buddy[buddy_numb].entry)
			gtk_entry_set_text (GTK_ENTRY (buddy[buddy_numb].entry), "");
		else function_show_warning(LANG_CANT_EMPTY_ENTRY);

		// Set sender as me
		buddy[buddy_numb].who_sender = true;

		//Convert smileys
		function_get_smile(buddy[buddy_numb].buffer);

		// Scroll to end
		gtk_text_buffer_get_end_iter (buddy[buddy_numb].buffer, &end_iter);
		endmark = gtk_text_mark_new ("E1", 0);
		if (endmark)
		{
			gtk_text_buffer_add_mark (buddy[buddy_numb].buffer, endmark, &end_iter);
			gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (buddy[buddy_numb].text_view), endmark, 0.0, FALSE, 0, 0);
			gtk_text_buffer_delete_mark (buddy[buddy_numb].buffer, endmark);
		}
		else function_show_warning(LANG_ENMARK_XCREATED);

		// Get char count
		character_count = gtk_text_buffer_get_char_count(buddy[buddy_numb].buffer);

		// Update message structure
		messages[wait_counter].chat = buddy_numb;
		messages[wait_counter].waiting = true;
		messages[wait_counter].char_count = character_count;
	}

	// Dealloc
	if (message != NULL) g_free (message);
}