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 com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.AntrianPasienDiPeriksaDao;
import com.jasamedika.medifirst2000.dao.PapStatusFungsionalDao;
import com.jasamedika.medifirst2000.entities.AntrianPasienDiPeriksa;
import com.jasamedika.medifirst2000.entities.PapStatusFungsional;
import com.jasamedika.medifirst2000.entities.PengkajianAwalBaru;
import com.jasamedika.medifirst2000.entities.StatusFungsional;
import com.jasamedika.medifirst2000.entities.StatusYaTidak;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.PapStatusFungsionalService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.JsonUtil;
import com.jasamedika.medifirst2000.vo.PapStatusFungsionalVO;
import com.jasamedika.medifirst2000.vo.PengkajianAwalBaruVO;
import com.jasamedika.medifirst2000.vo.StatusFungsionalVO;
import com.jasamedika.medifirst2000.vo.StatusYaTidakVO;


@Service("PapStatusFungsionalService")
public class PapStatusFungsionalServiceImpl implements PapStatusFungsionalService {
	
	@Autowired
	private PapStatusFungsionalDao papStatusFungsionalDao;
	
	@Autowired
	private AntrianPasienDiPeriksaDao antrianPasienDiPeriksaDao;
	
	@Autowired
	private BaseConverterImpl<PapStatusFungsionalVO, PapStatusFungsional> papStatusFungsionalConverter;
	
	@Autowired
	private BaseConverterImpl<StatusFungsionalVO, StatusFungsional> statusFungsionalConverter;
	
	@Autowired
	private BaseConverterImpl<StatusYaTidakVO, StatusYaTidak> statusYaTidakConverter;
	
	@Autowired
	private BaseConverterImpl<PengkajianAwalBaruVO, PengkajianAwalBaru> pengkajianAwalBaruConverter;
	
	@Override
	public Map<String,Object> addStatusFungsional(PapStatusFungsionalVO vo) throws JpaSystemException, ServiceVOException {
		Map<String,Object> result=new HashMap<String,Object>();
		
		AntrianPasienDiPeriksa check = antrianPasienDiPeriksaDao.findOne(vo.getAntrianPasienDiPeriksa().getNoRec());
		
		if(CommonUtil.isNotNullOrEmpty(check)){
			PapStatusFungsional model=new PapStatusFungsional();
			
			if(CommonUtil.isNotNullOrEmpty(vo.getPengkajianAwalBaru())){
				model.setPengkajianAwalBaru(pengkajianAwalBaruConverter.transferVOToModel(vo.getPengkajianAwalBaru(), new PengkajianAwalBaru()));
			}
			
			model=papStatusFungsionalConverter.transferVOToModel(vo, model);
			AntrianPasienDiPeriksa antrianPasienDiPeriksa=antrianPasienDiPeriksaDao.findByNoRec(vo.getAntrianPasienDiPeriksa().getNoRec());
			model.setAntrianPasienDiPeriksa(antrianPasienDiPeriksa);
			if(CommonUtil.isNotNullOrEmpty(vo.getStatusFungsional())){
				model.setStatusFungsional(statusFungsionalConverter.transferVOToModel(vo.getStatusFungsional(), new StatusFungsional()));
			}
			if(CommonUtil.isNotNullOrEmpty(vo.getPernahJatuh())){
				model.setPernahJatuh(statusYaTidakConverter.transferVOToModel(vo.getPernahJatuh(), new StatusYaTidak()));
			}
			if(CommonUtil.isNotNullOrEmpty(vo.getMenggunakanAlatBantu())){
				model.setMenggunakanAlatBantu(statusYaTidakConverter.transferVOToModel(vo.getMenggunakanAlatBantu(), new StatusYaTidak()));
			}
			PapStatusFungsional resultModel=papStatusFungsionalDao.save(model);
			if(CommonUtil.isNotNullOrEmpty(resultModel)){	
				result.put("noRec", resultModel.noRec);
			}
		} else{
			result = null;
		}
		return result;
	}
	
	@Override
	public Map<String, Object> getStatusFungsional(String noRec){
		Map<String, Object> result = new HashMap<>();
		
		Map<String, Object> papStatusFungsional = papStatusFungsionalDao.getPapStatusFungsional(noRec);
		if(CommonUtil.isNotNullOrEmpty(papStatusFungsional)){
			result.put("noRec", papStatusFungsional.get("noRec"));
			result.put("antrianPasienDiPeriksa", papStatusFungsionalDao.getNoRegistrasi((String)papStatusFungsional.get("noRegistrasi")));
			result.put("statusFungsional", papStatusFungsionalDao.getStatusFungsional((Integer)papStatusFungsional.get("statuSFungsionalId")));
			result.put("pernahJatuh", papStatusFungsionalDao.getStatusYaTidak((Integer)papStatusFungsional.get("pernahJatuhId")));
			result.put("menggunakanAlatBantu",  papStatusFungsionalDao.getStatusYaTidak((Integer)papStatusFungsional.get("menggunakanAlatBantuId")));
		}
		return result;
	}
}
