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

// Local static variables
static const gchar *day_text;
static const gchar *month_text;
static const gchar *year_text;

// Local static widgets
static GtkWidget *history_hbox;
static GtkWidget *history_day;
static GtkWidget *history_month;
static GtkWidget *history_year;
static GtkWidget *history_vbox_view;
static GtkWidget *history_vbox;
static GtkWidget *history_scroll;

// Static handlers
int active_history_buddy;

// Function to do action when clicked history button
void function_show_history_window(GtkWidget *unused_button, gint input_buddy_number)
{
	// Set active history buddy to extern variable
	active_history_buddy = input_buddy_number;

	//Show window
	if (!gtk_widget_get_visible(history_window))
		gtk_widget_show_all(history_window);

	// Update label
	gtk_label_set_text(GTK_LABEL(history_label), LANG_ENTER_HISTORY_DATA);
}

// Function to request data from server
void signal_request_history_data()
{
	// Check if entries contain datas
	if( (strlen(gtk_entry_get_text (GTK_ENTRY (history_day))) < 1) ||
		(strlen(gtk_entry_get_text (GTK_ENTRY (history_month))) < 1) ||
		(strlen(gtk_entry_get_text (GTK_ENTRY (history_year))) < 1) )
	{
		function_show_warning(LANG_NO_TEXT_HISTORY);
		return;
	}

	// Check buddy number
	if((active_history_buddy == MAX_BUDDIES+1) || (active_history_buddy > MAX_BUDDIES+1))
	{
		function_show_warning(LANG_WRONG_BUDDY_PASSED);
		return;
	}

	// Check buddy name
	if(strlen(buddy[active_history_buddy].nick_name) < 1)
	{
		function_show_warning(LANG_WRONG_BUDDY_PASSED_NAME);
		return;
	}

	// Get datas
	day_text = gtk_entry_get_text(GTK_ENTRY (history_day));
	month_text = gtk_entry_get_text(GTK_ENTRY (history_month));
	year_text = gtk_entry_get_text(GTK_ENTRY (history_year));

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

	// Update text for server with "DX_OPCODE_NFR" 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_HIS, OPCODE_SIZE);
	memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE], buddy[active_history_buddy].nick_name, strlen(buddy[active_history_buddy].nick_name));
	memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE], day_text, 2);
	memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE+2], month_text, 2);
	memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE+4], year_text, 4);

	// Send socket
	function_send_data(sending_text, sending_text_len);

	// Empty buffer of text view
	gtk_text_buffer_set_text (GTK_TEXT_BUFFER(history_buffer),"", 0);
}

// Function to create history window
void create_history_window()
{
	// New window, parameters, title, closing functions
	history_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_widget_set_size_request (GTK_WIDGET (history_window), 200, 60);
	gtk_window_set_title (GTK_WINDOW (history_window), LANG_HISTORY_WINDOW_TITLE);
	gtk_widget_hide_on_delete(history_window);
	gtk_window_set_icon(GTK_WINDOW(history_window), default_icon);
	g_signal_connect_swapped (history_window, "delete-event", G_CALLBACK (signal_close_history), history_window);

	// Create new box and container
	history_vbox = gtk_vbox_new (FALSE, 0);
	gtk_container_add (GTK_CONTAINER (history_window), history_vbox);

	// Create label, wrapping, pack, show
	history_label = gtk_label_new (LANG_ENTER_HISTORY_DATA);
	gtk_label_set_justify (GTK_LABEL (history_label), GTK_JUSTIFY_FILL);
	gtk_label_set_line_wrap (GTK_LABEL (history_label), TRUE);
	gtk_box_pack_start (GTK_BOX (history_vbox), history_label, FALSE, TRUE, 5);

	// Create new box and container
	history_hbox = gtk_hbox_new (FALSE, 0);
	gtk_box_pack_start (GTK_BOX (history_vbox), history_hbox, FALSE, TRUE, 5);

	// Create text entry widget for username, lenght, set text, position, editable, pack, show
	history_day = gtk_entry_new ();
	gtk_entry_set_max_length (GTK_ENTRY (history_day), 2);
	gtk_entry_set_text (GTK_ENTRY (history_day), "DD");
	gtk_editable_select_region (GTK_EDITABLE (history_day), 0, GTK_ENTRY (history_day)->text_length);
	gtk_box_pack_start (GTK_BOX (history_hbox), history_day, TRUE, TRUE, 5);

	// Create text entry widget for username, lenght, set text, position, editable, pack, show
	history_month = gtk_entry_new ();
	gtk_entry_set_max_length (GTK_ENTRY (history_month), 2);
	gtk_entry_set_text (GTK_ENTRY (history_month), "MM");
	gtk_editable_select_region (GTK_EDITABLE (history_month), 0, GTK_ENTRY (history_month)->text_length);
	gtk_box_pack_start (GTK_BOX (history_hbox), history_month, TRUE, TRUE, 5);

	// Create text entry widget for username, lenght, set text, position, editable, pack, show
	history_year = gtk_entry_new ();
	gtk_entry_set_max_length (GTK_ENTRY (history_year), 4);
	gtk_entry_set_text (GTK_ENTRY (history_year), "YYY");
	gtk_editable_select_region (GTK_EDITABLE (history_year), 0, GTK_ENTRY (history_year)->text_length);
	gtk_box_pack_start (GTK_BOX (history_hbox), history_year, TRUE, TRUE, 5);

	// Set signals
	g_signal_connect (history_day, "activate", G_CALLBACK (signal_request_history_data), NULL);
	g_signal_connect (history_month, "activate", G_CALLBACK (signal_request_history_data), NULL);
	g_signal_connect (history_year, "activate", G_CALLBACK (signal_request_history_data), NULL);
}

// Function to create history window
void create_history_view_window()
{
	// New window, parameters, title, closing functions
	history_window_view = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_widget_set_size_request (history_window_view, 400, 200);
	gtk_window_set_title (GTK_WINDOW (history_window_view), LANG_HISTORY_WINDOW_TITLE);
	gtk_widget_hide_on_delete(history_window_view);
	gtk_window_set_icon(GTK_WINDOW(history_window_view), default_icon);
	g_signal_connect_swapped (history_window_view, "delete-event", G_CALLBACK (signal_close_history_view), history_window_view);

	// Create new box and container
	history_vbox_view = gtk_vbox_new (FALSE, 0);
	gtk_container_add (GTK_CONTAINER (history_window_view), history_vbox_view);

	// Create scroll, set options, and pack into vbox
	history_scroll = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(history_scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

	// Create text view widget, pack it, set buffer, non editable, wrapping
	// Widget size, set empty buffer, font, margin, design, insert into scroll and show them
	history_view = gtk_text_view_new ();
	history_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (history_view));
	gtk_text_view_set_editable (GTK_TEXT_VIEW (history_view), 0);
	gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (history_view), GTK_WRAP_CHAR);
	gtk_widget_set_size_request (history_view, 400, 200);
	gtk_text_view_set_left_margin (GTK_TEXT_VIEW (history_view), 5);
	gtk_text_view_set_right_margin (GTK_TEXT_VIEW (history_view), 5);
	gtk_text_view_set_pixels_above_lines (GTK_TEXT_VIEW (history_view), 0);

	// Create formating flags
	gtk_text_buffer_create_tag (history_buffer, "bold", "weight", PANGO_WEIGHT_BOLD, NULL);
	gtk_text_buffer_create_tag (history_buffer, "italic", "style", PANGO_STYLE_ITALIC, NULL);
	gtk_text_buffer_create_tag (history_buffer, "font", "font", "fixed", NULL);

	// Add to containers
	gtk_box_pack_start(GTK_BOX(history_vbox_view), history_scroll, TRUE, TRUE, 0);
	gtk_container_add(GTK_CONTAINER(history_scroll), history_view);
}