package com.eactive.eai.rms.bap.manage.mmi;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.HashMap;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class EIMSBAPParser implements MMIBAPParserKey {
	
    public static HashMap<String,Object> parseInfo(String data) throws Exception {
    	HashMap<String,Object> result = new HashMap<String,Object>();
    	HashMap<String,Object> param = new HashMap<String,Object>();
    	InputStream is = null;
    	try { 
    		is = StringToStream(data);
    		SAXBuilder builder = new SAXBuilder();
    		Document doc = builder.build(is);
    		
    		Element inter = doc.getRootElement();
            
            result.putAll(getAttr(doc.getRootElement()));
            result.putAll(getAttr(inter.getChild(COMMON_INFO)));
            result.putAll(getAttr(inter.getChild(SVC_INFO)));
            result.putAll(getAttr(inter.getChild(EAI_SVC_INFO)));
            result.putAll(getAttr(inter.getChild(OSID_INFO)));
            
            result.get(MMIBAPParserKey.OSID_INSTI_SYS_ID);
    		// TSEAIBJ01 데이터 처리
    		String intfType = result.get(MMIBAPParserKey.INTERFACE_TYPE).toString();
    		String eaiAfterShellPath = result.get(MMIBAPParserKey.EAI_AFTER_SHELL_PATH).toString();
    		String eaiAfterShellName = result.get(MMIBAPParserKey.EAI_AFTER_SHELL_NAME).toString();
    		
    		param.put( MMIBAPParserKey.MMI_INTF_ID, 				result.get(MMIBAPParserKey.MMI_INTF_ID));
    		param.put( MMIBAPParserKey.INTF_ID, 					result.get(MMIBAPParserKey.INTF_ID));
    		param.put( MMIBAPParserKey.VERSION, 					result.get(MMIBAPParserKey.VERSION));
    		param.put( MMIBAPParserKey.OSID_INSTI_SYS_ID, 		    result.get(MMIBAPParserKey.OSID_INSTI_SYS_ID));
    		param.put( MMIBAPParserKey.DEPLOY_ID, 		            result.get(MMIBAPParserKey.DEPLOY_ID));
    		param.put( MMIBAPParserKey.EAI_FILE_NAME, 		        result.get(MMIBAPParserKey.EAI_FILE_NAME));
    		param.put( MMIBAPParserKey.OSID_INSTI_SYS_ID, 		    result.get(MMIBAPParserKey.OSID_INSTI_SYS_ID));

    	    param.put( MMIBAPSyncField.bjobMsgDsticName.name(), 	result.get(MMIBAPParserKey.EAI_FILE_KOR_NAME));
    	    param.put( MMIBAPSyncField.bjobTranDstcdName.name(), 	result.get(MMIBAPParserKey.EAI_FILE_NAME));
    	    param.put( MMIBAPSyncField.bjobTelgmHdrSize.name(),		result.get(MMIBAPParserKey.HEADER_LEN));
    	    param.put( MMIBAPSyncField.bjobTelgmRecSize.name(),		result.get(MMIBAPParserKey.RECORD_LEN));
    	    param.put( MMIBAPSyncField.bjobTelgmTrailSize.name(), 	result.get(MMIBAPParserKey.TRAILER_LEN));
    	    param.put( MMIBAPSyncField.eaiFileSendUseYn.name(),		intfType.equals("S") ? "1" : "" );
    	    param.put( MMIBAPSyncField.eaiFileRecvUseYn.name(),		intfType.equals("R") ? "1" : "" );
    	    param.put( MMIBAPSyncField.msgPrcssPrity.name(), 		"999");
    	    param.put( MMIBAPSyncField.thisMsgUseYn.name(),			"1");
    	    param.put( MMIBAPSyncField.uapplCd.name(), 				result.get(MMIBAPParserKey.APP_CD));
    	    param.put( MMIBAPSyncField.userid.name(), 				result.get(MMIBAPParserKey.MOD_ID));
    	    param.put( MMIBAPSyncField.sndRcvFileName.name(), 		result.get(MMIBAPParserKey.SVC_FILE_NAME));
    	    param.put( MMIBAPSyncField.thisMsgChrgIDs.name(), 		result.get(MMIBAPParserKey.MOD_ID));
    	    param.put( MMIBAPSyncField.groupCoCd.name(), 			"B");
    	    param.put( MMIBAPSyncField.rmtNodeName.name(), 			result.get(MMIBAPParserKey.SYS_ID));
    	    param.put( MMIBAPSyncField.sndScriptName.name(), 		eaiAfterShellPath+File.separator+eaiAfterShellName);
//    	    param.put( MMIBAPSyncField.logCretnYn.name(), 			"1");
    	    param.put( MMIBAPSyncField.bzwkRsEmpName.name(), 		result.get(MMIBAPParserKey.USER_NAME));
    	    param.put( MMIBAPSyncField.bzwkRsEmpTelno.name(), 		result.get(MMIBAPParserKey.USER_TELNO));
    	    param.put( MMIBAPSyncField.osidInstiCode.name(), 		result.get(MMIBAPParserKey.OSID_INSTI_CD));
    	    param.put( MMIBAPSyncField.osidInstiBzwkRsEmpName.name(), result.get(MMIBAPParserKey.OSID_INSTI_USER_NAME));
    	    param.put( MMIBAPSyncField.osidInstiTelno.name(), 		result.get(MMIBAPParserKey.OSID_INSTI_TELNO));
    	    param.put( MMIBAPSyncField.fileTransCond.name(), 		result.get(MMIBAPParserKey.FILE_TRAN_SCHDR_CNDT));
    	    param.put( MMIBAPSyncField.fileTransSche.name(), 		result.get(MMIBAPParserKey.FILE_TRAN_SCHDR_TIME));
    	    param.put( MMIBAPSyncField.fileTransAlam.name(), 		result.get(MMIBAPParserKey.NOSIGNAL_ALRAM_TIME));

    	    
    	    
//    		// TSEAIBS01 데이터 처리
//    		String fileTranSchdrTime = result.get(MMIBAPParserKey.FILE_TRAN_SCHDR_TIME).toString();
//    		String reTralTime = fileTranSchdrTime.substring(9,11);
//    		
    	    param.put( MMIBAPSyncField.bjobPtrnDstcd.name(), 		  intfType.equals("S") ? "RS" : "RR" );		
//    	    param.put( MMIBAPSyncField.bjobTranDstcdName.name(), 	  result.get(MMIBAPParserKey.EAI_FILE_NAME));		
////    		param.put( MMIBAPSyncField.osidInstiDstcd.name(), 		  result.get(MMIBAPParserKey.OSID_INSTI_SYS_ID).toString().substring(0,4));
    	    param.put( MMIBAPSyncField.sndrcvStartHMS.name(), 		  "0000");  // 0000
    	    param.put( MMIBAPSyncField.sndrcvEndHMS.name(), 		  "2359");  // 2359
    	    param.put( MMIBAPSyncField.msgSndrID.name(), 			  result.get(MMIBAPParserKey.DEPLOY_ID));
    	    param.put( MMIBAPSyncField.rqstRecvReTralCnt.name(), 	  0);
    	    param.put( MMIBAPSyncField.rqstRecvReTralUseYn.name(), 	  "0");
    	    param.put( MMIBAPSyncField.rqstRecvReTralIntvalTtm.name(),"0");
    	    param.put( MMIBAPSyncField.holdyPrcssDstcd.name(), 		  "1");
    	    param.put( MMIBAPSyncField.startDlayNoday.name(), 		   0);
    	    param.put( MMIBAPSyncField.endDlayNoday.name(),			   0);
    	    param.put( MMIBAPSyncField.thisMsgUseYn.name(),			   "1");
    	    param.put( MMIBAPSyncField.rpttYn.name(), 				   "0");
    	    param.put( MMIBAPSyncField.userid.name(), 				  result.get(MMIBAPParserKey.MOD_ID));
    	    
    	}catch(Exception e){
    		throw new Exception("info parsing error "+e.getMessage(),e);
    	}finally{
    		if (is != null){try{is.close();}catch(Exception e ){}}
    	}
    	return param;
    	
    }

	public static HashMap<String,String> getAttr(Element doc){
		if (doc == null) return  new HashMap<String,String>();
		HashMap<String,String> map = new HashMap<String,String>();
        for(Object obj : doc.getAttributes()){
        	if (obj instanceof Attribute){
        		Attribute attr = (Attribute)obj;
        		map.put(attr.getName(), attr.getValue());
        	}
        }
		return map;
	}

	public static InputStream StringToStream(String s){
		ByteArrayInputStream buffer = new ByteArrayInputStream(s.getBytes());
		return buffer;
	}
	
	public static HashMap<String,Object> parseDelete(String data) throws Exception{
    	HashMap<String,Object> result = new HashMap<String,Object>();
    	HashMap<String,Object> param = new HashMap<String,Object>();
    	InputStream is = null;
    	try { 
    		is = StringToStream(data);
			SAXBuilder builder = new SAXBuilder();
	        Document doc = builder.build(is);
	
	        Element delete = doc.getRootElement();
	        
            result.putAll(getAttr(doc.getRootElement()));
            result.putAll(getAttr(delete.getChild(COMMON_INFO)));
            result.putAll(getAttr(delete.getChild(SVC_INFO)));
            result.putAll(getAttr(delete.getChild(EAI_SVC_INFO)));
            result.putAll(getAttr(delete.getChild(OSID_INFO)));
            
    		// TSEAIBJ01 delete key 데이터 처리
    		param.put( MMIBAPParserKey.MMI_INTF_ID, 			result.get(MMIBAPParserKey.MMI_INTF_ID));
    		param.put( MMIBAPParserKey.INTF_ID, 				result.get(MMIBAPParserKey.INTF_ID));
    		param.put( MMIBAPParserKey.VERSION, 				result.get(MMIBAPParserKey.VERSION));
    		param.put( MMIBAPSyncField.bjobBzwkDstcd.name(), 	result.get(MMIBAPParserKey.SYS_ID).toString().substring(0, 5));
    	    param.put( MMIBAPSyncField.bjobMsgDsticName.name(),	result.get(MMIBAPParserKey.EAI_FILE_KOR_NAME));
    	    
    	    // TSEAIBS01 delete key 데이터 처리
    	    param.put( MMIBAPSyncField.osidInstiDstcd.name(), 	result.get(MMIBAPParserKey.OSID_INSTI_SYS_ID).toString().substring(0,4));
	        
		}catch(Exception e){
			//e.printStackTrace();
			throw e;
    	}finally{
    		if (is != null){try{is.close();}catch(Exception e ){}}
    	}
		
		return param;
	}
}
