package com.jasamedika.medifirst2000.controller;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
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.data.jpa.domain.Specifications;
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.PathVariable;
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 com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.jasamedika.medifirst2000.constants.Constants;
import com.jasamedika.medifirst2000.constants.MessageResource;
import com.jasamedika.medifirst2000.controller.base.IBaseRestController;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.core.web.WebConstants;
import com.jasamedika.medifirst2000.entities.PapSkriningGizi;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.PapSkriningGiziService;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import com.jasamedika.medifirst2000.vo.InputPAPSkriningGiziVO;
import com.jasamedika.medifirst2000.vo.PapSkriningGiziVO;

import net.kaczmarzyk.spring.data.jpa.domain.In;
import net.kaczmarzyk.spring.data.jpa.domain.Like;
import net.kaczmarzyk.spring.data.jpa.web.annotation.Or;
import net.kaczmarzyk.spring.data.jpa.web.annotation.Spec;

/**
 * Controller class for Pengakjian Awal Gawat Darurat Business
 * 
 * @author Askur
 */
@RestController
@RequestMapping("/skrining-gizi")
@JsonIgnoreProperties(ignoreUnknown = true) 
public class PapSkriningGiziController extends LocaleController<PapSkriningGiziVO> implements
		IBaseRestController<PapSkriningGiziVO> {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(PapSkriningGiziController.class);

	@Autowired
	private PapSkriningGiziService papSkriningGiziService;

	@RequestMapping(value = "/save-skrining-gizi", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String,Object>> savePasien(@Valid @RequestBody PapSkriningGiziVO vo,HttpServletRequest request) {
		try {
			Map<String,Object> result = papSkriningGiziService.addSkriningGizi(vo);
			if (null != result){
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS, request));
				return RestUtil.getJsonResponse(result, HttpStatus.CREATED, mapHeaderMessage);
			} else{
				Map<String, String> headerMessageCustom = new HashMap<>();
				headerMessageCustom.put("label-success", "Pasien belum melakukan pendaftaran");
				return RestUtil.getJsonResponse(result, HttpStatus.CREATED, headerMessageCustom);
			}
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when add Pasien", 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 Pasien", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}

	@RequestMapping(value = "/find-by-noCm-and-tglInput/{noCm}/{tglInput}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String,Object>> findByNoCm(@PathVariable("noCm") String noCm,@PathVariable("tglInput") Date tglInput,HttpServletRequest request) {
		Map<String,Object> result = papSkriningGiziService.findByNoCmAndTglInput(noCm, tglInput);
		if (null != result){
			mapHeaderMessage.clear();
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS,request ));
		}else{
			mapHeaderMessage.clear();
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_ERROR,getMessage(MessageResource.LABEL_ERROR,request ));
		}
		return RestUtil.getJsonResponse(result, HttpStatus.OK,mapHeaderMessage);
	}
	
	@RequestMapping(value = "/findskoring", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String,Object>> findSkoring(HttpServletRequest request) {
		Map<String,Object> result = papSkriningGiziService.findSkoring();
		Boolean dataFound=new Boolean((boolean) result.get("dataFound"));
		if(dataFound){
			mapHeaderMessage.clear();
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS,request ));
		}else{
			mapHeaderMessage.clear();
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_ERROR,getMessage(MessageResource.LABEL_ERROR,request ));
		}
		return RestUtil.getJsonResponse(result, HttpStatus.OK,mapHeaderMessage);
	}

	@RequestMapping(value = "/find-skoring/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String,Object>> findSkoringParam(
			@RequestParam(value = "dateStart", required = false) String startDate,
			@RequestParam(value = "dateEnd", required = false) String endDate,
			@RequestParam(value = "namaPasien", required = false) String namaPasien,
			@RequestParam(value = "pilih", required = false) String pilih,
			HttpServletRequest request) {
		String namaPasienBaru="";
		if(namaPasien!=null)
			namaPasienBaru=namaPasien.toLowerCase();
		Map<String,Object> result = papSkriningGiziService.findSkoring(startDate,endDate,namaPasienBaru,pilih);
		Boolean dataFound=new Boolean((boolean) result.get("dataFound"));
		if(dataFound){
			mapHeaderMessage.clear();
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS,request ));
		}else{
			mapHeaderMessage.clear();
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_ERROR,getMessage(MessageResource.LABEL_ERROR,request ));
		}
		return RestUtil.getJsonResponse(result, HttpStatus.OK,mapHeaderMessage);
	}

	@RequestMapping(value = "/find-skoring-all", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String,Object>> findSkoringAll(HttpServletRequest request) {
		Map<String,Object> result = papSkriningGiziService.findSkoringAll();
		Boolean dataFound=new Boolean((boolean) result.get("dataFound"));
		if(dataFound){
			mapHeaderMessage.clear();
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS,request ));
		}else{
			mapHeaderMessage.clear();
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_ERROR,getMessage(MessageResource.LABEL_ERROR,request ));
		}
		return RestUtil.getJsonResponse(result, HttpStatus.OK,mapHeaderMessage);
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/find-skoring-paging/")
	@ResponseBody
	public ResponseEntity<List<Object>> findSkoringParamPaging(
			@RequestParam(value = "dateStart", required = false) String startDate,
			@RequestParam(value = "dateEnd", required = false) String endDate,
			@RequestParam(value = "namaPasien", required = false) String namaPasien,
			@RequestParam(value = "pilih", required = false) String pilih,
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "take", required = false, defaultValue = "10") Integer limit,
			@RequestParam(value = "sort", required = false, defaultValue = "id") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@Spec(path = "qRuangan", params = "qRuangan", spec = In.class) Specification<PapSkriningGizi> noRuangan,
			@Or({ @Spec(params = "namaRuangan", path = "namaRuangan", spec = Like.class),
			@Spec(params = "kdRuangan", path = "kdRuangan", spec = Like.class) }) Specification<PapSkriningGizi> nameSpec) {
		String namaPasienBaru="";
		if(namaPasien!=null)
			namaPasienBaru=namaPasien.toLowerCase();

		Map<String, Object> resultPageMap = papSkriningGiziService.findSkoringPaging(startDate,endDate,namaPasienBaru,pilih,page,limit,sort,dir);

		return constructListPageResult(resultPageMap);
	}
	
	
	@RequestMapping(value = "/find-skrining-gizi-detail", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String,Object>> findSkriningGiziDetailByNoRecSkriningGizi(@RequestParam(value = "noRec", required = true) String noRec,HttpServletRequest request) {
		Map<String,Object> result = papSkriningGiziService.findSkriningGiziDetailByNoRecSkriningGizi(noRec);
		Boolean dataFound=new Boolean((boolean) result.get("dataFound"));
		if(dataFound){
			mapHeaderMessage.clear();
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS,request ));
		}else{
			mapHeaderMessage.clear();
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_ERROR,getMessage(MessageResource.LABEL_ERROR,request ));
		}
		return RestUtil.getJsonResponse(result, HttpStatus.OK,mapHeaderMessage);
	}

	@Override
	public ResponseEntity<Collection<PapSkriningGiziVO>> getAllVOWithQueryString(HttpServletRequest request,
			Integer page, Integer limit, String sort, String dir) {
		return null;
	}

	@Override
	public ResponseEntity<PapSkriningGiziVO> getVO(Integer id) {
		return null;
	}

	@Override
	public ResponseEntity<String> addVO(PapSkriningGiziVO vo) {
		return null;
	}

	@Override
	public ResponseEntity<String> editVO(PapSkriningGiziVO vo) {
		return null;
	}

	@Override
	public ResponseEntity<String> deleteVO(Integer id) {
		return null;
	}

	@Override
	public ResponseEntity<List<PapSkriningGiziVO>> getAllVO() {
		return null;
	}

	
}
