package com.jasamedika.medifirst2000.service.impl;

import java.util.ArrayList;
import java.util.Date;
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 com.jasamedika.medifirst2000.converter.DataPemeriksaanPraKerjaConverter;
import com.jasamedika.medifirst2000.converter.HasilPemeriksaanPraKerjaConverter;
import com.jasamedika.medifirst2000.dao.DataPemeriksaanPraKerjaDao;
import com.jasamedika.medifirst2000.dao.HasilPemeriksaanPraKerjaDao;
import com.jasamedika.medifirst2000.entities.PapKeluhanTambahan;
import com.jasamedika.medifirst2000.entities.PapLaboratorium;
import com.jasamedika.medifirst2000.entities.RisikoKerjaRuangan;
import com.jasamedika.medifirst2000.entities.DataPemeriksaanPraKerja;
import com.jasamedika.medifirst2000.entities.HasilPemeriksaanPraKerja;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.HasilPemeriksaanPraKerjaService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.JsonUtil;
import com.jasamedika.medifirst2000.vo.DataPemeriksaanPraKerjaVO;
import com.jasamedika.medifirst2000.vo.HasilPemeriksaanPraKerjaVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import com.jasamedika.medifirst2000.vo.RuanganVO;

/**
 * Implement class for HasilPemeriksaanPraKerjaService
 * 
 * @author Askur
 */
@Service("HasilPemeriksaanPraKerjaService")
public class HasilPemeriksaanPraKerjaServiceImpl implements HasilPemeriksaanPraKerjaService {
	@Autowired
	private HasilPemeriksaanPraKerjaDao hasilPemeriksaanPraKerjaDao;
	@Autowired
	private DataPemeriksaanPraKerjaDao dataPemeriksaanPraKerjaDao;
	@Autowired
	private HasilPemeriksaanPraKerjaConverter hasilPemeriksaanPraKerjaConverter;
	@Autowired
	private DataPemeriksaanPraKerjaConverter dataPemeriksaanPraKerjaConverter;
	
	@Override
	public Map<String,Object> addHasilPemeriksaanPraKerja(HasilPemeriksaanPraKerjaVO vo) throws JpaSystemException, ServiceVOException {
		Map<String,Object> result=new HashMap<String,Object>();
		HasilPemeriksaanPraKerja model=hasilPemeriksaanPraKerjaConverter.transferVOToModel(vo, new HasilPemeriksaanPraKerja());
		List<DataPemeriksaanPraKerja> detail=new ArrayList<DataPemeriksaanPraKerja>();
		for(DataPemeriksaanPraKerjaVO detailVo:vo.getDataPemeriksaanPraKerjaSet()){
			DataPemeriksaanPraKerja data=dataPemeriksaanPraKerjaConverter.transferVOToModel(detailVo, new DataPemeriksaanPraKerja());
			data.setHasilPemeriksaanPraKerja(model);
			detail.add(data);
		}
		model.getDataPemeriksaanPraKerjaSet().clear();
		model.getDataPemeriksaanPraKerjaSet().addAll(detail);

		HasilPemeriksaanPraKerja resultModel=hasilPemeriksaanPraKerjaDao.save(model);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("hasilPemeriksaanPraKerja", resultModel);
		}
		return result;
	}

	@Override
	public Map<String,Object> addDataPemeriksaanPraKerja(DataPemeriksaanPraKerjaVO vo) throws JpaSystemException, ServiceVOException {
		Map<String,Object> result=new HashMap<String,Object>();
		DataPemeriksaanPraKerja model=dataPemeriksaanPraKerjaConverter.transferVOToModel(vo, new DataPemeriksaanPraKerja());
		DataPemeriksaanPraKerja resultModel=dataPemeriksaanPraKerjaDao.save(model);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("hasilPemeriksaanPraKerja", resultModel);
		}
		return result;
	}

	@Override
	public Map<String,Object> findByPegawaiId(Integer pegawaiId) {
		Map<String,Object> result=new HashMap<String,Object>();
		List<HasilPemeriksaanPraKerja> resultModel= hasilPemeriksaanPraKerjaDao.findByPegawaiId(pegawaiId);
		List<HasilPemeriksaanPraKerjaVO> vos = new ArrayList<HasilPemeriksaanPraKerjaVO>();
//		hasilPemeriksaanPraKerjaConverter.transferListOfModelToListOfVO(resultModel, vos);
		
		if(CommonUtil.isNotNullOrEmpty(resultModel)){			
			try {
				result.put("data", JsonUtil.ToMaps(resultModel));
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
			result.put("dataFound", true);			
		}else{
			result.put("dataFound", false);
		}		
		return result;
	}

	@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 {
		return null;
	}

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

}
