package com.jasamedika.medifirst2000.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.PapTandaVitalDao;
import com.jasamedika.medifirst2000.dao.custom.PapTandaVitalDaoCustom;
import com.jasamedika.medifirst2000.entities.AntrianPasienDiPeriksa;
import com.jasamedika.medifirst2000.entities.PapTandaVital;
import com.jasamedika.medifirst2000.service.PapTandaVitalService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.vo.AntrianPasienDiPeriksaVO;
import com.jasamedika.medifirst2000.vo.PapTandaVitalVO;

@Service("PAPTandaVitalService")
public class PapTandaVitalServiceImpl implements PapTandaVitalService {
	
	@Autowired
	private PapTandaVitalDao papTandaVitalDao;
	
	@Autowired
	private PapTandaVitalDaoCustom papTandaVitalDaoCustom;
	
	@Autowired
	private BaseConverterImpl<PapTandaVitalVO, PapTandaVital> PapTandaVitalConverter;
	
	@Autowired
	private BaseConverterImpl<AntrianPasienDiPeriksaVO, AntrianPasienDiPeriksa> antrianPasienDiPeriksaConverter;

	
	@Override
	public Map<String,Object> addTandaVital(PapTandaVitalVO vo){
		Map<String,Object> result = new HashMap<>();
		PapTandaVital papTandaVital = new PapTandaVital();
		
		if(CommonUtil.isNotNullOrEmpty(vo)){
			if(CommonUtil.isNotNullOrEmpty(vo.getNoRec())){				
				papTandaVital = papTandaVitalDao.findOne(vo.getNoRec());
				detailSaved(vo, papTandaVital);
			} else{
				if(CommonUtil.isNullOrEmpty(papTandaVitalDao.checkNoreg(vo.getPasienDaftar().getNoRec()))){
					papTandaVital = PapTandaVitalConverter.transferVOToModel(vo, new PapTandaVital());
					papTandaVital.setTglInput(DateUtil.now());
					papTandaVital.setStatusEnabled(true);
				}
				detailSaved(vo, papTandaVital);
			}
		}
		
		return result;
	}
	
	@Override
	public Map<String,Object> detailSaved(PapTandaVitalVO vo, PapTandaVital papTandaVital){
		Map<String,Object> result = new HashMap<>();
		
		if(CommonUtil.isNotNullOrEmpty(vo.getPasienDaftar())){
			papTandaVital.setPasienDaftar(antrianPasienDiPeriksaConverter.transferVOToModel(vo.getPasienDaftar(), new AntrianPasienDiPeriksa ()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getTglInput())){
			papTandaVital.setTglInput(vo.getTglInput());
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getSuhu())){
			papTandaVital.setSuhu(vo.getSuhu());
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getNadi())){
			papTandaVital.setNadi(vo.getNadi());
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getPernapasan())){
			papTandaVital.setPernapasan(vo.getPernapasan());
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getTekananDarah())){
			papTandaVital.setTekananDarah(vo.getTekananDarah());
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getAlergi())){
			papTandaVital.setAlergi(vo.getAlergi());
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getBeratBadan())){
			papTandaVital.setBeratBadan(vo.getBeratBadan());
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getTinggiBadan())){
			papTandaVital.setTinggiBadan(vo.getTinggiBadan());
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getLingkarKepala())){
			papTandaVital.setLingkarKepala(vo.getLingkarKepala());
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getImt())){
			papTandaVital.setImt(vo.getImt());
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getStatusGizi())){
			papTandaVital.setStatusGizi(vo.getStatusGizi());
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getKeterangan())){
			papTandaVital.setKeterangan(vo.getKeterangan());
		}
		if(CommonUtil.isNotNullOrEmpty(papTandaVital)){
			PapTandaVital papTandaVitalResult = papTandaVitalDao.save(papTandaVital);
			result.put("noRec", papTandaVitalResult.getNoRec());
		}
		return result;
	}
	
	@Override
	public List<Map<String, Object>> getAllByPasienDaftar(String noCm, String startDate, String endDate, Integer status){
		return papTandaVitalDaoCustom.getAllByPasienDaftar(noCm, startDate, endDate, status);
	}
}
