package com.jasamedika.medifirst2000.controller;

import java.util.HashMap;
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.ResponseBody;
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.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.util.rest.RestUtil;
import com.jasamedika.medifirst2000.vo.HeadAnggaranVO;
import com.jasamedika.medifirst2000.vo.VerifikasiSpekAnggaranVO;

@RestController
@RequestMapping("/anggaran")
public class AnggaranController extends LocaleController<HeadAnggaranVO> {

	@Autowired
	private AnggaranService service;

	@Autowired
	private DetailAnggaranService detailAnggaranService;

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

	@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", required = true) String gridFor,
			@RequestParam(value = "pengendaliId", required = false) Integer pengendaliId) {

		Map<String, Object> resultPageMap = service.detailSpekAnggaran(page, limit, sort, dir, dateStart, dateEnd,
				tahun, gridFor, pengendaliId);

		return resultPageMap;
	}

	@RequestMapping(value = "/save-pengajuan-usulan-anggaran-baru", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.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(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
						getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.CREATED, mapHeaderMessage);

		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when savePengajuanUsulanAnggaran", 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 savePengajuanUsulanAnggaran", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}

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

		return detailAnggaran;
	}

	@RequestMapping(value = "/save-verifikasi-spek-anggaran", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.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(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
						getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.CREATED, mapHeaderMessage);

		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when savePengajuanUsulanAnggaran", 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 savePengajuanUsulanAnggaran", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/save-un-verifikasi-spek-anggaran", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.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(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
						getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.CREATED, mapHeaderMessage);

		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when savePengajuanUsulanAnggaran", 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 savePengajuanUsulanAnggaran", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-kode-produk", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> gedProdukByNama(
			@RequestParam(value = "namaProduk", required = true) String namaProduk) {
		Map<String, Object> result = new HashMap<>();
		ResponseEntity<Map<String, Object>> restUtil = null;
		try {
			result = service.gedProdukByNama(namaProduk);
			if (null != result) {
				restUtil = RestUtil.getJsonResponse(result, HttpStatus.FOUND, mapHeaderMessage);
			} else {
				restUtil = RestUtil.getJsonResponse(null, HttpStatus.NOT_FOUND, mapHeaderMessage);
			}
		} catch (IllegalArgumentException e) {
			restUtil = RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		}
		return restUtil;
	}

	@RequestMapping(value = "/get-pengendali", method = RequestMethod.GET)
	public ResponseEntity<Pengendali> getPengendali(HttpServletRequest request) {
		try {
			Pengendali pengendali = service.getPengendaliByUserLogin();
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
					getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(pengendali, HttpStatus.OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when get all pengendali", 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 get all dokter", 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 get all dokter", 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 get all dokter", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}

	@RequestMapping(value = "/get-all-dokter", method = RequestMethod.GET)
	public ResponseEntity<Map<String, Object>> getJenisProduk(HttpServletRequest request) {
		try {
			Map<String, Object> listJenisProduk = service.getJenisProduk();
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
					getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(listJenisProduk, HttpStatus.OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when get all dokter", 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 get all dokter", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}

	// add by Arya 2017-01-19
	@RequestMapping(value = "/edit-pengajuan-usulan-anggaran", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.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(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
						getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.CREATED, mapHeaderMessage);

		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when editPengajuanUsulanAnggaran", 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 editPengajuanUsulanAnggaran", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}
	// end add by Arya 2017-01-19

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

		return detailAnggaran;
	}
	// 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) {

		Map<String, Object> resultPageMap = service.getDetailAnggaran(page, limit, sort, dir, dateStart, dateEnd,
				tahun, pengendaliId);

		return resultPageMap;
	}
	//end add by Arya 2017-02-02

}
