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.stereotype.Service;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.JadwalDpjpRawatInapDao;
import com.jasamedika.medifirst2000.dao.custom.JadwalDpjpRawatInapDaoCustom;
import com.jasamedika.medifirst2000.entities.JadwalDpjpRawatInap;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.service.JadwalDpjpRawatInapService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.vo.JadwalDpjpRawatInapVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import com.jasamedika.medifirst2000.vo.RuanganVO;

@Service("JadwalDpjpRawatInapService")
public class JadwalDpjpRawatInapServiceImpl implements JadwalDpjpRawatInapService{

	@Autowired
	private JadwalDpjpRawatInapDao jadwalDpjpRawatInapDao;
	
	@Autowired
	private JadwalDpjpRawatInapDaoCustom jadwalDpjpRawatInapDaoCustom;
	
	@Autowired
	private BaseConverterImpl<JadwalDpjpRawatInapVO, JadwalDpjpRawatInap> jadwalDpjpRawatInapConverter;
	
	@Autowired
	private BaseConverterImpl<RuanganVO, Ruangan> ruanganConverter;
	
	@Autowired
	private BaseConverterImpl<PegawaiVO, Pegawai> pegawaiConverter;
	
	@Override
	public Map<String, Object> save(JadwalDpjpRawatInapVO vo){
		Map<String, Object> result = new HashMap<>();
		JadwalDpjpRawatInap jadwalDpjpRawatInap = new JadwalDpjpRawatInap();
		
		if(CommonUtil.isNotNullOrEmpty(vo)){
			jadwalDpjpRawatInap = jadwalDpjpRawatInapConverter.transferVOToModel(vo, new JadwalDpjpRawatInap());
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getRuangan())){
			jadwalDpjpRawatInap.setRuangan(ruanganConverter.transferVOToModel(vo.getRuangan(), new Ruangan()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getDokter())){
			jadwalDpjpRawatInap.setDokter(pegawaiConverter.transferVOToModel(vo.getDokter(), new Pegawai()));
		}
		if(CommonUtil.isNotNullOrEmpty(jadwalDpjpRawatInap)){
			JadwalDpjpRawatInap jadwalDpjpRawatInapREsult = jadwalDpjpRawatInapDao.save(jadwalDpjpRawatInap);
			result.put("noRec", jadwalDpjpRawatInapREsult.getNoRec());
		}
		return result;
	}
	
	@Override
	public List<Map<String, Object>> getAll(Integer id){
		return jadwalDpjpRawatInapDao.getAll(id);
	}
	
	@Override
	public Map<String, Object> getLoadDataDpjp(){
		Map<String, Object> result = new HashMap<>();
		
		List<Map<String, Object>> ruangan = jadwalDpjpRawatInapDao.ruangan(
				Integer.parseInt(jadwalDpjpRawatInapDao.getSettingDataFixed("KdDepartemenInstalasiRawatInap")));
		if(CommonUtil.isNotNullOrEmpty(ruangan)){
			result.put("ruangan", ruangan);
		}else{
			result.put("ruangan", null);
		}
		
		List<Map<String, Object>> dokter = jadwalDpjpRawatInapDao.dokter(
				Integer.parseInt(jadwalDpjpRawatInapDao.getSettingDataFixed("KdJenisPegawaiDokter")));
		if(CommonUtil.isNotNullOrEmpty(dokter)){
			result.put("dokter", dokter);
		} else{
			result.put("dokter", null);
		}
		return result;
	}
	
	@Override
	public List<Map<String, Object>> getJadwalDpjpByRuangan(Integer id){
		return jadwalDpjpRawatInapDao.getJadwalDpjpByRuangan(id);
	}
	
	@Override
	public List<Map<String, Object>> getJadwalDpjpDokterPelayanan(Integer id){
		return jadwalDpjpRawatInapDao.getJadwalDpjpDokterPelayanan(id, DateUtil.now());
	}
	
	@Override
	public Map<String, Object> deleteDokterDpjp(String noRec){
		JadwalDpjpRawatInap jadwal = jadwalDpjpRawatInapDao.findOne(noRec);
		if(CommonUtil.isNotNullOrEmpty(jadwal)) {
			jadwalDpjpRawatInapDao.delete(jadwal);
			Map<String, Object> result = new HashMap<>();
			result.put("noRec", jadwal.getNoRec());
			return result;
		} else {
			return null;
		}
	}
}
