package com.eactive.eai.adapter.http;

import com.eactive.eai.transformer.util.CircularQueue;
import com.eactive.eai.transformer.util.ExceptionStackTrace;

import java.util.LinkedHashMap;


public class HttpMemoryLogger {
	final static int MAX_LOG_MESSAGE_SIZE = 500000;
	
	static LinkedHashMap<String,CircularQueue> txQueues = new LinkedHashMap<String,CircularQueue>();
	static LinkedHashMap<String,CircularQueue> errorQueues = new LinkedHashMap<String,CircularQueue>();
	static LinkedHashMap<String,Long> errorCounts = new LinkedHashMap<String,Long>();
	static LinkedHashMap<String,Long> txCounts = new LinkedHashMap<String,Long>();

	static private void checkTxQueue(String adapterGroupName){
		if (txQueues.get(adapterGroupName) == null){
			txQueues.put(adapterGroupName,new CircularQueue(20));
		}
	}
	static private void checkErrorQueue(String adapterGroupName){
		if (errorQueues.get(adapterGroupName) == null){
			errorQueues.put(adapterGroupName,new CircularQueue(20));
		}
	}
	static private void checkErrorCount(String adapterGroupName){
		if (errorCounts.get(adapterGroupName) == null){
			errorCounts.put(adapterGroupName,new Long(0));
		}
	}
	static private void checkTxCount(String adapterGroupName){
		if (txCounts.get(adapterGroupName) == null){
			txCounts.put(adapterGroupName,new Long(0));
		}
	}	
	static public long getTransactionCount(String adapterGroupName) {
		checkTxCount(adapterGroupName);
		if (txCounts.get(adapterGroupName) == null)
			return 0;
		return txCounts.get(adapterGroupName);
	}
	
	static public String[] getTransactions(String adapterGroupName) {
		checkTxQueue(adapterGroupName);
		return txQueues.get(adapterGroupName).getRecords();
	}
	

//	static public void txlog(String adapterGroupName, String message) {
//		if (message == null)
//			return;
//		if (message.length() > MAX_LOG_MESSAGE_SIZE)
//			message = message.substring(0, MAX_LOG_MESSAGE_SIZE);
//		checkTxQueue(adapterGroupName);
//		txQueues.get(adapterGroupName).addRecord(message);
//	}
	
	static public void txlog(String adapterGroupName, String message) {
		if (message == null)
			return;
		if (message.length() > MAX_LOG_MESSAGE_SIZE)
			message = message.substring(0, MAX_LOG_MESSAGE_SIZE);
		synchronized(HttpMemoryLogger.class) {
			checkTxCount(adapterGroupName);
			checkTxQueue(adapterGroupName);
			Long l = txCounts.get(adapterGroupName);
			txCounts.put(adapterGroupName,++l);
		}
		txQueues.get(adapterGroupName).addRecord(message);
	}	
	
	
	static public long getErrorCount(String adapterGroupName) {
		checkErrorCount(adapterGroupName);
		if (errorCounts.get(adapterGroupName) == null)
			return 0;
		return errorCounts.get(adapterGroupName);
	}
	
	static public String[] getErrors(String adapterGroupName) {
		checkErrorQueue(adapterGroupName);
		return errorQueues.get(adapterGroupName).getRecords();
	}

	static public void error(String adapterGroupName, String message) {
		if (message == null)
			return;
		if (message.length() > MAX_LOG_MESSAGE_SIZE)
			message = message.substring(0, MAX_LOG_MESSAGE_SIZE);
		
		synchronized(HttpMemoryLogger.class) {
			checkErrorCount(adapterGroupName);
			checkErrorQueue(adapterGroupName);
			Long l = errorCounts.get(adapterGroupName);
			errorCounts.put(adapterGroupName,++l);
		}
		errorQueues.get(adapterGroupName).addRecord(message);
		//txQueues.get(adapterGroupName).addRecord("<font color='blue'>" + message + "</font>");
	}

	static public void error(String adapterGroupName,String message, Throwable t) {
		if (message == null)
			return;
		if (message.length() > MAX_LOG_MESSAGE_SIZE)
			message = message.substring(0, MAX_LOG_MESSAGE_SIZE);
		
		synchronized(HttpMemoryLogger.class) {
			checkErrorCount(adapterGroupName);
			checkErrorQueue(adapterGroupName);
			Long l = errorCounts.get(adapterGroupName);
			errorCounts.put(adapterGroupName,++l);
		}
		errorQueues.get(adapterGroupName).addRecord(message + " | " + ExceptionStackTrace.dump(t));
		//txQueues.get(adapterGroupName).addRecord("<font color='blue'>" + message + "\n" + ExceptionStackTrace.dump(t) + "</font>");
	}
	static public boolean isMemoryLogEnabled(String adapterGroupName) {
		return true;
//    	PropGroupVO propGroupVO = PropManager.getInstance().getPropGroupVO("Transformer");
//    	if (propGroupVO == null)
//    		return false;
//    	String value = propGroupVO.getProperty("memory.log");
//    	if ("1".equals(value))
//    		return true;
//    	return false;
	}
}
