package com.jasamedika.medifirst2000.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.RehabilitasiFormulirKlaimDao;
import com.jasamedika.medifirst2000.entities.AntrianPasienDiPeriksa;
import com.jasamedika.medifirst2000.entities.DiagnosaTindakan;
import com.jasamedika.medifirst2000.entities.DiagnosaTindakans;
import com.jasamedika.medifirst2000.entities.RehabilitasiAsesmen;
import com.jasamedika.medifirst2000.entities.RehabilitasiFormulirKlaim;
import com.jasamedika.medifirst2000.service.RehabilitasiKlaimFormulirService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.AntrianPasienDiPeriksaVO;
import com.jasamedika.medifirst2000.vo.DiagnosaTindakanVO;
import com.jasamedika.medifirst2000.vo.DiagnosaTindakansVO;
import com.jasamedika.medifirst2000.vo.RehabilitasiAsesmenVO;
import com.jasamedika.medifirst2000.vo.RehabilitasiFormulirKlaimVO;

@Service("RehabilitasiKlaimFormulirService")
public class RehabilitasiFormulirKlaimServiceImpl implements RehabilitasiKlaimFormulirService {
	@Autowired
	BaseConverterImpl<RehabilitasiFormulirKlaimVO, RehabilitasiFormulirKlaim> rehabilitasiFormulirKlaimConverter;
	
	@Autowired
	BaseConverterImpl< AntrianPasienDiPeriksaVO, AntrianPasienDiPeriksa> antrianPasienDiPeriksaConverter;

	@Autowired
	BaseConverterImpl<DiagnosaTindakanVO, DiagnosaTindakan> diagnosaTindakanConverter;
	
	@Autowired
	RehabilitasiFormulirKlaimDao rehabilitasiFormulirKlaimDao;

	@Override
	public Map<String, Object> saveRehabilitasiKlaimFormulir(RehabilitasiFormulirKlaimVO vo) {
		Map<String, Object> result = new HashMap<String, Object>();
		Set<DiagnosaTindakans> diagnosaTindakanSet = new HashSet<DiagnosaTindakans>();
		if (CommonUtil.isNotNullOrEmpty(vo)) {
			RehabilitasiFormulirKlaim rehabilitasiFormulirKlaim = rehabilitasiFormulirKlaimConverter
					.transferVOToModel(vo, new RehabilitasiFormulirKlaim());
			for (DiagnosaTindakansVO vos : vo.getDiagnosisMedis()) {
				DiagnosaTindakans diagnosaTindakans = new DiagnosaTindakans();
				diagnosaTindakans.setDiagnosisMedis(true);
				if (CommonUtil.isNotNullOrEmpty(vos.getDiagnosaTindakan())) {
					diagnosaTindakans.setDiagnosaTindakan(diagnosaTindakanConverter
							.transferVOToModel(vos.getDiagnosaTindakan(), new DiagnosaTindakan()));
				}
				diagnosaTindakans.setRehabilitasiFormulirKlaim(rehabilitasiFormulirKlaim);
				diagnosaTindakanSet.add(diagnosaTindakans);
			}
			for (DiagnosaTindakansVO vos : vo.getDiagnosisFungsi()) {
				DiagnosaTindakans diagnosaTindakans = new DiagnosaTindakans();
				diagnosaTindakans.setDiagnosisFungsi(true);
				if (CommonUtil.isNotNullOrEmpty(vos.getDiagnosaTindakan())) {
					diagnosaTindakans.setDiagnosaTindakan(diagnosaTindakanConverter
							.transferVOToModel(vos.getDiagnosaTindakan(), new DiagnosaTindakan()));
				}
				diagnosaTindakans.setRehabilitasiFormulirKlaim(rehabilitasiFormulirKlaim);
				diagnosaTindakanSet.add(diagnosaTindakans);
			}
			for (DiagnosaTindakansVO vos : vo.getTataLaksanaKFR()) {
				DiagnosaTindakans diagnosaTindakans = new DiagnosaTindakans();
				diagnosaTindakans.setTataLaksanaKFR(true);
				if (CommonUtil.isNotNullOrEmpty(vos.getDiagnosaTindakan())) {
					diagnosaTindakans.setDiagnosaTindakan(diagnosaTindakanConverter
							.transferVOToModel(vos.getDiagnosaTindakan(), new DiagnosaTindakan()));
				}
				diagnosaTindakans.setRehabilitasiFormulirKlaim(rehabilitasiFormulirKlaim);
				diagnosaTindakanSet.add(diagnosaTindakans);
			}
			rehabilitasiFormulirKlaim.setDiagnosaMedis(diagnosaTindakanSet);
			if(CommonUtil.isNotNullOrEmpty(vo.getPasienDaftar()))
			{
				rehabilitasiFormulirKlaim.setPasien(antrianPasienDiPeriksaConverter.transferVOToModel(vo.getPasienDaftar(), new AntrianPasienDiPeriksa()));				
			}
			if(CommonUtil.isNotNullOrEmpty(vo.getNoRec()))
			{
				rehabilitasiFormulirKlaim.setNoRec(vo.getNoRec());
			}
			if(CommonUtil.isNotNullOrEmpty(rehabilitasiFormulirKlaim))
			{
				rehabilitasiFormulirKlaim.setTglInput(new Date());
				RehabilitasiFormulirKlaim model = rehabilitasiFormulirKlaimDao.save(rehabilitasiFormulirKlaim);
				result.put("noRec", model.getNoRec());
			}
		}
		return result;
	}

	@Override
	public List<Map<String, Object>> getListRehabilitasiKlaimFormulir(String noRec) {
		List<Map<String, Object>> result = rehabilitasiFormulirKlaimDao.getListRehabilitasiFormulirKlaim(noRec);
		for(Map<String, Object> data : result)
		{
			String norecRehab = data.get("noRec")+"";			
			data.put("diagnosisMedis", rehabilitasiFormulirKlaimDao.getDiagnosaMedis(norecRehab));
			data.put("diagnosisFungsi", rehabilitasiFormulirKlaimDao.getDiagnosaFungsi(norecRehab));
			data.put("tataLaksanaKFR", rehabilitasiFormulirKlaimDao.getDiagnostataLaksanaKFR(norecRehab));
		}
		return result;
	}

	@Override
	public Map<String, Object> getRehabilitasiKlaimFormulir(String noRec) {
		Map<String, Object> data = rehabilitasiFormulirKlaimDao.getRehabilitasiFormulirKlaim(noRec);
		String norecRehab = data.get("noRec")+"";			
		data.put("diagnosisMedis", rehabilitasiFormulirKlaimDao.getDiagnosaMedis(norecRehab));
		data.put("diagnosisFungsi", rehabilitasiFormulirKlaimDao.getDiagnosaFungsi(norecRehab));
		data.put("tataLaksanaKFR", rehabilitasiFormulirKlaimDao.getDiagnostataLaksanaKFR(norecRehab));
		
		return data;
	}
	

}
