/*************************************************************************************************************
* This file contains registration
* 2012/8 by Dlxko
* Version 3.0
**************************************************************************************************************/
#include "Registration.h"

// Local static variables
static char img_text[AVATAR_SIZE];
static int errors_counter;
static int avatar_link_len;

// Send registration data to socket
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;
	entry_text = gtk_entry_get_text (GTK_ENTRY (reg_entry1));
	memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE], entry_text, strlen(entry_text));

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

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

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

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

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

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

	// Copy gender
	if(strlen(gtk_entry_get_text (GTK_ENTRY (reg_entry7))) < 3)
		errors_counter = 7;
	entry_text = gtk_entry_get_text (GTK_ENTRY (reg_entry7));
	memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE + (DATA_ONE_SIZE*7)], entry_text, strlen(entry_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, AVATAR_SIZE);
	strcpy_s(img_text, AVATAR_SIZE, gtk_entry_get_text (GTK_ENTRY (reg_image)));

	// Get avatar link len
	avatar_link_len = strlen(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[avatar_link_len-4] == 'j'  && img_text[avatar_link_len-3] == 'p'  && img_text[avatar_link_len-2] == 'e' && img_text[avatar_link_len-1] == 'g')
		memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE + (DATA_ONE_SIZE*8)], img_text, strlen(img_text));
	else if (img_text[avatar_link_len-3] == 'j'  && img_text[avatar_link_len-2] == 'p' && img_text[avatar_link_len-1] == 'g')
		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), LANG_WRONG_NAME);
	else if (errors_counter == 2)
		gtk_label_set_text (GTK_LABEL (reg_label), LANG_WRONG_SURNAME);
	else if (errors_counter == 3)
		gtk_label_set_text (GTK_LABEL (reg_label), LANG_WRONG_NICKNAME);
	else if (errors_counter == 8)
		gtk_label_set_text (GTK_LABEL (reg_label), LANG_WRONG_PASSWORD);
	else if (errors_counter == 4)
		gtk_label_set_text (GTK_LABEL (reg_label), LANG_WRONG_CITY);
	else if (errors_counter == 5)
		gtk_label_set_text (GTK_LABEL (reg_label), LANG_WRONG_COUNTRY);
	else if (errors_counter == 6)
		gtk_label_set_text (GTK_LABEL (reg_label), LANG_WRONG_AGE);
	else if (errors_counter == 7)
		gtk_label_set_text (GTK_LABEL (reg_label), LANG_WRONG_GENDER);
	else if (errors_counter == 10 || errors_counter == 11)
		gtk_label_set_text (GTK_LABEL (reg_label), LANG_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 directly from loggin
void create_registration()
{
	// Update interface
	gtk_widget_hide(log_username);
	gtk_widget_hide(log_password);
	gtk_widget_hide(log_label);
	gtk_window_resize (GTK_WINDOW (log_window), 400, 340);

	// Create text entry widget for name, lenght, set text, position, editable, pack, show
	reg_entry1 = gtk_entry_new ();
	gtk_entry_set_max_length (GTK_ENTRY (reg_entry1), DATA_ONE_SIZE);
	gtk_entry_set_text (GTK_ENTRY (reg_entry1), LANG_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 ();
	gtk_entry_set_max_length (GTK_ENTRY (reg_entry2), DATA_ONE_SIZE);
	gtk_entry_set_text (GTK_ENTRY (reg_entry2), LANG_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 ();
	gtk_entry_set_max_length (GTK_ENTRY (reg_entry3), DATA_ONE_SIZE);
	gtk_entry_set_text (GTK_ENTRY (reg_entry3), LANG_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 ();
	gtk_entry_set_max_length (GTK_ENTRY (reg_entry8), DATA_ONE_SIZE);
	gtk_entry_set_text (GTK_ENTRY (reg_entry8), LANG_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 ();
	gtk_entry_set_max_length (GTK_ENTRY (reg_entry4), DATA_ONE_SIZE);
	gtk_entry_set_text (GTK_ENTRY (reg_entry4), LANG_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 ();
	gtk_entry_set_max_length (GTK_ENTRY (reg_entry5), DATA_ONE_SIZE);
	gtk_entry_set_text (GTK_ENTRY (reg_entry5), LANG_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 ();
	gtk_entry_set_max_length (GTK_ENTRY (reg_entry6), DATA_ONE_SIZE);
	gtk_entry_set_text (GTK_ENTRY (reg_entry6), LANG_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 ();
	gtk_entry_set_max_length (GTK_ENTRY (reg_entry7), DATA_ONE_SIZE);
	gtk_entry_set_text (GTK_ENTRY (reg_entry7), LANG_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 ();
	gtk_entry_set_max_length (GTK_ENTRY (reg_image), AVATAR_SIZE);
	gtk_entry_set_text (GTK_ENTRY (reg_image), LANG_GRAVATAR);
	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 ();
	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 (LANG_REGISTER);
	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);
	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);
}