/*************************************************************************************************************
* This file contains creating  and functionality of requests system
* 2012/8 by Dlxko
* Version 3.0
**************************************************************************************************************/
#include "Requests.h"

// Àdditional function to process request
void additional_function_requests_process_request(int function_event_id)
{
	// Destroy event and clean name in memory
	gtk_widget_destroy(req_events[function_event_id].ok_button);
	gtk_widget_destroy(req_events[function_event_id].shut_button);
	gtk_widget_destroy(req_events[function_event_id].label);
	gtk_widget_destroy(req_events[function_event_id].hbox);
	gtk_widget_destroy(req_events[function_event_id].vbox);
	memset(req_events[function_event_id].nick_name, 0, DATA_ONE_SIZE);

	// Make inactive also decrease counter
	req_events[function_event_id].active = false;
	--activated_events;

	// Destroy previous button
	gtk_container_remove (GTK_CONTAINER(buddy_toolbar), GTK_WIDGET(buddy_events));

	// If no events left
	if (activated_events == 0)
	{
		// Create new button or report
		buddy_events = gtk_tool_button_new_from_stock(GTK_STOCK_NO);

		// Try to hide requests window
		gtk_widget_hide(requests_window);
	}
	else
	{
		// Refresh toolbar button
		buddy_events = gtk_tool_button_new_from_stock(GTK_STOCK_YES);

		// Attach signal
		g_signal_connect (G_OBJECT(buddy_events), "clicked", G_CALLBACK (signal_show_requests), NULL);
	}

	// Pack button into toolbar
	gtk_toolbar_insert(GTK_TOOLBAR(buddy_toolbar), buddy_events, -1);
	gtk_widget_show(GTK_WIDGET(buddy_events));

	// Count lenght
	sending_text_len = LENGHT_SIZE + REQUESTS_ID_CHAR_SIZE + OPCODE_SIZE;
	sprintf_s(sending_text_len_char, LENGHT_SIZE, "%d", sending_text_len);

	// Update text for server with "DX_OPCODE_DEV" and id datas
	memset(sending_text,0,BUF_SIZE);
	memcpy(sending_text, sending_text_len_char, LENGHT_SIZE);
	memcpy(&sending_text[LENGHT_SIZE], DX_OPCODE_DEV, OPCODE_SIZE);
	memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE], req_events[function_event_id].id, strlen(req_events[function_event_id].id));

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

// Create new requests window in buddy list
void crate_requests()
{
	// Create window
	requests_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(GTK_WINDOW(requests_window), LANG_FRIEND_REQUESTS);
	gtk_window_set_default_size(GTK_WINDOW(requests_window), 310, 300);
	gtk_widget_hide_on_delete(requests_window);
	g_signal_connect_swapped (requests_window, "delete-event", G_CALLBACK (signal_close_requests), NULL);
	gtk_window_set_icon(GTK_WINDOW(requests_window), default_icon);

	// Create new vbox for everything to add in
	requests_vbox = gtk_vbox_new(FALSE, 0);
	gtk_container_add (GTK_CONTAINER (requests_window), requests_vbox);

	// Show box
	gtk_widget_show(requests_vbox);
}

// Show requests window
void signal_show_requests()
{
	// Just show requests
	gtk_widget_show_all(requests_window);
}

// If shut button pressed skip accepting
void signal_requests_shut(GtkWidget *button, gint event_id)
{
	//Process
	additional_function_requests_process_request(event_id);
}

// If accept button pressed
void signal_requests_ok(GtkWidget *button, gint event_id)
{
	// Count lenght
	sending_text_len = LENGHT_SIZE + DATA_ONE_SIZE + OPCODE_SIZE;
	sprintf_s(sending_text_len_char, LENGHT_SIZE, "%d", sending_text_len);

	if (strlen(req_events[event_id].nick_name) > 0)
	{
		// Update text for server with "DX_OPCODE_NAR" 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_NAR, OPCODE_SIZE);
		memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE], req_events[event_id].nick_name, strlen(req_events[event_id].nick_name));

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

	//Process
	additional_function_requests_process_request(event_id);
}