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.LimbahB3KeluarService;
import com.jasamedika.medifirst2000.service.LimbahB3MasukService;
import com.jasamedika.medifirst2000.vo.LimbahB3KeluarVO;
import com.jasamedika.medifirst2000.vo.LimbahB3MasukVO;
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("/limbah-b3-keluar")
public class LimbahB3KeluarController extends LocaleController<LimbahB3KeluarVO> {

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

	@Autowired
	private LimbahB3KeluarService<LimbahB3KeluarVO> limbahB3KeluarService;

	@Autowired
	private LimbahB3MasukService<LimbahB3MasukVO> limbahB3MasukService;

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

	@RequestMapping(value = "/update-limbah-b3-keluar/", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> updatLimbahB3Keluar(@Valid @RequestBody LimbahB3KeluarVO vo,
			HttpServletRequest request) {
		return addVO(vo, request);
	}

	@RequestMapping(value = "/delete-limbah-b3-keluar", method = GET)
	public ResponseEntity<String> deleteNamaBahan(@RequestParam(value = "noRec") String noRec) {
		try {
			if (limbahB3KeluarService.deleteLimbahB3Keluar(noRec))
				return getJsonResponse(noRec + " Dihapus", CREATED);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when deleteLimbahB3Keluar", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when deleteLimbahB3Keluar", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
		return getJsonHttpStatus(NOT_ACCEPTABLE);
	}

	@RequestMapping(value = "/get-rekanan", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getRekanan() {
		return this.limbahB3KeluarService.getRekanan();
	}

	@RequestMapping(value = "/get-sisa-limbah-di-tps-by-jenis-limbah", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getSisaLimbahDiTpsByJenisLimbah(
			@RequestParam(value = "jenisLimbah") Integer jenisLimbah) {
		return this.limbahB3KeluarService.getSisaLimbahdiTpsByJenisLimbah(jenisLimbah);
	}

	@RequestMapping(value = "/get-total-limbah-by-periode/", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Double>> getTotalByPeriode(
			@RequestParam(value = "periodeAwal") String periodeAwal,
			@RequestParam(value = "periodeAkhir") String periodeAkhir,
			@RequestParam(value = "jenisLibahB3MasukId") Integer jenisLibahB3MasukId, HttpServletRequest request) {
		try {
			Map<String, Double> result = limbahB3MasukService.getTotalBeratLimbah(periodeAwal, periodeAkhir,
					jenisLibahB3MasukId);
			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 getTotalBeratLimbah", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when getTotalBeratLimbah", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-total-limbah-by-periode-list/", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getTotalByPeriodeList(
			@RequestParam(value = "periodeAwal") String periodeAwal,
			@RequestParam(value = "periodeAkhir") String periodeAkhir, HttpServletRequest request) {
		try {
			Map<String, Object> result = limbahB3MasukService.getTotalBeratLimbahList(periodeAwal, periodeAkhir);
			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 getTotalBeratLimbahList", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when getTotalBeratLimbahList", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/list-limbah-b3-keluar")
	public Map<String, Object> lisLimbahB3Masuk(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "take", required = false, defaultValue = "100") 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 limbahB3KeluarService.lisLimbahB3Keluar(page, limit, sort, dir, dateStart, dateEnd, jenisLimbah);
	}

}
