/*************************************************************************************************************
* This file contains debug functions
* 2012/8 by Dlxko
* Version 3.0
**************************************************************************************************************/
#include "Dialogs.h"

// Local static variables
static GtkWidget *dialog_window;
static GtkTextIter end_iter;
static GtkTextMark *endmark;

//Additional function to scrool
void additional_function_dialogs_scroll()
{
	// Scroll to end
	gtk_text_buffer_get_end_iter (server_buffer, &end_iter);
	endmark = gtk_text_mark_new ("E1", 0);
	if (endmark)
	{
		gtk_text_buffer_add_mark (server_buffer, endmark, &end_iter);
		gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (server_view), endmark, 0.0, FALSE, 0, 0);
		gtk_text_buffer_delete_mark (server_buffer, endmark);
	}
}

// Function to show warning message
void function_show_warning(char msg[WARNING_TMP_SIZE])
{
	// Alloc message
	gchar *message;

	// Create message
	gtk_text_buffer_get_end_iter (server_buffer, &end_iter);
	message = g_strconcat ("Warning: ", msg, "\n", NULL);
	gtk_text_buffer_insert_with_tags_by_name (server_buffer, &end_iter, message, -1, "textcolor-warning", NULL);

	// Scroll
	additional_function_dialogs_scroll();

	// If message dealloc
	if(message) g_free(message);
}

// Function to show error message
void function_show_error(char msg[WARNING_TMP_SIZE])
{
	// Alloc message
	gchar *message;

	// Create message
	gtk_text_buffer_get_end_iter (server_buffer, &end_iter);
	message = g_strconcat ("Error: ", msg, "\n", NULL);
	gtk_text_buffer_insert_with_tags_by_name (server_buffer, &end_iter, message, -1, "textcolor-error", NULL);

	// Scroll
	additional_function_dialogs_scroll();

	// If message dealloc
	if(message) g_free(message);
}

// Function to show ready message
void function_show_ready(char msg[WARNING_TMP_SIZE])
{
	// Alloc message
	gchar *message;

	// Check if no empty
	if (strlen(msg) != 0)
		message = g_strconcat ("Ready: ", msg, "\n", NULL);
	else
		message = g_strconcat ("\n", NULL);

	// Create message
	gtk_text_buffer_get_end_iter (server_buffer, &end_iter);
	gtk_text_buffer_insert_with_tags_by_name (server_buffer, &end_iter, message, -1, "textcolor-ready", NULL);

	// Scroll
	additional_function_dialogs_scroll();

	// If message dealloc
	if(message) g_free(message);
}

// Function to show info message
void function_show_info(char msg[WARNING_TMP_SIZE])
{
	// Alloc message
	gchar *message;

	// Create message
	gtk_text_buffer_get_end_iter (server_buffer, &end_iter);
	message = g_strconcat ("Info: ", msg, "\n", NULL);
	gtk_text_buffer_insert_with_tags_by_name (server_buffer, &end_iter, message, -1, "textcolor-info", NULL);

	// Scroll
	additional_function_dialogs_scroll();

	// If message dealloc
	if(message) g_free(message);
}

// Function to show dialog with fatal error message
void function_showgtk_fatal(char msg[WARNING_TMP_SIZE])
{
	// Alloc dialog
	GtkWidget *dialog;

	// Create dialog
	dialog = gtk_message_dialog_new(GTK_WINDOW(dialog_window), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, msg);
	gtk_window_set_title(GTK_WINDOW(dialog), "Error");
	gtk_dialog_run(GTK_DIALOG(dialog));
	gtk_widget_destroy(dialog);

	// Close server
	signal_end_server();
}

// Function to show dialog with info message
void function_showgtk_info(char msg[WARNING_TMP_SIZE])
{
	// Alloc dialog
	GtkWidget *dialog;

	// Create dialog
	dialog = gtk_message_dialog_new(GTK_WINDOW(dialog_window), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, msg);
	gtk_window_set_title(GTK_WINDOW(dialog), "Warning");
	gtk_dialog_run(GTK_DIALOG(dialog));
	gtk_widget_destroy(dialog);
}

// Function to create dialog window
void create_dialog_window()
{
	// New window, parameters, title, closing
	dialog_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_widget_set_size_request (GTK_WIDGET (dialog_window), 300, 80);
	gtk_window_set_title (GTK_WINDOW (dialog_window), "Dialog window");
	gtk_window_set_icon(GTK_WINDOW(dialog_window), default_icon);
}