/*************************************************************************************************************
* This file contains creating chat window
* 2012/8 by Dlxko
* Version 3.0
**************************************************************************************************************/
#include "Chat.h"

// After clicking in buddy list
void function_create_chat(gint buddy_number)
{
	// if only hidden only show else create new one
	if(buddy[buddy_number].buddy_chat == true)
		gtk_widget_show(buddy[buddy_number].window);
	else
		create_buddy_chat(buddy_number);
}

// Create buddy chat window for selected buddy
void create_buddy_chat(int input_buddy_number)
{
	// New window, parameters, title, closing functions
	buddy[input_buddy_number].window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_widget_set_size_request (GTK_WIDGET (buddy[input_buddy_number].window), 470, 285);
	gtk_window_set_title (GTK_WINDOW (buddy[input_buddy_number].window), "DxSock");
	gtk_widget_hide_on_delete(buddy[input_buddy_number].window);
	g_signal_connect_swapped (buddy[input_buddy_number].window, "delete-event", G_CALLBACK (signal_close_chat), buddy[input_buddy_number].window);
	gtk_window_set_icon(GTK_WINDOW(buddy[input_buddy_number].window), default_icon);

	// Create new box and container
	buddy[input_buddy_number].vbox = gtk_vbox_new (FALSE, 0);
	gtk_container_add (GTK_CONTAINER (buddy[input_buddy_number].window), buddy[input_buddy_number].vbox);

	// Create toolbar
	buddy[input_buddy_number].toolbar = gtk_toolbar_new();
	gtk_toolbar_set_style(GTK_TOOLBAR(buddy[input_buddy_number].toolbar), GTK_TOOLBAR_ICONS);
	gtk_container_set_border_width(GTK_CONTAINER(buddy[input_buddy_number].toolbar), 2);

	//Create buttons
	buddy[input_buddy_number].menu_transfer = gtk_tool_button_new_from_stock(GTK_STOCK_NETWORK);
	gtk_toolbar_insert(GTK_TOOLBAR(buddy[input_buddy_number].toolbar), buddy[input_buddy_number].menu_transfer, -1);
	buddy[input_buddy_number].menu_buddy_info = gtk_tool_button_new_from_stock(GTK_STOCK_ORIENTATION_PORTRAIT);
	gtk_toolbar_insert(GTK_TOOLBAR(buddy[input_buddy_number].toolbar), buddy[input_buddy_number].menu_buddy_info, -1);
	buddy[input_buddy_number].menu_buddy_history = gtk_tool_button_new_from_stock(GTK_STOCK_INDEX);
	gtk_toolbar_insert(GTK_TOOLBAR(buddy[input_buddy_number].toolbar), buddy[input_buddy_number].menu_buddy_history, -1);

	// Pack it into box
	gtk_box_pack_start(GTK_BOX(buddy[input_buddy_number].vbox), buddy[input_buddy_number].toolbar, FALSE, FALSE, 0);

	// Set signals
	g_signal_connect(G_OBJECT(buddy[input_buddy_number].menu_transfer), "clicked", G_CALLBACK(signal_ftp_upload), (gint*) input_buddy_number);
	g_signal_connect(G_OBJECT(buddy[input_buddy_number].menu_buddy_info), "clicked", G_CALLBACK(function_info_request), (gint*) input_buddy_number);
	g_signal_connect(G_OBJECT(buddy[input_buddy_number].menu_buddy_history), "clicked", G_CALLBACK(function_show_history_window), (gint*) input_buddy_number);

	// Create scroll, set options, and pack into vbox
	buddy[input_buddy_number].scroll = gtk_scrolled_window_new(NULL, NULL);
	gtk_box_pack_start(GTK_BOX(buddy[input_buddy_number].vbox), buddy[input_buddy_number].scroll, TRUE, TRUE, 0);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(buddy[input_buddy_number].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
	buddy[input_buddy_number].text_view = gtk_text_view_new ();
	buddy[input_buddy_number].buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (buddy[input_buddy_number].text_view));
	gtk_text_view_set_editable (GTK_TEXT_VIEW (buddy[input_buddy_number].text_view), 0);
	gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (buddy[input_buddy_number].text_view), GTK_WRAP_CHAR);
	gtk_widget_set_size_request (buddy[input_buddy_number].text_view, 400, 200);
	gtk_text_view_set_left_margin (GTK_TEXT_VIEW (buddy[input_buddy_number].text_view), 5);
	gtk_text_view_set_right_margin (GTK_TEXT_VIEW (buddy[input_buddy_number].text_view), 5);
	gtk_text_view_set_pixels_above_lines (GTK_TEXT_VIEW (buddy[input_buddy_number].text_view), 0);

	// Create formating flags
	gtk_text_buffer_create_tag (buddy[input_buddy_number].buffer, "bold", "weight", PANGO_WEIGHT_BOLD, NULL);
	gtk_text_buffer_create_tag (buddy[input_buddy_number].buffer, "italic", "style", PANGO_STYLE_ITALIC, NULL);
	gtk_text_buffer_create_tag (buddy[input_buddy_number].buffer, "font", "font", "fixed", NULL);

	// Add to container
	gtk_container_add(GTK_CONTAINER(buddy[input_buddy_number].scroll), buddy[input_buddy_number].text_view);

	// Create status bar, pack, signal, show
	buddy[input_buddy_number].statusbar = gtk_statusbar_new();
	gtk_box_pack_start(GTK_BOX(buddy[input_buddy_number].vbox), buddy[input_buddy_number].statusbar, FALSE, FALSE, 0);
	g_signal_connect(buddy[input_buddy_number].buffer, "changed", G_CALLBACK(function_update_statusbar), buddy[input_buddy_number].statusbar);

	// Create text entry widget, lenght, action, default text, position, editable, pack, show
	buddy[input_buddy_number].entry = gtk_entry_new ();
	gtk_entry_set_max_length (GTK_ENTRY (buddy[input_buddy_number].entry), CHAT_MAX_MESSAGE_SIZE);
	g_signal_connect (buddy[input_buddy_number].entry, "activate", G_CALLBACK (signal_send_message), (gint*) input_buddy_number);
	buddy[input_buddy_number].tmp_pos = GTK_ENTRY (buddy[input_buddy_number].entry)->text_length;
	gtk_editable_select_region (GTK_EDITABLE (buddy[input_buddy_number].entry), 0, GTK_ENTRY (buddy[input_buddy_number].entry)->text_length);
	gtk_box_pack_start (GTK_BOX (buddy[input_buddy_number].vbox), buddy[input_buddy_number].entry, TRUE, TRUE, 0);

	// Show window and set title
	gtk_widget_show (buddy[input_buddy_number].window);
	gtk_window_set_title (GTK_WINDOW (buddy[input_buddy_number].window), buddy[input_buddy_number].nick_name);

	// Set active
	buddy[input_buddy_number].buddy_chat = true;
	buddy[input_buddy_number].who_sender = 3;

	// Show
	gtk_widget_show_all(buddy[input_buddy_number].window);
}