package org.bench4q.web.api;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bench4q.share.models.master.MonitorMemoryResponseModel;
import org.bench4q.share.models.master.MonitorNetworkReponseModel;
import org.bench4q.share.models.master.MonitorPhysicalDiskResponseModel;
import org.bench4q.share.models.master.MonitorProcessorResponseModel;
import org.bench4q.web.masterMessager.MonitorMessager;
import org.bench4q.web.model.ResultModel;
import org.bench4q.web.service.MonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

@Controller
@SessionAttributes({ "accessToken" })
public class MonitorController extends BaseController {
	private MonitorMessager monitorMessager;

	private final String port = "5556";
	private MonitorService monitorService;

	public MonitorMessager getMonitorMessager() {
		return monitorMessager;
	}

	@Autowired
	public void setMonitorMessager(MonitorMessager monitorMessager) {
		this.monitorMessager = monitorMessager;
	}

	public MonitorService getMonitorService() {
		return monitorService;
	}

	@Autowired
	public void setMonitorService(MonitorService monitorService) {
		this.monitorService = monitorService;
	}

	@RequestMapping("/{testPlanId}/{ip}/cpu/{fieldName}/{startTime}")
	@ResponseBody
	public Map<String, Object> getCpuResult(
			@ModelAttribute("accessToken") String accessToken,
			@PathVariable("testPlanId") String testplanId,
			@PathVariable String ip, @PathVariable String fieldName,
			@PathVariable String startTime) {
		Map<String, Object> map = new HashMap<String, Object>();
		MonitorProcessorResponseModel monitorProcessorResponseModel = this
				.getMonitorMessager().porcessor(accessToken, testplanId, ip,
						port, startTime);
		if (monitorProcessorResponseModel == null) {
			return fail(map, SERVER_ERROR);
		}

		List<ResultModel> resultModels = this.getMonitorService()
				.extractMonitorResult(
						monitorProcessorResponseModel.getProcessorModels(),
						fieldName);

		if (resultModels == null) {
			return fail(map, SERVER_ERROR);
		} else {
			success(map);
			map.put("result", resultModels);
			return map;
		}
	}

	// @RequestMapping("/{testPlanId}/SUT/{ip}/cpu/{cpuInstance}/{fieldName}/{startTime}")
	// @ResponseBody
	// public Map<String, Object> getCpuResult(
	// @ModelAttribute("accessToken") String accessToken,
	// @PathVariable("testPlanId") String testplanId,
	// @PathVariable String ip, @PathVariable String port,
	// @PathVariable String cpuInstance, @PathVariable String fieldName,
	// @PathVariable String startTime) {
	// Map<String, Object> map = new HashMap<String, Object>();
	// MonitorProcessorResponseModel monitorProcessorResponseModel = this
	// .getMonitorMessager().porcessor(accessToken, testplanId, ip,
	// port, startTime);
	// if (monitorProcessorResponseModel == null) {
	// return fail(map, SERVER_ERROR);
	// }
	//
	// List<ResultModel> resultModels = this.getMonitorService()
	// .extractCpuResultModelList(
	// monitorProcessorResponseModel.getProcessorModels(),
	// cpuInstance, fieldName);
	// if (resultModels == null) {
	// return fail(map, SERVER_ERROR);
	// } else {
	// success(map);
	// map.put("result", resultModels);
	// return map;
	// }
	//
	// }

	@RequestMapping("/{testPlanId}/{ip}/memory/{fieldName}/{startTime}")
	@ResponseBody
	public Map<String, Object> getMemoryStatus(
			@ModelAttribute("accessToken") String accessToken,
			@PathVariable("testPlanId") String testPlanId,
			@PathVariable String ip, @PathVariable String fieldName,
			@PathVariable String startTime) {

		Map<String, Object> map = new HashMap<String, Object>();
		MonitorMemoryResponseModel monitorMemoryResponseModel = this
				.getMonitorMessager().memory(accessToken, testPlanId, ip, port,
						startTime);
		if (monitorMemoryResponseModel == null) {
			return fail(map, SERVER_ERROR);
		}
		List<ResultModel> resultModels = this
				.getMonitorService()
				.extractMonitorResult(
						monitorMemoryResponseModel.getMemoryModels(), fieldName);
		if (resultModels == null) {
			return fail(map, SERVER_ERROR);
		} else {
			map = success(map);
			map.put("result", resultModels);
			return map;
		}
	}

	@RequestMapping("/{testPlanId}/{ip}/physicalDisk/{fieldName}/{startTime}")
	@ResponseBody
	public Map<String, Object> getPhysicalDisk(
			@ModelAttribute("accessToken") String accessToken,
			@PathVariable("testPlanId") String testPlanId,
			@PathVariable String ip, @PathVariable String fieldName,
			@PathVariable String startTime) {
		Map<String, Object> map = new HashMap<String,Object>();
		MonitorPhysicalDiskResponseModel monitorPhysicalDiskResponseModel = this
				.getMonitorMessager().physicalDisk(accessToken, testPlanId, ip,
						port, startTime);
		if (monitorPhysicalDiskResponseModel == null) {
			return fail(map, SERVER_ERROR);
		}
		List<ResultModel> resultModels = this
				.getMonitorService()
				.extractMonitorResult(
						monitorPhysicalDiskResponseModel
								.getPhysicalDiskModels(),
						fieldName);
		if (resultModels == null) {
			return fail(map, "");
		} else {
			map = success(map);
			map.put("result", resultModels);
			return map;
		}
	}

	@RequestMapping("/{testPlanId}/{ip}/network/{fieldName}/{startTime}")
	@ResponseBody
	public Map<String, Object> getNetworkStaus(
			@ModelAttribute("accessToken") String accessToken,
			@PathVariable("testPlanId") String testPlanId,
			@PathVariable String ip, @PathVariable String fieldName,
			@PathVariable String startTime) {
		Map<String, Object> map = new HashMap<String,Object>();
		MonitorNetworkReponseModel monitorNetworkReponseModel = this
				.getMonitorMessager().network(accessToken, testPlanId, ip,
						port, startTime);

		if (monitorNetworkReponseModel == null) {
			return fail(map, SERVER_ERROR);
		}
		List<ResultModel> resultModels = this.getMonitorService()
				.extractMonitorResult(monitorNetworkReponseModel.getModels(),
						fieldName);
		if (resultModels == null) {
			return fail(map, SERVER_ERROR);
		} else {
			map = success(map);
			map.put("result", resultModels);
			return map;
		}

	}
}
