package org.bench4q.master.domain.factory;

import org.bench4q.master.domain.RunningAgentInterface;
import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.domain.entity.Monitor;
import org.bench4q.master.domain.entity.PlanedConfig;
import org.bench4q.master.domain.entity.Port;
import org.bench4q.master.domain.entity.Script;
import org.bench4q.master.domain.entity.TestPlan;
import org.bench4q.master.domain.entity.User;
import org.bench4q.share.models.master.AgentModel;
import org.bench4q.share.models.master.MonitorModel;
import org.bench4q.share.models.master.PortModel;
import org.bench4q.share.models.master.RunningAgentModel;
import org.bench4q.share.models.master.ScriptModel;
import org.bench4q.share.models.master.TestPlanDBModel;
import org.bench4q.share.models.master.TestScriptConfig;
import org.bench4q.share.models.master.UserModel;
import org.springframework.stereotype.Component;

@Component
public class BusinessModelMapFactory {

	public static Agent toBusiness(AgentModel agentModel) {
		return Agent.createAgentWithoutId(agentModel.getHostName(),
				agentModel.getPort(), agentModel.getMaxLoad());
	}

	public static AgentModel toModel(Agent agent) {
		AgentModel agentModel = new AgentModel();
		agentModel.setCurrentEnumStatus(agent.getCurrentEnumStatus());
		agentModel.setHostName(agent.getHostName());
		agentModel.setId(agent.getId());
		agentModel.setMaxLoad(agent.getMaxLoad());
		agentModel.setPort(agent.getPort());
		agentModel.setRemainLoad(agent.getRemainLoad());
		return agentModel;
	}

	public static RunningAgentModel toModel(RunningAgentInterface runningAgent) {
		RunningAgentModel ret = new RunningAgentModel();
		ret.setAgent(BusinessModelMapFactory.toModel(runningAgent.getAgent()));
		ret.setAgentRunId(runningAgent.getAgentRunId());
		ret.setLoadInUse(runningAgent.getLoadInUse());
		ret.setScriptId(runningAgent.getScriptId());
		return ret;
	}

	public TestScriptConfig toModel(PlanedConfig config) {
		TestScriptConfig result = new TestScriptConfig();
		result.setWarmUp(config.getWarmUp());
		result.setExecuteRange(config.getExecuteRange());
		result.setCoolDown(config.getCoolDown());
		return result;
	}

	public static Monitor toBusiness(MonitorModel monitorModel) {
		Monitor monitor = new Monitor();
		monitor.setHostName(monitorModel.getHostName());
		monitor.setPort(monitorModel.getPort());
		return monitor;
	}

	public static MonitorModel toModel(Monitor monitorInBusiness) {
		MonitorModel ret = new MonitorModel();
		ret.setHostName(monitorInBusiness.getHostName());
		ret.setPort(monitorInBusiness.getPort());
		return ret;
	}

	public TestPlanDBModel toModel(TestPlan testPlanDB) {
		TestPlanDBModel ret = new TestPlanDBModel();
		ret.setCreateDateTime(testPlanDB.getCreateDateTime());
		ret.setCurrentStatus(testPlanDB.getCurrentStatus());
		ret.setFailTimes(testPlanDB.getFailTimes());
		ret.setId(testPlanDB.getId());
		ret.setName(testPlanDB.getName());
		ret.setTestPlanRunId(testPlanDB.getTestPlanRunId());
		ret.setUserModel(toModel(testPlanDB.getUser()));
		return ret;
	}

	public static UserModel toModel(User user) {
		UserModel ret = new UserModel();
		ret.setId(user.getId());
		ret.setPassword(user.getPassword());
		ret.setUserName(user.getUserName());
		ret.setScope(user.getScope());
		return ret;
	}

	public static PortModel toModel(Port port) {
		PortModel ret = new PortModel();
		ret.setId(port.getId());
		ret.setInUse(port.isInUse());
		ret.setPort(port.getPort());
		return ret;
	}

	public static Port toBusiness(PortModel portModel) {
		Port ret = new Port();
		ret.setId(portModel.getId());
		ret.setInUse(portModel.isInUse());
		ret.setPort(portModel.getPort());
		return ret;
	}

	public static ScriptModel toModel(Script script) {
		ScriptModel ret = new ScriptModel();
		ret.setBehaviorCount(script.getBehaviorCount());
		ret.setCreateDateTime(script.getCreateDateTime());
		ret.setId(script.getId());
		ret.setName(script.getName());
		ret.setScriptContent(script.getScriptContent());
		return ret;
	}
}
