package tools;
import tools.LogLevel;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.io.IOException;
import java.sql.Timestamp;


/** Helper to log.
 * @author	Louis Royer
 * @author	Flavien Haas
 * @author	JS Auge
 * @version	1.0
 */
public class Logger {
	private Path logFile;

	/** Constructor with logFile.
	 * @param logFile	name of the file to store logs.
	 */
	public Logger(String logFile) {
		assert logFile != null : "Logfile name is null";
		this.logFile = Paths.get(logFile);
		try {
			this.logFile.toFile().createNewFile();
		} catch (IOException e) {
			System.err.println("Error: cannot initialize logfile");
		}
	}

	/** Appends log to filelog and print to stderr.
	 * @param text	Text to log
	 */
	public void write(String text, LogLevel logLevel) {
		String colorize = "\u001B[0m";
		String msg = "[" + new Timestamp(System.currentTimeMillis()) + "] " + text + "\n";
		String level = null;
		switch (logLevel) {
			case Error:
				level = "[Error]";
				colorize = "\u001B[31m"; // RED
				break;
			case Info:
				level = "[Info]";
				colorize = "\u001B[32m"; // GREEN
				break;
			case Warning:
				level = "[Warning]";
				colorize = "\u001B[33m"; // YELLOW
				break;
			case Action:
				level = "[Action]";
				break;
			case Debug:
				level = "[Debug]";
				colorize = "\u001B[36m"; // CYAN
				break;
			default:
				System.err.println("Error: incorrect logLevel");
		}
		if (level != null) {
			msg = level + " " + msg;
		}
		switch (logLevel) {
			case Error:
			case Info:
			case Warning:
			case Debug:
			default:
				System.err.println(colorize + text + "\u001B[0m");
				break;
			case Action:
				break;
		}
		switch (logLevel) {
			case Debug:
				break;
			case Error:
			case Info:
			case Warning:
			default:
				try {
					Files.write(logFile, msg.getBytes(), StandardOpenOption.APPEND);
				} catch (IOException e) {
					System.err.println("Error: cannot write in logfile");
				}
				break;
		}
	}

	/** Appends log to filelog and print to stderr.
	 * Adds [TCP] in log line.
	 * @param text	Text to log
	 * @param logLevel	Type of log
	 */
	public void writeTCP(String text, LogLevel logLevel) {
		write("[TCP] " + text, logLevel);
	}

	/** Appends log to filelog and print to stderr.
	 * Adds [TCP] in log line.
	 * @param text	Text to log
	 * @param logLevel	Type of log
	 */
	public void writeTCP(Exception e, LogLevel logLevel) {
		writeTCP(e.toString(), logLevel);
		e.printStackTrace();
	}

	/** Appends log to filelog and print to stderr.
	 * Adds [UDP] in log line.
	 * @param text	Text to log
	 * @param logLevel	Type of log
	 */
	public void writeUDP(String text, LogLevel logLevel) {
		write("[UDP] " + text, logLevel);
	}

	/** Appends log to filelog and print to stderr.
	 * Adds [UDP] in log line.
	 * @param text	Text to log
	 * @param logLevel	Type of log
	 */
	public void writeUDP(Exception e, LogLevel logLevel) {
		writeUDP(e.toString(), logLevel);
		e.printStackTrace();
	}


}
