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

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

// For crypting password
static char *cryped_password;

// Send registration data to socket
void signal_send_registration()
{
	// Count lenght
	sending_text_len = LENGHT_SIZE+OPCODE_SIZE + SHA_PASS_SIZE+ (DATA_ONE_SIZE*7) + 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, copy lenght datas and opcode
	memset(sending_text,0,BUF_SIZE);
	memcpy(sending_text, sending_text_len_char, LENGHT_SIZE);
	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 and crypt it!
	if(strlen(gtk_entry_get_text (GTK_ENTRY (reg_entry8))) < 3)
		errors_counter = 8;
	entry_text = gtk_entry_get_text (GTK_ENTRY (reg_entry8));
	cryped_password = function_crypt_sha512(entry_text);
	memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE + (DATA_ONE_SIZE*3)], cryped_password, SHA_PASS_SIZE);

	// 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 + SHA_PASS_SIZE +(DATA_ONE_SIZE*3)], 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+SHA_PASS_SIZE+(DATA_ONE_SIZE*4)], 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 + SHA_PASS_SIZE+(DATA_ONE_SIZE*5)], 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 + SHA_PASS_SIZE+(DATA_ONE_SIZE*6)], 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 + SHA_PASS_SIZE+ (DATA_ONE_SIZE*7)], 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 + SHA_PASS_SIZE +(DATA_ONE_SIZE*7)], 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;
}

