package com.jasamedika.medifirst2000.controller;

import com.jasamedika.medifirst2000.constants.MessageResource;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.entities.UnitCost;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.UnitCostService;
import com.jasamedika.medifirst2000.vo.UnitCostVO;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
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.web.bind.annotation.RequestMethod.GET;

@RestController
@RequestMapping("/unit-cost")
public class DaftarKegiatanController extends LocaleController<UnitCostVO> {

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

	@Autowired
	private UnitCostService<UnitCost> unitCostService;

	@RequestMapping(value = "/detail-unit-cost", method = GET)
	public ResponseEntity<Map<String, Object>> findAllDepartemenById5And6(
			@RequestParam(value = "idRuangan", required = false) Integer idRuangan,
			@RequestParam(value = "idInstalasiDepartemen", required = false) Integer idInstalasiDepartemen,
			@RequestParam(value = "idLayanan", required = false) Integer idLayanan,
			@RequestParam(value = "periodeAwal", required = false) String periodeAwal,
			@RequestParam(value = "periodeAkhir", required = false) String periodeAkhir, HttpServletRequest request) {
		try {
			Map<String, Object> listDepartemen = unitCostService.findDetailCostUnit(idRuangan, idInstalasiDepartemen,
					periodeAwal, periodeAkhir, idLayanan);
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(listDepartemen, OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when findDetailCostUnit", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when findDetailCostUnit", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-list-unit-cost-jenis", method = GET)
	public ResponseEntity<Map<String, Object>> findUnitCostJenis(HttpServletRequest request) {
		try {
			Map<String, Object> mapResult = unitCostService.getListJenisUnitCost();
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(mapResult, CREATED, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when getListJenisUnitCost", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when getListJenisUnitCost", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-list-unit-cost-jenis-detail", method = GET)
	public ResponseEntity<Map<String, Object>> findUnitCostJenisDetail(
			@RequestParam(value = "idUnitCostJenis", required = false) Integer idUnitCostJenis,
			HttpServletRequest request) {
		try {
			Map<String, Object> mapResult = unitCostService.getListDetailJenisUnitCost(idUnitCostJenis);
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(mapResult, CREATED, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when getListDetailJenisUnitCost", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when getListDetailJenisUnitCost", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-list-unit-cost", method = GET)
	public ResponseEntity<Map<String, Object>> findUnitCost(
			@RequestParam(value = "idRuangan", required = false) Integer idRuangan,
			@RequestParam(value = "idInstalasi", required = false) Integer idInstalasi,
			@RequestParam(value = "namaKegiatan", required = false) String namaKegiatan, HttpServletRequest request) {
		try {
			Map<String, Object> mapResult = unitCostService.getListUnitCost(idRuangan, idInstalasi, namaKegiatan);
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(mapResult, OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when getListUnitCost", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when getListUnitCost", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

}
