/*************************************************************************************************************
* This file contains loggin create and loggin packet sending functions
* 2012/8 by Dlxko
* Version 3.0
**************************************************************************************************************/
#include "Loggining.h"

// Variables for getting informations
// This can't be deallocated
static const gchar *entry_text_user;

// Send loggin packet
static void signal_send_loggin()
{
	// Get user and pass texts or report
	if (log_username && log_password)
	{
		entry_text_user = gtk_entry_get_text (GTK_ENTRY (log_username));
		entry_text = gtk_entry_get_text (GTK_ENTRY (log_password));
	}
	else function_show_warning(LANG_XLOGGIN_WIDGETS);

	// Check loggin informations
	if((strlen(entry_text_user) == 0) || (strlen(entry_text) == 0))
		function_show_warning(LANG_XLOGIN_INFO);

	// Save my name for future use
	memset(my_name, 0, DATA_ONE_SIZE);
	memcpy(my_name, entry_text_user, DATA_ONE_SIZE);

	// Count lenght of whole packet
	sending_text_len = LENGHT_SIZE + DATA_ONE_SIZE + OPCODE_SIZE + strlen(entry_text);
	memset(sending_text_len_char, 0, LENGHT_SIZE);
	sprintf_s(sending_text_len_char, LENGHT_SIZE, "%d", sending_text_len);

	// Update text for server with "DX_OPCODE_AUC" containing name and password datas
	memset(sending_text,0,BUF_SIZE);
	memcpy(sending_text, sending_text_len_char, LENGHT_SIZE);
	memcpy(&sending_text[LENGHT_SIZE], DX_OPCODE_AUC, OPCODE_SIZE);
	memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE], entry_text_user, strlen(entry_text_user));
	memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE], entry_text, strlen(entry_text));

	// Send packet to socket
	function_send_data(sending_text, sending_text_len);
}

// Create loggin window
void create_loggin()
{
	// New window, parameters, title, closing
	log_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_widget_set_size_request (GTK_WIDGET (log_window), 300, 80);
	gtk_window_set_title (GTK_WINDOW (log_window), LANG_LOGIN);
	g_signal_connect_swapped (log_window, "delete-event", G_CALLBACK (signal_loggin_go_offline), NULL);
	gtk_window_set_icon(GTK_WINDOW(log_window), default_icon);

	// Create new box and container and show
	log_vbox = gtk_vbox_new (FALSE, 0);
	gtk_container_add (GTK_CONTAINER (log_window), log_vbox);

	// Create text entry widget for username, lenght, set text, position, editable, pack, show
	log_username = gtk_entry_new ();
	gtk_entry_set_max_length (GTK_ENTRY (log_username), DATA_ONE_SIZE);
	gtk_entry_set_text (GTK_ENTRY (log_username), LANG_ENTER_NAME);
	g_signal_connect (log_username, "activate", G_CALLBACK (create_registration), NULL);
	gtk_editable_select_region (GTK_EDITABLE (log_username), 0, GTK_ENTRY (log_username)->text_length);
	gtk_box_pack_start (GTK_BOX (log_vbox), log_username, TRUE, TRUE, 0);

	// Create text entry widget for pass, lenght, set text, action, position, editable, pack, show
	log_password = gtk_entry_new ();
	gtk_entry_set_max_length (GTK_ENTRY (log_password), DATA_ONE_SIZE);
	gtk_entry_set_text (GTK_ENTRY (log_password), LANG_ENTER_PASSWORD);
	gtk_entry_set_visibility(GTK_ENTRY (log_password), FALSE);
	g_signal_connect (log_password, "activate", G_CALLBACK (signal_send_loggin), NULL);
	gtk_editable_select_region (GTK_EDITABLE (log_password), 0, GTK_ENTRY (log_password)->text_length);
	gtk_box_pack_start (GTK_BOX (log_vbox), log_password, TRUE, TRUE, 0);

	// Create label, wrapping, pack, show
	log_label = gtk_label_new (LANG_LABEL_LOGIN);
	gtk_label_set_justify (GTK_LABEL (log_label), GTK_JUSTIFY_FILL);
	gtk_label_set_line_wrap (GTK_LABEL (log_label), TRUE);
	gtk_box_pack_start (GTK_BOX (log_vbox), log_label, FALSE, TRUE, 5);

	// Show window
	gtk_widget_show_all (log_window);
}