package stubs;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.Future;

import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.infrastructure.communication.AgentMessenger;
import org.bench4q.share.enums.master.AgentStatus;
import org.bench4q.share.models.agent.BehaviorBriefModel;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.RunScenarioResultModel;
import org.bench4q.share.models.agent.ServerStatusModel;
import org.bench4q.share.models.agent.StopTestModel;
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.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

@Component
public class Mock_AgentMessenger implements AgentMessenger {

	private UUID testId = UUID.randomUUID();

	@Override
	public RunScenarioResultModel bookTest(Agent agent, int requireLoad) {
		return new RunScenarioResultModel(this.testId);
	}

	@Override
	public RunScenarioResultModel submitScenrioWithParams(Agent agent,
			UUID agentRunId, List<File> paramFiles,
			RunScenarioModel runScenarioModel, Date realStartTime) {
		return new RunScenarioResultModel(this.testId);
	}

	@Override
	public RunScenarioResultModel runWithParams(Agent agent, UUID agentRunId) {
		return new RunScenarioResultModel(this.testId);
	}

	@Override
	public TestBriefStatusModel scriptBriefAll(Agent agent, UUID runId) {
		if (isDead(agent)) {
			return null;
		}
		TestBriefStatusModel result = new TestBriefStatusModel();
		AgentBriefStatusModel scenarioBriefModel = new AgentBriefStatusModel();
		scenarioBriefModel.setSuccessCountFromBegin(1000);
		result.setScenarioBriefModel(scenarioBriefModel);
		result.setPagesBriefModel(new AgentPagesBriefModel());
		AgentBehaviorsBriefModel behaviorsBriefModel = new AgentBehaviorsBriefModel();
		behaviorsBriefModel
				.setBehaviorBriefModels(new ArrayList<BehaviorBriefModel>());
		result.setBehaviorsBriefModel(behaviorsBriefModel);
		return result;
	}

	private boolean isDead(Agent agent) {
		return agent.getCurrentEnumStatus() == AgentStatus.BreakDown;
	}

	@Override
	public StopTestModel stop(Agent agent, UUID runId) {
		// if (runId == this.testId) {
		// return new StopTestModel(true);
		// }
		return new StopTestModel(true);
	}

	@Override
	public ServerStatusModel getStatus(Agent agent) {
		ServerStatusModel result = new ServerStatusModel();
		if (agent.getCurrentEnumStatus() == AgentStatus.InIdle) {
			return result;
		} else if (agent.getCurrentEnumStatus() == AgentStatus.BreakDown) {
			return null;
		} else if (agent.getCurrentEnumStatus() == AgentStatus.InRunning) {
			result.getRunningTests().add(this.testId);
			return result;
		}
		return null;
	}

	@Override
	public Future<ServerStatusModel> getStatusAsync(Agent agent) {
		return new AsyncResult<ServerStatusModel>(getStatus(agent));
	}
}
