package com.jasamedika.medifirst2000.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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.dao.BridgingDao;
import com.jasamedika.medifirst2000.service.PasienService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.AlamatVO;
import com.jasamedika.medifirst2000.vo.PasienVO;
import com.jasamedika.medifirst2000.vo.custom.BridgeSisRuteDataVO;
import com.jasamedika.medifirst2000.vo.custom.BridgeSisRuteKartuAsuransiVO;
import com.jasamedika.medifirst2000.vo.custom.BridgeSisRuteKartuIdentitasVO;
import com.jasamedika.medifirst2000.vo.custom.BridgeSisRuteKontakVO;
import com.jasamedika.medifirst2000.vo.custom.BridgeSisRuteReplyOkVO;

@RestController
@RequestMapping("/sisrute")

public class BridgingSisruteController {
	@Autowired
	private PasienService pasienService;
	
	@Autowired
	private BridgingDao bridgingDao;
	
	@RequestMapping(value = "/data", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
	public Object cekPasienSisRute(@RequestParam("norm") String noCM) {
		BridgeSisRuteReplyOkVO y = new BridgeSisRuteReplyOkVO();
		// Map<String, Object> resultPageMap =
		// pasienService.findByNoCmAndTglLahir(1, 30, "id", "asc", noCM,
		// "", "","","");
		PasienVO ww = pasienService.findByNoCm(noCM);

		if (ww != null) {
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			BridgeSisRuteDataVO data = new BridgeSisRuteDataVO();
			data.setNorm(noCM);
			for (AlamatVO alamatVO : ww.getAlamats()) {
				data.setAlamat(alamatVO.getAlamatLengkap());
				break;
			}
			data.setJenisKelamin((ww.getJenisKelamin().getId()).toString());
			data.setNama(ww.getNamaPasien());
			data.setTanggalLahir(dateFormat.format(ww.getTglLahir()));
			data.setTempatLahir(ww.getTempatLahir());

			//added by susy 24052017
			String idBPJS = bridgingDao.findIdBPJS();
			String noAsuransi = bridgingDao.findNoAsuransi(Integer.valueOf(idBPJS), noCM);
			BridgeSisRuteKartuAsuransiVO asuransi = new BridgeSisRuteKartuAsuransiVO();
			if(CommonUtil.isNotNullOrEmpty(noAsuransi))
			{
				asuransi.setJenis("2");
				asuransi.setNomor(noAsuransi);
			}			
			else
			{
				asuransi.setJenis("1");
				asuransi.setNomor("-");
			}
			
			data.addKartuAsuransi(asuransi);

			// Pada sistem hanya tersedia dua macam: KTP dan Paspor
			if(CommonUtil.isNotNullOrEmpty(ww.getNoIdentitas()))
			{
				BridgeSisRuteKartuIdentitasVO identitas = new BridgeSisRuteKartuIdentitasVO();
				identitas.setJenis("1");
				identitas.setNomor(ww.getNoIdentitas());
				identitas.setAlamat("");
				data.addKartuIdentitas(identitas);
			}
			if(CommonUtil.isNotNullOrEmpty(ww.getPaspor()))
			{
				BridgeSisRuteKartuIdentitasVO identitas = new BridgeSisRuteKartuIdentitasVO();
				identitas.setJenis("4");
				identitas.setNomor(ww.getPaspor());
				identitas.setAlamat("");
				data.addKartuIdentitas(identitas);
			}

			//modified by susy 24052017
			if(CommonUtil.isNotNullOrEmpty(ww.getNoTelepon()))
			{
				BridgeSisRuteKontakVO t1 = new BridgeSisRuteKontakVO();
				t1.setJenis("1");
				t1.setNomor(ww.getNoTelepon());
				data.addKontak(t1);
			}
			if(CommonUtil.isNotNullOrEmpty(ww.getNoHp()))
			{
				BridgeSisRuteKontakVO t1 = new BridgeSisRuteKontakVO();
				t1.setJenis("3");
				t1.setNomor(ww.getNoHp());
				data.addKontak(t1);
			}
			y.setDetail("Pasien Ditemukan");
			y.setStatus("200");
			y.setData(data);
		} else {
			y.setDetail("Pasien tidak Ditemukan");
			y.setStatus("404");
			y.setData(null);
		}
		return y;
	}
}
