/*************************************************************************************************************
* This file contains registration
* 2012/6 by Dlxko
**************************************************************************************************************/
#include "Registration.h"

// For storing datas from entries
static const gchar *entries_text;

// For creating packet
static char sending_text[BUF_SIZE];
static int sending_text_len;
static char sending_text_len_char[LENGHT_SIZE+1];

// For gravatar data storing
static char img_text[101];

// Actual error code
int errors_counter;

/********************* Send registration data ******************/
void signal_send_registration()
{
	// Count lenght
	sending_text_len = LENGHT_SIZE + OPCODE_SIZE + (DATA_ONE_SIZE*8) + strlen(gtk_entry_get_text (GTK_ENTRY (reg_image)));
	memset(sending_text_len_char, 0, LENGHT_SIZE);
	sprintf_s(sending_text_len_char, LENGHT_SIZE, "%d", sending_text_len);

	// Clean sending text buffer
	memset(sending_text,0,BUF_SIZE);

	// Copy lenght datas
	memcpy(sending_text, sending_text_len_char, LENGHT_SIZE);

	// Copy opcode
	memcpy(&sending_text[LENGHT_SIZE], DX_OPCODE_REG, OPCODE_SIZE);

	// Copy name
	if(strlen(gtk_entry_get_text (GTK_ENTRY (reg_entry1))) < 3)
		errors_counter = 1;
	entries_text = gtk_entry_get_text (GTK_ENTRY (reg_entry1));
	memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE], entries_text, strlen(entries_text));

	// Copy surname
	if(strlen(gtk_entry_get_text (GTK_ENTRY (reg_entry2))) < 3)
		errors_counter = 2;
	entries_text = gtk_entry_get_text (GTK_ENTRY (reg_entry2));
	memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE + DATA_ONE_SIZE], entries_text, strlen(entries_text));

	// Copy nickname
	if(strlen(gtk_entry_get_text (GTK_ENTRY (reg_entry3))) < 3)
		errors_counter = 3;
	entries_text = gtk_entry_get_text (GTK_ENTRY (reg_entry3));
	memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE + (DATA_ONE_SIZE*2)], entries_text, strlen(entries_text));

	// Copy password
	if(strlen(gtk_entry_get_text (GTK_ENTRY (reg_entry8))) < 3)
		errors_counter = 8;
	entries_text = gtk_entry_get_text (GTK_ENTRY (reg_entry8));
	memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE + (DATA_ONE_SIZE*3)], entries_text, strlen(entries_text));

	// Copy city
	if(strlen(gtk_entry_get_text (GTK_ENTRY (reg_entry4))) < 3)
		errors_counter = 4;
	entries_text = gtk_entry_get_text (GTK_ENTRY (reg_entry4));
	memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE + (DATA_ONE_SIZE*4)], entries_text, strlen(entries_text));

	// Copy country
	if(strlen(gtk_entry_get_text (GTK_ENTRY (reg_entry5))) < 3)
		errors_counter = 5;
	entries_text = gtk_entry_get_text (GTK_ENTRY (reg_entry5));
	memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE + (DATA_ONE_SIZE*5)], entries_text, strlen(entries_text));

	// Copy age
	if(strlen(gtk_entry_get_text (GTK_ENTRY (reg_entry6))) < 3)
		errors_counter = 6;
	entries_text = gtk_entry_get_text (GTK_ENTRY (reg_entry6));
	memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE + (DATA_ONE_SIZE*6)], entries_text, strlen(entries_text));

	// Copy gender
	if(strlen(gtk_entry_get_text (GTK_ENTRY (reg_entry7))) < 3)
		errors_counter = 7;
	entries_text = gtk_entry_get_text (GTK_ENTRY (reg_entry7));
	memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE + (DATA_ONE_SIZE*7)], entries_text, strlen(entries_text));

	// Get entry text from gravatar entry
	if(strlen(gtk_entry_get_text (GTK_ENTRY (reg_image))) < 3)
		errors_counter = 10;
	memset(img_text, 0, 100);
	strcpy_s(img_text, 100, gtk_entry_get_text (GTK_ENTRY (reg_image)));

	// Check adress for correct image, if ok send packet to server, if not write wrong gravatar link
	if (img_text[0] == 'h'  && img_text[1] == 't'   && img_text[2] == 't'  && img_text[3] == 'p'  && img_text[4] == ':'  &&  img_text[5] == '/'  && img_text[6] == '/'   &&
		img_text[7] == 'w'  && img_text[8] == 'w'   && img_text[9] == 'w'  && img_text[10] == '.' && img_text[11] == 'g' && img_text[12] == 'r'  &&  img_text[13] == 'a' &&
		img_text[14] == 'v' && img_text[15] == 'a'  && img_text[16] == 't' && img_text[17] == 'a' && img_text[18] == 'r' && img_text[19] == '.'  && img_text[20] == 'c'  &&
		img_text[21] == 'o' &&	img_text[22] == 'm' && img_text[23] == '/' && img_text[24] == 'a' && img_text[25] == 'v' && img_text[26] == 'a'  && img_text[27] == 't'  &&
		img_text[28] == 'a' && img_text[29] == 'r'  && img_text[30] == '/' )
		memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE + (DATA_ONE_SIZE*8)], img_text, strlen(img_text));

else if(img_text[0] == 'h'  && img_text[1] == 't'   && img_text[2] == 't'  && img_text[3] == 'p'  && img_text[4] == 's'  &&  img_text[5] == ':'  && img_text[6] == '/'   &&
		img_text[7] == '/'  && img_text[8] == 's'   && img_text[9] == 'k'  && img_text[10] == '.' && img_text[11] == 'g' && img_text[12] == 'r'  &&  img_text[13] == 'a' &&
		img_text[14] == 'v' && img_text[15] == 'a'  && img_text[16] == 't' && img_text[17] == 'a' && img_text[18] == 'r' && img_text[19] == '.'  && img_text[20] == 'c'  &&
		img_text[21] == 'o' &&	img_text[22] == 'm' && img_text[23] == '/' && img_text[24] == 'u' && img_text[25] == 's' && img_text[26] == 'e'  && img_text[27] == 'r'  &&
		img_text[28] == 'i' && img_text[29] == 'm'  && img_text[30] == 'a' )
		memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE + (DATA_ONE_SIZE*8)], img_text, strlen(img_text));
	else
		errors_counter = 11;

	// Now write possible errors
	if (errors_counter == 1)
		gtk_label_set_text (GTK_LABEL (reg_label), "Wrong Name!");
	else if (errors_counter == 2)
		gtk_label_set_text (GTK_LABEL (reg_label), "Wrong Surname!");
	else if (errors_counter == 3)
		gtk_label_set_text (GTK_LABEL (reg_label), "Wrong Nickname!");
	else if (errors_counter == 8)
		gtk_label_set_text (GTK_LABEL (reg_label), "Wrong Password!");
	else if (errors_counter == 4)
		gtk_label_set_text (GTK_LABEL (reg_label), "Wrong City!");
	else if (errors_counter == 5)
		gtk_label_set_text (GTK_LABEL (reg_label), "Wrong Country!");
	else if (errors_counter == 6)
		gtk_label_set_text (GTK_LABEL (reg_label), "Wrong Age!");
	else if (errors_counter == 7)
		gtk_label_set_text (GTK_LABEL (reg_label), "Wrong Gender!");
	else if (errors_counter == 10 || errors_counter == 11)
		gtk_label_set_text (GTK_LABEL (reg_label), "Wrong Gravatar!");

	// If everithing correct send packet else reset counter
	if (errors_counter == 0)
		send( s, sending_text, sending_text_len, 0 );
	else
		errors_counter = 0;
}

/************** Create reg window **********/
void create_registration()
{
	//Show loggin widgets if possible
	if ( log_entry || gtk_widget_get_visible(log_entry) || log_entry2 || gtk_widget_get_visible(log_entry2) || log_label || gtk_widget_get_visible(log_label) )
	{
		gtk_widget_hide(log_entry);
		gtk_widget_hide(log_entry2);
		gtk_widget_hide(log_label);
	}
	else function_show_warning("Can't hide log widgets. Doesn't exit or not showed?");

	// Resize window if posible
	if ( log_window && gtk_widget_get_visible(log_window))
		gtk_window_resize (GTK_WINDOW (log_window), 400, 340);
	else
		function_show_warning("Can't resize login window. Doesn't exit or not showed?");

	// Create text entry widget for name, lenght, set text, position, editable, pack, show
	reg_entry1 = gtk_entry_new ();
	if(!reg_entry1) function_show_warning("Can't create reg entry number 1");
	gtk_entry_set_max_length (GTK_ENTRY (reg_entry1), 9);
	gtk_entry_set_text (GTK_ENTRY (reg_entry1), "Name");
	gtk_editable_select_region (GTK_EDITABLE (reg_entry1), 0, GTK_ENTRY (reg_entry1)->text_length);
	gtk_box_pack_start (GTK_BOX (log_vbox), reg_entry1, TRUE, TRUE, 0);
	gtk_widget_show (reg_entry1);

	// Create text entry widget for name, lenght, set text, position, editable, pack, show
	reg_entry2 = gtk_entry_new ();
	if(!reg_entry2) function_show_warning("Can't create reg entry number 2");
	gtk_entry_set_max_length (GTK_ENTRY (reg_entry2), 9);
	gtk_entry_set_text (GTK_ENTRY (reg_entry2), "Surname");
	gtk_editable_select_region (GTK_EDITABLE (reg_entry2), 0, GTK_ENTRY (reg_entry2)->text_length);
	gtk_box_pack_start (GTK_BOX (log_vbox), reg_entry2, TRUE, TRUE, 0);
	gtk_widget_show (reg_entry2);

	// Create text entry widget for name, lenght, set text, position, editable, pack, show
	reg_entry3 = gtk_entry_new ();
	if(!reg_entry3) function_show_warning("Can't create reg entry number 3");
	gtk_entry_set_max_length (GTK_ENTRY (reg_entry3), 9);
	gtk_entry_set_text (GTK_ENTRY (reg_entry3), "Nickname");
	gtk_editable_select_region (GTK_EDITABLE (reg_entry3), 0, GTK_ENTRY (reg_entry3)->text_length);
	gtk_box_pack_start (GTK_BOX (log_vbox), reg_entry3, TRUE, TRUE, 0);
	gtk_widget_show (reg_entry3);

	// Create text entry widget for name, lenght, set text, position, editable, pack, show
	reg_entry8 = gtk_entry_new ();
	if(!reg_entry8) function_show_warning("Can't create reg entry number 8");
	gtk_entry_set_max_length (GTK_ENTRY (reg_entry8), 9);
	gtk_entry_set_text (GTK_ENTRY (reg_entry8), "Password");
	gtk_entry_set_visibility(GTK_ENTRY (reg_entry8), FALSE);
	gtk_editable_select_region (GTK_EDITABLE (reg_entry8), 0, GTK_ENTRY (reg_entry8)->text_length);
	gtk_box_pack_start (GTK_BOX (log_vbox), reg_entry8, TRUE, TRUE, 0);
	gtk_widget_show (reg_entry8);

	// Create text entry widget for name, lenght, set text, position, editable, pack, show
	reg_entry4 = gtk_entry_new ();
	if(!reg_entry4) function_show_warning("Can't create reg entry number 4");
	gtk_entry_set_max_length (GTK_ENTRY (reg_entry4), 9);
	gtk_entry_set_text (GTK_ENTRY (reg_entry4), "City");
	gtk_editable_select_region (GTK_EDITABLE (reg_entry4), 0, GTK_ENTRY (reg_entry4)->text_length);
	gtk_box_pack_start (GTK_BOX (log_vbox), reg_entry4, TRUE, TRUE, 0);
	gtk_widget_show (reg_entry4);

	// Create text entry widget for name, lenght, set text, position, editable, pack, show
	reg_entry5 = gtk_entry_new ();
	if(!reg_entry5) function_show_warning("Can't create reg entry number 5");
	gtk_entry_set_max_length (GTK_ENTRY (reg_entry5), 9);
	gtk_entry_set_text (GTK_ENTRY (reg_entry5), "Country");
	gtk_editable_select_region (GTK_EDITABLE (reg_entry5), 0, GTK_ENTRY (reg_entry5)->text_length);
	gtk_box_pack_start (GTK_BOX (log_vbox), reg_entry5, TRUE, TRUE, 0);
	gtk_widget_show (reg_entry5);

	// Create text entry widget for name, lenght, set text, position, editable, pack, show
	reg_entry6 = gtk_entry_new ();
	if(!reg_entry6) function_show_warning("Can't create reg entry number 6");
	gtk_entry_set_max_length (GTK_ENTRY (reg_entry6), 9);
	gtk_entry_set_text (GTK_ENTRY (reg_entry6), "Age");
	gtk_editable_select_region (GTK_EDITABLE (reg_entry6), 0, GTK_ENTRY (reg_entry6)->text_length);
	gtk_box_pack_start (GTK_BOX (log_vbox), reg_entry6, TRUE, TRUE, 0);
	gtk_widget_show (reg_entry6);

	// Create text entry widget for name, lenght, set text, position, editable, pack, show
	reg_entry7 = gtk_entry_new ();
	if(!reg_entry7) function_show_warning("Can't create reg entry number 7");
	gtk_entry_set_max_length (GTK_ENTRY (reg_entry7), 9);
	gtk_entry_set_text (GTK_ENTRY (reg_entry7), "Gender");
	gtk_editable_select_region (GTK_EDITABLE (reg_entry7), 0, GTK_ENTRY (reg_entry7)->text_length);
	gtk_box_pack_start (GTK_BOX (log_vbox), reg_entry7, TRUE, TRUE, 0);
	gtk_widget_show (reg_entry7);

	// Create text entry widget for name, lenght, set text, position, editable, pack, show
	reg_image = gtk_entry_new ();
	if(!reg_image) function_show_warning("Can't create reg_image");
	gtk_entry_set_max_length (GTK_ENTRY (reg_image), 100);
	gtk_entry_set_text (GTK_ENTRY (reg_image), "Insert Gravatar URL adress to your image");
	gtk_editable_select_region (GTK_EDITABLE (reg_image), 0, GTK_ENTRY (reg_image)->text_length);
	g_signal_connect (G_OBJECT(reg_image), "activate", G_CALLBACK (signal_send_registration), NULL);
	gtk_box_pack_start (GTK_BOX (log_vbox), reg_image, TRUE, TRUE, 0);
	gtk_widget_show (reg_image);

	// Create separator, size, pack, show
	reg_separator = gtk_hseparator_new ();
	if(!reg_separator) function_show_warning("Can't create reg_separator");
	gtk_widget_set_size_request (reg_separator, 400, 5);
	gtk_box_pack_start (GTK_BOX (log_vbox), reg_separator, FALSE, TRUE, 5);
	gtk_widget_show (reg_separator);

	// Create label, wrapping, pack, show
	reg_label = gtk_label_new ("Please register...");
	if(!reg_label) function_show_warning("Can't create reg_label");
	gtk_label_set_justify (GTK_LABEL (reg_label), GTK_JUSTIFY_FILL);
	gtk_label_set_line_wrap (GTK_LABEL (reg_label), TRUE);
	gtk_box_pack_start (GTK_BOX (log_vbox), reg_label, FALSE, TRUE, 5);
	gtk_widget_show (reg_label);

	// Create button for loggin, action, pack, mark, show
	reg_button = gtk_button_new_from_stock (GTK_STOCK_APPLY);
	if(!reg_button) function_show_warning("Can't create reg_button");
	g_signal_connect (G_OBJECT(reg_button), "clicked", G_CALLBACK (signal_send_registration), NULL);
	gtk_box_pack_start (GTK_BOX (log_vbox), reg_button, FALSE, TRUE, 5);
	gtk_widget_set_can_default (reg_button, TRUE);
	gtk_widget_grab_default (reg_button);
	gtk_widget_show (reg_button);
}