package com.jasamedika.medifirst2000.controller;

import com.jasamedika.medifirst2000.dto.BatalRegistrasiDto;
import com.jasamedika.medifirst2000.dto.RegistrasiPasienDto;
import com.jasamedika.medifirst2000.entities.*;
import com.jasamedika.medifirst2000.enums.TipePasienEnum;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.*;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.vo.*;
import com.jasamedika.medifirst2000.vo.custom.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

@RestController
@RequestMapping("/daftar-ol")
public class BridgingDaftarOnlineController {

	@Autowired
	private ModelService<RuanganVO> modelServiceRuangan;

	@Autowired
	private PasienService pasienService;

	@Autowired
	private RuanganService<RuanganVO> ruanganService;

	@Autowired
	private JadwalDokterService jadwalDokterService;

	@Autowired
	private PegawaiService pegawaiService;

	@Autowired
	private RegistrasiPasienService registrasiPasienService;

	@Autowired
	private PasienDaftarService pasienDaftarService;

	@Autowired
	private AntrianPasienDiPeriksaService antrianService;

	@Autowired
	private BatalRegistrasiService batalRegistrasiService;

	@Autowired
	private JadwalOperasiService jadwalOperasiService;

	@Autowired
	private BridgingService bridgingService;

	@Autowired
	private JadwalPraktikDokterService jadwalPraktikDokterService;

	@Autowired
	private AntrianPasienRegistrasiService<AntrianPasienRegistrasiVO> antrianPasienRegistrasiService;

	@RequestMapping(value = "/klinik_rs", method = RequestMethod.GET, produces = { MediaType.APPLICATION_XML_VALUE })
	public BridgeDaftarKlinikListVO daftarKlinikRs() {
		BridgeDaftarKlinikListVO y = new BridgeDaftarKlinikListVO();
		List<Map<String, Object>> ruanganT = bridgingService.getJadwalKlinik();
		for (Map<String, Object> ruanganTmp : ruanganT) {
			BridgeDaftarKlinikVO x = new BridgeDaftarKlinikVO();
			x.setIdKlinik(ruanganTmp.get("id").toString());
			x.setNamaKlinik(ruanganTmp.get("namaRuangan").toString());
			x.setHari(ruanganTmp.get("hari").toString());
			if (CommonUtil.isNullOrEmpty(ruanganTmp.get("jamBuka"))) {
				x.setJamBukaPelayanan("");
			} else {
				x.setJamBukaPelayanan(ruanganTmp.get("jamBuka").toString());
			}
			if (CommonUtil.isNullOrEmpty(ruanganTmp.get("jamTutup"))) {
				x.setJamTutupPelayanan("");
			} else {
				x.setJamTutupPelayanan(ruanganTmp.get("jamTutup").toString());
			}
			Calendar cal = Calendar.getInstance();
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			x.setUpdate(dateFormat.format(cal.getTime()));
			x.setKuota(30);
			y.addBridgePendaftaranDaftarKlinikVO(x);
		}
		return y;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/jadwal_dokters", method = RequestMethod.GET, produces = {
			MediaType.APPLICATION_XML_VALUE })
	public BridgeDaftarJadwalDokterListVO jadwalDokterRs() {
		BridgeDaftarJadwalDokterListVO y = new BridgeDaftarJadwalDokterListVO();
		List<Map<String, Object>> ruanganT = modelServiceRuangan.getAllData("Ruangan", "id,departemenId", 1500, null,
				null, null, null, null, null, null, null, null, null);
		ArrayList<Integer> ytemp = new ArrayList<>();
		for (Map<String, Object> stringObjectMap : ruanganT) {
			Integer deptId = (Integer) stringObjectMap.get("departemenId");
			if (deptId != null)
				if (((Integer) stringObjectMap.get("departemenId") == 18)
						|| ((Integer) stringObjectMap.get("departemenId") == 28))
					ytemp.add((Integer) stringObjectMap.get("id"));
		}
		for (Integer integer : ytemp) {
			Map<String, Object> jd;
			try {
				jd = jadwalDokterService.findByRuangan(integer);
			} catch (Exception ex) {
				jd = null;
			}
			if (jd != null)
				if (jd.get("data") != null) {
					List<JadwalDokter> temp = (List<JadwalDokter>) jd.get("data");
					for (JadwalDokter jadwalDokter : temp) {
						BridgeDaftarJadwalDokterVO x = new BridgeDaftarJadwalDokterVO();
						x.setIdKlinik(jadwalDokter.getRuangan().getId().toString());
						String hari = bridgingService
								.getTanggalJadwal(DateUtil.dayOfWeek(jadwalDokter.getTanggalJadwal()));
						x.setHari(hari);
						JadwalPraktek jp = jadwalDokter.getJadwalPraktek();
						if (CommonUtil.isNotNullOrEmpty(jp.getJamPraktek())) {
							String[] pp = jp.getJamPraktek().split("-");
							if (pp.length == 2) {
								x.setJamTutupPraktek(pp[1]);
								x.setJamMulaiPraktek(pp[0]);
							}
						} else {
							x.setJamTutupPraktek("");
							x.setJamMulaiPraktek("");
						}
						if (CommonUtil.isNotNullOrEmpty(jadwalDokter.getQuota())) {
							x.setKuota(jadwalDokter.getQuota().toString());
						} else {
							x.setKuota("30");
						}
						try {
							x.setIddokter(jadwalDokter.getDokter().getId().toString());
							x.setNamadokter(jadwalDokter.getDokter().getNamaLengkap());
						} catch (Exception ex) {
							x.setIddokter("?");
							x.setNamadokter("?");
						}
						Calendar cal = Calendar.getInstance();
						DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
						x.setUpdate(dateFormat.format(cal.getTime()));
						y.addBridgePendaftaranJadwalDokterVO(x);
					}
				}
		}
		return y;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/jadwal_dokter_rev", method = RequestMethod.GET, produces = {
			MediaType.APPLICATION_XML_VALUE })
	public BridgeDaftarJadwalDokterListVO jadwalDokterRse() {
		BridgeDaftarJadwalDokterListVO y = new BridgeDaftarJadwalDokterListVO();
		List<BridgeDaftarJadwalDokterVO> filterTemp = new ArrayList<>();
		List<Map<String, Object>> ruanganT = modelServiceRuangan.getAllData("Ruangan", "id,departemenId", 1500, null,
				null, null, null, null, null, null, null, null, null);
		ArrayList<Integer> ytemp = new ArrayList<>();
		for (Map<String, Object> stringObjectMap : ruanganT) {
			Integer deptId = (Integer) stringObjectMap.get("departemenId");
			if (deptId != null)
				if (((Integer) stringObjectMap.get("departemenId") == 18)
						|| ((Integer) stringObjectMap.get("departemenId") == 28))
					ytemp.add((Integer) stringObjectMap.get("id"));
		}
		for (Integer integer : ytemp) {
			Map<String, Object> jd;
			try {
				jd = jadwalDokterService.findByRuangan(integer);
			} catch (Exception ex) {
				jd = null;
			}
			if (jd != null)
				if (jd.get("data") != null) {
					List<JadwalDokter> temp = (List<JadwalDokter>) jd.get("data");
					for (JadwalDokter jadwalDokter : temp) {
						BridgeDaftarJadwalDokterVO x = new BridgeDaftarJadwalDokterVO();
						x.setIdKlinik(jadwalDokter.getRuangan().getId().toString());
						String hari = bridgingService
								.getTanggalJadwal(DateUtil.dayOfWeek(jadwalDokter.getTanggalJadwal()));
						x.setHari(hari);
						if (CommonUtil.isNullOrEmpty(hari))
							x.setHari("");
						JadwalPraktek jp = jadwalDokter.getJadwalPraktek();
						if (CommonUtil.isNotNullOrEmpty(jp.getJamPraktek())) {
							String[] pp = jp.getJamPraktek().split("-");
							if (pp.length == 2) {
								x.setJamTutupPraktek(pp[1]);
								x.setJamMulaiPraktek(pp[0]);
							}
						} else {
							x.setJamTutupPraktek("");
							x.setJamMulaiPraktek("");
						}
						if (CommonUtil.isNotNullOrEmpty(jadwalDokter.getQuota())) {
							x.setKuota(jadwalDokter.getQuota().toString());
						} else {
							x.setKuota("30");
						}
						try {
							x.setIddokter(jadwalDokter.getDokter().getId().toString());
							x.setNamadokter(jadwalDokter.getDokter().getNamaLengkap());
						} catch (Exception ex) {
							x.setIddokter("?");
							x.setNamadokter("?");
						}
						Calendar cal = Calendar.getInstance();
						DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
						x.setUpdate(dateFormat.format(cal.getTime()));
						if (!filterTemp.contains(x)) {
							filterTemp.add(x);
							y.addBridgePendaftaranJadwalDokterVO(x);
						}

					}
				}
		}
		return y;
	}

	@RequestMapping(value = "/jadwal_dokter", method = RequestMethod.GET, produces = {
			MediaType.APPLICATION_XML_VALUE })
	public BridgeDaftarJadwalDokterListVO jadwalDokterRseRev() {
		BridgeDaftarJadwalDokterListVO y = new BridgeDaftarJadwalDokterListVO();
		List<Map<String, Object>> data = jadwalPraktikDokterService.findAllJadwalPraktikDokter();
		Calendar cal = Calendar.getInstance();
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for (Map<String, Object> map : data) {
			BridgeDaftarJadwalDokterVO bridge = new BridgeDaftarJadwalDokterVO();
			bridge.setHari(map.get("namaHari").toString());
			bridge.setIddokter(map.get("idPegawai").toString());
			bridge.setIdKlinik(map.get("idRuangan").toString());
			bridge.setJamMulaiPraktek(map.get("waktuMulai").toString());
			bridge.setJamTutupPraktek(map.get("waktuSelesai").toString());
			bridge.setKuota(map.get("quota").toString());
			bridge.setNamadokter(map.get("namaLengkap").toString());
			bridge.setUpdate(dateFormat.format(cal.getTime()));
			y.addBridgePendaftaranJadwalDokterVO(bridge);
		}
		return y;
	}

	@RequestMapping(value = "/jam_kunjungan", method = RequestMethod.GET, produces = {
			MediaType.APPLICATION_XML_VALUE })
	public BridgeDaftarJadwalJamKunjunganListVO jamKunjungan(@RequestParam(value = "idklinik") Integer idKlinik,
			@RequestParam(value = "iddokter") Integer idDokter,
			@RequestParam(value = "tanggal", required = false) String tgl) {
		BridgeDaftarJadwalJamKunjunganListVO y = new BridgeDaftarJadwalJamKunjunganListVO();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date tanggal;
		try {
			tanggal = formatter.parse(tgl);
		} catch (ParseException e) {
			throw new ServiceVOException(e.getMessage());
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(tanggal);
		Integer tanggalId = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		List<Map<String, Object>> datas = bridgingService.getJadwalKlinikbyId(idDokter, idKlinik, tanggalId);
		if (CommonUtil.isNotNullOrEmpty(datas)) {
			for (Map<String, Object> data : datas) {
				BridgeDaftarJadwalJamKunjunganVO x = new BridgeDaftarJadwalJamKunjunganVO();
				x.setIdKlinik(data.get("idklinik").toString());
				String[] pp = data.get("jam").toString().split("-");
				x.setJamMulai(pp[0]);
				x.setJamSelesai(pp[1]);
				x.setKuota("30"); // harus ada perhitungan lagi mengegnai quota,
				x.setIdDokter(data.get("iddokter").toString());
				Calendar cal = Calendar.getInstance();
				DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				x.setUpdate(dateFormat.format(cal.getTime()));
				y.addBridgePendaftaranJadwalJamKunjunganVO(x);
			}
		}
		return y;
	}

	@RequestMapping(value = "/cara_bayar", method = RequestMethod.GET, produces = { MediaType.APPLICATION_XML_VALUE })
	public BridgeDaftarCaraBayarListVO caraBayar() {
		BridgeDaftarCaraBayarListVO y = new BridgeDaftarCaraBayarListVO();
		List<Map<String, Object>> caraBayar = modelServiceRuangan.getAllData("KelompokPasien", "*", 1500, null, null,
				null, null, null, null, null, null, null, null);
		for (Map<String, Object> stringObjectMap : caraBayar) {
			BridgeDaftarCaraBayarVO x = new BridgeDaftarCaraBayarVO();
			x.setIdCaraBayar(stringObjectMap.get("id").toString());
			x.setDeskripsi(stringObjectMap.get("kelompokPasien").toString());
			y.addBridgeDaftarCaraBayarVO(x);
		}
		return y;
	}

	@RequestMapping(value = "/verifikasi_pasien", method = RequestMethod.POST, produces = {
			MediaType.APPLICATION_XML_VALUE }, consumes = { MediaType.APPLICATION_XML_VALUE })
	public Object verifikasiPasien(@RequestBody BridgeDaftarVerifikasiPasienConsumeVO vox) {
		PasienVO p = pasienService.findByNoCm(vox.getObjVO().getNoCM());
		boolean pasienExist = p != null;
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String dd_post = vox.getObjVO().getTglLahir();
		String dd = "";
		if (pasienExist)
			dd = dateFormat.format(p.getTglLahir());
		if (dd.equals(dd_post) && p != null) {
			return getProduceVO(vox, p);
		} else {
			BridgeDaftarVerifikasiPasienNotFoundReplyVO z = new BridgeDaftarVerifikasiPasienNotFoundReplyVO();
			z.setPesan("Tidak ada");
			z.setStatus(false);
			return new BridgeDaftarVerifikasiPasienNotFoundReplyProduceVO(z);
		}
	}

	private static BridgeDaftarVerifikasiPasienFoundReplyProduceVO getProduceVO(
			BridgeDaftarVerifikasiPasienConsumeVO vox, PasienVO p) {
		BridgeDaftarVerifikasiPasienFoundReplyVO y = new BridgeDaftarVerifikasiPasienFoundReplyVO();
		String notel = p.getNoTelepon();
		if (notel == null)
			notel = "";
		y.setNomorContact(notel);
		y.setNama(p.getNamaPasien());
		y.setAlamat("");
		for (AlamatVO alamatVO : p.getAlamats()) {
			y.setAlamat(alamatVO.getAlamatLengkap());
			break;
		}
		y.setTgllahir(vox.getObjVO().getTglLahir());
		y.setStatus(true);
		return new BridgeDaftarVerifikasiPasienFoundReplyProduceVO(y);
	}

	@RequestMapping(value = "/pendaftaran_pasien", method = RequestMethod.POST, produces = {
			MediaType.APPLICATION_XML_VALUE }, consumes = { MediaType.APPLICATION_XML_VALUE })
	public Object pendaftaranPasien(@RequestBody BridgeDaftarPasienDaftarConsumeVO vox) {
		RegistrasiSmsVO vo = new RegistrasiSmsVO();
		Pasien p = new Pasien();
		boolean pasienValid;
		try {
			p = pasienService.findPasienByNoCm(vox.getData().getNoCM());
			pasienValid = true;
			vo.setKodeDokter(Short.parseShort(vox.getData().getIdDokter()));
			vo.setKodeRegulerEkslusif(0);
			vo.setKodeRuangan(vox.getData().getIdPoli());
			vo.setNoCm(vox.getData().getNoCM());
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			try {
				vo.setTanggal(dateFormat.parse(vox.getData().getTglKunjungan()));
			} catch (ParseException e) {
				vo.setTanggal(new Date());
			}
			vo.setTanggallahir(p.getTglLahir());
		} catch (Exception ex) {
			pasienValid = false;
		}
		AntrianPasienRegistrasi model = new AntrianPasienRegistrasi();
		AntrianPasienRegistrasi hasil = null;
		UUID noReservasi = null;
		Pegawai pegawai = pegawaiService.getById(Integer.parseInt(vox.getData().getIdDokter()));
		if (pasienValid) {
			Ruangan ruangan = ruanganService.findRuanganByDepartemenAndKdRuangan(vo.getKodeRuangan());
			model.setPasien(p);
			model.setRuangan(ruangan);
			model.setPegawai(pegawai);
			model.setPendidikan(p.getPendidikan());
			model.setTipePasien(TipePasienEnum.LAMA.getVal());
			model.setTglReservasi(vo.getTanggal());
			model.setType(vo.getKodeRegulerEkslusif() == 1 ? "REGULER"
					: (vo.getKodeRegulerEkslusif() == 2 ? "EKSLUSIF" : ""));
			model.setJenisKelamin(p.getJenisKelamin());
			noReservasi = UUID.randomUUID();
			model.setNoReservasi(noReservasi.toString().substring(1, 8));
			List<String> antrianPasienRegistrasi = antrianPasienRegistrasiService.findAntrianPasienLamaRegistrasi(
					p.getNoCm(), Integer.parseInt(vo.getKodeRuangan()), Integer.parseInt(vox.getData().getIdDokter()),
					vox.getData().getTglKunjungan());
			if (CommonUtil.isNullOrEmpty(antrianPasienRegistrasi))
				hasil = antrianPasienRegistrasiService.simpan(model);
		}
		if (hasil != null) {
			BridgeDaftarPasienDaftarOkVO y = new BridgeDaftarPasienDaftarOkVO();
			y.setNomorReservasi(noReservasi.toString().substring(1, 8));
			y.setJamKunjungan(vox.getData().getTglKunjungan());
			y.setStatus(true);
			return new BridgeDaftarPasienDaftarOkProduceVO(y);
		} else {
			BridgeDaftarPasienDaftarFailedVO z = new BridgeDaftarPasienDaftarFailedVO();
			z.setPesan("Gagal Daftar");
			z.setStatus(false);
			return new BridgeDaftarPasienDaftarFailedProduceVO(z);
		}
	}

	@RequestMapping(value = "/pendaftaran_pasien_baru", method = RequestMethod.POST, produces = {
			MediaType.APPLICATION_XML_VALUE }, consumes = { MediaType.APPLICATION_XML_VALUE })
	public Object pendaftaranPasienBaru(@RequestBody BridgeDaftarPasienDaftarBaruConsumeVO vo) {
		AntrianPasienRegistrasi model = new AntrianPasienRegistrasi();
		AntrianPasienRegistrasi hasil = null;
		UUID noReservasi = null;
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		DateFormat hourFormat = new SimpleDateFormat("HH:mm");
		String jamKunjungan = "";
		if (CommonUtil.isNotNullOrEmpty(vo.getData())) {
			Ruangan ruangan = ruanganService.findRuanganByDepartemenAndKdRuangan(vo.getData().getIdKlinik());
			Pendidikan pendidikan = bridgingService.findByIdPendidikan(13);
			int ids = 1;
			if (vo.getData().getCarabayar().equalsIgnoreCase("jkn")
					|| vo.getData().getCarabayar().equalsIgnoreCase("jamkesda"))
				ids = 2;
			KelompokPasien kelompokPasien = bridgingService.findKelompokPasienById(ids);
			Pegawai pegawai = pegawaiService.getById(Integer.parseInt(vo.getData().getIdDokter()));
			model.setNamaPasien(vo.getData().getNamaPasien());
			model.setTempatLahir(vo.getData().getTempatLahir());
			model.setNoTelepon(vo.getData().getNomorKontak());
			model.setRuangan(ruangan);
			model.setPegawai(pegawai);
			model.setJenisPasien(kelompokPasien);
			model.setType("REGULER");
			model.setTipePasien(TipePasienEnum.BARU.getVal());
			model.setPendidikan(pendidikan);
			String tanggal = vo.getData().getTglKunjungan();
			Date tglReservasi = null;
			Date jamReservasi;
			try {
				if (CommonUtil.isNotNullOrEmpty(tanggal))
					tglReservasi = dateFormat.parse(tanggal);
				if (CommonUtil.isNotNullOrEmpty(vo.getData().getJamKunjungan())) {
					jamKunjungan = vo.getData().getJamKunjungan();
				} else {
					jamKunjungan = "10:00";
				}
				jamReservasi = hourFormat.parse(jamKunjungan);
				if (CommonUtil.isNotNullOrEmpty(vo.getData().getTglLahir())) {
					Date tglLahir = dateFormat.parse(vo.getData().getTglLahir());
					model.setTglLahir(tglLahir);
				}
				Date reservasi = concatenateTime(tglReservasi, jamReservasi);
				model.setTglReservasi(reservasi);
			} catch (ParseException e) {
				throw new ServiceVOException(e.getMessage());
			}
			noReservasi = UUID.randomUUID();
			model.setNoReservasi(noReservasi.toString().substring(1, 8));
			List<String> antrianPasienRegistrasi = antrianPasienRegistrasiService.findAntrianPasienRegistrasi(
					vo.getData().getNamaPasien(), vo.getData().getTglLahir(),
					Integer.parseInt(vo.getData().getIdKlinik()), Integer.parseInt(vo.getData().getIdDokter()),
					vo.getData().getTglKunjungan());
			if (CommonUtil.isNullOrEmpty(antrianPasienRegistrasi)) {
				hasil = antrianPasienRegistrasiService.simpan(model);
			}
		}
		if (hasil != null) {
			BridgeDaftarPasienDaftarOkVO y = new BridgeDaftarPasienDaftarOkVO();
			y.setNomorReservasi(noReservasi.toString().substring(1, 8));
			y.setJamKunjungan(jamKunjungan);
			y.setStatus(true);
			return new BridgeDaftarPasienDaftarOkProduceVO(y);
		} else {
			BridgeDaftarPasienDaftarFailedVO z = new BridgeDaftarPasienDaftarFailedVO();
			z.setPesan("Gagal Daftar");
			z.setStatus(false);
			return new BridgeDaftarPasienDaftarFailedProduceVO(z);
		}
	}

	public Date concatenateTime(Date date, Date time) {
		Calendar aDateTime = Calendar.getInstance();
		Calendar aDate = Calendar.getInstance();
		if (CommonUtil.isNotNullOrEmpty(date))
			aDate.setTime(date);
		Calendar aTime = Calendar.getInstance();
		if (CommonUtil.isNotNullOrEmpty(time))
			aTime.setTime(time);
		aDateTime.set(Calendar.DAY_OF_MONTH, aDate.get(Calendar.DAY_OF_MONTH));
		aDateTime.set(Calendar.MONTH, aDate.get(Calendar.MONTH));
		aDateTime.set(Calendar.YEAR, aDate.get(Calendar.YEAR));
		aDateTime.set(Calendar.HOUR_OF_DAY, aTime.get(Calendar.HOUR_OF_DAY));
		aDateTime.set(Calendar.MINUTE, aTime.get(Calendar.MINUTE));
		return aDateTime.getTime();
	}

	@RequestMapping(value = "/bpjs/poli/list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Object> getListPoliklinik() {
		List<Map<String, Object>> entity = ruanganService.findRajalBPJS();
		return new ResponseEntity<>(entity, HttpStatus.OK);
	}

	@RequestMapping(value = "/bpjs/dpjp/list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Object> getListDpjp() {
		List<Map<String, Object>> entity = pegawaiService.getDokterBpjs();
		return new ResponseEntity<>(entity, HttpStatus.OK);
	}

	@RequestMapping(value = "/bpjs/patient/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Object> saveNewPatient(@Valid @RequestBody PasienVO vo) {
		try {
			PasienVO result = pasienService.add(vo);
			if (result != null)
				return new ResponseEntity<>(result.getId(), HttpStatus.CREATED);
			return new ResponseEntity<>(null, HttpStatus.NOT_ACCEPTABLE);
		} catch (Exception e) {
			return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping(value = "/bpjs/medical-record-id/patient/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<String> getPatientMedicalRecordId(@PathVariable Integer id) {
		PasienVO result = pasienService.findById(id);
		if (result != null)
			return new ResponseEntity<>(result.getNoCm(), HttpStatus.OK);
		return new ResponseEntity<>(null, HttpStatus.OK);
	}

	@RequestMapping(value = "/bpjs/card-number/{noBpjs}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Boolean> checkNoBPJSPasien(@PathVariable String noBpjs) {
		PasienVO result = pasienService.findByNoBpjs(noBpjs);
		if (result != null)
			return new ResponseEntity<>(true, HttpStatus.OK);
		return new ResponseEntity<>(false, HttpStatus.OK);
	}

	@RequestMapping(value = "/bpjs/patient/get/{noRM}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Object> getListPatient(@PathVariable String noRM) {
		List<Map<String, Object>> entity = pasienService.findByNoRM(noRM);
		return new ResponseEntity<>(entity, HttpStatus.OK);
	}

	@RequestMapping(value = "/bpjs/antrian/get/{tglMasuk}/{ruanganId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Object> getCountAntrean(
			@PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate tglMasuk, @PathVariable Integer ruanganId) {
		Map<String, Object> entity = antrianService.getAntreanByRuangan(tglMasuk, ruanganId);
		return new ResponseEntity<>(entity, HttpStatus.OK);
	}

	@RequestMapping(value = "/bpjs/registration/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Object> saveNewRegistration(@RequestBody RegistrasiPasienDto dto) {
		try {
			String result = registrasiPasienService.saveRegistrasiPasien(dto);
			return new ResponseEntity<>(result, HttpStatus.CREATED);
		} catch (Exception e) {
			return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping(value = "/bpjs/antrian/status/{idPasien}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Boolean> getStatusAntrianPasien(@PathVariable Integer idPasien) {
		Boolean exists = pasienDaftarService.checkStatusPasien(idPasien);
		return new ResponseEntity<>(exists, HttpStatus.OK);
	}

	@RequestMapping(value = "/bpjs/antrian/panggil/{idRuangan}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<String> getAntrianDipanggil(@PathVariable Integer idRuangan) {
		String noantrean = antrianService.currentAntrian(idRuangan);
		return new ResponseEntity<>(noantrean, HttpStatus.OK);
	}

	@RequestMapping(value = "/bpjs/antrian/sisa/{idRuangan}/{estimasiDilayani}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Integer> getSisaAntrian(@PathVariable Integer idRuangan,
			@PathVariable Long estimasiDilayani) {
		Integer countantrean = antrianService.countSisaAntrian(idRuangan, estimasiDilayani);
		return new ResponseEntity<>(countantrean, HttpStatus.OK);
	}

	@RequestMapping(value = "/bpjs/antrian/batal", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Object> batalAntrian(@RequestBody BatalRegistrasiDto dto) {
		try {
			Map<String, Object> result = batalRegistrasiService.batalAntrian(dto);
			return new ResponseEntity<>(result, HttpStatus.CREATED);
		} catch (Exception e) {
			return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping(value = "/bpjs/operasi/jadwal/{start}/{end}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Object> getJadwalOperasiPeriodik(
			@PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate start,
			@PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
		List<Map<String, Object>> entity = jadwalOperasiService.jadwalOperasiByPeriode(start, end);
		return new ResponseEntity<>(entity, HttpStatus.OK);
	}

	@RequestMapping(value = "/bpjs/operasi/jadwal/{noBpjs}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Object> getJadwalOperasiPasien(@PathVariable String noBpjs) {
		List<Map<String, Object>> entity = jadwalOperasiService.jadwalOperasiByPasien(noBpjs);
		return new ResponseEntity<>(entity, HttpStatus.OK);
	}
}
