/*
This source file is part of Scol
For the latest info, see http://www.scolring.org

Copyright (c) 2010 Stephane Bisaro, aka Iri <iri@irizone.net>

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser 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, or go to
http://www.gnu.org/copyleft/lesser.txt

For others informations, please contact us from http://www.scolring.org/
*/

#include <time.h>
#include <gtk/gtk.h>
#include <string.h>

#if ((defined __WIN32__) || (defined WIN32))
#include <windows.h>
#include "../include/scol_plugin_win.h"
#else
#include "../include/scol_plugin_lin.h"
#endif
#include "../include/version.h"


/* Debug activation
    - SCOL_GTK_DEBUG activation : write message to a specific log
    - SCOL_GTK_DEBUG_DIALOG : display a dialog box too
    To desactivate, comment it
*/
#define SCOL_GTK_DEBUG
/* #define SCOL_GTK_DEBUG_DIALOG*/


/* TODO
    - create a log file ... ;-)
    So, get the Scol path
*/


#ifdef SCOL_GTK_DEBUG
FILE * scol_2dgtk_log_create ()
{
    char buffer[256];
    char message[128];
    time_t timestamp;
    FILE *f;

    snprintf (message, 128, "%s%d:%d:%d%s%d%s%d%s%d%s", "SCOL 2dGTK library - Specific Logfile\nLibrary version : ", SCOL_LIB_GTK_MAJOR_VERSION, SCOL_LIB_GTK_MINOR_VERSION, SCOL_LIB_GTK_MICRO_VERSION, "\nGTK+ version : ", GTK_MAJOR_VERSION, ".", GTK_MINOR_VERSION, ".", GTK_MINOR_VERSION, "\n\n");

    timestamp = time (NULL);
    strftime (buffer, sizeof (buffer), "./logs/2dgtk__%Y_%m_%d__%H_%M_%S.log", localtime (&timestamp));
    f = fopen (buffer, "wb");
    if (f == NULL)
    {
        MMechostr (MSKDEBUG, "scol_2dgtk_log_create : file not created !\n");
        return f;
    }

    fwrite (g_locale_to_utf8 (message, -1, NULL, NULL, NULL), sizeof (char), strlen (message) + 1, f);
    return f;
}

void scol_2dgtk_log_handler (const gchar *log_domain, GLogLevelFlags log_level, const gchar *msg, gpointer user_data)
{
    char *message;
    FILE *pLogFile;
    #ifdef SCOL_GTK_DEBUG_DIALOG
    GtkWidget *win;
    #endif

    message = g_locale_to_utf8 (msg, -1, NULL, NULL, NULL);
    pLogFile = (FILE *) user_data;

    switch (log_level)
    {
        case (G_LOG_LEVEL_MESSAGE) :
            #ifdef SCOL_GTK_DEBUG_DIALOG
            win = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_OK_CANCEL, "%s", message);
            gtk_window_set_title (GTK_WINDOW (win), "2dGtk library -> information");
            gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_MOUSE);
            #endif
            fwrite ("information : ", sizeof (char), 16, pLogFile);
            MMechostr (MSKFOO, (char *) message);
            MMechostr (MSKFOO, "\n");

            break;

        case (G_LOG_LEVEL_WARNING) :
            #ifdef SCOL_GTK_DEBUG_DIALOG
            win = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, "%s", message);
            gtk_window_set_title (GTK_WINDOW (win), "2dGtk library -> warning");
            gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_MOUSE);
            #endif
            fwrite ("warning : ", sizeof (char), 16, pLogFile);
            MMechostr (MSKWARNING, (char *) message);
            MMechostr (MSKWARNING, "\n");

            break;

        case (G_LOG_LEVEL_CRITICAL) :
        case (G_LOG_LEVEL_ERROR) :
            #ifdef SCOL_GTK_DEBUG_DIALOG
            win = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", message);
            gtk_window_set_title (GTK_WINDOW (win), "2dGtk library -> error");
            gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_MOUSE);
            #endif
            fwrite ("error : ", sizeof (char), 16, pLogFile);
            MMechostr (0, (char *) message);
            MMechostr (0, "\n");

            break;

        case (G_LOG_FLAG_RECURSION) :
        case (G_LOG_FLAG_FATAL) :
        case (G_LOG_LEVEL_INFO) :
        case (G_LOG_LEVEL_DEBUG) :
        case (G_LOG_LEVEL_MASK) :
        default :
            #ifdef SCOL_GTK_DEBUG_DIALOG
            win = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_OTHER, GTK_BUTTONS_OK, "%s", message);
            gtk_window_set_title (GTK_WINDOW (win), "2dGtk library -> message");
            gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_MOUSE);
            #endif
            fwrite ("message : ", sizeof (char), 16, pLogFile);
            MMechostr (0, (char *) message);
            MMechostr (0, "\n");
    }

    #ifdef SCOL_GTK_DEBUG_DIALOG
    gtk_widget_show_all (win);
    gtk_dialog_run (GTK_DIALOG (win));
    gtk_widget_destroy (win);
    #endif

    fwrite (message, sizeof (char), strlen (message)+1, pLogFile);
    fwrite ("\n", sizeof (char), 2, pLogFile);

    g_free (message);

}

#else
FILE* scol_2dgtk_log_create ()
{;
    return NULL;
}

void scol_2dgtk_log_handler (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
{
    /*(void) log_domain;
    (void) log_level;
    (void) msg;
    (void) user_data;*/
    switch (log_level)
    {
        case (G_LOG_LEVEL_MESSAGE) :
            MMechostr (MSKFOO, (char *) message);
            MMechostr (MSKFOO, "\n");
            break;

        case (G_LOG_LEVEL_WARNING) :
            MMechostr (MSKWARNING, (char *) message);
            MMechostr (MSKWARNING, "\n");
            break;

        case (G_LOG_LEVEL_CRITICAL) :
        case (G_LOG_LEVEL_ERROR) :
            MMechostr (0, (char *) message);
            MMechostr (0, "\n");
            break;

        default :
            MMechostr (0, (char *) message);
            MMechostr (0, "\n");
    }
    return;
}
#endif
