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.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.DaftarStrukPenjualanService;
import com.jasamedika.medifirst2000.service.InputKantongDarahService;
import com.jasamedika.medifirst2000.service.LimbahB3MasukService;
import com.jasamedika.medifirst2000.vo.InputKantongDarahVO;
import com.jasamedika.medifirst2000.vo.KirimProdukRuanganVO;
import com.jasamedika.medifirst2000.vo.LimbahB3MasukVO;
import com.jasamedika.medifirst2000.vo.StrukPelayananVO;
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.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("/struk-pelayanan")
@JsonIgnoreProperties(ignoreUnknown = true)
public class StrukPelayananController extends LocaleController<StrukPelayananVO> {

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

	@Autowired
	private InputKantongDarahService inputKantongDarahService;

	@Autowired
	private LimbahB3MasukService<LimbahB3MasukVO> limbahB3MasukService;

	@Autowired
	private DaftarStrukPenjualanService service;

	@RequestMapping(value = "/kirim-produk-ke-ruangan", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> kirimProdukKeRuangan(@Valid @RequestBody KirimProdukRuanganVO vo,
			HttpServletRequest request) {
		try {
			Map<String, Object> result = inputKantongDarahService.kirimProdukKeRuangan(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 kirimProdukKeRuangan", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when kirimProdukKeRuangan", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/simpan-kantong-darah", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveInputKantongDarah(@Valid @RequestBody InputKantongDarahVO vo,
			HttpServletRequest request) {
		try {
			Map<String, Object> result = inputKantongDarahService.saveInputKantongDarah(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 saveInputKantongDarah", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when saveInputKantongDarah", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/simpan-limbah-b3-masuk", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveLimbahB3Masuk(@Valid @RequestBody LimbahB3MasukVO vo,
			HttpServletRequest request) throws IllegalAccessException {
		try {
			Map<String, Object> result = limbahB3MasukService.saveLimbahB3Masuk(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 saveLimbahB3Masuk", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when saveLimbahB3Masuk", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/update-limbah-b3-masuk", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> updateLimbahB3Masuk(@Valid @RequestBody LimbahB3MasukVO vo,
			HttpServletRequest request) throws IllegalAccessException {
		try {
			Map<String, Object> result = limbahB3MasukService.updateLimbahB3Masuk(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 updateLimbahB3Masuk", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when updateLimbahB3Masuk", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/delete-limbah-b3-masuk", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Boolean> deleteLimbahB3Masuk(@RequestParam(value = "noRec") String noRec,
			HttpServletRequest request) {
		try {
			Boolean result = limbahB3MasukService.deleteLimbahB3Masuk(noRec);
			if (result)
				mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when deleteLimbahB3Masuk", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException | IllegalAccessException e) {
			LOGGER.error("Got exception {} when deleteLimbahB3Masuk", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/list-limbah-b3-masuk")
	public Map<String, Object> lisLimbahB3Masuk(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "take", required = false, defaultValue = "10000") Integer limit,
			@RequestParam(value = "sort", required = false, defaultValue = "noRec") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@RequestParam(value = "dateStart", required = false) String dateStart,
			@RequestParam(value = "dateEnd", required = false) String dateEnd,
			@RequestParam(value = "jenisLimbah", required = false) Integer jenisLimbah) {
		return limbahB3MasukService.lisLimbahB3Masuk(page, limit, sort, dir, dateStart, dateEnd, jenisLimbah);
	}

	@RequestMapping(value = "/list-limbah-b3-masuk-bynorec")
	public Map<String, Object> lisLimbahB3MasukByNoRec(@RequestParam(value = "noRec", required = false) String noRec) {
		return limbahB3MasukService.lisLimbahB3MasukByNoRec(noRec);
	}

	@RequestMapping(value = "/save-retur-struk-pelayanan", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> returStrukPelayanan(@Valid @RequestBody StrukPelayananVO vo,
			HttpServletRequest request) {
		try {
			Map<String, Object> result = service.returPelayananPasien(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 returPelayananPasien", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when returPelayananPasien", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

}
