package com.jasamedika.medifirst2000.controller;

import com.jasamedika.medifirst2000.constants.MessageResource;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.K3LaporanKecelakaanKerjaService;
import com.jasamedika.medifirst2000.vo.K3LaporanKecelakaanKerjaVO;
import com.jasamedika.medifirst2000.vo.LkkIdentifikasiKejadianVO;
import com.jasamedika.medifirst2000.vo.LkkIdentifikasiKorbanVO;
import com.jasamedika.medifirst2000.vo.LkkSaksiKejadianVO;
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("/k3-laporan-kecelakaan-kerja")
public class K3LaporanKecelakaanKerjaController extends LocaleController<K3LaporanKecelakaanKerjaVO> {

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

	@Autowired
	private K3LaporanKecelakaanKerjaService service;

	@RequestMapping(value = "/get-login-pelapor", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getLoginUser(HttpServletRequest request) {
		try {
			Map<String, Object> result = service.getUserLogin();
			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 getUserLogin", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when getUserLogin", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-unit-ruangan", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getUnitRuangan(HttpServletRequest request) {
		try {
			Map<String, Object> result = service.getUnitRuangan();
			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 getUnitRuangan", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when getUnitRuangan", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

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

	@RequestMapping(value = "/get-identifikasi-kejadian", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getIdentifikasiKejadian(
			@RequestParam(value = "idPegawai") Integer idPegawai, HttpServletRequest request) {
		try {
			Map<String, Object> result = service.getIdentifikasiKejadian(idPegawai);
			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 getIdentifikasiKejadian", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when getIdentifikasiKejadian", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-lkk-by-norec", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getDataLkkByNoRec(@RequestParam(value = "noRec") String noRec,
			HttpServletRequest request) {
		try {
			Map<String, Object> result = service.getDataLkkByNoRec(noRec);
			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 getDataLkkByNoRec", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when getDataLkkByNoRec", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/update-identifikasi-kejadian", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
	public ResponseEntity<String> updateIdentifikasiKejadian(@Valid @RequestBody LkkIdentifikasiKejadianVO vo,
			HttpServletRequest request) {
		try {
			Integer result = service.updateIdentifikasiKejadian(vo);
			if (null != result)
				mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(vo.getNoRec(), OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when updateIdentifikasiKejadian", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when updateIdentifikasiKejadian", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/update-identifikasi-korban", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
	public ResponseEntity<String> updateIdentifikasiKorban(@Valid @RequestBody LkkIdentifikasiKorbanVO vo,
			HttpServletRequest request) {
		try {
			Integer result = service.updateIdentifikasiKorban(vo);
			if (null != result)
				mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(vo.getNoRec(), OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when updateIdentifikasiKorban", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when updateIdentifikasiKorban", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/update-saksi-kejadian", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
	public ResponseEntity<String> updateSaksiKejadian(@Valid @RequestBody LkkSaksiKejadianVO vo,
			HttpServletRequest request) {
		try {
			Integer result = service.updateSaksiKejadian(vo);
			if (null != result)
				mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(vo.getNoRec(), OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when updateSaksiKejadian", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when updateSaksiKejadian", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/delete-identifikasi-kejadian", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Boolean> deleteIdentifikasiKejadian(@RequestParam(value = "noRec") String noRec,
			HttpServletRequest request) {
		try {
			Boolean result = service.deleteIdentifikasiKejadian(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 deleteIdentifikasiKejadian", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when deleteIdentifikasiKejadian", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/delete-identifikasi-korban", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Boolean> deleteIdentifikasiKorban(@RequestParam(value = "noRec") String noRec,
			HttpServletRequest request) {
		try {
			Boolean result = service.deleteIdentifikasiKorban(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 deleteIdentifikasiKorban", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when deleteIdentifikasiKorban", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/delete-saksi-kejadian", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Boolean> deleteSaksiKejadian(@RequestParam(value = "noRec") String noRec,
			HttpServletRequest request) {
		try {
			Boolean result = service.deleteSaksiKejadian(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 deleteSaksiKejadian", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when deleteSaksiKejadian", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/verifikasi-lkk", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
	public ResponseEntity<String> verifLaporanKecelakaan(@Valid @RequestBody K3LaporanKecelakaanKerjaVO vo,
			HttpServletRequest request) {
		try {
			Integer result = service.verifLaporanKecelakaan(vo);
			if (null != result)
				mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(vo.getNoRec(), OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when verifLaporanKecelakaan", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when verifLaporanKecelakaan", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

}
