package com.jasamedika.medifirst2000.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jasamedika.medifirst2000.constants.Constants;
import com.jasamedika.medifirst2000.constants.MessageResource;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.core.web.WebConstants;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.GajiPegawaiService;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import com.jasamedika.medifirst2000.vo.MappingKirimDokumenPasienVO;
import com.jasamedika.medifirst2000.vo.PegawaiHistoriRekapGajiVO;
import com.jasamedika.medifirst2000.vo.StrukBuktiPengeluaranCaraBayarVO;
import com.jasamedika.medifirst2000.vo.StrukBuktiPengeluaranVO;

@RestController
@RequestMapping("/gaji-pegawai")
public class GajiPegawaiController extends LocaleController<PegawaiHistoriRekapGajiVO> {

	@Autowired
	private GajiPegawaiService service;

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

	@RequestMapping(value = "/daftar-gaji-pegawai-belum-verifikasi", method = RequestMethod.GET, produces = MediaType.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) {
		Map<String, Object> resultPageMap = service.getDaftarGajiPegawai(page, limit, sort, dir, dateStart, dateEnd,
				detailKategoryPegawai, noRec, gridFor, status);

		return resultPageMap;
	}

	@RequestMapping(value = "/daftar-gaji-pegawai-verifikasi", method = RequestMethod.GET, produces = MediaType.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) {
		Map<String, Object> resultPageMap = service.getDaftarGajiPegawaiVerifikasi(page, limit, sort, dir, dateStart,
				dateEnd, detailKategoryPegawai, noRec, gridFor, status);

		return resultPageMap;
	}

	@RequestMapping(value = "/detail-gaji-pegawai", method = RequestMethod.GET, produces = MediaType.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) {
		Map<String, Object> resultPageMap = service.getDetailGajiPegawai(page, limit, sort, dir, dateStart, dateEnd,
				detailKategoryPegawai, noRec, idPegawai);

		return resultPageMap;
	}

	@RequestMapping(value = "/total-gaji-pegawai", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> getTotalGajiPegawai(
			@RequestParam(value = "detailKategoryPegawai", required = false) String detailKategoryPegawai,
			@RequestParam(value = "dateStart", required = false) String dateStart,
			@RequestParam(value = "dateEnd", required = false) String dateEnd) {
		Map<String, Object> resultPageMap = service.getTotalGajiPegawai(dateStart, dateEnd, detailKategoryPegawai);

		return resultPageMap;
	}

	@RequestMapping(value = "/save-gaji-pegawai", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveGajiPegawai(@Valid @RequestBody StrukBuktiPengeluaranVO vo,
			HttpServletRequest request) {
		try {
			Map<String, Object> result = service.saveGajiPegawai(vo);
			if (null != result)
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
						getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.CREATED, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when saveGajiPegawai", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when saveGajiPegawai", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}

	@RequestMapping(value = "/get-ruangan", method = RequestMethod.GET)
	public ResponseEntity<Ruangan> getRuangan(HttpServletRequest request) {
		try {
			Ruangan ruangan = service.getRuanganByUserLogin();
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
					getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(ruangan, HttpStatus.OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when getRuangan", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when getRuangan", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}

	@RequestMapping(value = "/save-verifikasi-gaji-pegawai", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.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(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
						getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.CREATED, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when saveVerifikasiGajiPegawai", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when saveVerifikasiGajiPegawai", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}

	@RequestMapping(value = "/save-pembayaran-gaji-pegawai", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> savePembayaranGajiPegawai(
			@Valid @RequestBody StrukBuktiPengeluaranCaraBayarVO vo,
			@RequestParam(value = "idRekanan", required = false) Integer idRekanan,
			@RequestParam(value = "noRec", required = false) String noRec,
			@RequestParam(value = "status", required = false) String status, HttpServletRequest request) {
		try {
			Map<String, Object> result = service.savePembayaranGajiPegawai(vo, idRekanan, noRec, status);
			if (null != result)
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
						getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.CREATED, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when savePembayaranGajiPegawai", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when savePembayaranGajiPegawai", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}

	@RequestMapping(value = "/save-klaim-pembayaran-gaji-pegawai", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveKlaimPembayaranGajiPegawai(
			@Valid @RequestBody MappingKirimDokumenPasienVO vo, HttpServletRequest request) {
		try {
			Map<String, Object> result = service.saveKlaimPembayaranGajiPegawai(vo);
			if (null != result)
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
						getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.CREATED, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when saveKlaimPembayaranGajiPegawai", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when saveKlaimPembayaranGajiPegawai", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}

}
