package shadersmod.common;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.util.logging.StreamHandler;

import static java.util.logging.Level.*;

public abstract class SMCLog {

	private static class SMCFormatter extends Formatter {
		int tzOffset;
		SMCFormatter() {
			tzOffset = Calendar.getInstance().getTimeZone().getRawOffset();
		}
		@Override
		public String format(LogRecord record) {
			StringBuilder sb = new StringBuilder();
			//int time = (int)((record.getMillis() + tzOffset) % (24*60*60*1000));
			sb.append("[")
			//.append(time/36000000).append(time/3600000%10)//.append(":")
			//.append(time/600000%6).append(time/60000%10)//.append(":")
			//.append(time/10000%6).append(time/10000%10).append(".")
			//.append(time/100%10).append(time/10%10).append(time%10).append(" ")
			.append(record.getLoggerName()).append(" ")
			.append(record.getLevel()).append("]")
			.append(record.getMessage()).append("\n");
			return sb.toString();
		}
	}
	
	private static class SMCLogger extends Logger {
		SMCLogger(String name) {
			super(name,null);
			setUseParentHandlers(false);
			Formatter formatter = new SMCFormatter();
			Handler handler = new ConsoleHandler();
			handler.setFormatter(formatter);
			handler.setLevel(ALL);
			addHandler(handler);
			try {
				OutputStream outstr = new FileOutputStream("logs/shadersmod.log", false);
				handler = new StreamHandler(outstr,formatter) {
					@Override
					public synchronized void publish(LogRecord record) {
						super.publish(record);
						flush();
					}					
				};
				//handler = new FileHandler("logs/shadersmod-%u.log",1048576*16,1); 
				handler.setFormatter(formatter);
				handler.setLevel(ALL);
				addHandler(handler);
			} catch (IOException e) {
				e.printStackTrace();
			}
			setLevel(ALL);
		}
	}
	
	private static class SMCLevel extends Level {
		private SMCLevel(String name, int value) {
			super(name,value);
		}
	}
	
	public static final String smcLogName = "SMC";
	public static final Logger logger = new SMCLogger(smcLogName);
	public static final Level SMCINFO   = new SMCLevel("INF",850);
	public static final Level SMCCONFIG = new SMCLevel("CFG",840);
	public static final Level SMCFINE   = new SMCLevel("FNE",830);
	public static final Level SMCFINER  = new SMCLevel("FNR",820);
	public static final Level SMCFINEST = new SMCLevel("FNT",810);

	public static void log(Level level, String message) {
		if (logger.isLoggable(level))
			logger.log(level,message);
	}
	public static void severe(String message) {
		if (logger.isLoggable(SEVERE))
			logger.log(SEVERE,message);
	}
	public static void warning(String message) {
		if (logger.isLoggable(WARNING))
			logger.log(WARNING,message);
	}
	public static void info(String message) {
		if (logger.isLoggable(SMCINFO))
			logger.log(SMCINFO,message);
	}
	public static void config(String message) {
		if (logger.isLoggable(SMCCONFIG))
			logger.log(SMCCONFIG,message);
	}
	public static void fine(String message) {
		if (logger.isLoggable(SMCFINE))
			logger.log(SMCFINE,message);
	}
	public static void finer(String message) {
		if (logger.isLoggable(SMCFINER))
			logger.log(SMCFINER,message);
	}
	public static void finest(String message) {
		if (logger.isLoggable(SMCFINEST))
			logger.log(SMCFINEST,message);
	}
	
	public static void log(Level level, String format, Object ... args) {
		if (logger.isLoggable(level))
			logger.log(level,String.format(format, args));
	}
	public static void severe(String format, Object ... args) {
		if (logger.isLoggable(SEVERE))
			logger.log(SEVERE,String.format(format, args));
	}
	public static void warning(String format, Object ... args) {
		if (logger.isLoggable(WARNING))
			logger.log(WARNING,String.format(format, args));
	}
	public static void info(String format, Object ... args) {
		if (logger.isLoggable(SMCINFO))
			logger.log(SMCINFO,String.format(format, args));
	}
	public static void config(String format, Object ... args) {
		if (logger.isLoggable(SMCCONFIG))
			logger.log(SMCCONFIG,String.format(format, args));
	}
	public static void fine(String format, Object ... args) {
		if (logger.isLoggable(SMCFINE))
			logger.log(SMCFINE,String.format(format, args));
	}
	public static void finer(String format, Object ... args) {
		if (logger.isLoggable(SMCFINER))
			logger.log(SMCFINER,String.format(format, args));
	}
	public static void finest(String format, Object ... args) {
		if (logger.isLoggable(SMCFINEST))
			logger.log(SMCFINEST,String.format(format, args));
	}

}
