package com.jasamedika.medifirst2000.controller;

import com.jasamedika.medifirst2000.constants.Constants;
import com.jasamedika.medifirst2000.constants.MessageResource;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.core.web.WebConstants;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.PengukuranSuhuService;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import com.jasamedika.medifirst2000.vo.PengukuranKebisinganVO;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

import static org.slf4j.LoggerFactory.getLogger;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

@RestController
@RequestMapping("/pengukuran-suhu")
public class PengukuranSuhuController extends LocaleController<PengukuranKebisinganVO> {

	private static final Logger LOGGER = getLogger(PengukuranSuhuController.class);

	@Autowired
	private PengukuranSuhuService service;

	@RequestMapping(value = "/save-pengukuran-suhu", method = POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> savePengukuranPencahayaan(@Valid @RequestBody PengukuranKebisinganVO vo,
			HttpServletRequest request) {
		try {
			vo.setMenutype("suhu");
			Map<String, Object> result = service.savePengukuranSuhu(vo);
			if (null != result)
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
						getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.CREATED, mapHeaderMessage);

		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when savePengukuranPencahayaan", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
			return RestUtil.getJsonHttpStatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when savePengukuranPencahayaan", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttpStatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-pengukuran-suhu", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getPengukuranKelembaban(HttpServletRequest request) {
		try {
			Map<String, Object> result = service.getPengukuranSuhu();
			if (null != result) {
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
						getMessage(MessageResource.LABEL_SUCCESS, request));
				return RestUtil.getJsonResponse(result, HttpStatus.OK, mapHeaderMessage);
			} else {
				return RestUtil.getJsonResponse(result, HttpStatus.NOT_FOUND, mapHeaderMessage);
			}
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when getPengukuranKebisingan", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
			return RestUtil.getJsonHttpStatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when getPengukuranKebisingan", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttpStatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping("/lapPengukuranSuhu")
	public ModelAndView lapPengukuranSuhu(ModelAndView m,
			@RequestParam(value = "format", required = false) String format,
			@RequestParam(value = "startDate", required = true) String startDate,
			@RequestParam(value = "endDate", required = true) String endDate) {

		Map<String, Object> result = this.service.getRepotingPengukurangSuhu(startDate, endDate);

		Map<String, Object> header = (Map<String, Object>) result.get("header");
		String periode = header.get("startDate") == null ? "" : (String) header.get("startDate");
		periode = periode + (header.get("endDate") == null ? "" : " sd " + (String) header.get("endDate"));
		m.addObject("dataSource", result.get("listData"));
		m.addObject("periode", periode);
		m.addObject("format", "pdf");
		if (format != null && !format.isEmpty()) {
			m.addObject("format", format);
		}

		return m;
	}
}
