
package com.jasamedika.medifirst2000.controller;

import java.text.SimpleDateFormat;
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.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.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.LocaleController;
import com.jasamedika.medifirst2000.core.web.WebConstants;
import com.jasamedika.medifirst2000.dao.custom.HasilTriaseDaoCustom;
import com.jasamedika.medifirst2000.entities.HasilTriase;
import com.jasamedika.medifirst2000.entities.PasienDaftar;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.DataAsuransiService;
import com.jasamedika.medifirst2000.service.HasilTriaseService;
import com.jasamedika.medifirst2000.service.RegistrasiPasienRawatInapService;
import com.jasamedika.medifirst2000.service.RegistrasiPasienService;
import com.jasamedika.medifirst2000.service.RegistrasiPelayananService;
import com.jasamedika.medifirst2000.service.ReportingPelayananRekamMedisService;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import com.jasamedika.medifirst2000.vo.DataAsuransiVO;
import com.jasamedika.medifirst2000.vo.GridAntrianPasienDiPeriksaVO;
import com.jasamedika.medifirst2000.vo.IpsrsMaintenanceVO;
import com.jasamedika.medifirst2000.vo.IpsrsSertifikatKalibrasiVO;
import com.jasamedika.medifirst2000.vo.PasienDaftarVO;
import com.jasamedika.medifirst2000.vo.PasienVO;
import com.jasamedika.medifirst2000.vo.RegistrasiPasienPenunjangVO;
import com.jasamedika.medifirst2000.vo.RegistrasiPasienVO;


@RestController
@RequestMapping("/registrasi-pasien")
@JsonIgnoreProperties(ignoreUnknown = true) 
public class RegistrasiPasienController extends LocaleController<RegistrasiPasienVO> {

	@Autowired
	private RegistrasiPasienService registrasiPasienService;
	
	@Autowired
	private ReportingPelayananRekamMedisService reportingPelayananRekamMedisService;
	
	@Autowired
	private RegistrasiPelayananService registrasiPelayananService;
	
	@Autowired
	private HasilTriaseService hasilTriaseService;
	
	private static final Logger LOGGER = LoggerFactory
			.getLogger(RegistrasiPasienController.class);	
	

	@RequestMapping(value = "/save-registrasi-pasien", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveRegistrasiPasien(@Valid @RequestBody RegistrasiPasienVO vo,HttpServletRequest request) {
		try {
			if(vo.getPasien().getNamaDepan()!=null)
			vo.getPasien().setNamaPasien(vo.getPasien().getNamaDepan()+" "+vo.getPasien().getNamaBelakang());
			Map<String,Object> result=registrasiPasienService.saveRegistrasiPasien(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 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 = "/save-registrasi-pasien-penunjang", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveRegistrasiPasienPenunjang(@Valid @RequestBody RegistrasiPasienPenunjangVO vo,HttpServletRequest request) {
		try {
			Map<String,Object> result = registrasiPelayananService.savePasienDaftarPenunjang(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 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);
		}

	}
	@Autowired
	private DataAsuransiService dataAsuransiService;
	@RequestMapping(value = "/save-data-asuransi", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String,Object>> saveDataAsuransi(@Valid @RequestBody DataAsuransiVO vo,HttpServletRequest request) {
		try {	
			Map<String,Object> result=dataAsuransiService.saveDataAsuransi(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 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);
		}

	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/antrian-pasien-gawat-darurat/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> getAllNewVOWithQueryString(
			@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
			@RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit,
			@RequestParam(value = "sort", required = false, defaultValue = "noRec") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@RequestParam(value = "ruanganId", required = false) Integer ruanganId,
			@RequestParam(value = "dateStart", required = false) String dateStart,
			@RequestParam(value = "dateEnd", required = false) String dateEnd,
			@RequestParam(value = "namaPasien", required = false) String namaPasien) {
		Map<String, Object> resultPageMap = hasilTriaseService.findHasilTriase(page, limit, sort, dir, dateStart, dateEnd);
		List<HasilTriase> data=(List<HasilTriase>) resultPageMap.get("hasilTriase");
		Map<String,Object>result=new HashMap<String,Object>();
		result.put("data", data);
		return result;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/get-one-antrian-pasien-gawat-darurat/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> getOneVOWithQueryString(@RequestParam(value = "noRec", required = false) String noRec) {
		Map<String, Object> resultPageMap = hasilTriaseService.findHasilTriase(noRec);
		List<HasilTriase> data=(List<HasilTriase>) resultPageMap.get("hasilTriase");
		Map<String,Object>result=new HashMap<String,Object>();
		result.put("data", data);
		return result;
	}
	
	//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	
	@Autowired
	private RegistrasiPasienRawatInapService registrasiPasienInapJalanService;
	
	@RequestMapping(value = "/save-registrasi-pasien-bayi", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> registrasiPasien(@Valid @RequestBody PasienVO vo, HttpServletRequest request) {
		try {
			Map<String,Object> result = registrasiPasienInapJalanService.registrasiPasien(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 saveRegistrasiPasien", 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 saveRegistrasiPasien", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}
	
	@RequestMapping(value = "/save-pasien-daftar-bayi", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> savePasienDaftar(@Valid @RequestBody PasienDaftarVO vo, HttpServletRequest request) {
		try {
			Map<String,Object> result = registrasiPasienInapJalanService.registrasiRekamMedisPasien(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 savePasienDaftar", 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 savePasienDaftar", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}
	
	@RequestMapping(value = "/get-pasien-by-nocm", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getPasienByNocm(
			@RequestParam(value = "noCm", required = true) String noCm, HttpServletRequest request) {
		try {
			Map<String, Object> result = registrasiPelayananService.getDataPasien(noCm);
			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 getPasienByNocm", 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 getPasienByNocm", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}
	
	@RequestMapping(value = "/update-pasien-daftar", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> updatePasienDaftar(@Valid @RequestBody PasienVO vo,HttpServletRequest request) {
		try {
			Map<String,Object> result = registrasiPelayananService.updatePasienDaftar(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 updatePasienDaftar", 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 updatePasienDaftar", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}
	
	
	@RequestMapping(value = "/get-umur", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<String> getUmur(
			@RequestParam("tglLahir") String tglLahir,HttpServletRequest request){
		try {			
			String result = reportingPelayananRekamMedisService.getUmur(tglLahir);
			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 getUmur ", 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 getUmur", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}
}
