package com.jasamedika.medifirst2000.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

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

import com.jasamedika.medifirst2000.converter.PapImunisasiDetailConverter;
import com.jasamedika.medifirst2000.converter.PapRiwayatImunisasiConverter;
import com.jasamedika.medifirst2000.dao.AntrianPasienDiPeriksaDao;
import com.jasamedika.medifirst2000.dao.PapRiwayatImunisasiDao;
import com.jasamedika.medifirst2000.dao.PasienDao;
import com.jasamedika.medifirst2000.entities.AntrianPasienDiPeriksa;
import com.jasamedika.medifirst2000.entities.PapImunisasiDetail;
import com.jasamedika.medifirst2000.entities.PapRiwayatImunisasi;
import com.jasamedika.medifirst2000.entities.Pasien;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.PapRiwayatImunisasiService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.PapImunisasiDetailVO;
import com.jasamedika.medifirst2000.vo.PapRiwayatImunisasiVO;

/**
 * Implement class for PAPRiwayatImunisasiService
 * 
 * @author Askur
 */
@Service("PAPRiwayatImunisasiService")
public class PapRiwayatImunisasiServiceImpl implements PapRiwayatImunisasiService {
	@Autowired
	private PapRiwayatImunisasiDao papRiwayatImunisasiDao;
	@Autowired
	private AntrianPasienDiPeriksaDao antrianPasienDiPeriksaDao;
	@Autowired
	private PapRiwayatImunisasiConverter papRiwayatImunisasiConverter;
	@Autowired
	private PapImunisasiDetailConverter papImunisasiDetailConverter;
	@Autowired
	private PasienDao pasienDao;
	
	@Override
	public Map<String,Object> addRiwayatImunisasi(PapRiwayatImunisasiVO vo) throws JpaSystemException, ServiceVOException {
		Map<String,Object> result=new HashMap<String,Object>();
		
		AntrianPasienDiPeriksa pasienDaftar = antrianPasienDiPeriksaDao.findByNoRec(vo.getPasien().getNoRec());
		if(CommonUtil.isNotNullOrEmpty(pasienDaftar)){
			PapRiwayatImunisasi model = new PapRiwayatImunisasi();
			
			if(CommonUtil.isNotNullOrEmpty(vo)){
				model = papRiwayatImunisasiConverter.transferVOToModel(vo, new PapRiwayatImunisasi());
			}
			if(CommonUtil.isNotNullOrEmpty(pasienDaftar)){
				model.setPasien(pasienDaftar);
			}
			if(CommonUtil.isNotNullOrEmpty(vo.getPapImunisasiDetailSet())){
				Set<PapImunisasiDetail> detail = new HashSet<>();
				
				for(PapImunisasiDetailVO voDetail:vo.getPapImunisasiDetailSet()){
					PapImunisasiDetail modelDetail=papImunisasiDetailConverter.transferVOToModel(voDetail, new PapImunisasiDetail());
					modelDetail.setPapRiwayatImunisasi(model);
					detail.add(modelDetail);
				}
				if(CommonUtil.isNotNullOrEmpty(detail)){
					model.setPapImunisasiDetailSet(detail);
				}
			}
			if(CommonUtil.isNotNullOrEmpty(model)){
				PapRiwayatImunisasi resultModel = papRiwayatImunisasiDao.save(model);
				result.put("noRec", resultModel.noRec);
			}
		} else{
			result = null;
		}
		return result;
	}


	@Override
	public Map<String,Object> findByNoCmAndTglInput(String noRec, Date tglInput) {
		Map<String,Object> result=new HashMap<String,Object>();
		PapRiwayatImunisasi resultModel=papRiwayatImunisasiDao.findPapRiwayatImunisasiByNoCmAndTglInput(noRec, tglInput);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("RiwayatImunisasi", resultModel);
			result.put("dataFound", true);
		} else {
			result.put("dataFound", false);
		}
		return result;
	}

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

	@Override
	public Map<String,Object> findPasienByNoCm(String noCm) {
		Map<String,Object> result=new HashMap<String,Object>();
		List<Pasien> resultModel=pasienDao.findPasienBynoCm(noCm);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			for(Pasien pasien:resultModel){
				result.put("noCm", pasien.getNoCm());
				result.put("namaPasien", pasien.getNamaPasien());
				if(CommonUtil.isNotNullOrEmpty((pasien.getAlamats().iterator().next()))){
				result.put("alamatLengkap", (pasien.getAlamats().iterator().next()).getAlamatLengkap());
				}
				if(CommonUtil.isNotNullOrEmpty(pasien.getJenisKelamin())){
				result.put("jenisKelamin", pasien.getJenisKelamin().getNamaExternal());
				}
				if(CommonUtil.isNotNullOrEmpty(pasien.getStatusPerkawinan())){
				result.put("statusPerkawinan", pasien.getStatusPerkawinan().getNamaExternal());
				}
				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 Map findAllWithPageAndLimitAndSortByAndDirectionParameter(Integer page, Integer limit, String sort,
			String dir) {
		return null;
	}


}
