/*************************************************************************************************************
This file contains chat window
2012/6 by Dlxko
**************************************************************************************************************/
#include "Chat.h"

/******************** Create buddy chat ******************/
bool create_buddy_chat(int buddy_numb)
{
	// New window, parameters, title, closing functions
	buddy[buddy_numb].window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	if(!buddy[buddy_numb].window) return false;
	gtk_widget_set_size_request (GTK_WIDGET (buddy[buddy_numb].window), 470, 250);
	gtk_window_set_title (GTK_WINDOW (buddy[buddy_numb].window), "DxSock");
	gtk_widget_hide_on_delete(buddy[buddy_numb].window);
	g_signal_connect_swapped (buddy[buddy_numb].window, "delete-event", G_CALLBACK (signal_close_chat), buddy[buddy_numb].window);
	gtk_window_set_icon(GTK_WINDOW(buddy[buddy_numb].window), default_icon);

	// Create new box and container
	buddy[buddy_numb].vbox = gtk_vbox_new (FALSE, 0);
	if(!buddy[buddy_numb].vbox) return false;
	gtk_container_add (GTK_CONTAINER (buddy[buddy_numb].window), buddy[buddy_numb].vbox);
	gtk_widget_show (buddy[buddy_numb].vbox);

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

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

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

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

	// Create new box and container
	buddy[buddy_numb].hbox = gtk_hbox_new (FALSE, 0);
	if(!buddy[buddy_numb].hbox) return false;
	gtk_container_add (GTK_CONTAINER (buddy[buddy_numb].vbox), buddy[buddy_numb].hbox);
	gtk_widget_show (buddy[buddy_numb].hbox);

	// Create text entry widget, lenght, action, default text, position, editable, pack, show
	buddy[buddy_numb].entry = gtk_entry_new ();
	if(!buddy[buddy_numb].entry) return false;
	gtk_entry_set_max_length (GTK_ENTRY (buddy[buddy_numb].entry), 100);
	g_signal_connect (buddy[buddy_numb].entry, "activate", G_CALLBACK (signal_send_message), (gint*) buddy_numb);
	buddy[buddy_numb].tmp_pos = GTK_ENTRY (buddy[buddy_numb].entry)->text_length;
	gtk_editable_select_region (GTK_EDITABLE (buddy[buddy_numb].entry), 0, GTK_ENTRY (buddy[buddy_numb].entry)->text_length);
	gtk_box_pack_start (GTK_BOX (buddy[buddy_numb].hbox), buddy[buddy_numb].entry, TRUE, TRUE, 0);
	gtk_widget_show (buddy[buddy_numb].entry);

	// Create file button, connect with signal, pack, show
	buddy[buddy_numb].file_button = gtk_button_new_with_label("+");
	if(!buddy[buddy_numb].file_button) return false;
	g_signal_connect (G_OBJECT(buddy[buddy_numb].file_button), "clicked", G_CALLBACK (ftp_upload_action), (gint*) buddy_numb);
	gtk_widget_set_size_request(buddy[buddy_numb].file_button, 20, 10);
	gtk_box_pack_start (GTK_BOX (buddy[buddy_numb].hbox), buddy[buddy_numb].file_button, FALSE, FALSE, 0);
	gtk_widget_show (buddy[buddy_numb].file_button);

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

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

	return true;
}