package com.jasamedika.medifirst2000.service.impl;

import java.util.HashMap;
import java.util.HashSet;
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.ReportingDao;
import com.jasamedika.medifirst2000.dao.ResumeMedisDao;
import com.jasamedika.medifirst2000.entities.AntrianPasienDiPeriksa;
import com.jasamedika.medifirst2000.entities.DetailTerapiPulang;
import com.jasamedika.medifirst2000.entities.LanjutanPengobatan;
import com.jasamedika.medifirst2000.entities.ResumeMedis;
import com.jasamedika.medifirst2000.entities.StatusPulang;
import com.jasamedika.medifirst2000.service.ResumeMedisService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.AntrianPasienDiPeriksaVO;
import com.jasamedika.medifirst2000.vo.DetailTerapiPulangVO;
import com.jasamedika.medifirst2000.vo.LanjutanPengobatanVO;
import com.jasamedika.medifirst2000.vo.ResumeMedisVO;
import com.jasamedika.medifirst2000.vo.StatusPulangVO;

@Service("ResumeMedisService")
public class ResumeMedisServiceImpl extends BaseVoServiceImpl implements ResumeMedisService{
	
	@Autowired
	private ResumeMedisDao resumeMedisDao;
	
	@Autowired
	private ReportingDao reportingDao;	
	
	@Autowired
	private BaseConverterImpl<ResumeMedisVO, ResumeMedis> resumeMedisConverter;
	
	@Autowired
	private BaseConverterImpl<LanjutanPengobatanVO, LanjutanPengobatan> lanjutanPengobatanConverter;
	
	@Autowired
	private BaseConverterImpl<StatusPulangVO, StatusPulang> statusPulangConverter;
	
	@Autowired
	private BaseConverterImpl<DetailTerapiPulangVO, DetailTerapiPulang> detailTerapiPulangConverter;
	
	@Autowired
	private BaseConverterImpl<AntrianPasienDiPeriksaVO , AntrianPasienDiPeriksa> antrianPasienDiPeriksaConverter;

	@Override
	public Map<String, Object> saveResumeMedis(ResumeMedisVO vo) {
		Map<String, Object> result = new HashMap<String, Object>();
		if(CommonUtil.isNotNullOrEmpty(vo))
		{
			ResumeMedis resumeMedis = resumeMedisConverter.transferVOToModel(vo, new ResumeMedis());
			resumeMedis.setPasien(antrianPasienDiPeriksaConverter.transferVOToModel(vo.getPasienDaftar(), new AntrianPasienDiPeriksa()));
			resumeMedis.setKondisiWaktuKeluar(statusPulangConverter.transferVOToModel(vo.getKondisiWaktuKeluar(), new StatusPulang()));
			resumeMedis.setPengobatanDilanjutkan(lanjutanPengobatanConverter.transferVOToModel(vo.getPengobatanDilanjutkan(), new LanjutanPengobatan()));
			Set<DetailTerapiPulang> detailTerapiPulangSet = new HashSet<DetailTerapiPulang>();
			for(DetailTerapiPulangVO vos : vo.getDetailTerapiPulang())
			{
				DetailTerapiPulang detailTerapiPulang = detailTerapiPulangConverter.transferVOToModel(vos, new DetailTerapiPulang());
				detailTerapiPulang.setResumeMedis(resumeMedis);
				detailTerapiPulangSet.add(detailTerapiPulang);
			}
			resumeMedis.getDetailTerapiPulang().clear();
			resumeMedis.setDetailTerapiPulang(detailTerapiPulangSet);
			
			if(CommonUtil.isNotNullOrEmpty(resumeMedis))
			{
				ResumeMedis model = resumeMedisDao.save(resumeMedis);
				result.put("noRec", model.getNoRec());				
			}
		}
		return result;
	}

	@Override
	public Map<String, Object> getResumeMedis(String noRec) {
		Map<String, Object> result = reportingDao.findResumeMedis(noRec);
		result.put("terapiPulang", reportingDao.findDetailTerapiPulang(noRec));
		return result;
	}
	
}
