/*************************************************************************************************************
* This file contains loggin
* 2012/6 by Dlxko
**************************************************************************************************************/
#include "Loggining.h"

// Variables used to send  packet
static char sending_text[BUF_SIZE]; // For socket text
static int sending_text_len; // Socket text len
static char sending_text_len_char[LENGHT_SIZE+1]; // Socket text len

// Variables for getting informations
static const gchar *entry_text_pass; // For pass
static const gchar *entry_text_user; // For nickname

/** Create log window **/
bool create_loggin()
{
	// New window, parameters, title, closing
	log_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	if (!log_window) return false;
	gtk_widget_set_size_request (GTK_WIDGET (log_window), 300, 80);
	gtk_window_set_title (GTK_WINDOW (log_window), "Login");
	g_signal_connect_swapped (log_window, "delete-event", G_CALLBACK (signal_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);
	if (!log_vbox) return false;
	gtk_container_add (GTK_CONTAINER (log_window), log_vbox);
	gtk_widget_show (log_vbox);

	// Create text entry widget for username, lenght, set text, position, editable, pack, show
	log_entry = gtk_entry_new ();
	if (!log_entry) return false;
	gtk_entry_set_max_length (GTK_ENTRY (log_entry), 30);
	gtk_entry_set_text (GTK_ENTRY (log_entry), "Enter username");
	g_signal_connect (log_entry, "activate", G_CALLBACK (create_registration), NULL);
	gtk_editable_select_region (GTK_EDITABLE (log_entry), 0, GTK_ENTRY (log_entry)->text_length);
	gtk_box_pack_start (GTK_BOX (log_vbox), log_entry, TRUE, TRUE, 0);
	gtk_widget_show (log_entry);

	// Create text entry widget for pass, lenght, set text, action, position, editable, pack, show
	log_entry2 = gtk_entry_new ();
	if (!log_entry2) return false;
	gtk_entry_set_max_length (GTK_ENTRY (log_entry2), 100);
	gtk_entry_set_text (GTK_ENTRY (log_entry2), "Enter password");
	gtk_entry_set_visibility(GTK_ENTRY (log_entry2), FALSE);
	g_signal_connect (log_entry2, "activate", G_CALLBACK (signal_send_loggin), NULL);
	gtk_editable_select_region (GTK_EDITABLE (log_entry2), 0, GTK_ENTRY (log_entry2)->text_length);
	gtk_box_pack_start (GTK_BOX (log_vbox), log_entry2, TRUE, TRUE, 0);
	gtk_widget_show (log_entry2);

	// Create label, wrapping, pack, show
	log_label = gtk_label_new ("Please loggin...");
	if (!log_label) return false;
	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);
	gtk_widget_show (log_label);

	// Show window
	gtk_widget_show (log_window);
	return true;
}

/************ Send log data **************/
void signal_send_loggin()
{
	// Get user and pass texts
	if (log_entry && log_entry2)
	{
		entry_text_user = gtk_entry_get_text (GTK_ENTRY (log_entry));
		entry_text_pass = gtk_entry_get_text (GTK_ENTRY (log_entry2));
	}
	else function_show_warning("Loggin data widgets doesn't exist");

	if((strlen(entry_text_user) == 0) || (strlen(entry_text_pass) == 0))
		function_show_warning("Can't get loggin informations");

	// Save my name
	memset(my_name, 0, DATA_ONE_SIZE);
	strcpy_s(my_name, DATA_ONE_SIZE, entry_text_user);

	// Count lenght
	sending_text_len = LENGHT_SIZE + DATA_ONE_SIZE + OPCODE_SIZE + strlen(entry_text_pass);
	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" 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_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_pass, strlen(entry_text_pass));

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