/* ***********************IMPORTANT *********************************************
 *  Copyright (C) 2004-2007 Thierry Lagarde <tlagarde@users.sourceforge.net>
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License.
 *
 *
 *                      ********************
 *
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *
 *                      ********************
 *
 *
 * You are highly encouraged to send your changes to <tlagarde@users.sourceforge.net>
 * for possible incorporation into the main distribution.
 * By sending these changes to <tlagarde@users.sourceforge.net>,
 * it is assumed that you are offering <tlagarde@users.sourceforge.net>
 * the unlimited, non-exclusive right to reuse, modify, and relicense the code.
 *
 *
 *                      ********************
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 **************************************************************************************/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include "Interface/Root_Interface.h"
#include "Interface/Erreur_Interface.h"
#include "Interface/Debug_Interface.h"
#include "Debug.h"
#include "Reseau.h"
#include "Gconf.h"
#include "Quit.h"
#include "Log.h"
#include "Semaphore.h"
#include "Macro.h"


void debug_printf (gint Id_Reseau, int Niveau_Debug, const char *file, const char *Fonction, int line, char *fmt, ...)
{
	if (Debug >= Niveau_Debug) {
		PROTECT_CODE(Semaphore_Debug);
				
		struct Struct_Liste_Reseau *Reseau = NULL;
#ifdef __LOG_ENABLE
		struct struct_Cle_Privee* pCle = NULL;
		gchar	*Texte_Log = NULL;
#endif
		GtkTextBuffer *buffer_text = NULL;
		GtkTextIter iter;
		va_list   args;
		gchar    *Message = NULL;
		char      buffer[700];

		va_start (args, fmt);
		vsnprintf (buffer, 700, fmt, args);		
#ifdef __LOG_ENABLE
		
		if ((pCle = pthread_getspecific (Cle_privee_thread)) == NULL)
			pCle = Donnee_Cle_Main;
#endif		
		switch (Niveau_Debug) {
		    case D_ERRO:
				Id_Reseau_Erreur = Id_Reseau;
			    Reseau = Recupere_Pointeur_Structure_Reseau (Id_Reseau);
				switch (Id_Reseau) {
				case INTERFACE:
#ifdef __LOG_ENABLE
					
					Texte_Log = g_strdup_printf("[ERROR] [%s](%s:%d): %s\n",Fonction, file, line, buffer);
					Log(pCle,Texte_Log);
					MYFREE(Texte_Log);
#endif
					Message = g_strdup_printf ("File : [%s]\nFonction : [%s]\nLine : [%d] \n\nMessage :\n%s", file, Fonction, line, buffer);
					break;
				case AGENT:
					Message = g_strdup_printf ("Agent\nFile : [%s]\nFonction : [%s]\nLine : [%d] \n\nMessage :\n%s", file, Fonction, line, buffer);
					break;
				default:
					Message = g_strdup_printf ("Agent : [%s]\nFile : [%s]\nFonction : [%s]\nLine : [%d] \n\nMessage :\n%s", Reseau->Nom_Complet_Reseau, file, Fonction, line, buffer);
			    }
			    Affiche_Win_Erreur();
			    gtk_label_set_text (GTK_LABEL (label_Erreur), Message);
			    MYFREE (Message);
				gtk_main ();
			    break;
		    case D_WARN_USER:
			    if (Id_Reseau == INTERFACE){
#ifdef __LOG_ENABLE
					
					Texte_Log = g_strdup_printf("[D_WARN_USER] [%s]\n",buffer);
					Log(pCle,Texte_Log);
					MYFREE(Texte_Log);
#endif
			    	Message = g_strdup_printf ("****************************\nType : Warning\n%s\n\n\n\n", buffer);
			    }
				else {
				    if (Id_Reseau == 0)
					    Message = g_strdup_printf ("****************************\nType : Warning\n\nAgent \n\n%s\n\n\n\n", buffer);
				    else {
					    Reseau = Recupere_Pointeur_Structure_Reseau (Id_Reseau);
					    Message = g_strdup_printf ("****************************\nType : Warning\n\nAgent : [%s]\n\n%s\n\n\n\n", Reseau->Nom_Complet_Reseau, buffer);
				    }
			    }
			    Afficher_Win_Debug ();
			    gdk_threads_leave ();
			    buffer_text = gtk_text_view_get_buffer (GTK_TEXT_VIEW (textview_Debug_Message));
			    gtk_text_buffer_get_end_iter (buffer_text, &iter);
			    gtk_text_buffer_insert (buffer_text, &iter, (const gchar *) Message, -1);
			    gdk_threads_enter ();
			    refresh ();
			    gdk_threads_leave ();
			    gtk_text_buffer_get_iter_at_offset (buffer_text, &iter, -1);
			    gtk_text_view_scroll_to_iter (GTK_TEXT_VIEW (textview_Debug_Message), &iter, 0, FALSE, 0, 0);
			    gdk_threads_enter ();
			    refresh ();
			    gdk_threads_leave ();
				MYFREE (Message);
			    break;
		    case D_WARN:
#ifdef __LOG_ENABLE
				
				Texte_Log = g_strdup_printf("[WARNING] [%s](%s:%d): %s\n",Fonction, file, line, buffer);
				Log(pCle,Texte_Log);
				MYFREE(Texte_Log);
#endif
			    break;
		    case D_INFO_USER:
			    if (Id_Reseau == INTERFACE){
#ifdef __LOG_ENABLE
					
					Texte_Log = g_strdup_printf("[D_INFO_USER] [%s]\n",buffer);
					Log(pCle,Texte_Log);
					MYFREE(Texte_Log);
#endif
				    Message = g_strdup_printf ("****************************\nType : Information\nInterface : \n%s\n\n\n\n", buffer);
				}
				else {
				    Reseau = Recupere_Pointeur_Structure_Reseau (Id_Reseau);
				    Message = g_strdup_printf ("****************************\nType : Information\nAgent : [%s]\n\n%s\n\n\n\n", Reseau->Nom_Complet_Reseau, buffer);
			    }
			    Afficher_Win_Debug ();
			    gdk_threads_leave ();
			    buffer_text = gtk_text_view_get_buffer (GTK_TEXT_VIEW (textview_Debug_Message));
			    gtk_text_buffer_get_end_iter (buffer_text, &iter);
			    gtk_text_buffer_insert (buffer_text, &iter, (const gchar *) Message, -1);
			    gdk_threads_enter ();
			    refresh ();
			    gdk_threads_leave ();
			    gtk_text_buffer_get_iter_at_offset (buffer_text, &iter, -1);
			    gtk_text_view_scroll_to_iter (GTK_TEXT_VIEW (textview_Debug_Message), &iter, 0, FALSE, 0, 0);
			    gdk_threads_enter ();
			    refresh ();
			    gdk_threads_leave ();
				MYFREE (Message);
			    break;
		    case D_INFO:
#ifdef __LOG_ENABLE
				
				Texte_Log = g_strdup_printf("[INFO] [%s]: %s\n", Fonction, buffer);
				Log(pCle,Texte_Log);
				MYFREE(Texte_Log);
#endif
			    break;
		}
		va_end (args);
		DEPROTECT_CODE(Semaphore_Debug); 
	}
}


void Initialisation_Debug (void)
{
	File_Debug = NULL;
	Fonction_Debug = NULL;
	Debug = 6;//Lecture_Gconf_Int (INT_DEBUG);
}
