package com.jasamedika.medifirst2000.service.impl;

import java.util.HashMap;
import java.util.Map;

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

import com.jasamedika.medifirst2000.converter.AntrianPasienDiPeriksaConverter;
import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.converter.JenisDietConverter;
import com.jasamedika.medifirst2000.converter.PasienConverter;
import com.jasamedika.medifirst2000.converter.PreskripsiDietConverter;
import com.jasamedika.medifirst2000.converter.TindakLanjutGiziConverter;
import com.jasamedika.medifirst2000.dao.AsesmenGiziAwalDao;
import com.jasamedika.medifirst2000.dao.PreskripsiDietDao;
import com.jasamedika.medifirst2000.dao.TindakLanjutGiziDao;
import com.jasamedika.medifirst2000.entities.AntrianPasienDiPeriksa;
import com.jasamedika.medifirst2000.entities.AsesmenGiziAwal;
import com.jasamedika.medifirst2000.entities.JenisDiet;
import com.jasamedika.medifirst2000.entities.Pasien;
import com.jasamedika.medifirst2000.entities.PreskripsiDiet;
import com.jasamedika.medifirst2000.entities.TindakLanjutGizi;
import com.jasamedika.medifirst2000.service.AsesmenGiziAwalService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.AsesmenGiziAwalVO;
import com.jasamedika.medifirst2000.vo.PreskripsiDietVO;
import com.jasamedika.medifirst2000.vo.TindakLanjutGiziVO;




/**
 * Implement class for PreskripsiDietService
 * 
 * @author Adik
 * @param <T>
 */


@Service("asesmenGiziAwalService")
public class AsesmenGiziAwalServiceImpl extends BaseVoServiceImpl implements AsesmenGiziAwalService {
	@Autowired
	private AntrianPasienDiPeriksaConverter pasienConverter;
	
	@Autowired
	private PreskripsiDietConverter preskripsiDietConverter;
	@Autowired
	private PreskripsiDietDao preskripsiDietDao;
	
	@Autowired
	private TindakLanjutGiziDao tindakLanjutGiziDao;
	@Autowired
	private TindakLanjutGiziConverter tindakLanjutGiziConverter;
	
	@Autowired
	private AsesmenGiziAwalDao asesmenGiziAwalDao;
	
	@Autowired
	private JenisDietConverter jenisDietConverter;
	@Autowired
	private BaseConverterImpl<AsesmenGiziAwalVO, AsesmenGiziAwal> asesmentConverter;
	
	

	@Override
	public Map<String, Object> saveAsesmenGiziAwal(AsesmenGiziAwalVO vo) {
		AsesmenGiziAwal asesmenGiziAwal=new AsesmenGiziAwal();
		asesmentConverter.setUseGson(true);
		asesmenGiziAwal = asesmentConverter.transferVOToModel(vo, asesmenGiziAwal);
		if(CommonUtil.isNotNullOrEmpty(vo.getPasien())){
			asesmenGiziAwal.setPasien(pasienConverter.transferVOToModel(vo.getPasien(), new AntrianPasienDiPeriksa()));
		}
		asesmenGiziAwal.setTanggalPendaftaran(vo.getTanggalPendaftaran());
		
		for (PreskripsiDiet iterable_element : preskripsiDietDao.findByAsesmenGiziAwalNoRec(asesmenGiziAwal.getNoRec())) {
			preskripsiDietDao.delete(iterable_element);
		}
		for (TindakLanjutGizi iterable_element : tindakLanjutGiziDao.findByAsesmenGiziAwalNoRec( asesmenGiziAwal.getNoRec())) {
			tindakLanjutGiziDao.delete(iterable_element);
		}
		asesmenGiziAwal=asesmenGiziAwalDao.save(asesmenGiziAwal);
		
		for(PreskripsiDietVO preskripsiDietVO:vo.getPreskripsiDiet()){
			if(CommonUtil.isNotNullOrEmpty(preskripsiDietVO)){
				PreskripsiDiet preskripsiDiet=preskripsiDietConverter.transferVOToModel(preskripsiDietVO, new PreskripsiDiet());
				preskripsiDiet.setAsesmenGiziAwal(asesmenGiziAwal);
				preskripsiDiet.setJenisDiet(jenisDietConverter.transferVOToModel(preskripsiDietVO.getJenisDiet(), new JenisDiet()));
				preskripsiDietDao.save(preskripsiDiet);
			}
		}
		
		for(TindakLanjutGiziVO tindakLanjutGiziVO:vo.getTindakLanjutGizi()){
			if(CommonUtil.isNotNullOrEmpty(tindakLanjutGiziVO)){
				TindakLanjutGizi tindakLanjutGizi=tindakLanjutGiziConverter.transferVOToModel(tindakLanjutGiziVO, new TindakLanjutGizi());
				tindakLanjutGizi.setAsesmenGiziAwal(asesmenGiziAwal);
				tindakLanjutGiziDao.save(tindakLanjutGizi);
				//asesmenGiziAwal.getTindakLanjutGizis().add(tindakLanjutGizi);
			}
		}
		Map<String,Object> resultAsesmentGiziAwal=new HashMap<String,Object>();
		resultAsesmentGiziAwal.put("noRec", asesmenGiziAwal.getNoRec());
//		resultAsesmentGiziAwal.put("namaPasien", asesmenGiziAwal.getPasien().getPasienDaftar().getPasien().getNamaPasien());
//		resultAsesmentGiziAwal.put("noCm", asesmenGiziAwal.getPasien().getPasienDaftar().getPasien().getNoCm());
		return resultAsesmentGiziAwal;
	}

}
