package com.jasamedika.medifirst2000.service.impl;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.stereotype.Service;

import com.jasamedika.medifirst2000.converter.PapAnamesisConverter;
import com.jasamedika.medifirst2000.converter.PapRiwayatPenyakitPengobatanConverter;
import com.jasamedika.medifirst2000.converter.PasienConverter;
import com.jasamedika.medifirst2000.converter.RegistrasiPelayananPasienConverter;
import com.jasamedika.medifirst2000.dao.PapAnamesisDao;
import com.jasamedika.medifirst2000.entities.PapAnamesis;
import com.jasamedika.medifirst2000.entities.PapRiwayatPenyakitPengobatan;
import com.jasamedika.medifirst2000.entities.Pasien;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.InputPapAnamesisService;
import com.jasamedika.medifirst2000.vo.InputPAPAnamesisVO;
import com.jasamedika.medifirst2000.vo.PapAnamesisVO;
import com.jasamedika.medifirst2000.vo.PapRiwayatPenyakitPengobatanVO;
import com.jasamedika.medifirst2000.vo.PasienVO;

/**
 * Implement class for PAPAnamesisService
 * 
 * @author Askur
 */
@Service("InputPAPAnamesisService")
public class InputPapAnamesisServiceImpl implements InputPapAnamesisService {
	@Autowired
	private PapAnamesisDao papAnamesisDao;
	@Autowired
	private PapAnamesisConverter papAnamesisConverter;
	@Autowired
	private PapRiwayatPenyakitPengobatanConverter papRiwayatPenyakitPengobatanConverter;
	@Autowired
	private PasienConverter pasienConverter;
	@Autowired
	private RegistrasiPelayananPasienConverter registrasiPelayananPasienConverter;
	
	@Override
	public InputPAPAnamesisVO add(InputPAPAnamesisVO vo) throws JpaSystemException, ServiceVOException {
		PapAnamesis model=papAnamesisConverter.transferVOToModel(vo.getpAPAnamesis(), new PapAnamesis());
		Pasien pasien=pasienConverter.transferVOToModel(vo.getPasien(), new Pasien());
		List<PapRiwayatPenyakitPengobatan> list=new ArrayList<PapRiwayatPenyakitPengobatan>();
		PapRiwayatPenyakitPengobatan papRiwayatPenyakitPengobatan=null;
		for(PapRiwayatPenyakitPengobatanVO abc:vo.getpAPRiwayatPenyakitPengobatans()){
			papRiwayatPenyakitPengobatan=papRiwayatPenyakitPengobatanConverter.transferVOToModel(abc, new PapRiwayatPenyakitPengobatan());
			papRiwayatPenyakitPengobatan.setPapAnamesis(model);
			list.add(papRiwayatPenyakitPengobatan);
		}
		model.setPasien(pasien);
		model.setRegistrasiPelayananPasien(null);
		model.getPapRiwayatPenyakitPengobatanSet().addAll(list);
		PapAnamesis resultModel=papAnamesisDao.save(model);
		InputPAPAnamesisVO resultVo=new InputPAPAnamesisVO();
		//PAPAnamesisVO anamesisVo=pAPAnamesisConverter.transferModelToVO(resultModel, new PAPAnamesisVO());
		PapAnamesisVO anamesisVo=new PapAnamesisVO();
		PasienVO pasienVo=new PasienVO();
		pasienVo.setNoCm(resultModel.getPasien().getNoCm());
		anamesisVo.setPasien(pasienVo);
		anamesisVo.setTglInput(resultModel.getTglInput());
		resultVo.setpAPAnamesis(anamesisVo);
		return resultVo;
	}

	@Override
	public InputPAPAnamesisVO update(InputPAPAnamesisVO vo) throws JpaSystemException, ServiceVOException {
		/*
		PAPAnamesis model=pAPAnamesisDao.findOne(vo.getNoRec());
		PAPAnamesis resultModel=pAPAnamesisDao.save(model);
		PAPAnamesisVO resultVo=pAPAnamesisConverter.transferModelToVO(resultModel, new PAPAnamesisVO());
		return resultVo;
		*/
		return null;
	}

	@Override
	public Boolean delete(String key) throws JpaSystemException {
		return null;
	}

	@Override
	public InputPAPAnamesisVO findById(String key) throws JpaSystemException {
		return null;
	}

	@Override
	public List<InputPAPAnamesisVO> findAll() throws JpaSystemException {
		return null;
	}

	@Override
	public Map<String, Object> findAllWithPageAndLimitAndSortByAndDirectionParameter(Integer page, Integer limit,
			String sort, String dir) {
		return null;
	}

	@Override
	public PapAnamesisVO findByNoCmAndTglInput(String noCm, Date tglInput) {
		PapAnamesis resultModel=papAnamesisDao.findPapAnamesisByNoCmAndTglInput(noCm, tglInput);
		PapAnamesisVO resultVo=papAnamesisConverter.transferModelToVO(resultModel, new PapAnamesisVO());
		return resultVo;
	}


}
