/*************************************************************************************************************
* This file contains creating of server window function
* 2012/8 by Dlxko
* Version 3.0
**************************************************************************************************************/
#include "ServerWindow.h"

// Local static widgets
static gint server_tmppos;
static GtkWidget *server_window;
static GtkWidget *server_notebook;
static GtkWidget *server_notebox;
static GtkWidget *server_vbox;
static GtkWidget *server_scroll;
static GtkWidget *server_entry;
static GtkWidget *server_notebox2;
static GtkWidget *server_proglabel;
static GtkWidget *server_notebox3;
static GtkWidget *server_hbox;
static GtkWidget *server_vbox2;
static GtkWidget *server_separator;
static GtkWidget *server_notebox4;
static GtkWidget *server_label;

// Function to create server window
void create_server_window()
{
	// Create server window
	server_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_window_set_default_size(GTK_WINDOW(server_window), 700, 500);
	gtk_container_border_width (GTK_CONTAINER (server_window), 10);
	g_signal_connect_swapped (server_window, "delete-event", G_CALLBACK (signal_end_server), NULL);
	g_signal_connect(GTK_OBJECT(server_window), "destroy", G_CALLBACK(signal_end_server), NULL);
	gtk_widget_show(server_window);

	// Create new box and container
	server_notebox = gtk_vbox_new (FALSE, 0);
	gtk_container_add (GTK_CONTAINER (server_window), server_notebox);
	gtk_widget_show (server_notebox);

	// Create notebook
	server_notebook = gtk_notebook_new();
	gtk_box_pack_start(GTK_BOX(server_notebox), server_notebook, TRUE, TRUE, 0);
	gtk_notebook_set_scrollable(GTK_NOTEBOOK(server_notebook), TRUE);
	gtk_notebook_popup_enable(GTK_NOTEBOOK(server_notebook));
	gtk_widget_show(server_notebook);

	// Create notebook list box
	server_vbox = gtk_vbox_new (FALSE, 0);

	// Create scroll, set options, and pack into vbox
	server_scroll = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(server_scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
	gtk_container_add(GTK_CONTAINER(server_vbox), server_scroll);

	// 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
	server_view = gtk_text_view_new ();
	server_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (server_view));
	gtk_text_view_set_editable (GTK_TEXT_VIEW (server_view), 0);
	gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (server_view), GTK_WRAP_CHAR);
	gtk_widget_set_size_request (server_view, 650, 450);
	gtk_text_view_set_left_margin (GTK_TEXT_VIEW (server_view), 5);
	gtk_text_view_set_right_margin (GTK_TEXT_VIEW (server_view), 5);
	gtk_text_view_set_pixels_above_lines (GTK_TEXT_VIEW (server_view), 0);

	// Create formating flags
	gtk_text_buffer_create_tag (server_buffer, "bold", "weight", PANGO_WEIGHT_BOLD, NULL);
	gtk_text_buffer_create_tag (server_buffer, "italic", "style", PANGO_STYLE_ITALIC, NULL);
	gtk_text_buffer_create_tag (server_buffer, "font", "font", "fixed", NULL);
	gtk_text_buffer_create_tag (server_buffer, "textcolor-ready", "foreground", "#FF66FF", NULL);
	gtk_text_buffer_create_tag (server_buffer, "textcolor-warning", "foreground", "#FF9933", NULL);
	gtk_text_buffer_create_tag (server_buffer, "textcolor-error", "foreground", "#FF0033", NULL);
	gtk_text_buffer_create_tag (server_buffer, "textcolor-info", "foreground", "#33CC33", NULL);

	// Add to container
	gtk_container_add(GTK_CONTAINER(server_scroll), server_view);

	// Create text entry widget, lenght, action, default text, position, editable, pack, show
	server_entry = gtk_entry_new ();
	gtk_entry_set_max_length (GTK_ENTRY (server_entry), SERVER_INPUT_SIZE);
	//g_signal_connect (server_entry, "activate", G_CALLBACK (signal_send_message), (gint*) buddy_numb);
	server_tmppos = GTK_ENTRY (server_entry)->text_length;
	gtk_editable_select_region (GTK_EDITABLE (server_entry), 0, GTK_ENTRY (server_entry)->text_length);
	gtk_container_add(GTK_CONTAINER(server_vbox), server_entry);

	// Create notebook page
	gtk_notebook_append_page(GTK_NOTEBOOK(server_notebook), server_vbox, gtk_label_new("Debug"));
	gtk_widget_show (server_vbox);
	gtk_widget_show(server_scroll);
	gtk_widget_show (server_view);
	gtk_widget_show (server_entry);

	// Create noteboook box 2
	server_notebox2 = gtk_vbox_new (FALSE, 15);

	// Create label, wrapping, pack, show
	server_definfo = gtk_label_new ("No info...");
	gtk_misc_set_alignment(GTK_MISC(server_definfo), 0.01f, 0.0f);
	gtk_label_set_line_wrap (GTK_LABEL (server_definfo), TRUE);
	gtk_box_pack_start (GTK_BOX (server_notebox2), server_definfo, FALSE, FALSE, 15);
	gtk_widget_show (server_definfo);

	// Create notebook page 2
	gtk_notebook_append_page(GTK_NOTEBOOK(server_notebook), server_notebox2, gtk_label_new("Info"));
	gtk_widget_show(server_notebox2);
	gtk_widget_show(server_definfo);

	// Create noteboook box 3
	server_notebox3 = gtk_vbox_new (FALSE, 15);

	// Pack everything into vbox and hbox
	server_vbox2 = gtk_vbox_new(FALSE, 0);
	gtk_box_pack_start(GTK_BOX(server_notebox3), server_vbox2, FALSE, FALSE, 15);

	server_hbox = gtk_hbox_new(FALSE, 0);
	gtk_container_add(GTK_CONTAINER(server_vbox2), server_hbox);

	// Create label, wrapping, pack, show
	server_proglabel = gtk_label_new ("Clients on server");
	gtk_misc_set_alignment(GTK_MISC(server_proglabel), 0.01f, 0.0f);
	gtk_label_set_line_wrap (GTK_LABEL (server_proglabel), TRUE);
	gtk_box_pack_start (GTK_BOX (server_hbox), server_proglabel, FALSE, FALSE, 5);
	gtk_widget_set_size_request(server_proglabel, 50, 30);

	// Create progress bar
	progress1 = gtk_progress_bar_new();
	gtk_box_pack_start (GTK_BOX (server_hbox), progress1, FALSE, FALSE, 15);
	gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress1), 0);
	gtk_widget_set_size_request(progress1, 220, 25);

	// Update progress bar
	function_update_progress_clients();

	// Create separator, size, pack, show
	server_separator = gtk_hseparator_new ();
	gtk_widget_set_size_request (server_separator, 200, 15);
	gtk_box_pack_start (GTK_BOX (server_vbox2), server_separator, FALSE, TRUE, 5);
	gtk_widget_show (server_separator);

	// Create label, wrapping, pack, show
	server_datalabel = gtk_label_new("No info");
	gtk_label_set_markup(GTK_LABEL(server_datalabel), "<b>Sent:</b> <span foreground=\"blue\">0</span> mbytes (<span foreground=\"blue\">0</span> bytes)");
	gtk_misc_set_alignment(GTK_MISC(server_datalabel), 0.01f, 0.0f);
	gtk_label_set_line_wrap (GTK_LABEL (server_datalabel), TRUE);
	gtk_box_pack_start (GTK_BOX (server_vbox2), server_datalabel, FALSE, FALSE, 0);
	gtk_widget_set_size_request(server_datalabel, 200, 15);

	// Create label, wrapping, pack, show
	server_datalabel2 = gtk_label_new("No info");
	gtk_label_set_markup(GTK_LABEL(server_datalabel2), "<b>Received:</b> <span foreground=\"blue\">0</span> mbytes (<span foreground=\"blue\">0</span> bytes)");
	gtk_misc_set_alignment(GTK_MISC(server_datalabel2), 0.01f, 0.0f);
	gtk_label_set_line_wrap (GTK_LABEL (server_datalabel2), TRUE);
	gtk_box_pack_start (GTK_BOX (server_vbox2), server_datalabel2, FALSE, FALSE, 0);
	gtk_widget_set_size_request(server_datalabel2, 200, 15);

	// Create notebook page 3
	gtk_notebook_append_page(GTK_NOTEBOOK(server_notebook), server_notebox3, gtk_label_new("Stats"));
	gtk_widget_show (server_hbox);
	gtk_widget_show (server_vbox2);
	gtk_widget_show(server_notebox3);
	gtk_widget_show(server_proglabel);
	gtk_widget_show(server_datalabel);
	gtk_widget_show(server_datalabel2);
	gtk_widget_show(server_separator);
	gtk_widget_show(progress1);

	// Create page 4
	server_notebox4 = gtk_alignment_new(0.5, 0.5, 0, 0);

	// Create label
	server_label = gtk_label_new("No info");
	gtk_label_set_markup(GTK_LABEL(server_label), "<span font='12.5'><b>   DxSocks 3.0</b> \n<span foreground=\"orange\">Created by Dlxko</span> \n       08/2012</span>");
	gtk_container_add(GTK_CONTAINER(server_notebox4), server_label);

	// Apend to notebook
	gtk_notebook_append_page(GTK_NOTEBOOK(server_notebook), server_notebox4, gtk_label_new("About"));
	gtk_widget_show (server_notebox4);
	gtk_widget_show (server_label);
}