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.CairanFiksasiDetailDao;
import com.jasamedika.medifirst2000.dao.JaringanTubuhDetailDao;
import com.jasamedika.medifirst2000.dao.KeteranganLainLainDetailDao;
import com.jasamedika.medifirst2000.dao.ProdukDetailLabAnatomiDao;
import com.jasamedika.medifirst2000.dao.StatusGinekologiDetailDao;
import com.jasamedika.medifirst2000.dao.StrukOrderDao;
import com.jasamedika.medifirst2000.entities.CairanFiksasiDetail;
import com.jasamedika.medifirst2000.entities.DiagnosaOnkologiMorfologi;
import com.jasamedika.medifirst2000.entities.DiagnosaOnkologiTopologi;
import com.jasamedika.medifirst2000.entities.JaringanTubuhDetail;
import com.jasamedika.medifirst2000.entities.KeteranganLainLainDetail;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.Produk;
import com.jasamedika.medifirst2000.entities.ProdukDetailLabAnatomi;
import com.jasamedika.medifirst2000.entities.RiwayatKehamilanObstetri;
import com.jasamedika.medifirst2000.entities.StatusGinekologiDetail;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.ProdukDetailLabAnatomiService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.JsonUtil;
import com.jasamedika.medifirst2000.vo.CairanFiksasiDetailVO;
import com.jasamedika.medifirst2000.vo.DiagnosaOnkologiMorfologiVO;
import com.jasamedika.medifirst2000.vo.DiagnosaOnkologiTopologiVO;
import com.jasamedika.medifirst2000.vo.JaringanTubuhDetailVO;
import com.jasamedika.medifirst2000.vo.KeteranganLainLainDetailVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import com.jasamedika.medifirst2000.vo.ProdukDetailLabAnatomiVO;
import com.jasamedika.medifirst2000.vo.ProdukVO;
import com.jasamedika.medifirst2000.vo.StatusGinekologiDetailVO;

/**
 * Implement class for ProdukDetailLabAnatomiService
 * 
 * @author Askur
 */
@Service("ProdukDetailLabAnatomiService")
public class ProdukDetailLabAnatomiServiceImpl implements ProdukDetailLabAnatomiService<ProdukDetailLabAnatomiVO> {
	@Autowired
	private ProdukDetailLabAnatomiDao produkDetailLabAnatomiDao;
	@Autowired
	private JaringanTubuhDetailDao jaringanTubuhDetailDao;
	@Autowired
	private AntrianPasienDiPeriksaDao antrianPasienDiPeriksaDao;
	@Autowired
	private CairanFiksasiDetailDao cairanFiksasiDetailDao;
	@Autowired
	private KeteranganLainLainDetailDao keteranganLainLainDetailDao;
	@Autowired
	private StatusGinekologiDetailDao statusGinekologiDetailDao;
	@Autowired
	private StrukOrderDao strukOrderDao;
	@Autowired
	private BaseConverterImpl<ProdukDetailLabAnatomiVO, ProdukDetailLabAnatomi> produkDetailLabAnatomiConverter;
	@Autowired
	private BaseConverterImpl<ProdukVO, Produk> produkConverter;
	@Autowired
	private BaseConverterImpl<DiagnosaOnkologiMorfologiVO, DiagnosaOnkologiMorfologi> diagnosaOnkologiMorfologiConverter;
	@Autowired
	private BaseConverterImpl<DiagnosaOnkologiTopologiVO, DiagnosaOnkologiTopologi> diagnosaOnkologiTopologiConverter;
	@Autowired
	private BaseConverterImpl<PegawaiVO, Pegawai> dokterConverter;
	@Autowired
	private BaseConverterImpl<JaringanTubuhDetailVO, JaringanTubuhDetail> jaringanTubuhDetailConverter;
	@Autowired
	private BaseConverterImpl<CairanFiksasiDetailVO, CairanFiksasiDetail> cairanFiksasiDetailConverter;
	@Autowired
	private BaseConverterImpl<KeteranganLainLainDetailVO, KeteranganLainLainDetail> keteranganLainLainDetailConverter;
	@Autowired
	private BaseConverterImpl<StatusGinekologiDetailVO, StatusGinekologiDetail> statusGinekologiDetailConverter;

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

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

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

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

	@Override
	public List<ProdukDetailLabAnatomiVO> 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> addProdukDetailLabAnatomi(ProdukDetailLabAnatomiVO vo) {
		Map<String,Object> result=new HashMap<String,Object>();
		ProdukDetailLabAnatomi model=produkDetailLabAnatomiConverter.transferVOToModel(vo, new ProdukDetailLabAnatomi());
		for(JaringanTubuhDetail pernahDirawatDetailVO:jaringanTubuhDetailDao.findByJaringanTubuh(model.getNoRec())){							
			jaringanTubuhDetailDao.delete(pernahDirawatDetailVO);
		}
		for(CairanFiksasiDetail pernahDirawatDetailVO:cairanFiksasiDetailDao.findByCairanFiksasi(model.getNoRec())){							
			cairanFiksasiDetailDao.delete(pernahDirawatDetailVO);
		}
		for(StatusGinekologiDetail pernahDirawatDetailVO:statusGinekologiDetailDao.findByStatusGinekologi(model.getNoRec())){							
			statusGinekologiDetailDao.delete(pernahDirawatDetailVO);
		}
		for(KeteranganLainLainDetail pernahDirawatDetailVO:keteranganLainLainDetailDao.findByKeteranganLainLain(model.getNoRec())){							
			keteranganLainLainDetailDao.delete(pernahDirawatDetailVO);
		}
		
		List<JaringanTubuhDetail> listJaringanTubuhDetail=new ArrayList<JaringanTubuhDetail>();
		for(JaringanTubuhDetailVO local:vo.getDetailJaringanTubuhSet()){
			JaringanTubuhDetail jaringanTubuhDetail=jaringanTubuhDetailConverter.transferVOToModel(local, new JaringanTubuhDetail());
			jaringanTubuhDetail.setProdukDetailLabAnatomi(model);
			listJaringanTubuhDetail.add(jaringanTubuhDetail);
		}
		model.getDetailJaringanTubuhSet().clear();
		model.getDetailJaringanTubuhSet().addAll(listJaringanTubuhDetail);

		List<CairanFiksasiDetail> listCairanFiksasiDetail=new ArrayList<CairanFiksasiDetail>();
		for(CairanFiksasiDetailVO local:vo.getDetailCairanFiksasiSet()){
			CairanFiksasiDetail cairanFiksasiDetail=cairanFiksasiDetailConverter.transferVOToModel(local, new CairanFiksasiDetail());
			cairanFiksasiDetail.setProdukDetailLabAnatomi(model);
			listCairanFiksasiDetail.add(cairanFiksasiDetail);
		}
		model.getDetailCairanFiksasiSet().clear();
		model.getDetailCairanFiksasiSet().addAll(listCairanFiksasiDetail);
		
		List<KeteranganLainLainDetail> listKeteranganLainLainDetail=new ArrayList<KeteranganLainLainDetail>();
		for(KeteranganLainLainDetailVO local:vo.getDetailKeteranganLainLainSet()){
			KeteranganLainLainDetail keteranganLainLainDetail=keteranganLainLainDetailConverter.transferVOToModel(local, new KeteranganLainLainDetail());
			keteranganLainLainDetail.setProdukDetailLabAnatomi(model);
			listKeteranganLainLainDetail.add(keteranganLainLainDetail);
		}
		model.getDetailKeteranganLainLainSet().clear();
		model.getDetailKeteranganLainLainSet().addAll(listKeteranganLainLainDetail);
		
		List<StatusGinekologiDetail> listStatusGinekologiDetail=new ArrayList<StatusGinekologiDetail>();
		for(StatusGinekologiDetailVO local:vo.getDetailStatusGinekologiSet()){
			StatusGinekologiDetail statusGinekologiDetail=statusGinekologiDetailConverter.transferVOToModel(local, new StatusGinekologiDetail());
			statusGinekologiDetail.setProdukDetailLabAnatomi(model);
			listStatusGinekologiDetail.add(statusGinekologiDetail);
		}
		model.getDetailStatusGinekologiSet().clear();
		model.getDetailStatusGinekologiSet().addAll(listStatusGinekologiDetail);
		
		Produk produk=produkConverter.transferVOToModel(vo.getProduk(),new Produk());
		DiagnosaOnkologiMorfologi diagnosaOnkologiMorfologi=diagnosaOnkologiMorfologiConverter.transferVOToModel(vo.getDiagnosaOnkologiMorfologi(),new DiagnosaOnkologiMorfologi());
		DiagnosaOnkologiTopologi diagnosaOnkologiTopologi=diagnosaOnkologiTopologiConverter.transferVOToModel(vo.getDiagnosaOnkologiTopologi(),new DiagnosaOnkologiTopologi());
		if(CommonUtil.isNotNullOrEmpty(vo.getDokter())){
		Pegawai dokter=dokterConverter.transferVOToModel(vo.getDokter(),new Pegawai());
		model.setDokter(dokter);
		}
		model.setAntrianPasienDiPeriksa(antrianPasienDiPeriksaDao.findOne(vo.getNoRecAntrian()));
		model.setStrukOrder(strukOrderDao.findByNoOrder(vo.getNoOrder()));
		model.setProduk(produk);
		model.setDiagnosaOnkologiMorfologi(diagnosaOnkologiMorfologi);
		model.setDiagnosaOnkologiTopologi(diagnosaOnkologiTopologi);
		ProdukDetailLabAnatomi resultModel=produkDetailLabAnatomiDao.save(model);
		try {
			result.put("produkDetailLabAnatomi",JsonUtil.ToMap(resultModel));
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return result;
	}



	
}
