package it.micegroup.voila2.cartridge.library.javaservice;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;

/**
 * Class for logger customization
 * 
 * @author Jessica Pascarella <jessica.pascarella@micegroup.it>
 *
 */
public class LogFormatter extends Formatter {
	// ANSI escape code
	public static final String ANSI_RESET = "\u001B[0m";
	public static final String ANSI_BLACK = "\u001B[30m";
	public static final String ANSI_RED = "\u001b[1;31m";
	public static final String ANSI_GREEN = "\u001B[32m";
	public static final String ANSI_YELLOW = "\u001b[33m";
	public static final String ANSI_BLUE = "\u001B[34m";
	public static final String ANSI_PURPLE = "\u001B[35m";
	public static final String ANSI_CYAN = "\u001B[36m";
	public static final String ANSI_WHITE = "\u001B[37m";
	public static final String ANSI_ORANGE = "\033[38;2;255;165;0m";

	// Here you can configure the format of the output and
	// its color by using the ANSI escape codes defined above.

	// format is called for every console log message
	@Override
	public String format(LogRecord record) {
		// This example will print date/time, class, and log level in yellow,
		// followed by the log message and it's parameters in white .
		StringBuilder builder = new StringBuilder();
		if (record.getLevel() != null && record.getLevel() == Level.INFO)
			builder.append(ANSI_BLACK);
		else if (record.getLevel() != null && record.getLevel() == Level.SEVERE)
			builder.append(ANSI_RED);
		else if (record.getLevel() != null && record.getLevel() == Level.WARNING)
			builder.append(ANSI_BLACK);

		builder.append("[");
		builder.append(calcDate(record.getMillis()));
		builder.append("]");

//		builder.append(" [");
//		builder.append(record.getSourceClassName());
//		builder.append("]");

		if (record.getLevel() != null && record.getLevel() == Level.WARNING) {
			builder.append(ANSI_ORANGE);
			builder.append(" [");
			builder.append(record.getLevel().getName());
			builder.append("]");
			builder.append(ANSI_RESET);
			builder.append(ANSI_BLACK);
		} else if (record.getLevel() != null && record.getLevel() == Level.INFO) {
			builder.append(ANSI_BLUE);
			builder.append(" [");
			builder.append(record.getLevel().getName());
			builder.append("]");
			builder.append(ANSI_RESET);
			builder.append(ANSI_BLACK);
		} else if (record.getLevel() != null && record.getLevel() == Level.SEVERE) {
			builder.append(ANSI_RED);
			builder.append(" [");
			builder.append("ERROR");
			builder.append("]");
			builder.append(ANSI_RESET);
			builder.append(ANSI_RED);
		} else {
			builder.append(" [");
			builder.append(record.getLevel().getName());
			builder.append("]");
		}

		builder.append(" - ");
		builder.append(record.getMessage());

		Object[] params = record.getParameters();

		if (params != null) {
			builder.append("\t");
			for (int i = 0; i < params.length; i++) {
				builder.append(params[i]);
				if (i < params.length - 1)
					builder.append(", ");
			}
		}

		builder.append(ANSI_RESET);
//		builder.append("\n");
		return builder.toString();
	}

	private String calcDate(long millisecs) {
		SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date resultdate = new Date(millisecs);
		return date_format.format(resultdate);
	}
	
	public static String logFormat(LogRecord logRecord) {
		LogFormatter formatter = new LogFormatter();
		return formatter.format(logRecord);
	}
}
