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.ThtDao;
import com.jasamedika.medifirst2000.dao.ThtDataDao;
import com.jasamedika.medifirst2000.entities.AntrianPasienDiPeriksa;
import com.jasamedika.medifirst2000.entities.PapDataTandaVital;
import com.jasamedika.medifirst2000.entities.PapTandaVital;
import com.jasamedika.medifirst2000.entities.Tht;
import com.jasamedika.medifirst2000.entities.ThtData;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.ThtService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.JsonUtil;
import com.jasamedika.medifirst2000.vo.PapDataTandaVitalVO;
import com.jasamedika.medifirst2000.vo.ThtDataVO;
import com.jasamedika.medifirst2000.vo.ThtVO;

/**
 * Implement class for PAPThtService
 * 
 * @author Askur
 */
@Service("ThtService")
public class ThtServiceImpl implements ThtService<ThtVO> {
	@Autowired
	private BaseConverterImpl<ThtVO, Tht> thtConverter; 
	@Autowired
	private BaseConverterImpl<ThtDataVO, ThtData> thtDataConverter; 
	@Autowired
	private ThtDao thtDao;
	@Autowired
	private ThtDataDao thtDataDao;
	@Autowired
	private AntrianPasienDiPeriksaDao antrianPasienDiPeriksaDao;

	@Override
	public Map<String,Object> addTht2(List<ThtVO> vos) {
		Map<String,Object> result=new HashMap<String,Object>();
		for(ThtVO vo:vos){
			Tht model=thtConverter.transferVOToModel(vo, new Tht());
			AntrianPasienDiPeriksa antrian=antrianPasienDiPeriksaDao.findByNoRec(vo.getAntrianPasienDiPeriksa().getNoRec());
			model.setAntrianPasienDiPeriksa(antrian);
			List<ThtData> list=new ArrayList<ThtData>();
			if(CommonUtil.isNotNullOrEmpty(model.getNoRec())){
				for(ThtData thtData:thtDataDao.findThtDataByNoRecTht(model.getNoRec())){							
					thtDataDao.delete(thtData);
				}				
			}
			for(ThtDataVO voDetail:vo.getChildren()){
				ThtData modelDetail=thtDataConverter.transferVOToModel(voDetail, new ThtData());
				modelDetail.setTht(model);
				list.add(modelDetail);
			}
			model.getChildren().clear();
			model.getChildren().addAll(list);
			Tht resultModel=thtDao.save(model);
			if(CommonUtil.isNotNullOrEmpty(resultModel)){
				try {
					result.put("tht", JsonUtil.ToMap(resultModel));
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

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

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

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

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

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

	@Override
	public Map<String, Object> findAllWithPageAndLimitAndSortByAndDirectionParameter(Integer page, Integer limit,
			String sort, String dir) {
		return null;
	}

}
