package com.jasamedika.medifirst2000.service.impl;

import java.util.ArrayList;
import java.util.Date;
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 org.springframework.transaction.annotation.Transactional;

import com.jasamedika.medifirst2000.converter.PapDataTandaVitalConverter;
import com.jasamedika.medifirst2000.converter.PapGinekologiConverter;
import com.jasamedika.medifirst2000.converter.PapLaboratoriumConverter;
import com.jasamedika.medifirst2000.dao.PapGinekologiDao;
import com.jasamedika.medifirst2000.dao.PasienDao;
import com.jasamedika.medifirst2000.dao.RegistrasiPelayananPasienDao;
import com.jasamedika.medifirst2000.entities.PapKeluhanTambahan;
import com.jasamedika.medifirst2000.entities.PapLaboratorium;
import com.jasamedika.medifirst2000.entities.PapDataTandaVital;
import com.jasamedika.medifirst2000.entities.PapGinekologi;
import com.jasamedika.medifirst2000.entities.Pasien;
import com.jasamedika.medifirst2000.entities.RegistrasiPelayananPasien;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.PapGinekologiService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.JsonUtil;
import com.jasamedika.medifirst2000.vo.PapKeluhanTambahanVO;
import com.jasamedika.medifirst2000.vo.PapLaboratoriumVO;
import com.jasamedika.medifirst2000.vo.PapDataTandaVitalVO;
import com.jasamedika.medifirst2000.vo.PapGinekologiVO;

/**
 * Implement class for PAPGinekologiService
 * 
 * @author Askur
 */
@Service("PAPGinekologiService")
public class PapGinekologiServiceImpl implements PapGinekologiService {
	@Autowired
	private PapGinekologiDao papGinekologiDao;
	@Autowired
	private PapLaboratoriumConverter papLaboratoriumConverter;
	@Autowired
	private PapDataTandaVitalConverter papDataTandaVitalConverter;
	@Autowired
	private PapGinekologiConverter papGinekologiConverter;
	@Autowired
	private RegistrasiPelayananPasienDao registrasiPelayananPasienDao;
	@Autowired
	private PasienDao pasienDao;
	
	@Override
	@Transactional(readOnly=false)
	public Map<String,Object> addGinekologi(PapGinekologiVO vo) throws JpaSystemException, ServiceVOException {
		Map<String,Object> result=new HashMap<String,Object>();
		PapGinekologi model=papGinekologiConverter.transferVOToModel(vo, new PapGinekologi());
		List<PapLaboratorium> detail=new ArrayList<PapLaboratorium>();
		for(PapLaboratoriumVO voDetail:vo.getLaboratoriumSet()){
			PapLaboratorium modelDetail=papLaboratoriumConverter.transferVOToModel(voDetail, new PapLaboratorium());
			modelDetail.setPapGinekologi(model);
			detail.add(modelDetail);
		}
		model.getLaboratoriumSet().clear();
		model.getLaboratoriumSet().addAll(detail);
		List<PapDataTandaVital> detailPapDataTandaVital=new ArrayList<PapDataTandaVital>();
		for(PapDataTandaVitalVO voDetail:vo.getPapDataTandaVitalSet()){
			PapDataTandaVital modelDetail=papDataTandaVitalConverter.transferVOToModel(voDetail, new PapDataTandaVital());
			modelDetail.setPapGinekologi(model);
			detailPapDataTandaVital.add(modelDetail);
		}
		model.getPapDataTandaVitalSet().clear();
		model.getPapDataTandaVitalSet().addAll(detailPapDataTandaVital);
		model.setPasien(null);
		PapGinekologi resultModel=papGinekologiDao.save(model);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("papGinekologi", resultModel.noRec);
		}
		return result;
	}

	@Override
	public Map<String,Object> updateGinekologi(PapGinekologiVO vo) throws JpaSystemException, ServiceVOException {
		Map<String,Object> result=new HashMap<String,Object>();
		PapGinekologi model=papGinekologiConverter.transferVOToModel(vo, new PapGinekologi());
		List<PapLaboratorium> detail=new ArrayList<PapLaboratorium>();
		for(PapLaboratoriumVO voDetail:vo.getLaboratoriumSet()){
			PapLaboratorium modelDetail=papLaboratoriumConverter.transferVOToModel(voDetail, new PapLaboratorium());
			modelDetail.setPapGinekologi(model);
			detail.add(modelDetail);
		}
		model.getLaboratoriumSet().clear();
		model.getLaboratoriumSet().addAll(detail);
		List<PapDataTandaVital> detailPapDataTandaVital=new ArrayList<PapDataTandaVital>();
		for(PapDataTandaVitalVO voDetail:vo.getPapDataTandaVitalSet()){
			PapDataTandaVital modelDetail=papDataTandaVitalConverter.transferVOToModel(voDetail, new PapDataTandaVital());
			modelDetail.setPapGinekologi(model);
			detailPapDataTandaVital.add(modelDetail);
		}
		model.getPapDataTandaVitalSet().clear();
		model.getPapDataTandaVitalSet().addAll(detailPapDataTandaVital);
		PapGinekologi resultModel=papGinekologiDao.save(model);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("papGinekologi", resultModel.noRec);
			result.put("dataFound", true);
		}else{
			result.put("dataFound", false);			
		}
		return result;
	}

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

	@Override
	public Map<String,Object> findByNoCmAndTglInput(String noCm, Date tglInput) {
		Map<String,Object> result=new HashMap<String,Object>();
		PapGinekologi resultModel=papGinekologiDao.findPapGinekologiByNoCmAndTglInput(noCm, tglInput);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("papGinekologi", resultModel);
			result.put("dataFound", true);
		} else {
			result.put("dataFound", false);
		}
		return result;
	}

	@Override
	public Map<String,Object> findRegistrasiPelayananPasienByNoCmAndTglRegistrasi(String noCm, short kdProfile,Date tglRegistrasi) {
		Map<String,Object> result=new HashMap<String,Object>();
		RegistrasiPelayananPasien resultModel=registrasiPelayananPasienDao.finByNoCm(noCm,kdProfile,tglRegistrasi);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("noRec", resultModel.getNoRec());
			result.put("tglRegistrasi", resultModel.getTglMasuk());
			result.put("noAntrian", resultModel.getNoAntrian());
			result.put("dataFound", true);
			if(CommonUtil.isNotNullOrEmpty(resultModel.getPasien().getId())){
				result.put("pasien.noCm", resultModel.getPasien().getNoCm());
			}
		}else{
			result.put("dataFound", false);
		}
		return result;
	}

	@Override
	public Map<String,Object> findPasienByNoCm(String noCm) {
		Map<String,Object> result=new HashMap<String,Object>();
		List<Pasien> resultModel=pasienDao.findPasienBynoCm(noCm);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			for(Pasien pasien:resultModel){
				result.put("noCm", pasien.getNoCm());
				result.put("namaPasien", pasien.getNamaPasien());
				if(CommonUtil.isNotNullOrEmpty((pasien.getAlamats().iterator().next()))){
				result.put("alamatLengkap", (pasien.getAlamats().iterator().next()).getAlamatLengkap());
				}
				if(CommonUtil.isNotNullOrEmpty(pasien.getJenisKelamin())){
				result.put("jenisKelamin", pasien.getJenisKelamin().getNamaExternal());
				}
				if(CommonUtil.isNotNullOrEmpty(pasien.getStatusPerkawinan())){
				result.put("statusPerkawinan", pasien.getStatusPerkawinan().getNamaExternal());
				}
				result.put("dataFound", true);
			}
		}else{
			result.put("dataFound", false);
		}
		return result;
	}

	@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 Map findAllWithPageAndLimitAndSortByAndDirectionParameter(Integer page, Integer limit, String sort,
			String dir) {
		return null;
	}


}
