package com.jasamedika.medifirst2000.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
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.RequestBody;
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.RestController;
import org.springframework.web.servlet.ModelAndView;

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.entities.Ruangan;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.BakuMutuService;
import com.jasamedika.medifirst2000.service.HasilPemeriksaanSwaPantauLimbahCairService;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import com.jasamedika.medifirst2000.vo.BakuMutuVO;
import com.jasamedika.medifirst2000.vo.HasilPemeriksaanSwaPantauLimbahCairVO;

import net.kaczmarzyk.spring.data.jpa.domain.LessThanOrEqual;
import net.kaczmarzyk.spring.data.jpa.web.annotation.Spec;

@RestController
@RequestMapping("/hasil-pemeriksaan-swa-pantau-limbah-cair")
public class HasilPemeriksaanSwaPantauLimbahCairController extends LocaleController<HasilPemeriksaanSwaPantauLimbahCairVO> {
	
	@Autowired
	private HasilPemeriksaanSwaPantauLimbahCairService hasilPemeriksaanSwaPantauLimbahCairService;
	
	private static final Logger LOGGER = LoggerFactory
			.getLogger(HasilPemeriksaanSwaPantauLimbahCairController.class);
	
	@RequestMapping(value = "/save-hasil-pemeriksaan-swa-pantau-limbah-cair/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String,Object>> addVO(@Valid @RequestBody HasilPemeriksaanSwaPantauLimbahCairVO vo,HttpServletRequest request) {
		
		try{
			Map<String, Object> result = hasilPemeriksaanSwaPantauLimbahCairService.addHasilPemeriksaanSwaPantauLimbahCair(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 add hasilPemeriksaanSwaPantauLimbahCair", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE,
					e.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR,
					mapHeaderMessage);
			
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when add hasilPemeriksaanSwaPantauLimbahCair", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE,
					jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT,
					mapHeaderMessage);
		}
	}
	
	@RequestMapping(value = "/find-all-hasil-pemeriksaan-swa-pantau-limbah-cair/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	 public ResponseEntity<Map<String,Object>> getAllVO(HttpServletRequest request) {
	  
		  try {
			   Map<String,Object> result=hasilPemeriksaanSwaPantauLimbahCairService.findAllhasilPemeriksaanSwaPantauLimbahCair();
			   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 get all hasilPemeriksaanSwaPantauLimbahCair", e.getMessage());
			   addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE,
			     e.getMessage());
			   return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR,
			     mapHeaderMessage);
		  } catch (JpaSystemException jse) {
			   LOGGER.error("Got exception {} when get all hasilPemeriksaanSwaPantauLimbahCair", jse.getMessage());
			   addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE,
			     jse.getMessage());
			   return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT,
			     mapHeaderMessage);
		  }
	 }
	
	@RequestMapping(value = "/delete-hasil-pemeriksaan-swa-pantau-limbah-cair", method = RequestMethod.GET)
	public ResponseEntity<String> deleteNamaBahan(@RequestParam(value = "noRec", required = true) String noRec) {
		
		try {
			if (hasilPemeriksaanSwaPantauLimbahCairService.deleteHasilPemeriksaanSwaPantauLimbahCair(noRec) == true)
				return RestUtil.getJsonResponse(noRec +" Dihapus", HttpStatus.CREATED);

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

		return RestUtil.getJsonHttptatus(HttpStatus.NOT_ACCEPTABLE);
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/list-hasil-pemeriksaan-limbah-cair")
    @ResponseBody
    public Map<String,Object>   listHasilPemeriksaanLimbahCair(
    		@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "take", required = false, defaultValue = "100") Integer limit,
			@RequestParam(value = "sort", required = false, defaultValue = "noRec") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@RequestParam(value = "dateStart", required = false) String dateStart,
			@RequestParam(value = "dateEnd", required = false) String dateEnd) {

		Map<String, Object> resultPageMap = hasilPemeriksaanSwaPantauLimbahCairService.listHasilPemeriksaanSwaLimbahCair(page, limit, sort, dir, dateStart, dateEnd);
		return resultPageMap;
    }
	
	
	
	@RequestMapping(value = "/get-inlet-meter-lalu/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	 public ResponseEntity<Map<String,Double>> getInletMeterLalu(HttpServletRequest request) {
	  
		  try {
			   Map<String,Double> result=hasilPemeriksaanSwaPantauLimbahCairService.getInletMeterAirLalu();
			   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 get inlet", e.getMessage());
			   addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE,
			     e.getMessage());
			   return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR,
			     mapHeaderMessage);
		  } catch (JpaSystemException jse) {
			   LOGGER.error("Got exception {} when get all hasilPemeriksaanSwaPantauLimbahCair", jse.getMessage());
			   addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE,
			     jse.getMessage());
			   return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT,
			     mapHeaderMessage);
		  }
	 }
	
	@RequestMapping(value = "/get-outlet-meter-lalu/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	 public ResponseEntity<Map<String,Double>> getOutletMeterLalu(HttpServletRequest request) {
	  
		  try {
			   Map<String,Double> result=hasilPemeriksaanSwaPantauLimbahCairService.getOutletMeterAirLalu();
			   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 get outlet", e.getMessage());
			   addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE,
			     e.getMessage());
			   return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR,
			     mapHeaderMessage);
		  } catch (JpaSystemException jse) {
			   LOGGER.error("Got exception {} when get all hasilPemeriksaanSwaPantauLimbahCair", jse.getMessage());
			   addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE,
			     jse.getMessage());
			   return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT,
			     mapHeaderMessage);
		  }
	 }
	
	@RequestMapping(value = "/hasil-pemeriksaan-limbah-cair-detail")
	public Map<String,Object>  hasilPemeriksaanLimbahCairDetail(
			@RequestParam(value = "noRec", required = true) String noRec) {

		Map<String, Object> resultPageMap = hasilPemeriksaanSwaPantauLimbahCairService.hasilPemeriksaanLimbahCairDetail(noRec);
		
		return resultPageMap;
	}

	@RequestMapping(value = "/get-hasil-pemeriksaan-pantau-limbah-cair")
	public Map<String,Object>  getHasilPemeriksaanPantauLimbahCair(
			@RequestParam(value = "startDate", required = true) String startDate, @RequestParam(value="endDate", required=true)String endDate) {

		Map<String, Object> resultPageMap = this.hasilPemeriksaanSwaPantauLimbahCairService.getHasilPemeriksaanPantauLImbahCair(startDate, endDate);
		
		return resultPageMap;
	}
	
	@RequestMapping("/lapHasilPemeriksaanSWAPantauLimbahCair")
	public ModelAndView generateLapHasilPemeriksaanSWAPantauLimbahCair(ModelAndView m,
			@RequestParam(value = "format", required = false) String format,
			@RequestParam(value = "startDate", required = false) String startDate,
			@RequestParam(value = "endDate", required = false) String endDate) {

		Map<String, Object> result = this.hasilPemeriksaanSwaPantauLimbahCairService.getHasilPemeriksaanPantauLImbahCair(startDate, endDate);
		
		Map<String, Object> header = (Map<String, Object>) result.get("header");
		String periode = header.get("tanggalAwal") == null ? "" : (String) header.get("tanggalAwal");
		periode = periode + (header.get("tanggalAkhir") == null ? "" : " sd " + (String) header.get("tanggalAkhir"));
		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;
	}

}
