package org.bench4q.agent.scenario.behavior;

import java.util.Map;

import org.bench4q.agent.datacollector.DataCollector;
import org.bench4q.agent.datacollector.impl.BehaviorStatusCodeResult;
import org.bench4q.agent.scenario.Parameter;
import org.bench4q.share.models.agent.ParameterModel;
import org.bench4q.share.models.agent.scriptrecord.BehaviorModel;

public abstract class Behavior {
	private static final String CONTROL_BEHAVIOR = "CONTROLBEHAVIOR";
	private static final String USER_BEHAVIOR = "USERBEHAVIOR";
	private static final String TIMER_BEHAVIOR = "TIMERBEHAVIOR";
	private static final String TEST_BEHAVIOR = "TESTBEHAVIOR";

	private int id;
	private String use;
	private String name;
	private Parameter[] parameters;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUse() {
		return use;
	}

	public void setUse(String use) {
		this.use = use;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Parameter[] getParameters() {
		return parameters;
	}

	public void setParameters(Parameter[] parameters) {
		this.parameters = parameters;
	}

	public abstract boolean shouldBeCount();

	public abstract Map<Integer, BehaviorStatusCodeResult> getBehaviorBriefResult(
			DataCollector dataStatistics);

	public String getSpecificParamValue(String paramName) {
		for (Parameter parameter : this.getParameters()) {
			if (parameter.getKey().equalsIgnoreCase(paramName)) {
				return parameter.getValue();
			}
		}
		return "";
	}

	public void compile() {
		for (Parameter parameter : this.getParameters()) {
			parameter.compileToGenerateParts();
		}
	}

	public static Behavior buildWith(BehaviorModel behaviorModel) {
		Behavior behavior = null;
		if (behaviorModel.getType().equalsIgnoreCase(TIMER_BEHAVIOR)) {
			behavior = new TimerBehavior();
		} else if (behaviorModel.getType().equalsIgnoreCase(USER_BEHAVIOR)) {
			behavior = new UserBehavior();
		} else if (behaviorModel.getType().equalsIgnoreCase(CONTROL_BEHAVIOR)) {
			behavior = new ControlBehavior();
		} else if (behaviorModel.getType().equalsIgnoreCase(TEST_BEHAVIOR)) {
			behavior = new TestBehavior();
		} else {
			throw new IllegalArgumentException(
					"The input BehaviorModel's type is not proper");
		}
		behavior.setName(behaviorModel.getName());
		behavior.setUse(behaviorModel.getUse());
		behavior.setId(behaviorModel.getId());
		behavior.setParameters(new Parameter[behaviorModel.getParameters()
				.size()]);

		for (int k = 0; k < behaviorModel.getParameters().size(); k++) {
			ParameterModel parameterModel = behaviorModel.getParameters()
					.get(k);
			behavior.getParameters()[k] = new Parameter(
					parameterModel.getKey(), parameterModel.getValue());
		}
		return behavior;
	}
}
