package org.bench4q.master.domain.valueobject.datastatistics;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import javax.xml.bind.JAXBException;

import org.bench4q.master.domain.RunningAgentInterface;
import org.bench4q.share.models.agent.TestBriefStatusModel;
import org.bench4q.share.models.agent.statistics.AgentBehaviorsBriefModel;
import org.bench4q.share.models.agent.statistics.AgentBriefStatusModel;
import org.bench4q.share.models.agent.statistics.AgentPagesBriefModel;
import org.bench4q.share.models.agent.statistics.DataStatisticsModel;
import org.bench4q.share.models.master.statistics.ScriptBehaviorsBriefModel;
import org.bench4q.share.models.master.statistics.ScriptBriefResultModel;
import org.bench4q.share.models.master.statistics.ScriptPagesBriefModel;
import org.bench4q.share.models.master.statistics.ScriptResultModel;

public class RunningScriptSampler {
	private List<DataStatistics> dataStatisticsList;
	private List<RunningAgentInterface> runningAgents;

	private List<DataStatistics> getDataStatisticsList() {
		return dataStatisticsList;
	}

	private void setDataStatisticsList(List<DataStatistics> dataStatisticsList) {
		this.dataStatisticsList = dataStatisticsList;
	}

	public List<RunningAgentInterface> getRunningAgents() {
		return runningAgents;
	}

	private void setRunningAgents(List<RunningAgentInterface> runningAgents) {
		this.runningAgents = runningAgents;
	}

	private RunningScriptSampler() {
		this.setDataStatisticsList(new ArrayList<DataStatistics>());
		this.getDataStatisticsList().add(new ScriptBriefStatistics());
		this.getDataStatisticsList().add(new BehaviorsBriefStatistics());
		this.getDataStatisticsList().add(new PagesBriefStatistics());
		this.setRunningAgents(new LinkedList<RunningAgentInterface>());
	}

	public RunningScriptSampler(
			Collection<? extends RunningAgentInterface> runningAgents) {
		this();
		this.getRunningAgents().addAll(runningAgents);
	}

	public ScriptResultModel getResultModelFromAgent() throws JAXBException {
		for (RunningAgentInterface runningAgent : getRunningAgents()) {
			if (runningAgent.isBreakDown()) {
				continue;
			}
			TestBriefStatusModel testBriefStatusModel = runningAgent.briefAll();
			addScriptResultModelStatistics(testBriefStatusModel);
		}

		return getScriptResultModel();
	}

	private void addScriptResultModelStatistics(
			TestBriefStatusModel testBriefStatusModel) throws JAXBException {
		addAgentBriefStatusModel(testBriefStatusModel.getScenarioBriefModel());
		addScriptBehaviorsBriefModel(testBriefStatusModel
				.getBehaviorsBriefModel());
		addScriptPagesBriefModel(testBriefStatusModel.getPagesBriefModel());
	}

	private void addAgentBriefStatusModel(
			AgentBriefStatusModel agentBriefStatusModel) {
		if (agentBriefStatusModel != null)
			getScriptBriefStatistics().add(agentBriefStatusModel);

	}

	private void addScriptPagesBriefModel(
			AgentPagesBriefModel agentPagesBriefModel) {
		if (agentPagesBriefModel != null)
			this.getPagesBriefStatistics().add(agentPagesBriefModel);
	}

	private void addScriptBehaviorsBriefModel(
			AgentBehaviorsBriefModel agentBehaviorsBriefModel)
			throws JAXBException {
		if (agentBehaviorsBriefModel != null) {
			getBehaviorsBriefStatistics().add(agentBehaviorsBriefModel);
		}

	}

	private ScriptResultModel getScriptResultModel() {
		ScriptResultModel scriptResultModel = new ScriptResultModel();
		scriptResultModel
				.setScriptBehaviorsBriefModel((ScriptBehaviorsBriefModel) getBehaviorsBriefStatistics()
						.getStatistics());
		scriptResultModel
				.setScriptBriefResultModel((ScriptBriefResultModel) getScriptBriefStatistics()
						.getStatistics());
		scriptResultModel
				.setScriptPagesBriefModel((ScriptPagesBriefModel) getPagesBriefStatistics()
						.getStatistics());
		return scriptResultModel;
	}

	private DataStatistics getScriptBriefStatistics() {
		return getSpecificDataStatistics(ScriptBriefStatistics.class);
	}

	private DataStatistics getBehaviorsBriefStatistics() {
		return getSpecificDataStatistics(BehaviorsBriefStatistics.class);
	}

	public DataStatistics getPagesBriefStatistics() {
		return getSpecificDataStatistics(PagesBriefStatistics.class);
	}

	private DataStatistics getSpecificDataStatistics(
			Class<? extends DataStatistics> dataStatisticsClass) {
		for (DataStatistics dataStatistics : this.getDataStatisticsList()) {
			if (dataStatistics.getClass().equals(dataStatisticsClass)) {
				return dataStatistics;
			}
		}
		return new ScriptStatistics() {
			public void add(DataStatisticsModel dataUnit) {
			}

			public Object getStatistics() {
				return null;
			}
		};
	}
}
