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.converter.PapCatatanConverter;
import com.jasamedika.medifirst2000.dao.AntrianPasienDiPeriksaDao;
import com.jasamedika.medifirst2000.dao.CatatanDao;
import com.jasamedika.medifirst2000.dao.PapCatatanDao;
import com.jasamedika.medifirst2000.entities.AntrianPasienDiPeriksa;
import com.jasamedika.medifirst2000.entities.Catatan;
import com.jasamedika.medifirst2000.entities.PapCatatan;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.PapCatatanService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.JsonUtil;
import com.jasamedika.medifirst2000.vo.AntrianPasienDiPeriksaVO;
import com.jasamedika.medifirst2000.vo.PapCatatanVO;

@Service("PAPCatatanService")
public class PapCatatanServiceImpl implements PapCatatanService {
	
	@Autowired
	private PapCatatanDao papCatatanDao;
	
	@Autowired
	private CatatanDao catatanDao;
	
	@Autowired
	private AntrianPasienDiPeriksaDao antrianPasienDiPeriksaDao;
	
	@Autowired
	private BaseConverterImpl<PapCatatanVO, PapCatatan> papCatatanConverter;
	
	@Autowired
	private BaseConverterImpl<AntrianPasienDiPeriksaVO, AntrianPasienDiPeriksa> antrianPasienDiPeriksaConverter;
	
	@Override
	public Map<String, Object> addCatatan(List<PapCatatanVO> vos) {
		Map<String,Object> result = new HashMap<>();
		
		for(PapCatatanVO vo : vos){
			PapCatatan papCatatan = new PapCatatan();
			
			try{
				AntrianPasienDiPeriksa check = antrianPasienDiPeriksaDao.findOne(vo.getAntrianPasienDiPeriksa().getNoRec());
				if(CommonUtil.isNotNullOrEmpty(check)){
					if(CommonUtil.isNotNullOrEmpty(vo)){
						papCatatan = papCatatanConverter.transferVOToModel(vo, new PapCatatan());
						papCatatan.setIsNilai(vo.getStatusEnabled());
					}
					if(CommonUtil.isNotNullOrEmpty(vo.getAntrianPasienDiPeriksa())){
						papCatatan.setAntrianPasienDiPeriksa(antrianPasienDiPeriksaConverter.transferVOToModel(vo.getAntrianPasienDiPeriksa(), new AntrianPasienDiPeriksa()));
					}
					if(CommonUtil.isNotNullOrEmpty(vo.getId())){
						Catatan catatan = catatanDao.findOne(vo.getId());
						papCatatan.setRujukan(catatan);
					}
					if(CommonUtil.isNotNullOrEmpty(papCatatan)){
						PapCatatan papCatatanResult = papCatatanDao.save(papCatatan);
						result.put("noRec", papCatatanResult.noRec);
					}
				}
			}catch (Exception ex){
				result = null;
			}
		}
		return result;
	}
	
	public Map<String, Object> getCatatan(String noRec){
		Map<String, Object> result = new HashMap<>();
		
		List<Map<String, Object>> listData = new ArrayList<>();
		for(Map<String, Object> list : papCatatanDao.findAllByNoRec(noRec)){
			Map<String, Object> map = new HashMap<>();
			map.put("noRec", (String)list.get("noRec"));
			map.put("antrianPasienDiPeriksa", papCatatanDao.getNoRegistrasi((String)list.get("noRegistrasi")));
			//------------------------------------------------------------------------------------
			Map<String, Object> catatan = papCatatanDao.getCatatanMaster((Integer) list.get("id"));
			map.put("id", (Integer)catatan.get("id"));
			map.put("name", (String)catatan.get("catatan"));
			listData.add(map);
		}
		if(CommonUtil.isNotNullOrEmpty(listData)){
			result.put("listData", listData);
		}
		return result;
	}
	
	
}
