package utils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Calendar;

/**
 * Crear un archivo de registro Log
 *
 * @author diego
 *
 */
//marco
public class FileLogger {
    private String hexLineHeader = "";
    private String fileName = "";
    private String logDir = "";
    private char[] hexLinePart = null;
    private char[] asciiLinePart = null;
    private int charCounter;
    private int hexPartCounter;
    private int asciiPartCounter;
    private Calendar runDate = null;
    private FileOutputStream outFile = null;
    private PrintStream filePrinter = null;

    public FileLogger() {
        this.charCounter = 0;
        this.hexPartCounter = 0;
        this.asciiPartCounter = 0;
        this.hexLinePart = new char[39];
        this.asciiLinePart = new char[16];

        for (int i = 0; i < this.hexLinePart.length; i++) {
            this.hexLinePart[i] = ' ';
        }
        for (int i = 0; i < this.asciiLinePart.length; i++) {
            this.asciiLinePart[i] = ' ';
        }

        this.runDate = Calendar.getInstance();
        this.logDir = "log";
        this.fileName = ("log" + this.runDate.get(1));

        if (this.runDate.get(2) + 1 < 10)
            this.fileName = (this.fileName + "0" + (this.runDate.get(2) + 1));
        else {
            this.fileName += this.runDate.get(2) + 1;
        }
        if (this.runDate.get(5) < 10)
            this.fileName = (this.fileName + "0" + this.runDate.get(5));
        else {
            this.fileName += this.runDate.get(5);
        }
        this.fileName += ".txt";
        String longFileName=null;
        String home=null;
        try {
        	
  	      OSValidator osv = new OSValidator();
  	      
  	      if (osv.isWindows()){
  	    	longFileName = this.logDir + "\\" + this.fileName;
	    	  home = System.getenv("homepath");
	    	  home = home + "\\java\\emv2000-64\\bin\\";
	    	  longFileName= home + longFileName;  	    	
  	      } else {
  	    	longFileName = this.logDir + "/" + this.fileName;
  	      }
        	
            //String longFileName = this.logDir + "/" + this.fileName;
            this.outFile = new FileOutputStream(longFileName, true);
            this.filePrinter = new PrintStream(this.outFile);
        } catch (Exception e) {
            System.out.println("Cannot create log file " + this.fileName + " on directory " + this.logDir + " " + e.getMessage());
            System.out.println("archivo FileLogger: " + longFileName);
            System.exit(1);
        }
        // this.filePrinter.println("\n-----------------------------------------------------------------");
        printLineHeader();
    }

    private void printLineHeader() {
        String preHeader = "";
        this.hexLineHeader = Integer.toHexString(this.charCounter).toUpperCase();
        for (int i = 0; i < 7 - this.hexLineHeader.length(); i++)
            preHeader = "0" + preHeader;
        this.hexLineHeader = (preHeader + this.hexLineHeader + ": ");
        this.filePrinter.print(this.hexLineHeader);
    }

    public void printStream(String stream) {
        byte[] byteStream = stream.getBytes();
        for (int i = 0; i < byteStream.length; i++) {
            if ((byteStream[i] > 31) && (byteStream[i] < 127))
                this.asciiLinePart[(this.asciiPartCounter++)] = ((char) byteStream[i]);
            else
                this.asciiLinePart[(this.asciiPartCounter++)] = '.';
            String temp = Integer.toHexString(byteStream[i]).toUpperCase();
            if (temp.length() > 1) {
                this.hexLinePart[(this.hexPartCounter++)] = temp.charAt(0);
                this.hexLinePart[(this.hexPartCounter++)] = temp.charAt(1);
            } else {
                this.hexLinePart[(this.hexPartCounter++)] = '0';
                this.hexLinePart[(this.hexPartCounter++)] = temp.charAt(0);
            }

            this.charCounter += 1;

            if (this.asciiPartCounter % 16 == 0) {
                this.filePrinter.println(new String(this.hexLinePart) + "  " + new String(this.asciiLinePart));
                printLineHeader();
                this.hexPartCounter = 0;
                this.asciiPartCounter = 0;

                for (int ix = 0; ix < this.hexLinePart.length; ix++)
                    this.hexLinePart[ix] = ' ';
                for (int ix = 0; ix < this.asciiLinePart.length; ix++) {
                    this.asciiLinePart[ix] = ' ';
                }
            } else if (this.asciiPartCounter % 2 == 0) {
                this.hexLinePart[(this.hexPartCounter++)] = ' ';
            }
        }
        this.filePrinter.flush();
    }

    public void closeLogger() {
        this.filePrinter.print(new String(this.hexLinePart) + "  " + new String(this.asciiLinePart));
        this.filePrinter.close();
        try {
            this.outFile.close();
        } catch (IOException e) {
            System.out.println("Caution! output file " + this.fileName
                    + " might not be correctly closed, system resource might not be completely freed.");
        }
    }
}