
package com.jasamedika.medifirst2000.controller;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.jasamedika.medifirst2000.constants.MessageResource;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.entities.HasilTriase;
import com.jasamedika.medifirst2000.entities.PasienDaftar;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
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.vo.PasienVO;
import com.jasamedika.medifirst2000.vo.RegistrasiPasienPenunjangVO;
import com.jasamedika.medifirst2000.vo.RegistrasiPasienVO;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

import static com.jasamedika.medifirst2000.constants.Constants.MessageInfo.ERROR_MESSAGE;
import static com.jasamedika.medifirst2000.core.web.WebConstants.HttpHeaderInfo.LABEL_SUCCESS;
import static com.jasamedika.medifirst2000.util.rest.RestUtil.getJsonHttpStatus;
import static com.jasamedika.medifirst2000.util.rest.RestUtil.getJsonResponse;
import static org.slf4j.LoggerFactory.getLogger;
import static org.springframework.http.HttpStatus.*;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

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

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

	@Autowired
	private RegistrasiPasienService registrasiPasienService;

	@Autowired
	private RegistrasiPelayananService<PasienDaftar> registrasiPelayananService;

	@Autowired
	private HasilTriaseService hasilTriaseService;

	@Autowired
	private RegistrasiPasienRawatInapService registrasiPasienInapJalanService;

	@RequestMapping(value = "/save-registrasi-pasien", method = POST, produces = APPLICATION_JSON_VALUE, consumes = 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(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, CREATED, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when saveRegistrasiPasien", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when saveRegistrasiPasien", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/save-registrasi-pasien-penunjang", method = POST, produces = APPLICATION_JSON_VALUE, consumes = 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(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, CREATED, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when savePasienDaftarPenunjang", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when savePasienDaftarPenunjang", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/antrian-pasien-gawat-darurat/", method = GET, produces = 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<>();
		result.put("data", data);
		return result;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/get-one-antrian-pasien-gawat-darurat/", method = GET, produces = 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<>();
		result.put("data", data);
		return result;
	}

	@RequestMapping(value = "/save-registrasi-pasien-bayi", method = POST, produces = APPLICATION_JSON_VALUE, consumes = 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(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, CREATED, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when registrasiPasien", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when registrasiPasien", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-pasien-by-nocm", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getPasienByNocm(@RequestParam(value = "noCm") String noCm,
			HttpServletRequest request) {
		try {
			Map<String, Object> result = registrasiPelayananService.getDataPasien(noCm);
			if (null != result) {
				mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
				return getJsonResponse(result, OK, mapHeaderMessage);
			} else {
				return getJsonResponse(null, NOT_FOUND, mapHeaderMessage);
			}
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when getDataPasien", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when getDataPasien", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/update-pasien-daftar", method = POST, produces = APPLICATION_JSON_VALUE, consumes = 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(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, CREATED, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when updatePasienDaftar", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when updatePasienDaftar", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

}
