/*************************************************************************************************************
* This file contains creating and functionality of the Adder.
* 2012/10 by Dlxko
* Version 4.0
**************************************************************************************************************/
#include "Adder.h"

// Local static widgets
static GtkWidget *new_friend_vbox;
static GtkWidget *new_friend_username;
static bool handler_new_friend_window_shown;

// Function to handle window destroying
// Change handler to false and hide window
static void signal_close_new_friend_window()
{
	handler_new_friend_window_shown = false;
	gtk_widget_hide(new_friend_window);
}

// Signal after clicked add button
static void signal_new_friend()
{
	// Get name from text entry
	entry_text = gtk_entry_get_text (GTK_ENTRY (new_friend_username));

	// Check if name isn't empty
	if(strlen(gtk_entry_get_text (GTK_ENTRY (new_friend_username))) > 0)
	{
		// Count lenght of this packet
		sending_text_len = LENGHT_SIZE + DATA_ONE_SIZE + OPCODE_SIZE;
		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_NFR, OPCODE_SIZE);
		memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE], entry_text, strlen(entry_text));

		// Send packet
		function_send_data(sending_text, sending_text_len);
	}
	else function_show_warning(LANG_ADDER_NOTHING);
}

// Function to create buddy adder
void signal_create_buddy_adder()
{
	// Look if already exist
	if(handler_new_friend_window_shown)
	{
		gtk_widget_show_all(new_friend_window);
		return;
	}

	// Create new window
	new_friend_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_window_resize (GTK_WINDOW (new_friend_window), 280, 50);
	gtk_window_set_title (GTK_WINDOW (new_friend_window), LANG_ADD_OR_REM_FR);
	gtk_window_set_icon(GTK_WINDOW(new_friend_window), default_icon);
	gtk_window_set_position(GTK_WINDOW(new_friend_window), GTK_WIN_POS_CENTER_ON_PARENT);
	gtk_window_set_transient_for(GTK_WINDOW(new_friend_window), GTK_WINDOW(log_window));
	gtk_window_set_opacity(GTK_WINDOW(new_friend_window), 0.95);

	// Create new box
	new_friend_vbox = gtk_vbox_new (FALSE, 0);

	// Create new entry
	new_friend_username = gtk_entry_new ();
	gtk_entry_set_max_length (GTK_ENTRY (new_friend_username), DATA_ONE_SIZE);

	// Create new label
	new_friend_label = gtk_label_new (LANG_ENTER_NAME_TO_ADDER);
	gtk_label_set_justify(GTK_LABEL (new_friend_label), GTK_JUSTIFY_CENTER);
	gtk_label_set_line_wrap(GTK_LABEL (new_friend_label), FALSE);
	gtk_label_set_selectable(GTK_LABEL (new_friend_label), FALSE);
	gtk_label_set_single_line_mode(GTK_LABEL (new_friend_label), TRUE);

	// Design UI
	gtk_container_add (GTK_CONTAINER (new_friend_window), new_friend_vbox);
	gtk_box_pack_start (GTK_BOX (new_friend_vbox), new_friend_username, TRUE, TRUE, 5);
	gtk_box_pack_start (GTK_BOX (new_friend_vbox), new_friend_label, TRUE, TRUE, 5);

	// Set signals
	g_signal_connect (new_friend_username, "activate", G_CALLBACK (signal_new_friend), new_friend_username);
	g_signal_connect (new_friend_window, "delete-event", G_CALLBACK (signal_close_new_friend_window), NULL);

	// Show whole window and set handler
	gtk_widget_show_all(new_friend_window);
	handler_new_friend_window_shown = true;
}