package org.bench4q.master.api;

import java.util.ArrayList;
import java.util.List;

import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.domain.factory.BusinessModelMapFactory;
import org.bench4q.master.domain.service.AgentService;
import org.bench4q.master.domain.service.UserService;
import org.bench4q.share.models.master.AgentModel;
import org.bench4q.share.models.master.AgentResponseModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/agentManage")
public class AgentController extends BaseController {
	private AgentService agentService;

	public AgentService getAgentService() {
		return agentService;
	}

	@Autowired
	private void setAgentService(AgentService agentPoolService) {
		this.agentService = agentPoolService;
	}

	@RequestMapping(value = "/addAgentToPool", method = RequestMethod.POST)
	@ResponseBody
	public AgentResponseModel addAgentToPool(@RequestBody AgentModel agent) {
		if (!this.checkScope(UserService.SUPER_AUTHENTICATION)) {
			return setAgentResponseModel(false,
					"you don't have the power to add agent to pool!",
					new ArrayList<AgentModel>());
		}
		if (!this.getAgentService().addAgentToPool(
				BusinessModelMapFactory.toBusiness(agent))) {
			return setAgentResponseModel(false,
					"add agent to DB fails in addAgentToPool",
					new ArrayList<AgentModel>());
		}
		return setAgentResponseModel(true, "", new ArrayList<AgentModel>());
	}

	@RequestMapping(value = "/removeAgentFromPool", method = RequestMethod.GET)
	@ResponseBody
	public AgentResponseModel removeAgentFromPool(@RequestParam int agentId,
			@RequestParam String hostName) {

		if (!this.checkScope(UserService.SUPER_AUTHENTICATION)) {
			return setAgentResponseModel(false,
					"you don't have the power to remove agent from pool!",
					new ArrayList<AgentModel>());
		}
		if (!this.getAgentService().removeAgentFromPool(agentId)) {
			return setAgentResponseModel(false,
					"remove agent from DB fails in removeAgentFromPool",
					new ArrayList<AgentModel>());
		}
		return this
				.setAgentResponseModel(true, "", new ArrayList<AgentModel>());
	}

	@RequestMapping(value = "/queryAgentList", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public AgentResponseModel queryAgentList() {
		if (!this.checkScope(UserService.SUPER_AUTHENTICATION)) {
			return setAgentResponseModel(false,
					"you don't have the power to get agent from pool!",
					new ArrayList<AgentModel>());
		}
		List<Agent> list = this.getAgentService().loadAgentPoolFromDB();
		List<AgentModel> modelList = new ArrayList<AgentModel>();
		for (Agent agent : list) {
			modelList.add(BusinessModelMapFactory.toModel(agent));
		}
		return setAgentResponseModel(true, "", modelList);
	}

	private AgentResponseModel setAgentResponseModel(boolean isSuccess,
			String failCauseString, List<AgentModel> agents) {
		AgentResponseModel agentResponseModel = new AgentResponseModel();
		agentResponseModel.setSuccess(isSuccess);
		agentResponseModel.setFailCauseString(failCauseString);
		agentResponseModel.setAgents(agents);
		return agentResponseModel;
	}
}