package com.jasamedika.medifirst2000.controller;

import com.jasamedika.medifirst2000.constants.MessageResource;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.entities.Pengendali;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.AnggaranService;
import com.jasamedika.medifirst2000.service.DetailAnggaranService;
import com.jasamedika.medifirst2000.vo.HeadAnggaranVO;
import com.jasamedika.medifirst2000.vo.VerifikasiSpekAnggaranVO;
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.*;

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("/anggaran")
public class AnggaranController extends LocaleController<HeadAnggaranVO> {

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

	@Autowired
	private AnggaranService service;

	@Autowired
	private DetailAnggaranService detailAnggaranService;

	@RequestMapping(value = "/grid-detail-anggaran")
	@ResponseBody
	public Map<String, Object> andCondition(
			@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 = "tahun", required = false) Integer tahun,
			@RequestParam(value = "gridFor") String gridFor,
			@RequestParam(value = "pengendaliId", required = false) Integer pengendaliId) {
		return service.detailSpekAnggaran(page, limit, sort, dir, dateStart, dateEnd, tahun, gridFor, pengendaliId);
	}

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

	@RequestMapping(value = "/get-detail-anggaran/", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> findDetailSpekAnggaranByParent(@RequestParam(value = "noRec") String noRec,
			@RequestParam(value = "parameter", required = false) String parameter,
			@RequestParam(value = "asalProdukId", required = false) Integer asalProdukId,
			@RequestParam(value = "mataAnggaranId", required = false) Integer mataAnggaranId) {
		return detailAnggaranService.findDetailSpekAnggaranByParent(noRec, parameter, asalProdukId, mataAnggaranId);
	}

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

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

	@RequestMapping(value = "/get-pengendali", method = GET)
	public ResponseEntity<Pengendali> getPengendali(HttpServletRequest request) {
		try {
			Pengendali pengendali = service.getPengendaliByUserLogin();
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(pengendali, OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when getPengendaliByUserLogin", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when getPengendaliByUserLogin", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

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

	// add by Arya 2017-01-19
	@RequestMapping(value = "/edit-pengajuan-usulan-anggaran", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> editPengajuanUsulanAnggaran(@Valid @RequestBody HeadAnggaranVO vo,
			@RequestParam(value = "noRec", required = false) String noRec, HttpServletRequest request) {
		try {
			Map<String, Object> result = service.editPengajuanUsulanAnggaran(vo, 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 editPengajuanUsulanAnggaran", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when editPengajuanUsulanAnggaran", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}
	// end add by Arya 2017-01-19

	// add by Arya 2017-01-30
	@RequestMapping(value = "/get-pengajuan-usulan-anggaran-by-norec/", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> findPengajuanUsulanAnggaranByNoRec(@RequestParam(value = "noRec") String noRec) {
		return service.findPengajuanUsulanAnggaranByNoRec(noRec);
	}
	// end add by Arya 2017-01-30

	// add by Arya 2017-02-02
	@RequestMapping(value = "/daftar-detail-anggaran")
	@ResponseBody
	public Map<String, Object> getDetailAnggaran(
			@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 = "tahun", required = false) Integer tahun,
			@RequestParam(value = "pengendaliId", required = false) Integer pengendaliId) {
		return service.getDetailAnggaran(page, limit, sort, dir, dateStart, dateEnd, tahun, pengendaliId);
	}
	// end add by Arya 2017-02-02

}
