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.dao.AntrianPasienDiPeriksaDao;
import com.jasamedika.medifirst2000.dao.LayananDao;
import com.jasamedika.medifirst2000.dao.TindakanLainDao;
import com.jasamedika.medifirst2000.entities.Layanan;
import com.jasamedika.medifirst2000.entities.LayananDetail;
import com.jasamedika.medifirst2000.entities.ObservasiIGD;
import com.jasamedika.medifirst2000.entities.TindakanLain;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.LayananService;
import com.jasamedika.medifirst2000.service.PasienDaftarService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.LayananDetailVO;
import com.jasamedika.medifirst2000.vo.LayananVO;
import com.jasamedika.medifirst2000.vo.LayananVOCustom;
import com.jasamedika.medifirst2000.vo.ObservasiIGDVO;
import com.jasamedika.medifirst2000.vo.TindakanLainVO;

@Service("layananService")
public class LayananServiceImpl implements LayananService{
	@Autowired
	private AntrianPasienDiPeriksaDao antrianPasienDiPeriksaDao;
	@Autowired
	private BaseConverterImpl<LayananVO, Layanan> layananConverter;
	
	@Autowired
	private BaseConverterImpl<LayananDetailVO, LayananDetail> layananDetailConverter;
	
	@Autowired
	private BaseConverterImpl<ObservasiIGDVO, ObservasiIGD> observasiIGDConverter;
	
	@Autowired
	private BaseConverterImpl<TindakanLainVO, TindakanLain> tindakanLainConverter;
	
	@Autowired
	private TindakanLainDao tindakanLainDao;
	
	@Autowired
	private LayananDao layananDao;
	
	@Autowired
	private PasienDaftarService pasienDaftarService;
	
	@Override
	public Map<String, Object> findAllWithPageAndLimitAndSortByAndDirectionParameter(Integer page, Integer limit,
			String sort, String dir) {
		return null;
	}

	@Override
	public Map<String, Object> saveLayanan(LayananVOCustom vo) {
		Map <String, Object> result = new HashMap<String, Object>();
		for(LayananVO layananVO : vo.getLayanan())
		{
			Layanan layanan = layananConverter.transferVOToModel(layananVO, new Layanan());
			layanan.setLayananDetail(layananDetailConverter.transferVOToModel(layananVO.getLayananDetail(), new LayananDetail()));
			layanan.setPasien(antrianPasienDiPeriksaDao.findbyNoCmAndTglRegistrasi(layananVO.getPasien().getId(), layananVO.getTglRegistrasi()));
			Layanan resultModel = layananDao.save(layanan);			
			if(CommonUtil.isNotNullOrEmpty(resultModel)){
				result.put("idLayanan", resultModel.getNoRec());
				result.put("Keterangan", resultModel.getKeterangan());
			}
		}
		for(TindakanLainVO tindakanLainVO : vo.getTindakanLain())
		{
			TindakanLain tindakanLain = tindakanLainConverter.transferVOToModel(tindakanLainVO, new TindakanLain());
			tindakanLain.setPasien(antrianPasienDiPeriksaDao.findbyNoCmAndTglRegistrasi(tindakanLainVO.getPasien().getId(), tindakanLainVO.getTglRegistrasi()));
			TindakanLain resultModel = tindakanLainDao.save(tindakanLain);			
			if(CommonUtil.isNotNullOrEmpty(resultModel)){
				result.put("idTindakanLain", resultModel.getNoRec());
				result.put("Inhalasi", resultModel.getInhalasi());
			}

		}
				return result;
	}

	@Override
	public Map<String, Object> updateLayanan(LayananVOCustom vo) {
		Map <String, Object> result = new HashMap<String, Object>();
//		for(LayananVO layananVO:vo.getLayanan())
//		{
//			Layanan layanan = layananConverter.transferVOToModel(layananVO, new Layanan());
//			if(CommonUtil.isNotNullOrEmpty(layanan))
//			{
//				List<Layanan> listLayanan = layananDao.findByNoRec(layanan.getObservasiIGDId());
//				layananDao.delete(listLayanan);
//			}			
//		}
//		for(TindakanLainVO tindakanLainVO:vo.getTindakanLain())
//		{
//			TindakanLain tindakanLain = tindakanLainConverter.transferVOToModel(tindakanLainVO, new TindakanLain());
//			if(CommonUtil.isNotNullOrEmpty(tindakanLain))
//			{
//				List<TindakanLain> listTindakanLain = tindakanLainDao.findByNoRec(tindakanLain.getObservasiIGDId());
//				tindakanLainDao.delete(tindakanLain);
//			}			
//		}
//		for(LayananVO layananVO : vo.getLayanan())
//		{
//			Layanan layanan = layananConverter.transferVOToModel(layananVO, new Layanan());
//			layanan.setLayananDetail(layananDetailConverter.transferVOToModel(layananVO.getLayananDetail(), new LayananDetail()));
//			layanan.setObservasiIGD(observasiIGDConverter.transferVOToModel(layananVO.getObservasiIGD(), new ObservasiIGD()));
//			Layanan resultModel = layananDao.save(layanan);			
//			if(CommonUtil.isNotNullOrEmpty(resultModel)){
//				result.put("idLayanan", resultModel.getNoRec());
//				result.put("Keterangan", resultModel.getKeterangan());
//			}
//		}
//		for(TindakanLainVO tindakanLainVO : vo.getTindakanLain())
//		{
//			TindakanLain tindakanLain = tindakanLainConverter.transferVOToModel(tindakanLainVO, new TindakanLain());
//			tindakanLain.setObservasiIGD(observasiIGDConverter.transferVOToModel(tindakanLainVO.getObservasiIGD(), new ObservasiIGD()));
//			TindakanLain resultModel = tindakanLainDao.save(tindakanLain);			
//			if(CommonUtil.isNotNullOrEmpty(resultModel)){
//				result.put("idTindakanLain", resultModel.getNoRec());
//				result.put("Inhalasi", resultModel.getInhalasi());
//			}

//		}
		return result;
	}

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

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

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

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

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