package com.FitBank.common;

import java.io.PrintStream;

/**
 * Clase Debug.
 * 
 * @author FitBank
 */
public class Debug {
    /** Define si se esta en modo de debug. */
    public static final boolean DEBUG = true;

    /** Niveles para imprimir en caso de mensaje. */
    public static final int NIVELES_MENSAJE = 1;

    /** Niveles para imprimir en caso de error. */
    public static final int NIVELES_ERROR   = 4;

    /**
     * Imprime informacion sobre la ejecucion.
     */
    public static void imprimirInformacion() {
        if (DEBUG) {
            imprimir(null, null, new Throwable(""), System.out, NIVELES_MENSAJE, 1);
        }
    }

    /**
     * Imprime informacion sobre la ejecucion.
     * 
     * @param mensaje Mensaje a ser imprimido
     */
    public static void imprimirInformacion(String mensaje) {
        if (DEBUG) {
            imprimir(null, mensaje, new Throwable(""), System.out, NIVELES_MENSAJE, 1);
        }
    }

    /**
     * Imprime un error con informacion sobre la ejecucion.
     * 
     * @param mensaje Mensaje a ser imprimido
     */
    public static void imprimirError(String mensaje) {
        if (DEBUG) {
            imprimir("ERROR: " + mensaje, null, new Throwable(""), System.err, NIVELES_ERROR, 1);
        }
    }

    /**
     * Imprime un error con informacion sobre la ejecucion.
     * 
     * @param t Throwable (Excepcion) que se capturo
     */
    public static void imprimirError(Throwable t) {
        if (DEBUG) {
            imprimir("ERROR: ", "MENSAJE: " + t.getClass() + " -> " + t.getLocalizedMessage(), t,
            		 System.err, NIVELES_ERROR, 0);
        }
    }

    /**
     * Imprime un error con informacion sobre la ejecucion.
     * 
     * @param mensaje Mensaje a ser imprimido
     * @param t Throwable (Excepcion) que se capturo
     */
    public static void imprimirError(String mensaje, Throwable t) {
        if (DEBUG) {
            imprimir("ERROR: " + mensaje, "MENSAJE: " + t.getClass() + " -> " + t.getLocalizedMessage(), t,
            		 System.err, NIVELES_ERROR, 0);
        }
    }

    private static void imprimir(String mensajePre, String mensajePos, Throwable t, PrintStream p,
    							 int nivel, int inicio) {
        StackTraceElement[] st = t.getStackTrace();

        p.println("############################################################");
        if (st.length > 0 + inicio) {
            if (mensajePre != null) {
                p.println("\t" + mensajePre);
            }
            p.println("Ejecutando:");
            p.println("\t" + st[0 + inicio]);
            if (mensajePos != null) {
                p.println("\t" + mensajePos);
            }
        }

        if (st.length > 0 + nivel + inicio) {
            p.println("Llamado desde:");
        }

        for (int a = 1 + inicio; a < 1 + nivel + inicio && a < st.length; a++) {
            p.println("\t" + st[a]);
        }

        p.println("############################################################");
    }
}
