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.UsulanEvaluasiService;
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("/usulan-evaluasi")
public class UsulanEvaluasiController extends LocaleController<UsulanEvaluasiVO> {

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

	@Autowired
	private UsulanEvaluasiService usulanEvaluasiService;

	@RequestMapping(value = "/save-rencana-penyuluhan", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
	public ResponseEntity<String> saveRencanaPenyuluhan(@RequestBody UsulanEvaluasiVO vo, HttpServletRequest request) {
		try {
			String result = usulanEvaluasiService.saveUsulanEvaluasiDanKahianOrganisasi(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 saveUsulanEvaluasiDanKahianOrganisasi", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when saveUsulanEvaluasiDanKahianOrganisasi", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-no-usulan/", method = GET)
	public ResponseEntity<Map<String, Object>> getNoUsulanEvaluasi(HttpServletRequest request) {
		try {
			String noSurat = usulanEvaluasiService.noUsulanDanKajianOrganisasi();
			Map<String, Object> result = new HashMap<>();
			result.put("noSurat", noSurat);
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when noUsulanDanKajianOrganisasi", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when noUsulanDanKajianOrganisasi", 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 = usulanEvaluasiService.listUsulanEvaluasiByRuanganPembuat(tglAwal,
					tglAkhir, noUsulan, ruanganId);
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.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-usulan-evaluasi-by-nousulan/", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getUsulanEvaluasiByNoUsulan(
			@RequestParam(value = "noUsulan") String noUsulan) {
		Map<String, Object> resultPageMap = usulanEvaluasiService.usulanEvaluasiByNoUsulan(noUsulan);
		return getJsonResponse(resultPageMap, OK, mapHeaderMessage);
	}

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

}
