package gmt.logger;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configurator;

import gmt.common.type.LogLevelType;
import gmt.logger.event.IGmtLogSendHandler;

import java.io.File;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;

public class GmtLogManager {

	Logger log;
	IGmtLogSendHandler handler;
	private static String cfgpath = "";

	public GmtLogManager() {
		if (cfgpath.equals("")) {
			cfgpath = System.getProperty("user.dir");
			cfgpath = cfgpath + "/cfg/log4j2.xml";
		}

		File file = new File(this.cfgpath);
		LoggerContext context = (LoggerContext) LogManager.getContext(false);
		context.setConfigLocation(file.toURI());

		log = LogManager.getLogger("");

	}

	private static class Holder {
		public static final GmtLogManager INSTANCE = new GmtLogManager();

	}

	public static GmtLogManager getInstance() {
		return Holder.INSTANCE;
	}

	public static void setpath(String path) {
		if (path.equals("")) {
			path = System.getProperty("user.dir");
		}
		cfgpath = path + "/cfg/log4j2.xml";
	}


	public void writeLevelLog(String msg, LogLevelType loglevel, String loggername) {

		log = LogManager.getLogger(loggername);
		switch(loglevel.ordinal()) {
		case 0 : log.trace(msg);
				 break;
		case 1 : log.debug(msg);
			     break;
		case 2 : log.info(msg);
			     break;
		case 3 : log.warn(msg);
				 break;
		case 4 : log.error(msg);
				 break;
		case 5 : log.fatal(msg);
				 break;
		default: log.trace(msg);
		         break;
		}

		if (handler != null) {
			handler.sendlLog(msg, loggername, loglevel);
		}
	}

	public void writeLevelLog(String msg, LogLevelType loglevel, String loggername, Boolean sendtcp) {

		log = LogManager.getLogger(loggername);
		switch(loglevel.ordinal()) {
		case 0 : log.trace(msg);
				 break;
		case 1 : log.debug(msg);
			     break;
		case 2 : log.info(msg);
			     break;
		case 3 : log.warn(msg);
				 break;
		case 4 : log.error(msg);
				 break;
		case 5 : log.fatal(msg);
				 break;
		default: log.trace(msg);
		         break;
		}

		if (sendtcp.equals(true)) {

			if (handler != null) {
				handler.sendlLog(msg, loggername, loglevel);
			}
		}

	}

	public void setLogLevel(String loggername, LogLevelType loglevel) {
//		Configurator.setLevel(name, loglevel);
		switch(loglevel.ordinal()) {
		case 0  : Configurator.setLevel(loggername, Level.TRACE);
				  break;
		case 1  : Configurator.setLevel(loggername, Level.DEBUG);
		 		  break;
		case 2  : Configurator.setLevel(loggername, Level.INFO);
		 		  break;
		case 3  : Configurator.setLevel(loggername, Level.WARN);
		 		  break;
		case 4  : Configurator.setLevel(loggername, Level.ERROR);
		 		  break;
		case 5  : Configurator.setLevel(loggername, Level.FATAL);
		 		  break;
		default : Configurator.setLevel(loggername, Level.TRACE);
				  break;
		}
	}

	public void setLogSendHandler(IGmtLogSendHandler handler){
		this.handler = handler;
	}
}
