package com.eactive.eai.common.secure.inisafe;

import java.util.HashMap;

import com.eactive.eai.adapter.socket.common.CommonLib;
import com.eactive.eai.batch.doc.BatchDoc;
import com.eactive.eai.common.property.PropManager;
import com.eactive.eai.common.util.Logger;
import com.initech.inisafenet.HandShakeManager;
import com.initech.inisafenet.exception.INISAFENetException;

public class IniSafeManager {

	static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
	private static IniSafeManager myInstance = new IniSafeManager();
	private HashMap<String, HandShakeManager> map = new HashMap<String, HandShakeManager>();

	private static final String PROP_GROUP    = "IniSafeManager";
	private static final String PROP_KEY1     = "INISAFENET_PROPERTIES";

	private static final String INISAFENET_PROPERTIES = "D:/initech/inisafe/properties/INISAFENet.properties";

	public static IniSafeManager getInstance() {
		if(myInstance == null) {
			myInstance = new IniSafeManager();
		}
		return myInstance;
	}
	
	public byte[] initHandShakeClient( BatchDoc batchDoc ){
		String uuid = batchDoc.getBatchMsg().getHeader().getUUID();
		try {
			HandShakeManager hsm = map.get(uuid);
			if ( hsm == null ){
				logger.debug("getProp()-->" + getProp());
				hsm = new HandShakeManager( getProp());
				map.put(uuid, hsm);
			}
			return hsm.initHandShakeClient(null);
		} catch (INISAFENetException e) {
			logger.error(e.getMessage(), e);
		}
		return null;
	}

	public byte[] initHandShake( BatchDoc batchDoc ){
		String uuid = batchDoc.getBatchMsg().getHeader().getUUID();
		logger.debug("[initHandShake]getSendBuffer-->" + CommonLib.getDumpMessage(batchDoc.getBatchMsg().getBody().getSendBuffer()));
		try {
			HandShakeManager hsm = map.get(uuid);
			if ( hsm == null ){
				logger.debug("getProp()-->" + getProp());
				hsm = new HandShakeManager( getProp());
				map.put(uuid, hsm);
			}
			return hsm.initHandShake(batchDoc.getBatchMsg().getBody().getSendBuffer());
		} catch (INISAFENetException e) {
			logger.error(e.getMessage(), e);
		}
		return null;
	}

	public byte[] updateHandShakeClient( BatchDoc batchDoc ){
		String uuid = batchDoc.getBatchMsg().getHeader().getUUID();
		logger.debug("[updateHandShakeClient]getSendBuffer-->" + CommonLib.getDumpMessage(batchDoc.getBatchMsg().getBody().getSendBuffer()));
		try {
			HandShakeManager hsm = map.get(uuid);
			if ( hsm == null ){
				logger.debug("getProp()-->" + getProp());
				hsm = new HandShakeManager( getProp());
				map.put(uuid, hsm);
			}
			return hsm.updateHandShakeClient(batchDoc.getBatchMsg().getBody().getSendBuffer());
		} catch (INISAFENetException e) {
			logger.error(e.getMessage(), e);
		}
		return null;
	}

	public byte[] updateHandShake( BatchDoc batchDoc ){
		String uuid = batchDoc.getBatchMsg().getHeader().getUUID();
		logger.debug("[updateHandShake]getSendBuffer-->" + CommonLib.getDumpMessage(batchDoc.getBatchMsg().getBody().getSendBuffer()));
		try {
			HandShakeManager hsm = map.get(uuid);
			if ( hsm == null ){
				logger.debug("getProp()-->" + getProp());
				hsm = new HandShakeManager( getProp());
				map.put(uuid, hsm);
			}
			return hsm.updateHandShake(batchDoc.getBatchMsg().getBody().getSendBuffer());
		} catch (INISAFENetException e) {
			logger.error(e.getMessage(), e);
		}
		return null;
	}

	public byte[] finalHandShakeClient( BatchDoc batchDoc ){
		String uuid = batchDoc.getBatchMsg().getHeader().getUUID();
		logger.debug("[finalHandShakeClient]getSendBuffer-->" + CommonLib.getDumpMessage(batchDoc.getBatchMsg().getBody().getSendBuffer()));
		try {
			HandShakeManager hsm = map.get(uuid);
			if ( hsm == null ){
				logger.debug("getProp()-->" + getProp());
				hsm = new HandShakeManager( getProp());
				map.put(uuid, hsm);
			}
			return hsm.finalHandShakeClient(batchDoc.getBatchMsg().getBody().getSendBuffer());
		} catch (INISAFENetException e) {
			logger.error(e.getMessage(), e);
		}
		return null;
	}
	
	public int finalHandShake( BatchDoc batchDoc, byte[] finalPhase ){
		String uuid = batchDoc.getBatchMsg().getHeader().getUUID();
		logger.debug("[finalHandShake]getSendBuffer-->" + CommonLib.getDumpMessage(batchDoc.getBatchMsg().getBody().getSendBuffer()));
		try {
			HandShakeManager hsm = map.get(uuid);
			if ( hsm == null ){
				logger.debug("getProp()-->" + getProp());
				hsm = new HandShakeManager( getProp());
				map.put(uuid, hsm);
			}
			return hsm.finalHandShake(finalPhase);
		} catch (INISAFENetException e) {
			logger.error(e.getMessage(), e);
		}
		return -1;
	}

	public void removeManager( BatchDoc batchDoc){
		String uuid = batchDoc.getBatchMsg().getHeader().getUUID();
		map.remove(uuid);
		return;
	}

	public byte[] enc( BatchDoc batchDoc, byte[]data ){
		String uuid = batchDoc.getBatchMsg().getHeader().getUUID();
		try {
			HandShakeManager hsm = map.get(uuid);
			if ( hsm == null ){
				logger.debug("getProp()-->" + getProp());
				return data;
			}
			return hsm.i_encrypt(data);
		} catch (INISAFENetException e) {
			logger.error(e.getMessage(), e);
		}
		return null;
	}

	public byte[] dec( BatchDoc batchDoc, byte[]data ){
		String uuid = batchDoc.getBatchMsg().getHeader().getUUID();
		try {
			HandShakeManager hsm = map.get(uuid);
			if ( hsm == null ){
				logger.debug("getProp()-->" + getProp());
				return data;
			}
			return hsm.i_decrypt(data);
		} catch (INISAFENetException e) {
			logger.error(e.getMessage(), e);
		}
		return null;
	}

	private String getProp(){
		String prop = null;
		try{
			prop = PropManager.getInstance().getProperty(PROP_GROUP,PROP_KEY1);
		}catch ( Exception e ){}
		if ( prop != null )
			return prop;
		return INISAFENET_PROPERTIES;
	}

}
