package com.eactive.eai.inbound;

import com.eactive.eai.batch.common.BatchDirUtil;
import com.eactive.eai.batch.common.StringUtil;
import com.eactive.eai.batch.doc.BatchDoc;
import com.eactive.eai.common.routing.Process;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.inbound.remote.RemoteTransfer;
import com.eactive.eai.inbound.remote.RemoteTransferLogKeys;

public class RecvFileEventListener extends Process{
	
    // FileLoger
    static Logger logger = Logger.getLogger(Logger.LOGGER_FILEEVENT);



    
//	private String dirPath;
//	private String fileName;


    public void procFileEvent(String dirPath, String fileName, String lines){


    	logger.info("[Recv File Event] =========================================================================");
        logger.info("[Recv File Event] ▶▶▶▶▶▶▶▶▶▶ Local 수신 FILE EVENT 발생 !! ◀◀◀◀◀◀◀◀◀◀");
    	
        logger.info("[Recv File Event] ※ Event 디렉토리  : [" + dirPath         +"]");
        logger.info("[Recv File Event] ※ Event 파일명    : [" + fileName        +"]");
        logger.info("[Recv File Event] =========================================================================");

        
        try {
    		String recvRoot = BatchDirUtil.getResponseRootDir();
        	if ( dirPath.indexOf(recvRoot) < 0 ){
        		logger.error("경로 지정 오류입니다. 이벤트 발생 경로-->" + dirPath + ", 수신_Root-->" + recvRoot);
        		return;
        	}
        	String instPath = StringUtil.replaceStr( dirPath, recvRoot, "" );
        	if ( instPath.startsWith("/"))
        		instPath = instPath.substring(1);
        	
        	if ( instPath.endsWith("/"))
        		instPath = instPath.substring(0, instPath.length()-1);

        	String[] arr = instPath.split("/");
        	if ( arr.length < 2 ){
        		logger.error("수신 기관 경로 지정 오류입니다. 수신 기관 경로-->" + instPath);
        		return;
        	}
        	String procCode = arr[0];
        	String instCode = arr[1];
        	logger.debug("procCode -->" + procCode);
        	logger.debug("instCode -->" + instCode);

            RemoteTransfer trans = new RemoteTransfer();
            trans.sendFile(procCode, instCode, fileName, RemoteTransferLogKeys.START_BY_EVENT, RemoteTransferLogKeys.STARTER_SYSTEM, false);
            trans.sendFile(procCode, instCode, fileName, RemoteTransferLogKeys.START_BY_EVENT, RemoteTransferLogKeys.STARTER_SYSTEM, true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
    }


	@Override
	public BatchDoc clientRequestwithReturn(
			BatchDoc batchDoc) throws Exception {
		return null;
	}
	@Override
	public void clientRequest() throws Exception {
	}
}
