package com.eactive.eai.agent.command;

import java.lang.reflect.Constructor;

import com.eactive.eai.rms.onl.common.exception.EMSRuntimeException;
import com.eactive.eai.rms.onl.common.service.AgentUtilService;

import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

@SuperBuilder
@NoArgsConstructor
public class CommonCommand extends Command {

	private static final long serialVersionUID = 1L;

	public static final String RELOAD_EAI_MESSAGE_COMMAND = "com.eactive.eai.agent.eaimessage.ReloadEAIMessageCommand";

	public static final String RELOAD_TRANSFORM_COMMAND = "com.eactive.eai.agent.transformer.ReloadTransformCommand";
	public static final String REMOVE_TRANSFORM_COMMAND = "com.eactive.eai.agent.transformer.RemoveTransformCommand";

	public static final String RELOAD_TRANSFORM_LIST_COMMAND = "com.eactive.eai.agent.transformer.ReloadTransformListCommand";

	public static final String RELOAD_MESSAGE_KEY_COMMAND = "com.eactive.eai.agent.messagekey.ReloadMessageKeyCommand";
	public static final String REMOVE_MESSAGE_KEY_COMMAND = "com.eactive.eai.agent.messagekey.RemoveMessageKeyCommand";
	
	public static final String RELOAD_EAI_SERVER_COMMAND = "com.eactive.eai.agent.eaiserver.ReloadEAIServerCommand";
	public static final String REMOVE_EAI_SERVER_COMMAND = "com.eactive.eai.agent.eaiserver.RemoveEAIServerCommand";
	
	public static final String RELOAD_SUB_MESSAGE_COMMAND = "com.ext.eai.agent.submessage.ReloadSubMessageCommand";
	public static final String REMOVE_SUB_MESSAGE_COMMAND = "com.ext.eai.agent.submessage.RemoveSubMessageCommand";
	
	public static final String RELOAD_CLIENT_COMMAND = "com.eactive.eai.agent.authserver.ReloadClientCommand";
	public static final String RELOAD_API_SCOPE_COMMAND = "com.eactive.eai.agent.authserver.ReloadApiScopeCommand";
	
	public static final String RELOAD_ERROR_CODE_COMMAND = "com.eactive.eai.agent.errorcode.ReloadErrorCodeCommand";
	public static final String REMOVE_ERROR_CODE_COMMAND = "com.eactive.eai.agent.errorcode.RemoveErrorCodeCommand";
	
	
	public CommonCommand(String name, Object args) {
		this.name = name;
		this.args = args;
	}

	public CommonCommand(String name) {
		this.name = name;
	}

	public Object execute() throws CommandException {
		if (name.indexOf(".") >= 0) {
			try {
				@SuppressWarnings("rawtypes")
				Class cl = Class.forName(this.name);
				@SuppressWarnings({ "rawtypes", "unchecked" })
				Constructor constructor = cl.getConstructor();
				Object obj = constructor.newInstance();
				Command c = (Command) obj;
				c.setArgs(args);
				return c.execute();
			} catch (Exception e) {
				String rspErrorCode = "RECEAIMCM002";
				String msg = makeException(rspErrorCode, null);
				throw new CommandException(msg);
			}
		} else {
			String rspErrorCode = "RECEAIMCM001";
			String msg = makeException(rspErrorCode, null);
			throw new CommandException(msg);
		}
	}

	public CommandResult broadcast(AgentUtilService agentUtilService) throws EMSRuntimeException {
		try {
			return new CommandResult(agentUtilService.broadcast(this));
		} catch (Exception e) {
			throw new EMSRuntimeException(e);
		}
	}

}
