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.orm.jpa.JpaSystemException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.PioDao;
import com.jasamedika.medifirst2000.entities.DetailPio;
import com.jasamedika.medifirst2000.entities.Pasien;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.Pio;
import com.jasamedika.medifirst2000.entities.Produk;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.PioService;
import com.jasamedika.medifirst2000.vo.DetailPioVO;
import com.jasamedika.medifirst2000.vo.PasienVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import com.jasamedika.medifirst2000.vo.PioVO;
import com.jasamedika.medifirst2000.vo.ProdukVO;


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


@Service("pioService")
public class PioServiceImpl extends BaseVoServiceImpl implements PioService {

	@Autowired
	private PioDao pioDao;
	
	@Autowired
	private BaseConverterImpl<PioVO, Pio> pioConverter;
	
	@Autowired
	private BaseConverterImpl<ProdukVO, Produk> produkConverter;
	
	@Autowired
	private BaseConverterImpl<DetailPioVO, DetailPio> detailPioConverter;
	
	@Autowired
	private BaseConverterImpl<PegawaiVO, Pegawai> pegawaiConverter;
	
	@Autowired
	private BaseConverterImpl<PasienVO, Pasien> pasienConverter;
	@Override
	public Object add(Object vo) throws JpaSystemException, ServiceVOException {
		return null;
	}

	@Override
	public Object update(Object vo) throws JpaSystemException, ServiceVOException {
		return null;
	}

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

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

	@Override
	public List findAll() throws JpaSystemException {
		List<PioVO> vos=new ArrayList<PioVO>();
		List<Pio> models=(List<Pio>) pioDao.findAll();
		pioConverter.transferListOfModelToListOfVO(models, vos);
		return vos;
	}

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

	
	@Override
	@Transactional
	public Map<String,Object> savePio(PioVO vo) throws JpaSystemException, ServiceVOException {
		Pio pio=new Pio();
		pio=pioConverter.transferVOToModel(vo, new Pio());
		if(vo.getPegawai()!=null)
		pio.setPegawai(pegawaiConverter.transferVOToModel(vo.getPegawai(), new Pegawai()));
		if(vo.getProduk()!=null)
		pio.setProduk(produkConverter.transferVOToModel(vo.getProduk(), new Produk()));
		
		if(vo.getPasien()!=null)
			pio.setPasien(pasienConverter.transferVOToModel(vo.getPasien(), new Pasien()));
		
		for(DetailPioVO detailPioVo:vo.getDetailPio()){
			DetailPio model=new DetailPio();
			model=detailPioConverter.transferVOToModelData(detailPioVo, new DetailPio());
			model.setPio(pio);
			pio.getDetailPio().add(model);
		}
		pio=pioDao.save(pio);	
		Map<String,Object> result=new HashMap<String,Object>();
		result.put("noRec", pio.getNoRec());
		return result;
	}

}
