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.GajiPegawaiService;
import com.jasamedika.medifirst2000.vo.PegawaiHistoriRekapGajiVO;
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.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("/gaji-pegawai")
public class GajiPegawaiController extends LocaleController<PegawaiHistoriRekapGajiVO> {

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

	@Autowired
	private GajiPegawaiService service;

	@RequestMapping(value = "/daftar-gaji-pegawai-belum-verifikasi", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getDaftarGajiPegawai(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "limit", required = false, defaultValue = "50") Integer limit,
			@RequestParam(value = "sort", required = false, defaultValue = "tahun") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@RequestParam(value = "detailKategoryPegawai", required = false) String detailKategoryPegawai,
			@RequestParam(value = "dateStart", required = false) String dateStart,
			@RequestParam(value = "dateEnd", required = false) String dateEnd,
			@RequestParam(value = "noRec", required = false) String noRec,
			@RequestParam(value = "gridFor", required = false) String gridFor,
			@RequestParam(value = "status", required = false) String status) {
		return service.getDaftarGajiPegawai(page, limit, sort, dir, dateStart, dateEnd, detailKategoryPegawai, noRec,
				gridFor, status);
	}

	@RequestMapping(value = "/daftar-gaji-pegawai-verifikasi", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getDaftarGajiPegawaiVerifikasi(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "limit", required = false, defaultValue = "50") Integer limit,
			@RequestParam(value = "sort", required = false, defaultValue = "tahun") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@RequestParam(value = "detailKategoryPegawai", required = false) String detailKategoryPegawai,
			@RequestParam(value = "dateStart", required = false) String dateStart,
			@RequestParam(value = "dateEnd", required = false) String dateEnd,
			@RequestParam(value = "noRec", required = false) String noRec,
			@RequestParam(value = "gridFor", required = false) String gridFor,
			@RequestParam(value = "status", required = false) String status) {
		return service.getDaftarGajiPegawaiVerifikasi(page, limit, sort, dir, dateStart, dateEnd, detailKategoryPegawai,
				noRec, gridFor, status);
	}

	@RequestMapping(value = "/detail-gaji-pegawai", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getDetailGajiPegawai(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "limit", required = false, defaultValue = "50") Integer limit,
			@RequestParam(value = "sort", required = false, defaultValue = "tahun") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@RequestParam(value = "detailKategoryPegawai", required = false) String detailKategoryPegawai,
			@RequestParam(value = "dateStart", required = false) String dateStart,
			@RequestParam(value = "dateEnd", required = false) String dateEnd,
			@RequestParam(value = "noRec", required = false) String noRec,
			@RequestParam(value = "idPegawai", required = false) Integer idPegawai) {
		return service.getDetailGajiPegawai(page, limit, sort, dir, dateStart, dateEnd, detailKategoryPegawai, noRec,
				idPegawai);
	}

	@RequestMapping(value = "/save-verifikasi-gaji-pegawai", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveVerifikasiGajiPegawai(
			@RequestParam(value = "noRec", required = false) String noRec, HttpServletRequest request) {
		try {
			Map<String, Object> result = service.saveVerifikasiGajiPegawai(noRec);
			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 saveVerifikasiGajiPegawai", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when saveVerifikasiGajiPegawai", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

}
