package com.jasamedika.medifirst2000.controller;

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.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.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.KirimDokumenPasienService;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import com.jasamedika.medifirst2000.vo.MappingKirimDokumenPasienVO;

@RestController
@RequestMapping("/kirim-dokumen-pasien")
public class KirimDokumenPasienController extends LocaleController<MappingKirimDokumenPasienVO> {

	@Autowired
	private KirimDokumenPasienService service;

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

	@RequestMapping(value = "/save-kirim-dokumen-pasien", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveKirimDokumenPasien(
			@Valid @RequestBody MappingKirimDokumenPasienVO vo, HttpServletRequest request) {
		try {
			Map<String, Object> result = service.saveKirimDokumenPasien(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 saveKirimDokumenPasien", 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 saveKirimDokumenPasien", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}

	@RequestMapping(value = "/save-terima-dokumen-pasien", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveTerimaDokumenPasien(
			@Valid @RequestBody MappingKirimDokumenPasienVO vo,
			@RequestParam(value = "noRec", required = false) String noRec, HttpServletRequest request) {
		try {
			Map<String, Object> result = service.saveTerimaDokumenPasien(vo, noRec);
			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 saveTerimaDokumenPasien", 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 saveTerimaDokumenPasien", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}

	@RequestMapping(value = "/save-kirim-balik-dokumen-pasien", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveKirimBalikDokumenPasien(
			@Valid @RequestBody MappingKirimDokumenPasienVO vo,
			@RequestParam(value = "noRec", required = false) String noRec, HttpServletRequest request) {
		try {
			Map<String, Object> result = service.saveKirimBalikDokumenPasien(vo, noRec);
			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 saveKirimBalikDokumenPasien", 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 saveKirimBalikDokumenPasien", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}

	@RequestMapping(value = "/daftar-dokumen-rekam-medis-pasien", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> getDaftarDokumenRekamMedisPasien(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "take", required = false, defaultValue = "200") Integer limit,
			@RequestParam(value = "sort", required = false, defaultValue = "namaPasien") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@RequestParam(value = "dateStartMasuk", required = false) String dateStartMasuk,
			@RequestParam(value = "dateEndMasuk", required = false) String dateEndMasuk,
			@RequestParam(value = "dateStartPulang", required = false) String dateStartPulang,
			@RequestParam(value = "dateEndPulang", required = false) String dateEndPulang,
			@RequestParam(value = "ruanganPelayanan", required = false) String ruanganPelayanan,
			@RequestParam(value = "ruanganAsal", required = false) String ruanganAsal,
			@RequestParam(value = "idRuangan", required = false) Integer idRuangan,
			@RequestParam(value = "idPasien", required = false) Integer idPasien,
			@RequestParam(value = "namaPasien", required = false) String namaPasien,
			@RequestParam(value = "noCm", required = false) String noCm,
			@RequestParam(value = "ruanganPasien", required = false) String ruanganPasien) {
		Map<String, Object> resultPageMap = service.getDaftarDokumenRekamMedisPasien(page, limit, sort, dir,
				dateStartMasuk, dateEndMasuk, dateStartPulang, dateEndPulang, ruanganPelayanan, idRuangan, idPasien,
				namaPasien, noCm, ruanganPasien);

		return resultPageMap;
	}
	
	@RequestMapping(value = "/daftar-dokumen-rekam-medis-pasien-terima", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> getDaftarDokumenRekamMedisPasienTerima(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "take", required = false, defaultValue = "200") Integer limit,
			@RequestParam(value = "sort", required = false, defaultValue = "namaPasien") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@RequestParam(value = "dateStartMasuk", required = false) String dateStartMasuk,
			@RequestParam(value = "dateEndMasuk", required = false) String dateEndMasuk,
			@RequestParam(value = "dateStartPulang", required = false) String dateStartPulang,
			@RequestParam(value = "dateEndPulang", required = false) String dateEndPulang,
			@RequestParam(value = "ruanganPelayanan", required = false) String ruanganPelayanan,
			@RequestParam(value = "ruanganAsal", required = false) String ruanganAsal,
			@RequestParam(value = "idRuangan", required = false) Integer idRuangan,
			@RequestParam(value = "idPasien", required = false) Integer idPasien,
			@RequestParam(value = "namaPasien", required = false) String namaPasien,
			@RequestParam(value = "noCm", required = false) String noCm,
			@RequestParam(value = "ruanganPasien", required = false) String ruanganPasien) {
		Map<String, Object> resultPageMap = service.getDaftarDokumenRekamMedisPasienTerima(page, limit, sort, dir,
				dateStartMasuk, dateEndMasuk, dateStartPulang, dateEndPulang, ruanganPelayanan, idRuangan, idPasien,
				namaPasien, noCm, ruanganPasien);

		return resultPageMap;
	}

	@RequestMapping(value = "/daftar-kirim-dokumen-rekam-medis-pasien", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> getDaftarKirimDokumenRekamMedisPasien(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "take", required = false, defaultValue = "200") Integer limit,
			@RequestParam(value = "sort", required = false, defaultValue = "namaPasien") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@RequestParam(value = "dateStartMasuk", required = false) String dateStartMasuk,
			@RequestParam(value = "dateEndMasuk", required = false) String dateEndMasuk,
			@RequestParam(value = "dateStartPulang", required = false) String dateStartPulang,
			@RequestParam(value = "dateEndPulang", required = false) String dateEndPulang,
			@RequestParam(value = "dateStartKirim", required = false) String dateStartKirim,
			@RequestParam(value = "dateEndKirim", required = false) String dateEndKirim,
			@RequestParam(value = "ruanganPelayanan", required = false) String ruanganPelayanan,
			@RequestParam(value = "ruanganAsal", required = false) String ruanganAsal,
			@RequestParam(value = "idRuangan", required = false) Integer idRuangan,
			@RequestParam(value = "idPasien", required = false) Integer idPasien,
			@RequestParam(value = "namaPasien", required = false) String namaPasien,
			@RequestParam(value = "noCm", required = false) String noCm,
			@RequestParam(value = "ruanganPasien", required = false) String ruanganPasien) {
		Map<String, Object> resultPageMap = service.getDaftarKirimDokumenRekamMedisPasien(page, limit, sort, dir,
				dateStartMasuk, dateEndMasuk, dateStartPulang, dateEndPulang, dateStartKirim, dateEndKirim,
				ruanganPelayanan, ruanganAsal, idRuangan, idPasien, namaPasien, noCm, ruanganPasien);

		return resultPageMap;
	}

}
