package com.eactive.eai.adapter.http.dynamic;

import com.eactive.eai.adapter.AdapterPropManager;
import com.eactive.eai.common.server.EAIServerManager;
import com.eactive.eai.common.util.Logger;

import java.util.HashMap;
import java.util.Properties;

public class HttpLoggerManager {

	private static HttpLoggerManager instance = new HttpLoggerManager();
	
	private HashMap<String, Logger> pool = new HashMap<String,Logger>(); // KEY:adapterGroupName, VALUE:Logger
	private String eaiServer;
	private static String DEFAULT_LOGGER_NAME = "HTT";
	private static String ERROR_LOGGER_NAME = "ERRORHTT";
	
	public HttpLoggerManager() {
		eaiServer = EAIServerManager.getInstance().getLocalServerName();
		if (eaiServer == null)	eaiServer = "";
	}
	
	public static HttpLoggerManager getInstance() {
		return instance;
	}
	
	public void stop() {
		this.stop();
	}
	
	public void removeAll() {
		pool.clear();
	}
	
	public void start(String adapterGroupName) {
		this.addLogger(adapterGroupName);
	}
	
	public synchronized void addLogger(String adapterGroupName) {
		String loggerName = this.getLoggerName( adapterGroupName );
		Logger logger = pool.get( loggerName );
		if (logger == null) {
			synchronized (pool) {
				logger = pool.get(loggerName);
				if (logger == null) {
					Logger logger2 = Logger.getLogger(loggerName);
					pool.put(loggerName,  logger2);
				}
			}
		}
	}
	
	private String getLoggerName(String adapterGroupName) {
		StringBuffer sb = new StringBuffer();
		sb.append("FileLogger{").append(adapterGroupName).append("}_").append(this.eaiServer);
		
		return sb.toString();
	}
	
	private String getLoggerBase(String adapterGroupName) {
		StringBuffer strBuff = new StringBuffer();
		strBuff.append("FileLogger{").append(adapterGroupName).append("}");
		
		return strBuff.toString();
	}
	
	public void removeLogger(String adapterGroupName) {
		pool.remove(this.getLoggerName(adapterGroupName));
	}
	
	public void stop(String adapterGroupName) {
		// adapterGroupNameÀÇ Session Stop & Clear
		this.removeLogger(adapterGroupName);
	}
	
	public Logger getLogger(String adapterGroupName) {
		return getLoggerGroup(adapterGroupName);
	}

	public Logger getErrLogger() {
		return getLoggerGroup(ERROR_LOGGER_NAME);
	}
	
	public Logger getLoggerGroup(String adapterGroupName) {
		
		Logger logger = pool.get(this.getLoggerName(adapterGroupName));
		if (logger == null) {
			if (DEFAULT_LOGGER_NAME.equals(adapterGroupName)) {
				this.addLogger(adapterGroupName);
				logger = pool.get(this.getLoggerName(adapterGroupName));
			} else {
				AdapterPropManager propManager = AdapterPropManager.getInstance();
				Properties loggerInfo = null;
				
				try {
					loggerInfo = propManager.getProperties(getLoggerBase(adapterGroupName));
				} catch(Exception e) {
					// ¹«½Ã
				}
				
				if (loggerInfo == null) {
					logger = getLogger();
				} else {
					this.addLogger(adapterGroupName);
					logger = pool.get(this.getLoggerName(adapterGroupName));
				}
			}
		}
		
		return logger;
	}
	
	public Logger getLogger() {
		Logger logger = pool.get(this.getLoggerName(this.DEFAULT_LOGGER_NAME));
		if (logger == null) {
			this.addLogger(DEFAULT_LOGGER_NAME);
			logger = pool.get(this.getLoggerName(DEFAULT_LOGGER_NAME));
		}
		
		return logger;
	}
}
