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.EvaluasiRekananService;
import com.jasamedika.medifirst2000.vo.UsulanEvaluasiVO;
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 java.util.HashMap;
import java.util.List;
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;

/**
 * class Usulan Evaluasi Controller
 * 
 * @author Lukman Hakim
 */
@RestController
@RequestMapping("/evaluasi-rekanan")
public class EvaluasiRekananController extends LocaleController<UsulanEvaluasiVO> {

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

	@Autowired
	private EvaluasiRekananService evaluasiRekananService;

	@RequestMapping(value = "/save", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
	public ResponseEntity<String> saveEvaluasiRekanan(@RequestBody UsulanEvaluasiVO vo, HttpServletRequest request) {
		try {
			String result = evaluasiRekananService.saveEvaluasiRekanan(vo);
			if (result != null)
				mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, CREATED, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when saveEvaluasiRekanan", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when saveEvaluasiRekanan", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-no-evaluasi", method = GET)
	public ResponseEntity<Map<String, Object>> getNoEvaluasi(HttpServletRequest request) {
		try {
			String noSurat = evaluasiRekananService.noEvaluasiRekanan();
			Map<String, Object> result = new HashMap<>();
			result.put("noSurat", noSurat);
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(LABEL_SUCCESS, request));
			return getJsonResponse(result, OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when noEvaluasiRekanan", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when noEvaluasiRekanan", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-list-evaluasi-by-ruangan-pembuat", method = GET)
	public ResponseEntity<List<Map<String, Object>>> getListUsulanEvaluasiByRuanganPembuat(
			@RequestParam(value = "noUsulan", required = false) String noUsulan,
			@RequestParam(value = "tglAwal") String tglAwal, @RequestParam(value = "tglAkhir") String tglAkhir,
			@RequestParam(value = "ruanganId") Integer ruanganId, HttpServletRequest request) {
		try {
			List<Map<String, Object>> noSurat = evaluasiRekananService.listUsulanEvaluasiByRuanganPembuat(tglAwal,
					tglAkhir, noUsulan, ruanganId);
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(LABEL_SUCCESS, request));
			return getJsonResponse(noSurat, OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when listUsulanEvaluasiByRuanganPembuat", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when listUsulanEvaluasiByRuanganPembuat", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-by-nousulan/", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getUsulanEvaluasiByNoUsulan(
			@RequestParam(value = "noUsulan") String noUsulan) {
		Map<String, Object> resultPageMap = evaluasiRekananService.usulanEvaluasiByNoUsulan(noUsulan);
		return getJsonResponse(resultPageMap, OK, mapHeaderMessage);
	}

}
