package com.jasamedika.medifirst2000.service.impl;

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.PapAssesmentGiziConverter;
import com.jasamedika.medifirst2000.dao.PapAssesmentGiziDao;
import com.jasamedika.medifirst2000.dao.PasienDao;
import com.jasamedika.medifirst2000.dao.RegistrasiPelayananPasienDao;
import com.jasamedika.medifirst2000.entities.PapKeluhanTambahan;
import com.jasamedika.medifirst2000.entities.PapAssesmentGizi;
import com.jasamedika.medifirst2000.entities.Pasien;
import com.jasamedika.medifirst2000.entities.RegistrasiPelayananPasien;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.PapAssesmentGiziService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.PapKeluhanTambahanVO;
import com.jasamedika.medifirst2000.vo.PapAssesmentGiziVO;

/**
 * Implement class for PAPPapAssesmentGiziService
 * 
 * @author Askur
 */
@Service("PAPAssesmentGiziService")
public class PapAssesmentGiziServiceImpl implements PapAssesmentGiziService {
	@Autowired
	private PapAssesmentGiziDao papPapAssesmentGiziDao;
	@Autowired
	private PapAssesmentGiziConverter papPapAssesmentGiziConverter;
	@Autowired
	private RegistrasiPelayananPasienDao registrasiPelayananPasienDao;
	@Autowired
	private PasienDao pasienDao;
	
	@Override
	public Map<String,Object> addAssesmentGizi(PapAssesmentGiziVO vo) throws JpaSystemException, ServiceVOException {
		Map<String,Object> result=new HashMap<String,Object>();
		PapAssesmentGizi model=papPapAssesmentGiziConverter.transferVOToModel(vo, new PapAssesmentGizi());
		PapAssesmentGizi resultModel=papPapAssesmentGiziDao.save(model);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("noRec", resultModel.getNoRec());
			result.put("tglInput", resultModel.getTglInput());
			result.put("pasien.id", resultModel.getPasien().getId());
			result.put("pasien.noCm2", resultModel.getPasien().getNoCm());
			if(CommonUtil.isNotNullOrEmpty(resultModel.getPasien().getId())){
				result.put("pasien.noCm", resultModel.getPasien().getNoCm());
			}
		}
		return result;
	}

	@Override
	public Map<String,Object> updateAssesmentGizi(PapAssesmentGiziVO vo) throws JpaSystemException, ServiceVOException {
		Map<String,Object> result=new HashMap<String,Object>();
		PapAssesmentGizi model=papPapAssesmentGiziDao.findOne(vo.getNoRec());
		PapAssesmentGizi resultModel=papPapAssesmentGiziDao.save(model);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("noRec", resultModel.getNoRec());
			result.put("tglInput", resultModel.getTglInput());
			result.put("dataFound", true);
			if(CommonUtil.isNotNullOrEmpty(resultModel.getPasien().getId())){
				result.put("pasien.noCm", resultModel.getPasien().getNoCm());
			}
		}else{
			result.put("dataFound", false);			
		}
		return result;
	}

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

	@Override
	public Map<String,Object> findByNoCmAndTglInput(String noCm, Date tglInput) {
		Map<String,Object> result=new HashMap<String,Object>();
		PapAssesmentGizi resultModel=papPapAssesmentGiziDao.findPapAssesmentGiziByNoCmAndTglInput(noCm, tglInput);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("noRec", resultModel.getNoRec());
			result.put("tglInput", resultModel.getTglInput());
			result.put("dataFound", true);
			if(CommonUtil.isNotNullOrEmpty(resultModel.getPasien().getId())){
				result.put("pasien.noCm", resultModel.getPasien().getNoCm());
			}
		} else {
			result.put("dataFound", false);
		}
		return result;
	}

	@Override
	public Map<String,Object> findRegistrasiPelayananPasienByNoCmAndTglRegistrasi(String noCm, short kdProfile,Date tglRegistrasi) {
		Map<String,Object> result=new HashMap<String,Object>();
		RegistrasiPelayananPasien resultModel=registrasiPelayananPasienDao.finByNoCm(noCm,kdProfile,tglRegistrasi);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("noRec", resultModel.getNoRec());
			result.put("tglRegistrasi", resultModel.getTglMasuk());
			result.put("noAntrian", resultModel.getNoAntrian());
			result.put("dataFound", true);
			if(CommonUtil.isNotNullOrEmpty(resultModel.getPasien().getId())){
				result.put("pasien.noCm", resultModel.getPasien().getNoCm());
			}
		}else{
			result.put("dataFound", false);
		}
		return result;
	}

	@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;
	}


}
