/*************************************************************************************************************
* This file contains loading of pixmaps
* 2012/8 by Dlxko
* Version 3.0
**************************************************************************************************************/
#include "Pixmaps.h"

// Local static variables
static GtkTextIter match_start;
static GtkTextIter match_end;
static GtkTextIter end_iter;

// Pixmaps variables
static GdkPixbuf *px_sm_angel;
static GdkPixbuf *px_sm_colonthree;
static GdkPixbuf *px_sm_confused;
static GdkPixbuf *px_sm_cry;
static GdkPixbuf *px_sm_devil;
static GdkPixbuf *px_sm_frown;
static GdkPixbuf *px_sm_gasp;
static GdkPixbuf *px_sm_glasses;
static GdkPixbuf *px_sm_grin;
static GdkPixbuf *px_sm_grumpy;
static GdkPixbuf *px_sm_heart;
static GdkPixbuf *px_sm_kiki;
static GdkPixbuf *px_sm_kiss;
static GdkPixbuf *px_sm_pacman;
static GdkPixbuf *px_sm_smile;
static GdkPixbuf *px_sm_squint;
static GdkPixbuf *px_sm_sunglasses;
static GdkPixbuf *px_sm_tongue;
static GdkPixbuf *px_sm_unsure;
static GdkPixbuf *px_sm_upset;
static GdkPixbuf *px_sm_wink;

// For cycling
static int cycler;

// Additional function to refresh smile
void additional_function_pixmaps_refresh_smile_add_counter(GtkTextBuffer *function_buffer, GdkPixbuf *function_pixbuf)
{
	gtk_text_buffer_delete(function_buffer, &match_start, &match_end);
	gtk_text_buffer_insert_pixbuf(function_buffer, &match_start, function_pixbuf);
	++cycler;
}

// Buffer pixmaps
bool create_pixmaps()
{
	// Alloc error
	GError *px_rcv_error = NULL;

	// Load default avatar
	default_buddy_pixbuf = gdk_pixbuf_new_from_file("images/core/def_avatar.png", &px_rcv_error);

	// Load default icon
	default_icon = gdk_pixbuf_new_from_file("images/core/def_icon.png", &px_rcv_error);

	// Load pixbuf or report error
	px_received = gdk_pixbuf_new_from_file ("images/core/received.png", &px_rcv_error);
	px_sending = gdk_pixbuf_new_from_file ("images/core/sending.gif", &px_rcv_error);
	px_send = gdk_pixbuf_new_from_file ("images/core/send.gif", &px_rcv_error);
	px_failed = gdk_pixbuf_new_from_file ("images/core/px_failed.gif", &px_rcv_error);
	px_receiving = gdk_pixbuf_new_from_file ("images/core/px_receiving.gif", &px_rcv_error);

	// Load smiles
	px_sm_angel = gdk_pixbuf_new_from_file ("images/smileys/angel.gif", &px_rcv_error);
	px_sm_colonthree = gdk_pixbuf_new_from_file ("images/smileys/colonthree.gif", &px_rcv_error);
	px_sm_confused = gdk_pixbuf_new_from_file ("images/smileys/confused.gif", &px_rcv_error);
	px_sm_cry = gdk_pixbuf_new_from_file ("images/smileys/cry.gif", &px_rcv_error);
	px_sm_devil = gdk_pixbuf_new_from_file ("images/smileys/devil.gif", &px_rcv_error);
	px_sm_frown = gdk_pixbuf_new_from_file ("images/smileys/frown.gif", &px_rcv_error);
	px_sm_gasp = gdk_pixbuf_new_from_file ("images/smileys/gasp.gif", &px_rcv_error);
	px_sm_glasses = gdk_pixbuf_new_from_file ("images/smileys/glasses.gif", &px_rcv_error);
	px_sm_grin = gdk_pixbuf_new_from_file ("images/smileys/grin.gif", &px_rcv_error);
	px_sm_grumpy = gdk_pixbuf_new_from_file ("images/smileys/grumpy.gif", &px_rcv_error);
	px_sm_heart = gdk_pixbuf_new_from_file ("images/smileys/heart.gif", &px_rcv_error);
	px_sm_kiki = gdk_pixbuf_new_from_file ("images/smileys/kiki.gif", &px_rcv_error);
	px_sm_kiss = gdk_pixbuf_new_from_file ("images/smileys/kiss.gif", &px_rcv_error);
	px_sm_pacman = gdk_pixbuf_new_from_file ("images/smileys/pacman.gif", &px_rcv_error);
	px_sm_smile = gdk_pixbuf_new_from_file ("images/smileys/smile.gif", &px_rcv_error);
	px_sm_squint = gdk_pixbuf_new_from_file ("images/smileys/squint.gif", &px_rcv_error);
	px_sm_sunglasses = gdk_pixbuf_new_from_file ("images/smileys/sunglasses.gif", &px_rcv_error);
	px_sm_tongue = gdk_pixbuf_new_from_file ("images/smileys/tongue.gif", &px_rcv_error);
	px_sm_unsure = gdk_pixbuf_new_from_file ("images/smileys/unsure.gif", &px_rcv_error);
	px_sm_upset = gdk_pixbuf_new_from_file ("images/smileys/upset.gif", &px_rcv_error);
	px_sm_wink = gdk_pixbuf_new_from_file ("images/smileys/wink.gif", &px_rcv_error);

	// If error close
	if (px_rcv_error)
	{
		g_error_free (px_rcv_error);
		memset(dialog_text, 0, DIALOG_BUFFER_SIZE);
		sprintf_s(dialog_text, DIALOG_BUFFER_SIZE, "%s : %s", LANG_CANT_LOAD_PIXBUF, px_rcv_error);
		function_show_error(dialog_text);
		return false;
	}
	return true;
}

// Function to show smiles
void function_get_smile(GtkTextBuffer *buffer)
{
	do
	{
		// Get end iter
		gtk_text_buffer_get_end_iter (buffer, &end_iter);

		if( gtk_text_iter_backward_search(&end_iter, ":)", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) ||
			gtk_text_iter_backward_search(&end_iter, ":-)", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) ||
			gtk_text_iter_backward_search(&end_iter, "=)", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) ||
			gtk_text_iter_backward_search(&end_iter, ":]", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL)  )
			additional_function_pixmaps_refresh_smile_add_counter(buffer,px_sm_smile);
		else if( gtk_text_iter_backward_search(&end_iter, ":(", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) ||
			gtk_text_iter_backward_search(&end_iter, ":-(", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) ||
			gtk_text_iter_backward_search(&end_iter, ":[", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) ||
			gtk_text_iter_backward_search(&end_iter, "=(", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL)  )
			additional_function_pixmaps_refresh_smile_add_counter(buffer,px_sm_frown);
		else if( gtk_text_iter_backward_search(&end_iter, ":P", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) ||
			gtk_text_iter_backward_search(&end_iter, ":-P", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) ||
			gtk_text_iter_backward_search(&end_iter, "=(", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL)  )
			additional_function_pixmaps_refresh_smile_add_counter(buffer,px_sm_tongue);
		else if( gtk_text_iter_backward_search(&end_iter, ":D", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) ||
			gtk_text_iter_backward_search(&end_iter, ":-D", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) ||
			gtk_text_iter_backward_search(&end_iter, "=D", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL)  )
			additional_function_pixmaps_refresh_smile_add_counter(buffer,px_sm_grin);
		else if( gtk_text_iter_backward_search(&end_iter, ":O", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) ||
			gtk_text_iter_backward_search(&end_iter, ":-O", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) ||
			gtk_text_iter_backward_search(&end_iter, "=O", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL)  )
			additional_function_pixmaps_refresh_smile_add_counter(buffer,px_sm_gasp);
		else if( gtk_text_iter_backward_search(&end_iter, ";)", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) ||
			gtk_text_iter_backward_search(&end_iter, ";-)", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL)  )
			additional_function_pixmaps_refresh_smile_add_counter(buffer,px_sm_wink);
		else if( gtk_text_iter_backward_search(&end_iter, "8)", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) ||
			gtk_text_iter_backward_search(&end_iter, "8-)", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) ||
			gtk_text_iter_backward_search(&end_iter, "B)", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) ||
			gtk_text_iter_backward_search(&end_iter, "B-)", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL)  )
			additional_function_pixmaps_refresh_smile_add_counter(buffer,px_sm_glasses);
		else if( gtk_text_iter_backward_search(&end_iter, "8|", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) ||
			gtk_text_iter_backward_search(&end_iter, "8-|", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) ||
			gtk_text_iter_backward_search(&end_iter, "B|", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) ||
			gtk_text_iter_backward_search(&end_iter, "B-|", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL)  )
			additional_function_pixmaps_refresh_smile_add_counter(buffer,px_sm_sunglasses);
		else if( gtk_text_iter_backward_search(&end_iter, ">:(", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) ||
			gtk_text_iter_backward_search(&end_iter, ">:-(", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL)  )
			additional_function_pixmaps_refresh_smile_add_counter(buffer,px_sm_grumpy);
		else if( gtk_text_iter_backward_search(&end_iter, ":/", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) ||
			gtk_text_iter_backward_search(&end_iter, ":-/", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL)  )
			additional_function_pixmaps_refresh_smile_add_counter(buffer,px_sm_unsure);
		else if( gtk_text_iter_backward_search(&end_iter, ":'(", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) ||
			gtk_text_iter_backward_search(&end_iter, "='(", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL)  )
			additional_function_pixmaps_refresh_smile_add_counter(buffer,px_sm_cry);
		else if( gtk_text_iter_backward_search(&end_iter, "3:)", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) ||
			gtk_text_iter_backward_search(&end_iter, "3:-)", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL)  )
			additional_function_pixmaps_refresh_smile_add_counter(buffer,px_sm_devil);
		else if( gtk_text_iter_backward_search(&end_iter, "O:)", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) ||
			gtk_text_iter_backward_search(&end_iter, "O:-)", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL)  )
			additional_function_pixmaps_refresh_smile_add_counter(buffer,px_sm_angel);
		else if( gtk_text_iter_backward_search(&end_iter, ":*", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) ||
			gtk_text_iter_backward_search(&end_iter, ":-*", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL)  )
			additional_function_pixmaps_refresh_smile_add_counter(buffer,px_sm_kiss);
		else if( gtk_text_iter_backward_search(&end_iter, "<3", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) )
			additional_function_pixmaps_refresh_smile_add_counter(buffer,px_sm_heart);
		else if( gtk_text_iter_backward_search(&end_iter, "^_^", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) )
			additional_function_pixmaps_refresh_smile_add_counter(buffer,px_sm_kiki);
		else if( gtk_text_iter_backward_search(&end_iter, "-_-", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) )
			additional_function_pixmaps_refresh_smile_add_counter(buffer,px_sm_squint);
		else if( gtk_text_iter_backward_search(&end_iter, "O.o", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) )
			additional_function_pixmaps_refresh_smile_add_counter(buffer,px_sm_confused);
		else if( gtk_text_iter_backward_search(&end_iter, ">:O", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) ||
			gtk_text_iter_backward_search(&end_iter, ">:-O", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) )
			additional_function_pixmaps_refresh_smile_add_counter(buffer,px_sm_upset);
		else if( gtk_text_iter_backward_search(&end_iter, ":v", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) )
			additional_function_pixmaps_refresh_smile_add_counter(buffer,px_sm_pacman);
		else if( gtk_text_iter_backward_search(&end_iter, ":3", GTK_TEXT_SEARCH_TEXT_ONLY, &match_start, &match_end, NULL) )
			additional_function_pixmaps_refresh_smile_add_counter(buffer,px_sm_colonthree);
		else
			break;
	}
	// Do this while limit
	while (cycler < SMILE_LIMIT_CYCLE);
}