/*************************************************************************************************************
* This file contains Chat SingleBodies system
* 2012/10 by Dlxko
* Version 4.0
**************************************************************************************************************/
#include "ChatSingleBodies.h"

// Local defines
#define HISTORY 1
#define HISTORY_VIEW 2
#define CHAT 3
#define INFO 4

// Function to append new page into chat
void additional_function_chat_single_bodies_append_page(int function_buddy_number)
{
	buddy[function_buddy_number].vbox = gtk_vbox_new (FALSE, 0);
	buddy[function_buddy_number].notebook_position = gtk_notebook_append_page(GTK_NOTEBOOK(conversation_notebook), buddy[function_buddy_number].vbox, gtk_label_new(buddy[function_buddy_number].nick_name));
	buddy[function_buddy_number].apended = true;
	++conversation_tabs;
}

// Function to delete all widgets before refreshing another ones
void additional_function_chat_single_bodies_delete_oldies(int function_buddy_number)
{
	// If history is shown
	if(buddy[function_buddy_number].singlebody_history_shown)
	{
		gtk_widget_destroy(buddy[function_buddy_number].history_view);
		gtk_widget_destroy(buddy[function_buddy_number].scroll);
		gtk_widget_destroy(buddy[function_buddy_number].history_year);
		gtk_widget_destroy(buddy[function_buddy_number].history_month);
		gtk_widget_destroy(buddy[function_buddy_number].history_day);
		gtk_widget_destroy(buddy[function_buddy_number].history_hbox);
		gtk_widget_destroy(buddy[function_buddy_number].history_label);
		gtk_widget_destroy(buddy[function_buddy_number].history_label_spaced);
		gtk_widget_destroy(buddy[function_buddy_number].toolbar);
		buddy[function_buddy_number].singlebody_history_shown = false;
	}
	// If history view is shown
	else if (buddy[function_buddy_number].singlebody_history_view_shown)
	{
		gtk_widget_destroy(buddy[function_buddy_number].history_view);
		gtk_widget_destroy(buddy[function_buddy_number].scroll);
		gtk_widget_destroy(buddy[function_buddy_number].history_entry);
		gtk_widget_destroy(buddy[function_buddy_number].history_label_spaced);
		gtk_widget_destroy(buddy[function_buddy_number].toolbar);
		buddy[function_buddy_number].singlebody_history_view_shown = false;
	}
	// If conversation shown
	else if (buddy[function_buddy_number].singlebody_conversation_shown)
	{
		gtk_widget_destroy(buddy[function_buddy_number].entry);
		gtk_widget_destroy(buddy[function_buddy_number].text_view);
		gtk_widget_destroy(buddy[function_buddy_number].scroll);
		gtk_widget_destroy(buddy[function_buddy_number].toolbar);
		gtk_widget_destroy(buddy[function_buddy_number].info_label);
		buddy[function_buddy_number].singlebody_conversation_shown = false;
	}
	// If conversation info shown
	else if (buddy[function_buddy_number].singlebody_info_shown)
	{
		gtk_widget_destroy(buddy[function_buddy_number].entry);
		gtk_widget_destroy(buddy[function_buddy_number].text_view);
		gtk_widget_destroy(buddy[function_buddy_number].scroll);
		gtk_widget_destroy(buddy[function_buddy_number].info_label);
		gtk_widget_destroy(buddy[function_buddy_number].toolbar);
		buddy[function_buddy_number].singlebody_info_shown = false;
	}
}

// Function to create new widgets
void additional_function_chat_single_bodies_create_freshes(int function_buddy_number, int function_type)
{
	// For history window
	if(function_type == HISTORY)
	{
		singlebody_widget_history_label(function_buddy_number);
		singlebody_widget_history_label_spaced(function_buddy_number);
		singlebody_widget_history_hbox(function_buddy_number);
		singlebody_widget_history_day(function_buddy_number);
		singlebody_widget_history_month(function_buddy_number);
		singlebody_widget_history_year(function_buddy_number);
		singlebody_widget_history_view(function_buddy_number);
		singlebody_widget_chat_toolbar(function_buddy_number);
		singlebody_widget_chat_scroll(function_buddy_number);
	}
	// For history view window
	else if(function_type == HISTORY_VIEW)
	{
		singlebody_widget_history_label_spaced(function_buddy_number);
		singlebody_widget_history_entry(function_buddy_number);
		singlebody_widget_history_view(function_buddy_number);
		singlebody_widget_chat_toolbar(function_buddy_number);
		singlebody_widget_chat_scroll(function_buddy_number);
	}
	// For conversation
	else if(function_type == CHAT)
	{
		singlebody_widget_chat_toolbar(function_buddy_number);
		singlebody_widget_chat_scroll(function_buddy_number);
		singlebody_widget_info_label(function_buddy_number);
		singlebody_widget_chat_textview(function_buddy_number);
		singlebody_widget_chat_entry(function_buddy_number);
	}
	// For info
	else if (function_type == INFO)
	{
		singlebody_widget_chat_toolbar(function_buddy_number);
		singlebody_widget_chat_scroll(function_buddy_number);
		singlebody_widget_info_label(function_buddy_number);
		singlebody_widget_chat_textview(function_buddy_number);
		singlebody_widget_chat_entry(function_buddy_number);
	}
}

// Function to composite design
void additional_function_chat_single_bodies_do_design(int function_buddy_number, int function_type)
{
	// For history
	if(function_type == HISTORY)
	{
		gtk_widget_show_all(conversation_window);
		gtk_box_pack_start(GTK_BOX(buddy[function_buddy_number].vbox), buddy[function_buddy_number].toolbar, FALSE, FALSE, 0);
		gtk_box_pack_start (GTK_BOX (buddy[function_buddy_number].vbox), buddy[function_buddy_number].history_label, FALSE, TRUE, 5);
		gtk_box_pack_start (GTK_BOX (buddy[function_buddy_number].vbox), buddy[function_buddy_number].history_hbox, FALSE, TRUE, 5);
		gtk_box_pack_start (GTK_BOX (buddy[function_buddy_number].history_hbox), buddy[function_buddy_number].history_day, TRUE, TRUE, 5);
		gtk_box_pack_start (GTK_BOX (buddy[function_buddy_number].history_hbox),buddy[function_buddy_number].history_month, TRUE, TRUE, 5);
		gtk_box_pack_start (GTK_BOX (buddy[function_buddy_number].history_hbox), buddy[function_buddy_number].history_year, TRUE, TRUE, 5);
		gtk_box_pack_start(GTK_BOX(buddy[function_buddy_number].vbox), buddy[function_buddy_number].scroll, TRUE, TRUE, 0);
		gtk_container_add(GTK_CONTAINER(buddy[function_buddy_number].scroll), buddy[function_buddy_number].history_view);
		gtk_box_pack_start (GTK_BOX (buddy[function_buddy_number].vbox), buddy[function_buddy_number].history_label_spaced, FALSE, TRUE, 5);
		gtk_widget_show_all(buddy[function_buddy_number].vbox);
	}
	// For history view
	else if(function_type == HISTORY_VIEW)
	{
		gtk_widget_show_all(conversation_window);
		gtk_box_pack_start(GTK_BOX(buddy[function_buddy_number].vbox), buddy[function_buddy_number].toolbar, FALSE, FALSE, 0);
		gtk_box_pack_start(GTK_BOX(buddy[function_buddy_number].vbox), buddy[function_buddy_number].history_entry, TRUE, TRUE, 3);
		gtk_box_pack_start(GTK_BOX(buddy[function_buddy_number].vbox), buddy[function_buddy_number].scroll, TRUE, TRUE, 0);
		gtk_container_add(GTK_CONTAINER(buddy[function_buddy_number].scroll), buddy[function_buddy_number].history_view);
		gtk_box_pack_start (GTK_BOX (buddy[function_buddy_number].vbox), buddy[function_buddy_number].history_label_spaced, FALSE, TRUE, 5);
		gtk_widget_show_all(buddy[function_buddy_number].vbox);
	}
	// For conversation
	else if(function_type == CHAT)
	{
		gtk_widget_show_all(conversation_window);
		gtk_box_pack_start(GTK_BOX(buddy[function_buddy_number].vbox), buddy[function_buddy_number].toolbar, FALSE, FALSE, 0);
		gtk_box_pack_start(GTK_BOX(buddy[function_buddy_number].vbox), buddy[function_buddy_number].info_label, FALSE, FALSE, 0);
		gtk_box_pack_start(GTK_BOX(buddy[function_buddy_number].vbox), buddy[function_buddy_number].scroll, TRUE, TRUE, 0);
		gtk_container_add(GTK_CONTAINER(buddy[function_buddy_number].scroll), buddy[function_buddy_number].text_view);
		gtk_box_pack_start (GTK_BOX (buddy[function_buddy_number].vbox), buddy[function_buddy_number].entry, TRUE, TRUE, 0);
		gtk_widget_set_size_request(buddy[function_buddy_number].text_view, 400, 225);
		gtk_widget_set_size_request(buddy[function_buddy_number].info_label, 400, 0);
		gtk_widget_show_all(buddy[function_buddy_number].vbox);
		gtk_widget_hide(buddy[function_buddy_number].info_label);
	}
	// For info
	else if(function_type == INFO)
	{
		gtk_widget_show_all(conversation_window);
		gtk_box_pack_start(GTK_BOX(buddy[function_buddy_number].vbox), buddy[function_buddy_number].toolbar, FALSE, FALSE, 0);
		gtk_box_pack_start(GTK_BOX(buddy[function_buddy_number].vbox), buddy[function_buddy_number].info_label, FALSE, FALSE, 0);
		gtk_box_pack_start(GTK_BOX(buddy[function_buddy_number].vbox), buddy[function_buddy_number].scroll, TRUE, TRUE, 0);
		gtk_container_add(GTK_CONTAINER(buddy[function_buddy_number].scroll), buddy[function_buddy_number].text_view);
		gtk_box_pack_start (GTK_BOX (buddy[function_buddy_number].vbox), buddy[function_buddy_number].entry, TRUE, TRUE, 0);
		gtk_widget_set_size_request(buddy[function_buddy_number].text_view, 400, 205);
		gtk_widget_set_size_request(buddy[function_buddy_number].info_label, 400, 20);
		gtk_widget_show_all(buddy[function_buddy_number].vbox);
	}
}

// Function to update handlers
void additional_function_chat_single_bodies_update_handlers(int function_buddy_number, int function_type)
{
	// For history
	if(function_type == HISTORY)
	{
		conversation_shown = true;
		buddy[function_buddy_number].singlebody_history_shown = true;
		buddy[function_buddy_number].singlebody_conversation_shown = false;
		buddy[function_buddy_number].singlebody_history_view_shown = false;
		buddy[function_buddy_number].singlebody_info_shown = false;
	}
	// For history view
	else if(function_type == HISTORY_VIEW)
	{
		conversation_shown = true;
		buddy[function_buddy_number].singlebody_history_shown = false;
		buddy[function_buddy_number].singlebody_conversation_shown = false;
		buddy[function_buddy_number].singlebody_history_view_shown = true;
		buddy[function_buddy_number].singlebody_info_shown = false;
	}
	// For conversation
	else if(function_type == CHAT)
	{
		conversation_shown = true;
		buddy[function_buddy_number].singlebody_history_shown = false;
		buddy[function_buddy_number].singlebody_conversation_shown = true;
		buddy[function_buddy_number].singlebody_history_view_shown = false;
		buddy[function_buddy_number].singlebody_info_shown = false;
		buddy[function_buddy_number].who_sender = 3;
		buddy[function_buddy_number].chat_buffer_created = true;
	}
	// For info
	else if(function_type == INFO)
	{
		conversation_shown = true;
		buddy[function_buddy_number].singlebody_history_shown = false;
		buddy[function_buddy_number].singlebody_conversation_shown = false;
		buddy[function_buddy_number].singlebody_history_view_shown = false;
		buddy[function_buddy_number].singlebody_info_shown = true;
		buddy[function_buddy_number].request_info_status = false;
		buddy[function_buddy_number].chat_buffer_created = true;
	}
}

// Function to process scripts
void additional_function_chat_single_bodies_process_scripts(int function_buddy_number, int function_type)
{
	additional_function_chat_single_bodies_delete_oldies(function_buddy_number);
	additional_function_chat_single_bodies_create_freshes(function_buddy_number, function_type);
	additional_function_chat_single_bodies_do_design(function_buddy_number, function_type);
	additional_function_chat_single_bodies_update_handlers(function_buddy_number, function_type);
}

// Function to redraw just info label
void additional_function_chat_single_bodies_update_info_chat(int function_buddy_number, int function_type)
{
	// For creating info
	if (function_type == INFO)
	{
		gtk_widget_show(buddy[function_buddy_number].info_label);
		gtk_widget_set_size_request(buddy[function_buddy_number].text_view, 400, 205);
		gtk_widget_set_size_request(buddy[function_buddy_number].info_label, 400, 20);
		additional_function_chat_single_bodies_update_handlers(function_buddy_number, 4);
	}
	// For creating chat
	else if (function_type == CHAT)
	{
		gtk_widget_hide(buddy[function_buddy_number].info_label);
		gtk_widget_set_size_request(buddy[function_buddy_number].text_view, 400, 225);
		gtk_widget_set_size_request(buddy[function_buddy_number].info_label, 400, 0);
		additional_function_chat_single_bodies_update_handlers(function_buddy_number, 3);
	}
}

// Function to show history
void singlebody_chat_history_shown(GtkWidget *unused_widget, int function_buddy_number)
{
	// If already shown return
	if(buddy[function_buddy_number].singlebody_history_shown)
		return;

	// Process script
	additional_function_chat_single_bodies_process_scripts(function_buddy_number, HISTORY);
}

void singlebody_chat_history_view_shown(int function_buddy_number)
{
	// If already shown return
	if(buddy[function_buddy_number].singlebody_history_view_shown)
		return;

	// Process script
	additional_function_chat_single_bodies_process_scripts(function_buddy_number, HISTORY_VIEW);
}

// Function to show info or conversarion using function mode
void singlebody_chat_conversation_info_shown(GtkWidget *unused_widget, int function_buddy_number, int function_mode)
{
	// If mode type is info
	if(function_mode == INFO)
	{
		// Check if already shown
		if(buddy[function_buddy_number].singlebody_info_shown)
			return;

		// If conversation shown just show info intead of creating it!
		if(buddy[function_buddy_number].singlebody_conversation_shown)
		{
			additional_function_chat_single_bodies_update_info_chat(function_buddy_number, INFO);
			return;
		}

		// Process scripts
		additional_function_chat_single_bodies_process_scripts(function_buddy_number, INFO);
	}

	// If mode type is chat
	else if(function_mode == CHAT)
	{
		// Check if already shown
		if(buddy[function_buddy_number].singlebody_conversation_shown)
			return;

		// If info shown just hide it intead of creating conversation!
		if(buddy[function_buddy_number].singlebody_info_shown)
		{
			additional_function_chat_single_bodies_update_info_chat(function_buddy_number, CHAT);
			return;
		}

		// Check if already added to notebook
		if (buddy[function_buddy_number].apended == false)
			additional_function_chat_single_bodies_append_page(function_buddy_number);

		// Process scripts
		additional_function_chat_single_bodies_process_scripts(function_buddy_number, CHAT);
	}
}

// Function to show info or conversarion using signal
void singlebody_chat_signal_conversation_info_shown(GtkWidget *unused_widget, int function_buddy_number)
{
	// If not received data and info show - means we need chat
	if((buddy[function_buddy_number].request_info_status == false) && (buddy[function_buddy_number].singlebody_info_shown == true))
	{
		// Check if already appended in notebook
		if (buddy[function_buddy_number].apended == false)
			additional_function_chat_single_bodies_append_page(function_buddy_number);

		// If info shown just hide it intead of creating conversation!
		if(buddy[function_buddy_number].singlebody_info_shown)
		{
			additional_function_chat_single_bodies_update_info_chat(function_buddy_number, CHAT);
			return;
		}

		// Process scripts
		additional_function_chat_single_bodies_process_scripts(function_buddy_number, CHAT);
	}

	// If requested data and conversation shown - means we need info
	else if((buddy[function_buddy_number].request_info_status == true) && (buddy[function_buddy_number].singlebody_conversation_shown == true))
	{
		// If conversation shown just show info intead of creating it!
		if(buddy[function_buddy_number].singlebody_conversation_shown)
		{
			additional_function_chat_single_bodies_update_info_chat(function_buddy_number, INFO);
			return;
		}

		// Process scripts
		additional_function_chat_single_bodies_process_scripts(function_buddy_number, INFO);
	}

	// If not received data and conversation shown - means we need to request data
	else if((buddy[function_buddy_number].request_info_status == false) && (buddy[function_buddy_number].singlebody_conversation_shown == true))
		function_info_request(NULL, function_buddy_number);

	// If requested data and info shown - nothing to do
	else if((buddy[function_buddy_number].request_info_status == true) && (buddy[function_buddy_number].singlebody_info_shown == true))
		return;
	
	// Else show chat
	else 
	{
		// Check if already appended in notebook
		if (buddy[function_buddy_number].apended == false)
			additional_function_chat_single_bodies_append_page(function_buddy_number);

		// Process scripts
		additional_function_chat_single_bodies_process_scripts(function_buddy_number, CHAT);
	}
}