package com.jasamedika.medifirst2000.service.impl;

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.RencanaDao;
import com.jasamedika.medifirst2000.entities.AntrianPasienDiPeriksa;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.Rencana;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.service.RencanaService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.AntrianPasienDiPeriksaVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import com.jasamedika.medifirst2000.vo.RencanaCustomVO;
import com.jasamedika.medifirst2000.vo.RencanaVO;
import com.jasamedika.medifirst2000.vo.RuanganVO;

@Service("RencanaService")
public class RencanaServiceImpl  extends BaseVoServiceImpl implements RencanaService {

	@Autowired
	private BaseConverterImpl<RuanganVO, Ruangan> ruanganConverter; 
	
	@Autowired
	private BaseConverterImpl<PegawaiVO, Pegawai> pegawaiConverter; 
	
	@Autowired
	private BaseConverterImpl<RencanaVO, Rencana> rencanaConverter;
	
	@Autowired
	private BaseConverterImpl<AntrianPasienDiPeriksaVO, AntrianPasienDiPeriksa> antrianPasienDiperiksaConverter; 
	
	@Autowired
	private RencanaDao rencanaDao;
	
	@Override
	public Map<String, Object> saveRencana(RencanaCustomVO vo) {
		Map<String, Object> result = new HashMap<String, Object>();
		if(CommonUtil.isNotNullOrEmpty(vo))
		{
			for(RencanaVO voTemp : vo.getRencana())
			{
				Rencana rencana = rencanaConverter.transferVOToModel(voTemp, new Rencana());
				if(CommonUtil.isNotNullOrEmpty(voTemp.getRuangan()))
				{
					Ruangan ruangan = ruanganConverter.transferVOToModel(voTemp.getRuangan(), new Ruangan());
					rencana.setRuangan(ruangan);
				}
				if(CommonUtil.isNotNullOrEmpty(voTemp.getPetugas()))
				{
					rencana.setPetugas(pegawaiConverter.transferVOToModel(voTemp.getPetugas(), new Pegawai()));
				}
				if(CommonUtil.isNotNullOrEmpty(voTemp.getPasienDaftar()))
				{
					rencana.setPasien(antrianPasienDiperiksaConverter.transferVOToModel(voTemp.getPasienDaftar(), new AntrianPasienDiPeriksa()));
				}
				
				if(CommonUtil.isNotNullOrEmpty(rencana))
				{
					Rencana model = rencanaDao.save(rencana);
					result.put(model.getTanggalInput()+"", model.getNoRec());
				}				
			}
		}
		
		return result;
	}

	@Override
	public List<Map<String, Object>> getRencana(String noCm) {
		List<Map<String, Object>> result = rencanaDao.findRencana(noCm);
		return result;
	}

}
