package com.jasamedika.medifirst2000.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.converter.PapObstetriConverter;
import com.jasamedika.medifirst2000.dao.AntrianPasienDiPeriksaDao;
import com.jasamedika.medifirst2000.dao.KeadaanUmumObstetriDetailDao;
import com.jasamedika.medifirst2000.dao.PapObstetriDao;
import com.jasamedika.medifirst2000.dao.PasienDao;
import com.jasamedika.medifirst2000.dao.RiwayatKehamilanObstetriDao;
import com.jasamedika.medifirst2000.entities.AntrianPasienDiPeriksa;
import com.jasamedika.medifirst2000.entities.DetailRiwayatObstetri;
import com.jasamedika.medifirst2000.entities.KeadaanUmumObstetri;
import com.jasamedika.medifirst2000.entities.KeadaanUmumObstetriDetail;
import com.jasamedika.medifirst2000.entities.KeteranganLainLainDetail;
import com.jasamedika.medifirst2000.entities.PapObstetri;
import com.jasamedika.medifirst2000.entities.RiwayatKehamilanObstetri;
import com.jasamedika.medifirst2000.service.PapObstetriService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.DetailRiwayatObstetriVO;
import com.jasamedika.medifirst2000.vo.KeadaanUmumObstetriDetailVO;
import com.jasamedika.medifirst2000.vo.PapObstetriVO;
import com.jasamedika.medifirst2000.vo.RiwayatKehamilanObstetriVO;

/**
 * Implement class for PAPObsetriService
 * 
 * @author Askur
 */
@Service("papObstetriService")
public class PapObstetriServiceImpl implements PapObstetriService {
	@Autowired
	private PapObstetriDao papObstetriDao;
	@Autowired
	private RiwayatKehamilanObstetriDao riwayatKehamilanObstetriDao;
	@Autowired
	private KeadaanUmumObstetriDetailDao keadaanUmumObstetriDetailDao;
	@Autowired
	private AntrianPasienDiPeriksaDao antrianPasienDiPeriksaDao;
	@Autowired
	private PapObstetriConverter papObstetriConverter;
	@Autowired
	private BaseConverterImpl<DetailRiwayatObstetriVO, DetailRiwayatObstetri> detailRiwayatObstetriConverter;
	@Autowired
	private BaseConverterImpl<RiwayatKehamilanObstetriVO, RiwayatKehamilanObstetri> riwayatKehamilanObstetriConverter;
	@Autowired
	private BaseConverterImpl<KeadaanUmumObstetriDetailVO, KeadaanUmumObstetriDetail> keadaanUmumObstetriDetailConverter;
	@Autowired
	private PasienDao pasienDao;
	@Override
	public Map<String, Object> savePapObstetri(PapObstetriVO vo) {
		Map<String,Object> result=new HashMap<String,Object>();
		PapObstetri model=papObstetriConverter.transferVOToModel(vo, new PapObstetri());
		AntrianPasienDiPeriksa pasienDaftar = antrianPasienDiPeriksaDao.findByNoRec(vo.getNoRec());
		model.setPasien(pasienDaftar);
		for(RiwayatKehamilanObstetri voLocal:riwayatKehamilanObstetriDao.findByRiwayatKehamilan(model.getNoRec())){							
			riwayatKehamilanObstetriDao.delete(voLocal);
		}

		for(KeadaanUmumObstetriDetail voLocal:keadaanUmumObstetriDetailDao.findByKeadaanUmumObstetri(model.getNoRec())){							
			keadaanUmumObstetriDetailDao.delete(voLocal);
		}

		List<RiwayatKehamilanObstetri> detailKehamilan=new ArrayList<RiwayatKehamilanObstetri>();
		for(RiwayatKehamilanObstetriVO voDetail:vo.getRiwayatKehamilanObstestri()){
			RiwayatKehamilanObstetri modelDetail=riwayatKehamilanObstetriConverter.transferVOToModel(voDetail, new RiwayatKehamilanObstetri());
			modelDetail.setPapObstetri(model);
			detailKehamilan.add(modelDetail);
		}
		model.getRiwayatKehamilanObstestri().clear();
		model.getRiwayatKehamilanObstestri().addAll(detailKehamilan);
		List<KeadaanUmumObstetriDetail> detailKeadaanUmumObstetri=new ArrayList<KeadaanUmumObstetriDetail>();
		for(KeadaanUmumObstetriDetailVO voDetail:vo.getKeadaanUmumObstetriDetail()){
			KeadaanUmumObstetriDetail modelDetail=keadaanUmumObstetriDetailConverter.transferVOToModel(voDetail, new KeadaanUmumObstetriDetail());
			modelDetail.setPapObstetri(model);
			detailKeadaanUmumObstetri.add(modelDetail);
		}
		model.getKeadaanUmumObstetriDetail().clear();
		model.getKeadaanUmumObstetriDetail().addAll(detailKeadaanUmumObstetri);
		PapObstetri resultModel=papObstetriDao.save(model);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("obstetri", resultModel.getNoRec());
		}
		return result;	
		}
	
}
