package org.bench4q.web.api;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.bench4q.share.communication.HttpRequester.HttpResponse;
import org.bench4q.share.helper.ExceptionLog;
import org.bench4q.share.models.master.TestPlanResponseModel;
import org.bench4q.web.masterMessager.TestPlanMessager;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

@Controller
@SessionAttributes({ "accessToken" })
public class TestPlanHistoryController extends BaseController {

	private TestPlanMessager testPlanMessager;

	public TestPlanMessager getTestPlanMessager() {
		return testPlanMessager;
	}

	@Autowired
	public void setTestPlanMessager(TestPlanMessager testPlanMessager) {
		this.testPlanMessager = testPlanMessager;
	}

	@RequestMapping(value = "loadTestPlans", method = { RequestMethod.GET,
			RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> loadTestPlans(
			@ModelAttribute("accessToken") String accessToken) {
		Map<String, Object> map = new HashMap<String, Object>();
		TestPlanResponseModel testPlanResponseModel = this
				.getTestPlanMessager().loadTestPlans(accessToken);
		
		if (testPlanResponseModel == null || !testPlanResponseModel.isSuccess()) {
			return fail(map, "");

		} else {
			success(map);
			map.put("testPlans", testPlanResponseModel.getTestPlanDBModels());
			return map;
		}
	}

	@RequestMapping(value = "downloadReport", method = { RequestMethod.GET,
			RequestMethod.POST })
	public void downloadReport(HttpServletResponse response,
			@ModelAttribute("accessToken") String accessToken,
			@RequestParam String testPlanId) {
		HttpResponse httpResponse = this.getTestPlanMessager().loadReport(
				accessToken, testPlanId);
		if (httpResponse == null || httpResponse.getContent() == "") {
			return;
		}
		try {
			byte[] pdfBuffer = httpResponse.getContent().getBytes();
			ServletOutputStream outputStream = response.getOutputStream();
			response.reset();
			response.setHeader("Content-disposition", "attachment; filename=\""
					+ testPlanId.toString() + ".pdf\"");
			response.addHeader("Content-Length", "" + pdfBuffer.length);
			response.setContentType("application/pdf");
			outputStream.write(pdfBuffer);
			outputStream.flush();
			outputStream.close();
		} catch (Exception e) {
			Logger.getLogger(this.getClass()).info(
					ExceptionLog.getStackTrace(e));
			return;
		}

	}

	@RequestMapping(value = "deleteTest", method = { RequestMethod.GET,
			RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> deleteTest(HttpServletResponse response,
			@ModelAttribute("accessToken") String accessToken,
			@RequestParam String testPlanId) {
		Map<String, Object> map = new HashMap<String, Object>();
		TestPlanResponseModel testPlanResponseModel = this
				.getTestPlanMessager().deleteTestPlan(accessToken, testPlanId);
		if (testPlanResponseModel == null || !testPlanResponseModel.isSuccess()) {
			return fail(map, "");
		} else {
			return success(map);
		}

	}
}
