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.PapInstruksiPulangConverter;
import com.jasamedika.medifirst2000.dao.PapInstruksiPulangDao;
import com.jasamedika.medifirst2000.entities.PapInstruksiPulang;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.PapInstruksiPulangService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.PapInstruksiPulangVO;

/**
 * Implement class for PapInstruksiPulangService
 * 
 * @author Askur
 */
@Service("PapInstruksiPulangService")
public class PapInstruksiPulangServiceImpl implements PapInstruksiPulangService {
	@Autowired
	private PapInstruksiPulangDao papInstruksiPulangDao;
	@Autowired
	private PapInstruksiPulangConverter papInstruksiPulangConverter;
	
	@Override
	public Map<String,Object> addInstruksiPulang(PapInstruksiPulangVO vo) throws JpaSystemException, ServiceVOException {
		Map<String,Object> result=new HashMap<String,Object>();
		PapInstruksiPulang model=papInstruksiPulangConverter.transferVOToModel(vo, new PapInstruksiPulang());
		PapInstruksiPulang resultModel=papInstruksiPulangDao.save(model);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("noRec", resultModel.getNoRec());
			result.put("tglInput", resultModel.getTglInput());
			if(CommonUtil.isNotNullOrEmpty(resultModel.getPasien().getId())){
				result.put("pasien.noCm", resultModel.getPasien().getNoCm());
			}
		}		
		return result;
	}

	@Override
	public Map<String,Object> updateInstruksiPulang(PapInstruksiPulangVO vo) throws JpaSystemException, ServiceVOException {
		Map<String,Object> result=new HashMap<String,Object>();
		PapInstruksiPulang model=papInstruksiPulangDao.findOne(vo.getNoRec());
		PapInstruksiPulang resultModel=papInstruksiPulangDao.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 Map<String,Object>  findByNoCmAndTglInput(String noCm, Date tglInput) {
		Map<String,Object> result=new HashMap<String,Object>();
		PapInstruksiPulang resultModel=papInstruksiPulangDao.findPapInstruksiPulangByNoCmAndTglInput(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 PapInstruksiPulangVO add(PapInstruksiPulangVO vo) throws JpaSystemException, ServiceVOException {
		return null;
	}

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

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

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

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

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

}
