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.AlatTerpasangDetailConverter;
import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.converter.CheckInConverter;
import com.jasamedika.medifirst2000.dao.AlatYangTerpasangDao;
import com.jasamedika.medifirst2000.dao.AntrianPasienDiPeriksaDao;
import com.jasamedika.medifirst2000.dao.CheckInDao;
import com.jasamedika.medifirst2000.dao.PasienDaftarDao;
import com.jasamedika.medifirst2000.dao.PengobatanKhususDao;
import com.jasamedika.medifirst2000.entities.AlatTerpasang;
import com.jasamedika.medifirst2000.entities.AlatTerpasangDetail;
import com.jasamedika.medifirst2000.entities.AlatYangTerpasangCheckIn;
import com.jasamedika.medifirst2000.entities.AntrianPasienDiPeriksa;
import com.jasamedika.medifirst2000.entities.CheckIn;
import com.jasamedika.medifirst2000.entities.JenisOperasi;
import com.jasamedika.medifirst2000.entities.PengobatanKhusus;
import com.jasamedika.medifirst2000.entities.PengobatanKhususCheckIn;
import com.jasamedika.medifirst2000.entities.PengobatanKhususDetail;
import com.jasamedika.medifirst2000.entities.Puasa;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.CheckInService;
import com.jasamedika.medifirst2000.service.PasienDaftarService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.AlatTerpasangDetailVO;
import com.jasamedika.medifirst2000.vo.AlatTerpasangVO;
import com.jasamedika.medifirst2000.vo.CheckInVO;
import com.jasamedika.medifirst2000.vo.JenisOperasiVO;
import com.jasamedika.medifirst2000.vo.PengobatanKhususDetailVO;
import com.jasamedika.medifirst2000.vo.PengobatanKhususVO;
import com.jasamedika.medifirst2000.vo.PuasaVO;

@Service("CheckInService")
public class CheckInServiceImpl implements CheckInService {
	
	@Autowired
	private CheckInDao checkInDao;
	
	@Autowired
	private CheckInConverter checkInConverter;
	
	@Autowired
	private PasienDaftarDao pasienDaftarDao;
	
	@Autowired
	private PasienDaftarService pasienDaftarService;
	
	@Autowired
	private AlatYangTerpasangDao alatTerpasangDao;
	
	@Autowired
	private PengobatanKhususDao pengobatanKhususDao;
	
	@Autowired
	private AlatTerpasangDetailConverter alatTerpasangDetailConverter;
	
	@Autowired
	private BaseConverterImpl<PengobatanKhususDetailVO, PengobatanKhususDetail> pengobatanKhususDetailConverter;
	
	@Autowired
	private BaseConverterImpl<PengobatanKhususVO, PengobatanKhusus> pengobatanKhususConverter;
	
	@Autowired
	private BaseConverterImpl<AlatTerpasangVO, AlatTerpasang> alatTerpasangConverter;
	
	@Autowired
	private AntrianPasienDiPeriksaDao antrianPasienDiPeriksaDao;
	
	@Autowired
	private BaseConverterImpl<JenisOperasiVO, JenisOperasi> jenisOperasiConverter;
	
	@Autowired
	private BaseConverterImpl<PuasaVO, Puasa> puasaConverter;
	
	@Override
	public Object add(Object vo) throws JpaSystemException, ServiceVOException {
		return null;
	}
	@Override
	public Object update(Object vo) throws JpaSystemException, ServiceVOException {
		return null;
	}
	@Override
	public Boolean delete(Object key) throws JpaSystemException {
		return null;
	}
	@Override
	public Object findById(Object key) throws JpaSystemException {
		return null;
	}
	@Override
	public List findAll() throws JpaSystemException {
		return null;
	}
	@Override
	public Map findAllWithPageAndLimitAndSortByAndDirectionParameter(Integer page, Integer limit, String sort,
			String dir) {
		return null;
	}

	@Override
	public Map<String, Object> addCheckIn(CheckInVO vo) {
		Map<String,Object> result=new HashMap<String,Object>();
		CheckIn model = checkInConverter.transferVOToModel(vo, new CheckIn());	
		AntrianPasienDiPeriksa p = antrianPasienDiPeriksaDao.findByNoRec(vo.getPasien().getNoRec());			
		model.setPasien(p);	

		model.setNoRec(vo.getNoRec());
			
		model.setBeratBadan(vo.getBeratBadan());
		model.setTinggiBadan(vo.getTinggiBadan());
		model.setJumlah(vo.getJumlah());
		model.setKesadaran(vo.getKesadaran());
		model.setMakanMinumTerakhir(vo.getMakanMinumTerakhir());
		model.setN(vo.getN());
		model.setTd(vo.getTd());
		model.setRr(vo.getRr());
		model.setTanggalPendaftaran(p.getTglRegistrasi());
		model.setStatusEnabled(vo.getStatusEnabled());
		if(CommonUtil.isNotNullOrEmpty(vo.getJenisOperasi())) {
			model.setJenisOperasi(jenisOperasiConverter.transferVOToModel(vo.getJenisOperasi(), new JenisOperasi()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getPuasa())){
			model.setPuasa(puasaConverter.transferVOToModel(vo.getPuasa(), new Puasa()));
		}
		
		//set pengobatan khusus
		model.getPengobatanKhusus().clear();
		for(PengobatanKhususDetailVO pengobatan : vo.getPengobatanKhusus())
		{
			PengobatanKhususDetail pengobatanKhusus = pengobatanKhususDetailConverter.transferVOToModel(pengobatan, new PengobatanKhususDetail());
			PengobatanKhususCheckIn pengobatanKhususItem = new PengobatanKhususCheckIn();
			pengobatanKhususItem.setId(pengobatan.getPengobatanKhusus().getId());
			pengobatanKhusus.setPengobatanKhusus(pengobatanKhususItem);
			pengobatanKhusus.setCheckIn(model);
			pengobatanKhusus.setValue(pengobatan.getValue());	
			pengobatanKhusus.setNoRec(pengobatan.getNoRec());
			pengobatanKhusus.setStatusEnabled(pengobatan.getStatusEnabled());

			if(CommonUtil.isNotNullOrEmpty(pengobatanKhusus.getPengobatanKhusus())) {
				model.getPengobatanKhusus().add(pengobatanKhusus);
			}
				
		}
		
		//set Alat Terpasang
		model.getAlatTerpasang().clear();
		for(AlatTerpasangDetailVO alat : vo.getAlatTerpasang())
		{
			AlatTerpasangDetail alatTerpasangDetail = alatTerpasangDetailConverter.transferVOToModel(alat, new AlatTerpasangDetail());
			AlatYangTerpasangCheckIn alatTerpasangItem = new AlatYangTerpasangCheckIn();
			alatTerpasangItem.setId(alat.getAlatTerpasang().getId());
			alatTerpasangDetail.setAlatTerpasang(alatTerpasangItem);
			alatTerpasangDetail.setNoRec(alat.getNoRec());
			alatTerpasangDetail.setStatusEnabled(alat.getStatusEnabled());
			alatTerpasangDetail.setCheckIn(model);	
			if(CommonUtil.isNotNullOrEmpty(alatTerpasangDetail.getAlatTerpasang())) {
				model.getAlatTerpasang().add(alatTerpasangDetail);
			}
				
		}

		CheckIn resultModel  = this.checkInDao.save(model);
				
		//update noantrian 
		//by iwankasan
		//ketika chekin status antrian berubah menjadi dipanggil perawat
		//06.11.2017
		checkInDao.updateNoAntrianByNoRec(p.getNoRec());
				
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("noRec", resultModel.getNoRec());
		}

		return result;
	}
	
	
	@Override
	public Map updateCheckIn(CheckInVO vo) {
		Map<String,Object> result=new HashMap<String,Object>();
	   // CheckIn model = checkInDao.findOne(vo.getNoRec());
		CheckIn modelCheckIn = checkInConverter.transferVOToModel(vo, new CheckIn());	
		List<PengobatanKhususDetail> modelPengobatan= pengobatanKhususDao.findPengobatanByNoRec(vo.getNoRec());
		List<AlatTerpasangDetail> modelAlat=alatTerpasangDao.findAlatByNoRec(vo.getNoRec());
		
		if(CommonUtil.isNotNullOrEmpty(modelPengobatan)){
			pengobatanKhususDao.delete(modelPengobatan);			
		}
		
		if(CommonUtil.isNotNullOrEmpty(modelAlat)){
			alatTerpasangDao.delete(modelAlat);			
		}
		List<PengobatanKhususDetail> listpengobatan=new ArrayList<PengobatanKhususDetail>();
		List<AlatTerpasangDetail> listalat=new ArrayList<AlatTerpasangDetail>();
		
		AntrianPasienDiPeriksa pasien = antrianPasienDiPeriksaDao.findByNoRec(vo.getPasien().getNoRec());			
		modelCheckIn.setPasien(pasien);			
		modelCheckIn.getPengobatanKhusus().clear();
		modelCheckIn.getAlatTerpasang().clear();
		CheckIn resultModel  = checkInDao.save(modelCheckIn);
		for(PengobatanKhususDetailVO pengobatan : vo.getPengobatanKhusus())
		{
			PengobatanKhususDetail pengobatanKhusus = pengobatanKhususDetailConverter.transferVOToModel(pengobatan, new PengobatanKhususDetail());
		//	pengobatanKhusus.setPengobatanKhusus(pengobatanKhususConverter.transferVOToModel(pengobatan.getPengobatanKhusus(), new PengobatanKhusus()));
			pengobatanKhusus.setCheckIn(modelCheckIn);		
			pengobatanKhususDao.save(pengobatanKhusus);
		}
	
		for(AlatTerpasangDetailVO alat : vo.getAlatTerpasang())
		{
			AlatTerpasangDetail alatTerpasangDetail = alatTerpasangDetailConverter.transferVOToModel(alat, new AlatTerpasangDetail());	
			//alatTerpasangDetail.setAlatTerpasang(alatTerpasangConverter.transferVOToModel(alat.getAlatTerpasang(), new AlatTerpasang()));
			alatTerpasangDetail.setCheckIn(modelCheckIn);
			alatTerpasangDao.save(alatTerpasangDetail);
		}
			
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("id", resultModel.getNoRec());
		}		
		return result;
	}
	
	@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> checkInMap = new HashMap<>();
		List<Map<String, Object>> checkIn = this.checkInDao.getCheckInByNoRec(noRec);
		//List<Map<String, Object>> checkIn = this.checkInDao.getCheckInByNoRecAntrian(noRec);
		
		//get data Alat Terpasang
		try {
			
			Map<String, Object> alatTerpasangMap = new HashMap<>();
			List<Map<String, Object>> tempList = new ArrayList<>();
			List<Map<String, Object>> alatTerpasangList = this.checkInDao.getAlatTerpasangDetail(noRec);
			//List<Map<String, Object>> alatTerpasangList = this.checkInDao.getAlatTerpasangDetail((String) checkIn.get(0).get("noRec"));
			
			for(Map<String, Object> data : alatTerpasangList){
				Map<String, Object> tempMap = new HashMap<>();
				tempMap.put("noRec", (String) data.get("noRec"));
				tempMap.put("noRecCheckIn", (String) data.get("noRecCheckIn"));
				tempMap.put("id", (Integer) data.get("idAlatTerpasang"));
				tempMap.put("namaAlatTerpasang", (String) data.get("namaAlatTerpasang"));
				tempMap.put("statusEnabled", (Boolean) data.get("statusEnabled"));
				tempList.add(tempMap);
			}
			
			alatTerpasangMap.put("AlatTerpasang", tempList);
			listData.add(alatTerpasangMap);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//get data Pengobatan Khusus
		try {
					
			Map<String, Object> alatTerpasangMap = new HashMap<>();
			List<Map<String, Object>> tempList = new ArrayList<>();
			List<Map<String, Object>> alatTerpasangList = this.checkInDao.getPengobatanKhususDetail(noRec);
			//List<Map<String, Object>> alatTerpasangList = this.checkInDao.getPengobatanKhususDetail((String) checkIn.get(0).get("noRec"));
					
			for(Map<String, Object> data : alatTerpasangList){
				Map<String, Object> tempMap = new HashMap<>();
				tempMap.put("noRec", (String) data.get("noRec"));
				tempMap.put("noRecCheckIn", (String) data.get("noRecCheckIn"));
				tempMap.put("id", (Integer) data.get("idPengobatanKhusus"));
				tempMap.put("namePengobatanKhusus", (String) data.get("namePengobatanKhusus"));
				tempMap.put("value", (String) data.get("value"));
				tempMap.put("statusEnabled", (Boolean) data.get("statusEnabled"));
				tempList.add(tempMap);
			}
					
			alatTerpasangMap.put("PengobatanKhusus", tempList);
			listData.add(alatTerpasangMap);
					
			} catch (Exception e) {
					e.printStackTrace();
			}
		
		checkInMap.put("noRecCheckIn", checkIn);
		listData.add(checkInMap);
		result.put("showData", listData);
		
		return result;
	}
	
	
	@Override
	public Map<String, Object> getCheckInByNoRecAntrian(String noRec) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		List<Map<String, Object>> resultData = this.checkInDao.getCheckInByNoRecAntrian(noRec);
		
		if(CommonUtil.isNotNullOrEmpty(resultData)) {
			result.put("showData", resultData);
		}
		return result;
		
	}
	
	

}
