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.PelaksanaConverter;
import com.jasamedika.medifirst2000.converter.TimeOutControlConverter;
import com.jasamedika.medifirst2000.dao.AntrianPasienDiPeriksaDao;
import com.jasamedika.medifirst2000.dao.PelaksanaDao;
import com.jasamedika.medifirst2000.dao.TimeOutControlDao;
import com.jasamedika.medifirst2000.entities.AntiBiotikProfilaksis;
import com.jasamedika.medifirst2000.entities.AntrianPasienDiPeriksa;
import com.jasamedika.medifirst2000.entities.CekKelengkapanAlkes;
import com.jasamedika.medifirst2000.entities.CekKelengkapanAlkesDetail;
import com.jasamedika.medifirst2000.entities.JenisPegawai;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.Pelaksana;
import com.jasamedika.medifirst2000.entities.PhotoRadiologi;
import com.jasamedika.medifirst2000.entities.StatusLengkapTidakLengkap;
import com.jasamedika.medifirst2000.entities.StatusYaTidak;
import com.jasamedika.medifirst2000.entities.TimeOutControl;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.TimeOutControlService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.AntiBiotikProfilaksisVO;
import com.jasamedika.medifirst2000.vo.CekKelengkapanAlkesDetailVO;
import com.jasamedika.medifirst2000.vo.JenisPegawaiVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import com.jasamedika.medifirst2000.vo.PelaksanaVO;
import com.jasamedika.medifirst2000.vo.PhotoRadiologiVO;
import com.jasamedika.medifirst2000.vo.StatusYaTidakVO;
import com.jasamedika.medifirst2000.vo.TimeOutControlVO;

@Service("TimeOutControlService")
public class TimeOutControlServiceImpl implements TimeOutControlService {
	@Autowired
	private BaseConverterImpl<JenisPegawaiVO, JenisPegawai> jenisPegawaiConverter;
	
	@Autowired
	private BaseConverterImpl<PegawaiVO, Pegawai> pegawaiConverter;

	@Autowired
	private TimeOutControlDao timeOutControlDao;

	@Autowired
	private TimeOutControlConverter timeOutControlConverter;

	@Autowired
	private PelaksanaConverter pelaksanaConverter;

	@Autowired
	private BaseConverterImpl<StatusYaTidakVO, StatusYaTidak> fotoRadiologiConverter;

	@Autowired
	private BaseConverterImpl<StatusYaTidakVO, StatusYaTidak> statusAntibiotikProfilaksisConverter;
	
	@Autowired
	private AntrianPasienDiPeriksaDao antrianPasienDiPeriksaDao;
	
	@Autowired
	private PelaksanaDao pelaksanaDao;

	@Override
	public Map<String, Object> saveTimeOutControl(TimeOutControlVO vo) {
		Map<String, Object> result = new HashMap<>();

		TimeOutControl timeOutControl = timeOutControlConverter.transferVOToModel(vo, new TimeOutControl());
		
		AntrianPasienDiPeriksa norec = antrianPasienDiPeriksaDao.findByNoRec(vo.getPasienDaftar().getNoRec());
		timeOutControl.setPasienDaftar(norec);

		timeOutControl.setNoRec(vo.getNoRec());
		
		if (CommonUtil.isNotNullOrEmpty(vo.getFotoRadiologi())) {
			timeOutControl.setFotoRadiologi(
					fotoRadiologiConverter.transferVOToModel(vo.getFotoRadiologi(), new StatusYaTidak()));
		}
		if (CommonUtil.isNotNullOrEmpty(vo.getStatusAntibiotikProfilaksis())) {
			timeOutControl.setStatusAntibiotikProfilaksis(statusAntibiotikProfilaksisConverter
					.transferVOToModel(vo.getStatusAntibiotikProfilaksis(), new StatusYaTidak()));
		}

		timeOutControl.getPelaksana().clear();
		for (PelaksanaVO pelaksanaVO : vo.getPelaksana()) {
			
				Pelaksana pelaksana = pelaksanaConverter.transferVOToModel(pelaksanaVO, new Pelaksana());
				pelaksana.setTimeOutControl(timeOutControl);
				
				pelaksana.setNoRec(vo.getNoRec());
				
				//if(CommonUtil.isNotNullOrEmpty(pelaksanaVO.getJenisPegawai())) {
					pelaksana.setJenisPegawai(jenisPegawaiConverter.transferVOToModel(pelaksanaVO.getJenisPegawai(), new JenisPegawai()));
				//}
				
				if(CommonUtil.isNotNullOrEmpty(pelaksanaVO.getNamaPelaksana())) {
					pelaksana.setNamaPelaksana(pegawaiConverter.transferVOToModel(pelaksanaVO.getNamaPelaksana(), new Pegawai()));
				}
				
				if(CommonUtil.isNotNullOrEmpty(pelaksana)) {
					timeOutControl.getPelaksana().add(pelaksana);
				}
				
		}

			timeOutControl = timeOutControlDao.save(timeOutControl);

			if(CommonUtil.isNotNullOrEmpty(timeOutControl)) {
				result.put("noRec", timeOutControl.getNoRec());
			}
		
		return result;
	}

	@Override
	public Map<String, Object> updateTimeOutControl(TimeOutControlVO vo) {

		Map<String, Object> result = new HashMap<String, Object>();
		TimeOutControl model = timeOutControlDao.findOne(vo.getNoRec());
		model = timeOutControlConverter.transferVOToModel(vo, model);
		TimeOutControl resultModel = timeOutControlDao.save(model);
		if (CommonUtil.isNotNullOrEmpty(resultModel)) {
			result.put("id", resultModel.getNoRec());
		}
		return result;
	}

	@Override
	public TimeOutControlVO add(TimeOutControlVO vo) throws JpaSystemException, ServiceVOException {
		return null;
	}

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

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

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

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

	@Override
	public Map<String, Object> findAllWithPageAndLimitAndSortByAndDirectionParameter(Integer page, Integer limit,
			String sort, String dir) {
		return null;
	}
	
	@Override
	public Map<String, Object> findByNoRec(String noRec) {
		Map<String, Object> result = new HashMap<String, Object>();
		List<Map<String, Object>> listData = new ArrayList<>();

		Map<String, Object> timeOutMap = new HashMap<>();
		List<Map<String, Object>> timeOut = this.timeOutControlDao.getTimeOutByNoRec(noRec);

		//get data Pelaksana
		try {
		
			Map<String, Object> pelaksanaMap = new HashMap<>();
			List<Map<String, Object>> tempList = new ArrayList<>();
			List<Map<String, Object>> pelaksanaList = this.timeOutControlDao.getPelaksanaDetail(noRec);
		
			for(Map<String, Object> data : pelaksanaList){
				Map<String, Object> tempMap = new HashMap<>();
				tempMap.put("noRec", (String)data.get("noRec"));
				tempMap.put("noRecTimeOutControl", (String)data.get("noRecTimeOutControl"));
				tempMap.put("idJenisPegawai", (Integer)data.get("idJenisPegawai"));
				tempMap.put("jenisPegawai", (String)data.get("jenisPegawai"));
				tempMap.put("idPegawai", (Integer)data.get("idPegawai"));
				tempMap.put("namaPegawai", (String)data.get("namaPegawai"));
	
				tempList.add(tempMap);
			}
			
			pelaksanaMap.put("Pelaksana", tempList);
			listData.add(pelaksanaMap);

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		timeOutMap.put("TimeOut", timeOut);
		listData.add(timeOutMap);
		result.put("showData", listData);
		
		return result;
	}
	
	
	@Override
	public Map<String, Object> getTimeOutByNoRecAntrian(String noRec) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		List<Map<String, Object>> resultData = this.timeOutControlDao.getTimeOutByNoRecAntrian(noRec);
		
		if(CommonUtil.isNotNullOrEmpty(resultData)) {
			result.put("showData", resultData);
		}
		return result;
		
	}


}
