/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Clase que ofrece un servicio que muestra el flujo de ejecución de la aplicación mientras ésta está ejecuntando
 *
 * @author Enrique M.L
 */
public class AyudaDebug {
    
    private static int auxPrint = 0;
    private static boolean mostrarPrint = true;
    private static Object ultimoMetodo = ":init";

    /**
     * Incrementa auxPrint una unidad
     * 
     */
    private synchronized static void incrAuxPrint(){
        auxPrint++;
    }
    
    /**
     * Decerementa auxPrint una unidad
     * 
     */
    private synchronized static void decrAuxPrint(){
        auxPrint--;
    }
    
    /**
     * Restaura auxPrint a 1
     * 
     */
    public synchronized static void restartAuxPrint(){
    	auxPrint = 1;
    }
    
    /**
     * Si 'b' es true: se muestran los mensajes de AyudaDebug
     * Si 'b' es false: no se muestran los mensajes de AyudaDebug
     * 
     * @param b 
     */
    public synchronized static void setMostrarPrint(boolean b){
        mostrarPrint = b;
    }
    
    /**
     * Printea un mensaje 'msg'
     * 
     * @param msg 
     */
    public static void print(String msg){
        if (mostrarPrint){
            System.out.println(msg);
        }
    }
    
    /**
     * Printea el comando 'command'
     * 
     * @param command 
     */
    public static void printCommand(ArrayList<String> command){
        if (mostrarPrint){  
            Iterator<String> ite = command.iterator();
            String finished_command = new String();
            while (ite.hasNext()) {
                finished_command = finished_command + ite.next() + " ";
            }
            System.out.println("Command = " + finished_command);
        }
    }
    
    /**
     * Printea el contenido de la lista 'arrayList'
     * 
     * @param arrayList
     */
    public static void printArrayList(ArrayList<String> arrayList){
        if (mostrarPrint){
            Iterator<String> ite = arrayList.iterator();
            String finished = new String();
            while (ite.hasNext()) {
                finished = finished + ite.next() + " ";
            }
            System.out.println("Contenido = " + finished);
        }
    }
	    
	/**
	 * Printea el stdout y stderr del arraylist 'result'
	 * 
	 * @param result
	 */
    public static void printResult(ArrayList<String> result){
        if (mostrarPrint){
            System.out.println("Result");
            System.out.println("-stdout: \n" + result.get(0));
            System.out.println("-stderr: \n" + result.get(1));
        }
    }

    /**
     * Printea el metodo
     * Indica si inicia o termina
     * 
     * @param msg 
     */
    public static void printMetodo(String msg){
        if (mostrarPrint){ 
            if(msg.substring(msg.length()-5, msg.length()).equals(":init")){
                if (ultimoMetodo.equals(":init")){
                    System.out.println();
                }
                incrAuxPrint();
                ultimoMetodo = ":init";
            }
            for(int i = 0; i < auxPrint; i++){
                System.out.print("*");
            }
            System.out.println(" " + msg);
            
            if(msg.substring(msg.length()-4, msg.length()).equals(":end")){
                System.out.println();
                ultimoMetodo = ":end";
                decrAuxPrint();
            }
            
        }
    }

    /**
     * Printea un error 'msg'(algo que no debia suceder si todo esta bien controlado)
     * 
     * @param msg 
     */
    public static void printError(String msg) {
        if (mostrarPrint){
            System.out.println("##Error## " + msg);
        }
    }
    
}
