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.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.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.entities.TransferPasienInternal;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.DetailAktivitasKemandirianService;
import com.jasamedika.medifirst2000.service.DetailAlasanPemindahanPasienService;
import com.jasamedika.medifirst2000.service.DetailInformasiMedisService;
import com.jasamedika.medifirst2000.service.DetailPsikologisService;
import com.jasamedika.medifirst2000.service.PeralatanDetailService;
import com.jasamedika.medifirst2000.service.ProdukService;
import com.jasamedika.medifirst2000.service.ResikoJatuhDetailService;
import com.jasamedika.medifirst2000.service.TransferPasienInternalService;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import com.jasamedika.medifirst2000.vo.AlasanPemindahanPasienVO;
import com.jasamedika.medifirst2000.vo.PeralatanDetailCustomVO;
import com.jasamedika.medifirst2000.vo.ResikoJatuhDetailCustomVO;
import com.jasamedika.medifirst2000.vo.TransferPasienAktivitasKemandirianVO;
import com.jasamedika.medifirst2000.vo.TransferPasienAlasanPindahPasienVO;
import com.jasamedika.medifirst2000.vo.TransferPasienInformasiMedisVO;
import com.jasamedika.medifirst2000.vo.TransferPasienPsikologisVO;
import com.jasamedika.medifirst2000.vo.TransferPasienInternalVO;

/**
 * Controller class for Registrasi Pasien Business
 * 
 * @author Adik
 */
@RestController
@RequestMapping("/transfer-pasien-internal")
@JsonIgnoreProperties(ignoreUnknown = true) 
public class TransferPasienInternalController extends LocaleController<AlasanPemindahanPasienVO> {

	@Autowired
	private DetailAlasanPemindahanPasienService detailAlasanPemindahanPasienService;
	
	@Autowired
	private DetailPsikologisService detailPsikologisService;
	
	@Autowired
	private DetailInformasiMedisService detailInformasiMedisService;
	
	@Autowired
	private TransferPasienInternalService transferPasienInternalService;
	
	@Autowired
	private DetailAktivitasKemandirianService detailAktivitasKemandirianService;
	
	@Autowired
	private PeralatanDetailService peralatanDetailService;
	
	@Autowired
	private ProdukService produkService;
	
	@Autowired
	private ResikoJatuhDetailService resikoJatuhDetailService;
	
	private static final Logger LOGGER = LoggerFactory.getLogger(TransferPasienInternalController.class);	
	
	
	@RequestMapping(value = "/save-detail-psikologis", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveDetailStatusPsikologis(@Valid @RequestBody TransferPasienPsikologisVO vo,HttpServletRequest request) {
		try {
			Map<String,Object> result=detailPsikologisService.saveDetailPsikologis(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 Detail Psikologi", 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 Detail Psikologi", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

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

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

	}
	
	@RequestMapping(value = "/save-aktivitas-kemandirian", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveTransferPasienInternal(@Valid @RequestBody TransferPasienAktivitasKemandirianVO vo,HttpServletRequest request) {
		try {
			Map<String,Object> result=detailAktivitasKemandirianService.saveAktivitasKemandirian(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 Transfer Aktivias Kemandirian Internal", 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 Transfer Pasien Internal", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}
	
	@RequestMapping(value = "/save-peralatan-detail/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String,Object>> savePeralatanDetail(@Valid @RequestBody PeralatanDetailCustomVO vo, HttpServletRequest request) {
		
		try {
			Map<String,Object> result=peralatanDetailService.addPeralatan(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 Peralatan", 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  Peralatan", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE,
					jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT,
					mapHeaderMessage);
		}
	}
	
	@RequestMapping(value = "/save-resiko-jatuh/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String,Object>> saveResikoJatuh(@Valid @RequestBody ResikoJatuhDetailCustomVO vo, HttpServletRequest request) {
		
		try {
			Map<String,Object> result=resikoJatuhDetailService.addResikoJatuh(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 Resiko Jatuh", 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  Resiko Jatuh", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE,
					jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT,
					mapHeaderMessage);
		}
	}
	
	@RequestMapping(value = "/get-list-obat", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getListObat(
			HttpServletRequest request) {
		List<Map<String, Object>> result = null;
		try {
			
			result = produkService.findObat();

			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
					getMessage(MessageResource.LABEL_SUCCESS, request));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return RestUtil.getJsonResponse(result, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/get-list-peralatan", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getListPeralatan(
			HttpServletRequest request) {
		List<Map<String, Object>> result = null;
		try {
			
			result = transferPasienInternalService.findPeralatan(false);

			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
					getMessage(MessageResource.LABEL_SUCCESS, request));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return RestUtil.getJsonResponse(result, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/get-list-metode-pemindahan", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getListMetodePemindahan(
			HttpServletRequest request) {
		List<Map<String, Object>> result = null;
		try {
			
			result = transferPasienInternalService.findPeralatan(true);

			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
					getMessage(MessageResource.LABEL_SUCCESS, request));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return RestUtil.getJsonResponse(result, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/get-transfer-internal/{startDate}/{endDate}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String,Object>> getListTransferInternal(@PathVariable("startDate") String startDate,
			@PathVariable("endDate") String endDate,
			HttpServletRequest request) {
		Map<String,Object> result = null;
		try {
			
			result = transferPasienInternalService.findTransferInternal(startDate, endDate);

			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
					getMessage(MessageResource.LABEL_SUCCESS, request));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return RestUtil.getJsonResponse(result, HttpStatus.OK);
	}

}
