package com.eactive.eai.rms.common.util;

import java.util.HashMap;
import java.util.Iterator;

import com.eactive.eai.agent.command.CommonCommand;
import com.eactive.eai.rms.onl.common.service.AgentUtilService;

public class AgentParserUtils {

	private static String SUCCESS = "S";
	private static String FAIL    = "F";
	private static String DEFAULT_STATUS_NAME = "status";
	private static String DEFAULT_MESSAGE_NAME = "message";

    public AgentParserUtils() {
        super();
    }

    public static HashMap<String, String> parse(CommonCommand commonCommand, AgentUtilService agentUtilService) {
    	return parse(commonCommand,agentUtilService,DEFAULT_STATUS_NAME,DEFAULT_MESSAGE_NAME);
    }
    public static HashMap<String, String> parse(CommonCommand commonCommand, AgentUtilService agentUtilService,String statusName,String messageName) {
    	
    	HashMap<String,Object> map    = new HashMap<String,Object>();
    	HashMap<String,String> result = new HashMap<String,String>();
    	try {
	        map = agentUtilService.broadcast(commonCommand);
	        result.put(statusName, SUCCESS);
			result.put(messageName, StringUtils.cuttings(CommonUtil.getMapString(map, ","), 4000)[0]);
	
	        Iterator<String> keys = map.keySet().iterator();
	        while( keys.hasNext() ) {
	        	String key = (String)keys.next();
	        	if( map.get(key) == null || ((String)map.get(key)).indexOf("success") == -1 ) {
			        result.put(statusName, FAIL);
	    	        break;
	        	}
	        }
    	} catch(Exception e) {
    		//logger.error(e.getMessage(),e);
	        result.put(statusName, FAIL);
			result.put(messageName, StringUtils.cuttings(CommonUtil.getArrayString(e.getStackTrace(), "\n"), 4000)[0]);
    	}
    	return result; 
    }    
}
