package org.bench4q.web.masterMessager;

import java.util.HashMap;
import java.util.Map;
import org.bench4q.share.communication.HttpRequester.HttpResponse;
import org.bench4q.share.helper.MarshalHelper;
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.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Component
public class MonitorMessager extends MasterMessager {
	public MonitorMessager() {
		super(MasterAddressManamger.getMasterAddress() + "/monitorController");
	}

	@RequestMapping(value = "/memorySUTInfo", method = { RequestMethod.GET,
			RequestMethod.POST })
	public MonitorMemoryResponseModel memory(String accessToken,
			String testPlanRunId, String hostName, String port,
			String duationBegin) {
		String url = this.getBaseUrl() + "/memorySUTInfo";
		Map<String, String> params = createParmsMap(testPlanRunId, hostName,
				port, duationBegin);
		HttpResponse httpResponse = null;
		try {
			httpResponse = this.getHttpRequester().sendPost(url, params,
					makeAccessTockenMap(accessToken));
			if (!validateHttpResponse(httpResponse)) {
				handleInvalidatedResponse(url);
				return null;
			}

			MonitorMemoryResponseModel memoryResponseModel = (MonitorMemoryResponseModel) MarshalHelper
					.unmarshal(MonitorMemoryResponseModel.class,
							httpResponse.getContent());
			return memoryResponseModel;

		} catch (Exception e) {
			this.handleException(httpResponse, e);
			return null;
		}
	}

	public MonitorProcessorResponseModel porcessor(String accessToken,
			String testPlanRunId, String hostName, String port,
			String duationBegin) {

		String url = this.baseUrl + "/processorSUTInfo";
		Map<String, String> params = createParmsMap(testPlanRunId, hostName,
				port, duationBegin);
		HttpResponse httpResponse = null;
		try {
			httpResponse = this.getHttpRequester().sendGet(url, params,
					makeAccessTockenMap(accessToken));
			if (!validateHttpResponse(httpResponse)) {
				return null;

			}
			MonitorProcessorResponseModel monitorProcessorResponseModel = (MonitorProcessorResponseModel) MarshalHelper
					.unmarshal(MonitorProcessorResponseModel.class,
							httpResponse.getContent());
			return monitorProcessorResponseModel;
		} catch (Exception e) {
			this.handleException(httpResponse, e);
			return null;
		}
	}

	public MonitorNetworkReponseModel network(String accessToken,
			String testPlanRunId, String hostName, String port,
			String duationBegin) {
		String url = this.baseUrl + "/networkInfo";
		Map<String, String> params = createParmsMap(testPlanRunId, hostName,
				port, duationBegin);
		HttpResponse httpResponse = null;
		try {
			httpResponse = this.getHttpRequester().sendGet(url, params,
					makeAccessTockenMap(accessToken));
			if (!validateHttpResponse(httpResponse)) {
				handleInvalidatedResponse(url);
				return null;
			}
			MonitorNetworkReponseModel networkInterfaceModel = (MonitorNetworkReponseModel) MarshalHelper
					.unmarshal(MonitorNetworkReponseModel.class,
							httpResponse.getContent());
			return networkInterfaceModel;

		} catch (Exception e) {
			this.handleException(httpResponse, e);
			return null;
		}
	}

	public MonitorPhysicalDiskResponseModel physicalDisk(String accessToken,
			String testPlanRunId, String hostName, String port,
			String duationBegin) {
		String url = this.baseUrl + "/logicDiskMonitorSUTInfo" + "/"
				+ testPlanRunId + "/" + hostName + "/" + port + "/"
				+ duationBegin;
		HttpResponse httpResponse = null;
		try {
			httpResponse = this.getHttpRequester().sendGet(url, null,
					makeAccessTockenMap(accessToken));
			if (!validateHttpResponse(httpResponse)) {
				this.handleInvalidatedResponse(url);
				return null;
			}
			MonitorPhysicalDiskResponseModel monitorPhysicalDiskResponseModel = (MonitorPhysicalDiskResponseModel) MarshalHelper
					.unmarshal(MonitorPhysicalDiskResponseModel.class,
							httpResponse.getContent());
			return monitorPhysicalDiskResponseModel;
		} catch (Exception e) {
			this.handleException(httpResponse, e);
			return null;
		}
	}

	private Map<String, String> createParmsMap(String testPlanRunId,
			String hostName, String port, String duationBegin) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("testPlanRunId", testPlanRunId);
		params.put("hostName", hostName);
		params.put("port", port);
		params.put("duationBegin", duationBegin);
		return params;

	}

}
