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.CheckOutOperationConverter;
import com.jasamedika.medifirst2000.dao.AntrianPasienDiPeriksaDao;
import com.jasamedika.medifirst2000.dao.CheckOutOperationDao;
import com.jasamedika.medifirst2000.entities.AntrianPasienDiPeriksa;
import com.jasamedika.medifirst2000.entities.CekKelengkapanAlkes;
import com.jasamedika.medifirst2000.entities.CekKelengkapanAlkesDetail;
import com.jasamedika.medifirst2000.entities.CheckOutOperation;
import com.jasamedika.medifirst2000.entities.KelengkapanCheckOut;
import com.jasamedika.medifirst2000.entities.KelengkapanCheckOutDetail;
import com.jasamedika.medifirst2000.entities.KesadaranCheckOut;
import com.jasamedika.medifirst2000.entities.KesadaranCheckOutDetail;
import com.jasamedika.medifirst2000.entities.PemasanganCheckOut;
import com.jasamedika.medifirst2000.entities.PemasanganCheckOutDetail;
import com.jasamedika.medifirst2000.entities.PemeriksaanCheckOut;
import com.jasamedika.medifirst2000.entities.PemeriksaanCheckOutDetail;
import com.jasamedika.medifirst2000.entities.PemeriksaanJaringanCairanTubuh;
import com.jasamedika.medifirst2000.entities.PemeriksaanJaringanCairanTubuhDetail;
import com.jasamedika.medifirst2000.entities.SignOutOperation;
import com.jasamedika.medifirst2000.entities.StatusLengkapTidakLengkap;
import com.jasamedika.medifirst2000.entities.StatusYaTidak;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.CheckOutOperationService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.CekKelengkapanAlkesDetailVO;
import com.jasamedika.medifirst2000.vo.CheckOutOperationVO;
import com.jasamedika.medifirst2000.vo.KelengkapanCheckOutDetailVO;
import com.jasamedika.medifirst2000.vo.KelengkapanCheckOutVO;
import com.jasamedika.medifirst2000.vo.KesadaranCheckOutDetailVO;
import com.jasamedika.medifirst2000.vo.KesadaranCheckOutVO;
import com.jasamedika.medifirst2000.vo.PemasanganCheckOutDetailVO;
import com.jasamedika.medifirst2000.vo.PemasanganCheckOutVO;
import com.jasamedika.medifirst2000.vo.PemeriksaanCheckOutDetailVO;
import com.jasamedika.medifirst2000.vo.PemeriksaanCheckOutVO;
import com.jasamedika.medifirst2000.vo.PemeriksaanJaringanCairanTubuhDetailVO;
import com.jasamedika.medifirst2000.vo.StatusYaTidakVO;

/**
 * Implement class for CheckOutOperationServiceImpl
 * 
 * @author shakato
 * @param <T>
 */
@Service("CheckOutOperationService")
public class CheckOutOperationServiceImpl implements CheckOutOperationService {
	
	@Autowired
	private CheckOutOperationConverter checkOutOperationConverter;
	
	@Autowired
	private BaseConverterImpl<KesadaranCheckOutVO, KesadaranCheckOut> kesadaranCheckOutConverter;
	
	@Autowired
	private BaseConverterImpl<KesadaranCheckOutDetailVO, KesadaranCheckOutDetail> kesadaranCheckOutDetailConverter;
	
	@Autowired
	private BaseConverterImpl<KelengkapanCheckOutVO, KelengkapanCheckOut> kelangkapanCheckOutConverter;
	
	@Autowired
	private BaseConverterImpl<KelengkapanCheckOutDetailVO, KelengkapanCheckOutDetail> kelangkapanCheckOutDetailConverter;
	
	@Autowired
	private BaseConverterImpl<PemeriksaanCheckOutVO, PemeriksaanCheckOut> pemeriksaanCheckOutConverter;
	
	@Autowired
	private BaseConverterImpl<PemeriksaanCheckOutDetailVO, PemeriksaanCheckOutDetail> pemeriksaanCheckOutDetailConverter;
	
	@Autowired
	private BaseConverterImpl<PemasanganCheckOutVO, PemasanganCheckOut> pemasanganCheckOutConverter;
	
	@Autowired
	private BaseConverterImpl<PemasanganCheckOutDetailVO, PemasanganCheckOutDetail> pemasanganCheckOutDetailConverter;
	
	@Autowired
	private BaseConverterImpl<StatusYaTidakVO, StatusYaTidak> statusYaTidakConverter;
	
	@Autowired
	private AntrianPasienDiPeriksaDao antrianPasienDiPeriksaDao;
	
	@Autowired
	private CheckOutOperationDao checkOutOperationDao;
	
	@Override
	public CheckOutOperationVO add(CheckOutOperationVO vo) throws JpaSystemException, ServiceVOException {
		return null;
	}

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

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

	@Override
	public CheckOutOperationVO findById(Integer key) throws JpaSystemException {
		return null;
	}

	@Override
	public List<CheckOutOperationVO> 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> saveCheckOutOperationService(CheckOutOperationVO vo) {
		CheckOutOperation checkOutOperation = new CheckOutOperation();
		checkOutOperation = checkOutOperationConverter.transferVOToModel(vo, checkOutOperation);
		AntrianPasienDiPeriksa norec = antrianPasienDiPeriksaDao.findByNoRec(vo.getPasienDaftar().getNoRec());
		checkOutOperation.setPasienDaftar(norec);
		
		checkOutOperation.getKesadaranCheckOutDetail().clear();
		for (KesadaranCheckOutDetailVO kesadaranCheckOutDetailVO:vo.getKesadaranCheckOutDetail()){
			KesadaranCheckOutDetail kesadaranCheckOutDetail = new KesadaranCheckOutDetail();
			kesadaranCheckOutDetail = kesadaranCheckOutDetailConverter.transferVOToModel(kesadaranCheckOutDetailVO, new KesadaranCheckOutDetail());
			kesadaranCheckOutDetail.setCheckOutOperation(checkOutOperation);
			kesadaranCheckOutDetail.setKesadaranCheckOut(kesadaranCheckOutConverter.transferVOToModel(kesadaranCheckOutDetailVO.getKesadaranCheckOut(), new KesadaranCheckOut()));
			if(CommonUtil.isNotNullOrEmpty(kesadaranCheckOutDetail.getCheckOutOperation())){
				checkOutOperation.getKesadaranCheckOutDetail().add(kesadaranCheckOutDetail);
			}
		}
		
		checkOutOperation.getKelengkapanCheckOutDetail().clear();
		for (KelengkapanCheckOutDetailVO kelengkapanCheckOutDetailVO:vo.getKelengkapanCheckOutDetail()){
			KelengkapanCheckOutDetail kelengkapanCheckOutDetail = new KelengkapanCheckOutDetail(); 
			kelengkapanCheckOutDetail = kelangkapanCheckOutDetailConverter.transferVOToModel(kelengkapanCheckOutDetailVO, new KelengkapanCheckOutDetail());
			kelengkapanCheckOutDetail.setCheckOutOperation(checkOutOperation);
			kelengkapanCheckOutDetail.setKelengkapanCheckOut(kelangkapanCheckOutConverter.transferVOToModel(kelengkapanCheckOutDetailVO.getKelengkapanCheckOut(), new KelengkapanCheckOut()));
			kelengkapanCheckOutDetail.setStatusYaTidak(statusYaTidakConverter.transferVOToModel(kelengkapanCheckOutDetailVO.getStatusYaTidak(), new StatusYaTidak()));
			if(CommonUtil.isNotNullOrEmpty(kelengkapanCheckOutDetail.getCheckOutOperation())){
				checkOutOperation.getKelengkapanCheckOutDetail().add(kelengkapanCheckOutDetail);
			}
		}
		
		checkOutOperation.getPemeriksaanCheckOutDetail().clear();
		for (PemeriksaanCheckOutDetailVO pemeriksaanCheckOutDetailVO:vo.getPemeriksaanCheckOutDetail()){
			PemeriksaanCheckOutDetail pemeriksaanCheckOutDetail = pemeriksaanCheckOutDetailConverter.transferVOToModel(pemeriksaanCheckOutDetailVO, new PemeriksaanCheckOutDetail());
			pemeriksaanCheckOutDetail.setCheckOutOperation(checkOutOperation);
			pemeriksaanCheckOutDetail.setPemeriksaanCheckOut(pemeriksaanCheckOutConverter.transferVOToModel(pemeriksaanCheckOutDetailVO.getPemeriksaanCheckOut(), new PemeriksaanCheckOut()));
			pemeriksaanCheckOutDetail.setStatusYaTidak(statusYaTidakConverter.transferVOToModel(pemeriksaanCheckOutDetailVO.getStatusYaTidak(), new StatusYaTidak()));
			if(CommonUtil.isNotNullOrEmpty(pemeriksaanCheckOutDetail.getCheckOutOperation())){
				checkOutOperation.getPemeriksaanCheckOutDetail().add(pemeriksaanCheckOutDetail);
			}
		}
		
		checkOutOperation.getPemasanganCheckOutDetail().clear();	
		for (PemasanganCheckOutDetailVO pemasanganCheckOutDetailVO:vo.getPemasanganCheckOutDetail()){
			PemasanganCheckOutDetail pemasanganCheckOutDetail = pemasanganCheckOutDetailConverter.transferVOToModel(pemasanganCheckOutDetailVO, new PemasanganCheckOutDetail());
			pemasanganCheckOutDetail.setCheckOutOperation(checkOutOperation);
			pemasanganCheckOutDetail.setPemasanganCheckOut(pemasanganCheckOutConverter.transferVOToModel(pemasanganCheckOutDetailVO.getPemasanganCheckOut(), new PemasanganCheckOut()));
			pemasanganCheckOutDetail.setStatusYaTidak(statusYaTidakConverter.transferVOToModel(pemasanganCheckOutDetailVO.getStatusYaTidak(), new StatusYaTidak()));
			if(CommonUtil.isNotNullOrEmpty(pemasanganCheckOutDetail.getCheckOutOperation())){
				checkOutOperation.getPemasanganCheckOutDetail().add(pemasanganCheckOutDetail);
			}
		}
		CheckOutOperation model = checkOutOperationDao.save(checkOutOperation);
		Map<String, Object> result = new HashMap<String, Object>();
		if (CommonUtil.isNotNullOrEmpty(model)) {
			result.put("noRec", model.getNoRec());
		}
		return result;
	}

	@Override
	public Map<String, Object> getKesadaranCheckOut() {
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> data = this.checkOutOperationDao.getKesadaranCheckOut();
		if(CommonUtil.isNotNullOrEmpty(data)) {
			result.put("id", data);
		}
		return  result;
	}

	@Override
	public Map<String, Object> getKelengkapanCheckOut() {
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> data = this.checkOutOperationDao.getKelengkapanCheckOut();
		if(CommonUtil.isNotNullOrEmpty(data)) {
			result.put("data", data);
		}
		return result;
	}
	
	
	@Override
	public Map<String, Object> getCheckOutByNoRec(String noRec) {
		Map<String, Object> result = new HashMap<String, Object>();
		List<Map<String, Object>> listData = new ArrayList<>();

		Map<String, Object> checkOutMap = new HashMap<>();
		List<Map<String, Object>> checkOut = this.checkOutOperationDao.getCheckOutByNoRec(noRec);
		
		//get data KesadaranCheckOutDetail
		try {
			
			Map<String, Object> kesadaranMap = new HashMap<>();
			List<Map<String, Object>> tempList = new ArrayList<>();
			List<Map<String, Object>> kesadaranList = this.checkOutOperationDao.
					getKesadaranCheckOutDetail(noRec);
			//List<Map<String, Object>> alatTerpasangList = this.checkInDao.getAlatTerpasangDetail((String) checkIn.get(0).get("noRec"));
			
			for(Map<String, Object> data : kesadaranList){
				Map<String, Object> tempMap = new HashMap<>();
				tempMap.put("noRec", (String) data.get("noRec"));
				tempMap.put("status", (Boolean) data.get("status"));
				tempMap.put("noRecCheckOutOperation", (String) data.get("noRecCheckOutOperation"));
				tempMap.put("idKesadaranCheckOut", (Integer) data.get("idKesadaranCheckOut"));
				tempMap.put("nameKesadaranCheckOut", (String) data.get("nameKesadaranCheckOut"));
				tempList.add(tempMap);
			}
			
			kesadaranMap.put("KesadaranCheckOutDetail", tempList);
			listData.add(kesadaranMap);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//get data KelengkapanCheckOutDetail
		try {
					
			Map<String, Object> kelengkapanlMap = new HashMap<>();
			List<Map<String, Object>> tempList = new ArrayList<>();
			List<Map<String, Object>> kelengkapanList = this.checkOutOperationDao.
					getKelengkapanCheckOutDetail(noRec);
			
			for(Map<String, Object> data : kelengkapanList){
				Map<String, Object> tempMap = new HashMap<>();
				tempMap.put("noRec", (String) data.get("noRec"));
				tempMap.put("noRecCheckOutOperation", (String) data.get("noRecCheckOutOperation"));
				tempMap.put("keterangan", (String) data.get("keterangan"));
				tempMap.put("id", (Integer) data.get("idKelengkapanCheckOut"));
				tempMap.put("namekelengkapanCheckOut", (String) data.get("namekelengkapanCheckOut"));
				tempMap.put("idStatusYaTidak", (Integer) data.get("idStatusYaTidak"));
				tempMap.put("nameStatusYaTidak", (String) data.get("nameStatusYaTidak"));
				tempList.add(tempMap);
			}
					
			kelengkapanlMap.put("KelengkapanCheckOutDetail", tempList);
			listData.add(kelengkapanlMap);
					
			} catch (Exception e) {
					e.printStackTrace();
			}
		
		
		//get data PemeriksaanCheckOutDetail
		try {
							
			Map<String, Object> pemeriksaanMap = new HashMap<>();
			List<Map<String, Object>> tempList = new ArrayList<>();
			List<Map<String, Object>> pemeriksaanList = this.checkOutOperationDao.
				getPemeriksaanCheckOutDetailDetail(noRec);
			
			for(Map<String, Object> data : pemeriksaanList){
				Map<String, Object> tempMap = new HashMap<>();
				tempMap.put("noRec", (String) data.get("noRec"));
				tempMap.put("noRecCheckOutOperation", (String) data.get("noRecCheckOutOperation"));
				tempMap.put("keterangan", (String) data.get("keterangan"));
				tempMap.put("id", (Integer) data.get("idpemeriksaanCheckOut"));
				tempMap.put("namePemeriksaanCheckOut", (String) data.get("namePemeriksaanCheckOut"));
				tempMap.put("idStatusYaTidak", (Integer) data.get("idStatusYaTidak"));
				tempMap.put("nameStatusYaTidak", (String) data.get("nameStatusYaTidak"));
				tempList.add(tempMap);
			}
							
			pemeriksaanMap.put("PemeriksaanCheckOutDetail", tempList);
			listData.add(pemeriksaanMap);
							
		} catch (Exception e) {
			e.printStackTrace();
		}
			
		//get data PemasanganCheckOutDetail
		try {
									
			Map<String, Object> pemasanganMap = new HashMap<>();
			List<Map<String, Object>> tempList = new ArrayList<>();
			List<Map<String, Object>> pemasanganList = this.checkOutOperationDao.
				getPemasanganCheckOutDetail(noRec);
					
			for(Map<String, Object> data : pemasanganList){
				Map<String, Object> tempMap = new HashMap<>();
				tempMap.put("noRec", (String) data.get("noRec"));
				tempMap.put("noRecCheckOutOperation", (String) data.get("noRecCheckOutOperation"));
				tempMap.put("keterangan", (String) data.get("keterangan"));
				tempMap.put("id", (Integer) data.get("idPemasanganCheckOut"));
				tempMap.put("namePemasanganCheckOut", (String) data.get("namePemasanganCheckOut"));
				tempMap.put("idStatusYaTidak", (Integer) data.get("idStatusYaTidak"));
				tempMap.put("nameStatusYaTidak", (String) data.get("nameStatusYaTidak"));
				tempList.add(tempMap);
			}
									
			pemasanganMap.put("PemasanganCheckOutDetail", tempList);
			listData.add(pemasanganMap);
									
		} catch (Exception e) {
			e.printStackTrace();
		}
				
		checkOutMap.put("noRecCheckOut", checkOut);
		listData.add(checkOutMap);
		result.put("showData", listData);
		
		return result;
	}
	
	
	@Override
	public Map<String, Object> getCheckOutByNoRecAntrian(String noRec) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		List<Map<String, Object>> resultData = this.checkOutOperationDao.getCheckOutByNoRecAntrian(noRec);
		
		if(CommonUtil.isNotNullOrEmpty(resultData)) {
			result.put("showData", resultData);
		}
		return result;
		
	}

	
	
	
	

}