package com.eactive.eai.agent.testcall;

import java.util.Properties;

import com.eactive.eai.agent.command.Command;
import com.eactive.eai.agent.command.CommandException;
import com.eactive.eai.common.property.PropManager;
import com.eactive.eai.common.testcall.TestCallManager;
import com.eactive.eai.common.util.Logger;

/**
 * 1. 기능 : TestCallManager를 시작 또는 종료 2. 처리 개요 : TestCallManager를 시작 또는 종료 3.
 * 주의사항
 *
 * @author
 * @version v 1.0.0
 * @see 관련 기능을 참조
 * @since
 * 
 */
public class TestCallCommand extends Command {

	private static final long serialVersionUID = 1L;

	public TestCallCommand() {
		super("TestCallCommand");
	}

	/**
	 * 1. 기능 : TestCallManager를 시작 또는 종료 2. 처리 개요 : arg로 전달된 Property의 값이 START이면
	 * TestCallManager를 시작하고 STOP이면 TestCallManager 종료 3. 주의사항
	 *
	 * @param
	 * @return
	 * @exception CommandException
	 **/
	public Object execute() throws CommandException {
		Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
		if (logger.isInfo())
			logger.info(this.name + " is executed");

		if (!(args instanceof Properties)) {
			String rspErrorCode = "RECEAIMCM001";
			String msg = makeException(rspErrorCode, null);
			if (logger.isError())
				logger.error(msg);
			throw new CommandException(msg);
		}

		Properties prop = new Properties();
		try {
			TestCallManager manager = TestCallManager.getInstance();
			PropManager pmanager = PropManager.getInstance();

			prop = (Properties) args;

			String run = "";
			String tmpperiod = "";

			run = prop.getProperty("RUN");
			tmpperiod = prop.getProperty("PERIOD");

			if (tmpperiod != null && tmpperiod.length() > 0) {
				long period = Long.parseLong(tmpperiod);
				pmanager.setProperty(manager.PROP_GROUP, manager.PROP_PERIOD, "" + period);
			}
			if (run != null && run.length() > 0) {
				if ("START".equals(run)) {
//                    System.out.println("start..");
					manager.start();
				} else if ("STOP".equals(run)) {
//                    System.out.println("stop..");
					manager.stop();
				}
			}

		} catch (Exception e) {
			String rspErrorCode = "RECEAIMCM002";
			String msg = makeException(rspErrorCode, e);
			if (logger.isError())
				logger.error(msg, e);
			throw new CommandException(msg);
		}
		return "success";
	}

	// public static void main(String args[]) throws Exception {
//	   test(String args[]);
//}

//private static void test(String args[]) throws Exception {
//        
//        String run = "";
//        if(args.length >0 )
//            run = args[0];
//        else
//            run = "START";    
//        
//        Properties prop = new Properties();
//        try{
//
//            prop.setProperty("RUN", run);
//            
//            Command command = new TestCallCommand();
//            command.setArgs(prop);
//            AgentUtil.broadcast(command);   
//        }catch(Exception e){
//        	System.out.println("error");
////            e.printStackTrace();
//        }
//    }

}
