package org.bench4q.master.domain.factory;

import java.util.UUID;

import org.bench4q.master.domain.RunningAgentInterface;
import org.bench4q.master.domain.RunningScriptInterface;
import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.domain.entity.RunningAgentDB;
import org.bench4q.master.domain.entity.TestPlanScript;
import org.bench4q.master.domain.repository.AgentRepository;
import org.bench4q.master.infrastructure.communication.AgentMessenger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RunningAgentFactory {

	private AgentMessenger agentMessenger;

	private AgentRepository agentRepository;

	private AgentMessenger getAgentMessenger() {
		return agentMessenger;
	}

	@Autowired
	private void setAgentMessenger(AgentMessenger agentMessenger) {
		this.agentMessenger = agentMessenger;
	}

	private AgentRepository getAgentRepository() {
		return agentRepository;
	}

	@Autowired
	private void setAgentRepository(AgentRepository agentRepository) {
		this.agentRepository = agentRepository;
	}

	public RunningAgentInterface buildRunningAgentDBWithoutId(Agent agent,
			int loadInUse, RunningScriptInterface runningScript, UUID agentRunId) {
		RunningAgentDB runningAgent = RunningAgentDB
				.buildRunningAgentDBWithoutId(agent, loadInUse,
						(TestPlanScript) runningScript, agentRunId);
		this.convertToDomain(runningAgent);
		return runningAgent;
	}

	public RunningAgentInterface buildRunningAgentWithOutIdAndRunningScript(
			Agent agent, int loadInUse, UUID agentRunId) {
		RunningAgentDB runningAgent = RunningAgentDB
				.buildRunningAgentDBWithoutIdAndRunningScript(agent, loadInUse,
						agentRunId);
		this.convertToDomain(runningAgent);
		return runningAgent;
	}

	public RunningAgentInterface fillRunningScript(
			RunningAgentInterface target, RunningScriptInterface runningScript) {
		return target.completeWithRunningScript(runningScript);
	}

	public void convertToDomain(RunningAgentDB fromReop) {
		fromReop.setAgentMessenger(this.getAgentMessenger());
		fromReop.setAgentRepository(this.getAgentRepository());
	}
}
