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.TagihanRekananService;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import com.jasamedika.medifirst2000.vo.MappingKirimDokumenPasienVO;
import com.jasamedika.medifirst2000.vo.StrukBuktiPengeluaranCaraBayarVO;
import com.jasamedika.medifirst2000.vo.StrukPelayananVO;

@RestController
@RequestMapping("/tagihan-rekanan")
public class TagihanRekananController extends LocaleController<StrukPelayananVO> {

	@Autowired
	private TagihanRekananService service;

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

	@RequestMapping(value = "/daftar-tagihan-rekanan-belum-verifikasi", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> getDaftarTagihanRekanan(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "limit", required = false, defaultValue = "5000") Integer limit,
			@RequestParam(value = "sort", required = false, defaultValue = "namaRekanan") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@RequestParam(value = "noFaktur", required = false) String noFaktur,
			@RequestParam(value = "namaRekanan", required = false) String namaRekanan,
			@RequestParam(value = "dateStart", required = false) String dateStart,
			@RequestParam(value = "dateEnd", required = false) String dateEnd,
			@RequestParam(value = "noTerima", required = false) String noTerima,
			@RequestParam(value = "status", required = false) String status,
			@RequestParam(value = "noRec", required = false) String noRec,
			@RequestParam(value = "gridFor", required = false) String gridFor) {
		Map<String, Object> resultPageMap = service.getDaftarTagihanRekanan(page, limit, sort, dir, dateStart, dateEnd,
				noFaktur, namaRekanan, noTerima, status, noRec, gridFor);

		return resultPageMap;
	}

	@RequestMapping(value = "/daftar-tagihan-rekanan-verifikasi", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> getDaftarTagihanRekananVerifikasi(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "limit", required = false, defaultValue = "5000") Integer limit,
			@RequestParam(value = "sort", required = false, defaultValue = "namaRekanan") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@RequestParam(value = "noFaktur", required = false) String noFaktur,
			@RequestParam(value = "namaRekanan", required = false) String namaRekanan,
			@RequestParam(value = "dateStart", required = false) String dateStart,
			@RequestParam(value = "dateEnd", required = false) String dateEnd,
			@RequestParam(value = "noTerima", required = false) String noTerima,
			@RequestParam(value = "status", required = false) String status,
			@RequestParam(value = "noRec", required = false) String noRec,
			@RequestParam(value = "gridFor", required = false) String gridFor) {
		Map<String, Object> resultPageMap = service.getDaftarTagihanRekananVerifikasi(page, limit, sort, dir, dateStart,
				dateEnd, noFaktur, namaRekanan, noTerima, status, noRec, gridFor);

		return resultPageMap;
	}

	@RequestMapping(value = "/detail-tagihan-rekanan", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> getDetailTagihanRekanan(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "limit", required = false, defaultValue = "5000") Integer limit,
			@RequestParam(value = "sort", required = false, defaultValue = "namaRekanan") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@RequestParam(value = "noFaktur", required = false) String noFaktur,
			@RequestParam(value = "namaRekanan", required = false) String namaRekanan,
			@RequestParam(value = "dateStart", required = false) String dateStart,
			@RequestParam(value = "dateEnd", required = false) String dateEnd,
			@RequestParam(value = "noTerima", required = false) String noTerima,
			@RequestParam(value = "noRec", required = false) String noRec) {
		Map<String, Object> resultPageMap = service.getDetailTagihanRekanan(page, limit, sort, dir, dateStart, dateEnd,
				noFaktur, namaRekanan, noTerima, noRec);

		return resultPageMap;
	}

	@RequestMapping(value = "/detail-total-harga-rekanan", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> getDetailTotalTagihanRekanan(
			@RequestParam(value = "noFaktur", required = false) String noFaktur,
			@RequestParam(value = "namaRekanan", required = false) String namaRekanan,
			@RequestParam(value = "dateStart", required = false) String dateStart,
			@RequestParam(value = "dateEnd", required = false) String dateEnd,
			@RequestParam(value = "noTerima", required = false) String noTerima,
			@RequestParam(value = "noRec", required = false) String noRec) {
		Map<String, Object> resultPageMap = service.getDetailTotalTagihanRekanan(dateStart, dateEnd, noFaktur,
				namaRekanan, noTerima, noRec);

		return resultPageMap;
	}

	@RequestMapping(value = "/riwayat-pembayaran-tagihan", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> getRiwayatPembayaranTagihan(
			@RequestParam(value = "idRekanan", required = false) Integer idRekanan,
			@RequestParam(value = "noRec", required = false) String noRec) {
		Map<String, Object> resultPageMap = service.getRiwayatPembayaranTagihan(idRekanan, noRec);

		return resultPageMap;
	}

	@RequestMapping(value = "/save-pembayaran-tagihan", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> savePembayaranTagihan(
			@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.savePembayaranTagihan(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 savePembayaranTagihan", 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 savePembayaranTagihan", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}

	@RequestMapping(value = "/save-verifikasi-tagihan-rekanan", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveVerifikasiTagihanRekanan(
			@RequestParam(value = "noRec", required = false) String noRec, HttpServletRequest request) {
		try {
			Map<String, Object> result = service.saveVerifikasiTagihanRekanan(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 saveVerifikasiTagihanRekanan", 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 saveVerifikasiTagihanRekanan", 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-klaim-pembayaran", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveKlaimPembayaran(@Valid @RequestBody MappingKirimDokumenPasienVO vo,
			HttpServletRequest request) {
		try {
			Map<String, Object> result = service.saveKlaimPembayaran(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 saveKlaimPembayaran", 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 saveKlaimPembayaran", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}

	@RequestMapping(value = "/daftar-klaim-pembayaran", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> getKlaimPembayaran(
			@RequestParam(value = "noRegistrasi", required = false) String noRegistrasi) {
		Map<String, Object> resultPageMap = service.getKlaimPembayaran(noRegistrasi);

		return resultPageMap;
	}

	@RequestMapping(value = "/detail-kwitansi", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> getDetailKwitansi(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "take", required = false, defaultValue = "100") Integer limit,
			@RequestParam(value = "sort", required = false, defaultValue = "noSbm") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@RequestParam(value = "noRec", required = false) String noRec,
			@RequestParam(value = "noSbm", required = false) String noSbm) {
		Map<String, Object> resultPageMap = service.getDetailKwitansi(page, limit, sort, dir, noRec, noSbm);

		return resultPageMap;
	}

	@RequestMapping(value = "/detail-kwitansi-noregistrasi", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> getDetailKwitansiNoRegistrasi(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "take", required = false, defaultValue = "100") Integer limit,
			@RequestParam(value = "sort", required = false, defaultValue = "noRegistrasi") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@RequestParam(value = "noRec", required = false) String noRec,
			@RequestParam(value = "noSbm", required = false) String noSbm,
			@RequestParam(value = "noRegistrasi", required = false) String noRegistrasi) {
		Map<String, Object> resultPageMap = service.getDetailKwitansiNoRegistrasi(page, limit, sort, dir, noRec, noSbm,
				noRegistrasi);

		return resultPageMap;
	}

	@RequestMapping(value = "/kartu-piutang-perusahaan", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> getKartuPiutangPerusahaan(
			@RequestParam(value = "dateStart", required = false) String dateStart,
			@RequestParam(value = "dateEnd", required = false) String dateEnd,
			@RequestParam(value = "noCm", required = false) String noCm,
			@RequestParam(value = "namaPasien", required = false) String namaPasien,
			@RequestParam(value = "noRegistrasi", required = false) String noRegistrasi,
			@RequestParam(value = "kdRekanan", required = false) Short kdRekanan,
			@RequestParam(value = "namaRekanan", required = false) String namaRekanan) {
		Map<String, Object> resultPageMap = service.getKartuPiutangPerusahaan(dateStart, dateEnd, noCm, namaPasien,
				noRegistrasi, kdRekanan, namaRekanan);

		return resultPageMap;
	}

	@RequestMapping(value = "/verifikasi", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> getDaftarVerifikasi(
			@RequestParam(value = "dateStart", required = false) String dateStart,
			@RequestParam(value = "dateEnd", required = false) String dateEnd,
			@RequestParam(value = "dateStartVerif", required = false) String dateStartVerif,
			@RequestParam(value = "dateEndVerif", required = false) String dateEndVerif,
			@RequestParam(value = "idKelTransaksi", required = false) Integer idKelTransaksi,
			@RequestParam(value = "kelTransaksi", required = false) String kelTransaksi) {
		Map<String, Object> resultPageMap = service.getDaftarVerifikasi(dateStart, dateEnd, dateStartVerif,
				dateEndVerif, idKelTransaksi, kelTransaksi);

		return resultPageMap;
	}

}
