package org.bench4q.master.api;

import java.util.UUID;

import org.bench4q.master.domain.service.MonitorResultService;
import org.bench4q.master.domain.service.UserService;
import org.bench4q.master.exception.Bench4QException;
import org.bench4q.share.models.master.MonitorPhysicalDiskResponseModel;
import org.bench4q.share.models.master.MonitorMemoryResponseModel;
import org.bench4q.share.models.master.MonitorNetworkReponseModel;
import org.bench4q.share.models.master.MonitorProcessorResponseModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
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(value = "/monitorController")
public class MonitorController extends BaseController {
	private MonitorResultService monitorResultService;

	private MonitorResultService getMonitorResultService() {
		return monitorResultService;
	}

	@Autowired
	private void setMonitorResultService(
			MonitorResultService monitorResultService) {
		this.monitorResultService = monitorResultService;
	}

	@RequestMapping(value = "/logicDiskMonitorSUTInfo/{testPlanRunId}/{hostName}/{port}/{duationBegin}", method = RequestMethod.GET)
	@ResponseBody
	public MonitorPhysicalDiskResponseModel logicDiskMonitorSUTInfo(
			@PathVariable UUID testPlanRunId, @PathVariable String hostName,
			@PathVariable int port, @PathVariable long duationBegin)
			throws Bench4QException {
		if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) {
			throw new Bench4QException(400 + "", "not permitted",
					"/{testPlanRunId}/script/{scriptId}/{fieldName}");
		}
		MonitorPhysicalDiskResponseModel ret = this.getMonitorResultService()
				.physicalDiskDiskResults(testPlanRunId, hostName, port,
						duationBegin);
		return ret == null ? new MonitorPhysicalDiskResponseModel() : ret;
	}

	@RequestMapping(value = "/memorySUTInfo", method = { RequestMethod.GET,
			RequestMethod.POST })
	@ResponseBody
	public MonitorMemoryResponseModel memorySUTInfo(
			@RequestParam UUID testPlanRunId, @RequestParam String hostName,
			@RequestParam int port, @RequestParam long duationBegin)
			throws Bench4QException {
		if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) {
			throw new Bench4QException(400 + "", "not permitted",
					"/memorySUTInfo");
		}
		MonitorMemoryResponseModel ret = this.getMonitorResultService()
				.loadMemoryModels(testPlanRunId, hostName, port, duationBegin);
		return ret == null ? new MonitorMemoryResponseModel() : ret;
	}

	@RequestMapping(value = "/processorSUTInfo", method = { RequestMethod.GET })
	@ResponseBody
	public MonitorProcessorResponseModel processorSUTInfo(
			@RequestParam UUID testPlanRunId, @RequestParam String hostName,
			@RequestParam int port, @RequestParam long duationBegin)
			throws Bench4QException {
		if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) {
			throw new Bench4QException(400 + "", "not permitted",
					"/processorSUTInfo");
		}
		MonitorProcessorResponseModel ret = this.getMonitorResultService()
				.loadProcessorModels(testPlanRunId, hostName, port,
						duationBegin);
		return ret == null ? new MonitorProcessorResponseModel() : ret;
	}

	@RequestMapping(value = "/networkInfo", method = { RequestMethod.GET })
	@ResponseBody
	public MonitorNetworkReponseModel networkInfo(
			@RequestParam UUID testPlanRunId, @RequestParam String hostName,
			@RequestParam int port, @RequestParam long duationBegin)
			throws Bench4QException {
		if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) {
			throw new Bench4QException(400 + "", "not permitted",
					"/networkInfo");
		}
		MonitorNetworkReponseModel ret = this.getMonitorResultService()
				.loadNetworkInterfaceModels(testPlanRunId, hostName, port,
						duationBegin);
		return ret == null ? new MonitorNetworkReponseModel() : ret;
	}
}
