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.MappingDiagnosaKeperawatanDao;
import com.jasamedika.medifirst2000.entities.DetailImplementasi;
import com.jasamedika.medifirst2000.entities.DetailIntervensi;
import com.jasamedika.medifirst2000.entities.DiagnosaKeperawatan;
import com.jasamedika.medifirst2000.entities.Implementasi;
import com.jasamedika.medifirst2000.entities.Intervensi;
import com.jasamedika.medifirst2000.entities.MappingDiagnosaKeperawatan;
import com.jasamedika.medifirst2000.service.MappingDiagnosaKeperawatanService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.DetailImplementasiVO;
import com.jasamedika.medifirst2000.vo.DetailIntervensiVO;
import com.jasamedika.medifirst2000.vo.DiagnosaKeperawatanVO;
import com.jasamedika.medifirst2000.vo.ImplementasiVO;
import com.jasamedika.medifirst2000.vo.IntervensiVO;
import com.jasamedika.medifirst2000.vo.MappingDiagnosaKeperawatanVO;

@Service("MappingDiagnosaKeperawatanService")
public class MappingDiagnosaKeperawatanServiceImpl extends BaseVoServiceImpl implements MappingDiagnosaKeperawatanService {

	@Autowired
	private BaseConverterImpl<MappingDiagnosaKeperawatanVO, MappingDiagnosaKeperawatan> mappingDiagnosaKeperawatanConverter; 
	
	@Autowired
	private BaseConverterImpl<DetailImplementasiVO, DetailImplementasi> detailImplementasiConverter; 
	
	@Autowired
	private BaseConverterImpl<ImplementasiVO, Implementasi> implementasiConverter; 
	
	@Autowired
	private BaseConverterImpl<DiagnosaKeperawatanVO, DiagnosaKeperawatan> diagnosaKeperawatanConverter; 
	
	@Autowired
	private BaseConverterImpl<DetailIntervensiVO, DetailIntervensi> detailIntervensiConverter; 
	
	@Autowired
	private BaseConverterImpl<IntervensiVO, Intervensi> intervensiConverter; 
	
	@Autowired
	private MappingDiagnosaKeperawatanDao mappingDiagnosaKeperawatanDao;
	
	@Override
	public Map<String, Object> saveMappingDiagnosaKeperawatan(MappingDiagnosaKeperawatanVO vo) {
		Map<String, Object> result = new HashMap<String, Object>();
		Set<DetailImplementasi> setDetailImplementasi = new HashSet<DetailImplementasi>();
		Set<DetailIntervensi> setDetailIntervensi = new HashSet<DetailIntervensi>();
		if(CommonUtil.isNotNullOrEmpty(vo))
		{
			MappingDiagnosaKeperawatan mappingDiagnosaKeperawatan = mappingDiagnosaKeperawatanConverter.transferVOToModel(vo, new MappingDiagnosaKeperawatan());
			if(CommonUtil.isNotNullOrEmpty(vo.getDiagnosaKeperawatan()))
			{
				mappingDiagnosaKeperawatan.setDiagnosaKeperawatan(diagnosaKeperawatanConverter.transferVOToModel(vo.getDiagnosaKeperawatan(), new DiagnosaKeperawatan()));
			}
			if(CommonUtil.isNotNullOrEmpty(vo.getImplementasi()))
			{
				for(DetailImplementasiVO temp : vo.getImplementasi())
				{
					DetailImplementasi detailImplementasi = detailImplementasiConverter.transferVOToModel(temp, new DetailImplementasi());
					if(CommonUtil.isNotNullOrEmpty(temp.getImplementasi()))
					{
						detailImplementasi.setImplementasi(implementasiConverter.transferVOToModel(temp.getImplementasi(), new Implementasi()));
					}					
					detailImplementasi.setMappingDiagnosaKeperawatan(mappingDiagnosaKeperawatan);
					setDetailImplementasi.add(detailImplementasi);
				}
			}			
			if(CommonUtil.isNotNullOrEmpty(vo.getIntervensi())){
				for(DetailIntervensiVO temp : vo.getIntervensi())
				{
					DetailIntervensi detailIntervensi = detailIntervensiConverter.transferVOToModel(temp, new DetailIntervensi());
					if(CommonUtil.isNotNullOrEmpty(temp.getIntervensi()))
					{
						detailIntervensi.setIntervensi(intervensiConverter.transferVOToModel(temp.getIntervensi(), new Intervensi()));
					}
					detailIntervensi.setMappingDiagnosaKeperawatan(mappingDiagnosaKeperawatan);
					setDetailIntervensi.add(detailIntervensi);
				}
				
			}
			mappingDiagnosaKeperawatan.getImplementasi().clear();
			mappingDiagnosaKeperawatan.getIntervensi().clear();
			mappingDiagnosaKeperawatan.setImplementasi(setDetailImplementasi);
			mappingDiagnosaKeperawatan.setIntervensi(setDetailIntervensi);
			if(CommonUtil.isNotNullOrEmpty(mappingDiagnosaKeperawatan))
			{
				MappingDiagnosaKeperawatan model = mappingDiagnosaKeperawatanDao.save(mappingDiagnosaKeperawatan);
				result.put("noRec", model.getNoRec());
			}
		}
		return result;
	}

}
