package com.jasamedika.medifirst2000.service.impl;

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 com.jasamedika.medifirst2000.converter.PapGigiKananDuaConverter;
import com.jasamedika.medifirst2000.converter.PapGigiKananEmpatConverter;
import com.jasamedika.medifirst2000.converter.PapGigiKananSatuConverter;
import com.jasamedika.medifirst2000.converter.PapGigiKananTigaConverter;
import com.jasamedika.medifirst2000.converter.PapGigiKiriDuaConverter;
import com.jasamedika.medifirst2000.converter.PapGigiKiriEmpatConverter;
import com.jasamedika.medifirst2000.converter.PapGigiKiriSatuConverter;
import com.jasamedika.medifirst2000.converter.PapGigiKiriTigaConverter;
import com.jasamedika.medifirst2000.converter.PapGigiMulutConverter;
import com.jasamedika.medifirst2000.dao.PapGigiMulutDao;
import com.jasamedika.medifirst2000.dao.PasienDao;
import com.jasamedika.medifirst2000.dao.RegistrasiPelayananPasienDao;
import com.jasamedika.medifirst2000.entities.PapKeluhanTambahan;
import com.jasamedika.medifirst2000.entities.PapGigiKananDua;
import com.jasamedika.medifirst2000.entities.PapGigiKananEmpat;
import com.jasamedika.medifirst2000.entities.PapGigiKananSatu;
import com.jasamedika.medifirst2000.entities.PapGigiKananTiga;
import com.jasamedika.medifirst2000.entities.PapGigiKiriDua;
import com.jasamedika.medifirst2000.entities.PapGigiKiriEmpat;
import com.jasamedika.medifirst2000.entities.PapGigiKiriSatu;
import com.jasamedika.medifirst2000.entities.PapGigiKiriTiga;
import com.jasamedika.medifirst2000.entities.PapGigiMulut;
import com.jasamedika.medifirst2000.entities.Pasien;
import com.jasamedika.medifirst2000.entities.RegistrasiPelayananPasien;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.PapGigiMulutService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.PapKeluhanTambahanVO;
import com.jasamedika.medifirst2000.vo.PapGigiMulutVO;

/**
 * Implement class for PAPGigiMulutService
 * 
 * @author Askur
 */
@Service("PAPGigiMulutService")
public class PapGigiMulutServiceImpl implements PapGigiMulutService {
	@Autowired
	private PapGigiMulutDao papGigiMulutDao;
	@Autowired
	private PapGigiMulutConverter papGigiMulutConverter;
	@Autowired
	private PapGigiKiriSatuConverter papGigiKiriSatuConverter;
	@Autowired
	private PapGigiKiriDuaConverter papGigiKiriDuaConverter;
	@Autowired
	private PapGigiKiriTigaConverter papGigiKiriTigaConverter;
	@Autowired
	private PapGigiKiriEmpatConverter papGigiKiriEmpatConverter;
	@Autowired
	private PapGigiKananSatuConverter papGigiKananSatuConverter;
	@Autowired
	private PapGigiKananDuaConverter papGigiKananDuaConverter;
	@Autowired
	private PapGigiKananTigaConverter papGigiKananTigaConverter;
	@Autowired
	private PapGigiKananEmpatConverter papGigiKananEmpatConverter;
	@Autowired
	private RegistrasiPelayananPasienDao registrasiPelayananPasienDao;
	@Autowired
	private PasienDao pasienDao;
	
	@Override
	public Map<String,Object> addGigiMulut(PapGigiMulutVO vo) throws JpaSystemException, ServiceVOException {
		Map<String,Object> result=new HashMap<String,Object>();
		PapGigiMulut model=papGigiMulutConverter.transferVOToModel(vo, new PapGigiMulut());
		PapGigiKiriSatu kiriSatu=papGigiKiriSatuConverter.transferVOToModel(vo.getPapGigiKiriSatu(),new PapGigiKiriSatu());
		PapGigiKiriDua kiriDua=papGigiKiriDuaConverter.transferVOToModel(vo.getPapGigiKiriDua(),new PapGigiKiriDua());
		PapGigiKiriTiga kiriTiga=papGigiKiriTigaConverter.transferVOToModel(vo.getPapGigiKiriTiga(),new PapGigiKiriTiga());
		PapGigiKiriEmpat kiriEmpat=papGigiKiriEmpatConverter.transferVOToModel(vo.getPapGigiKiriEmpat(),new PapGigiKiriEmpat());
		PapGigiKananSatu kananSatu=papGigiKananSatuConverter.transferVOToModel(vo.getPapGigiKananSatu(),new PapGigiKananSatu());
		PapGigiKananDua kananDua=papGigiKananDuaConverter.transferVOToModel(vo.getPapGigiKananDua(),new PapGigiKananDua());
		PapGigiKananTiga kananTiga=papGigiKananTigaConverter.transferVOToModel(vo.getPapGigiKananTiga(),new PapGigiKananTiga());
		PapGigiKananEmpat kananEmpat=papGigiKananEmpatConverter.transferVOToModel(vo.getPapGigiKananEmpat(),new PapGigiKananEmpat());
		model.setPapGigiKiriSatu(kiriSatu);
		model.setPapGigiKiriDua(kiriDua);
		model.setPapGigiKiriTiga(kiriTiga);
		model.setPapGigiKiriEmpat(kiriEmpat);
		model.setPapGigiKananSatu(kananSatu);
		model.setPapGigiKananDua(kananDua);
		model.setPapGigiKananTiga(kananTiga);
		model.setPapGigiKananEmpat(kananEmpat);
		kiriSatu.setPapGigiMulut(model);
		kiriDua.setPapGigiMulut(model);
		kiriTiga.setPapGigiMulut(model);
		kiriEmpat.setPapGigiMulut(model);
		kananSatu.setPapGigiMulut(model);
		kananDua.setPapGigiMulut(model);
		kananTiga.setPapGigiMulut(model);
		kananEmpat.setPapGigiMulut(model);
		PapGigiMulut resultModel=papGigiMulutDao.save(model);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("papGigiMulut", resultModel);
		}
		return result;
	}

	@Override
	public Map<String,Object> updateGigiMulut(PapGigiMulutVO vo) throws JpaSystemException, ServiceVOException {
		Map<String,Object> result=new HashMap<String,Object>();
		PapGigiMulut model=papGigiMulutConverter.transferVOToModel(vo, new PapGigiMulut());
		PapGigiKiriSatu kiriSatu=papGigiKiriSatuConverter.transferVOToModel(vo.getPapGigiKiriSatu(),new PapGigiKiriSatu());
		PapGigiKiriDua kiriDua=papGigiKiriDuaConverter.transferVOToModel(vo.getPapGigiKiriDua(),new PapGigiKiriDua());
		PapGigiKiriTiga kiriTiga=papGigiKiriTigaConverter.transferVOToModel(vo.getPapGigiKiriTiga(),new PapGigiKiriTiga());
		PapGigiKiriEmpat kiriEmpat=papGigiKiriEmpatConverter.transferVOToModel(vo.getPapGigiKiriEmpat(),new PapGigiKiriEmpat());
		PapGigiKananSatu kananSatu=papGigiKananSatuConverter.transferVOToModel(vo.getPapGigiKananSatu(),new PapGigiKananSatu());
		PapGigiKananDua kananDua=papGigiKananDuaConverter.transferVOToModel(vo.getPapGigiKananDua(),new PapGigiKananDua());
		PapGigiKananTiga kananTiga=papGigiKananTigaConverter.transferVOToModel(vo.getPapGigiKananTiga(),new PapGigiKananTiga());
		PapGigiKananEmpat kananEmpat=papGigiKananEmpatConverter.transferVOToModel(vo.getPapGigiKananEmpat(),new PapGigiKananEmpat());
		model.setPapGigiKiriSatu(kiriSatu);
		model.setPapGigiKiriDua(kiriDua);
		model.setPapGigiKiriTiga(kiriTiga);
		model.setPapGigiKiriEmpat(kiriEmpat);
		model.setPapGigiKananSatu(kananSatu);
		model.setPapGigiKananDua(kananDua);
		model.setPapGigiKananTiga(kananTiga);
		model.setPapGigiKananEmpat(kananEmpat);
		kiriSatu.setPapGigiMulut(model);
		kiriDua.setPapGigiMulut(model);
		kiriTiga.setPapGigiMulut(model);
		kiriEmpat.setPapGigiMulut(model);
		kananSatu.setPapGigiMulut(model);
		kananDua.setPapGigiMulut(model);
		kananTiga.setPapGigiMulut(model);
		kananEmpat.setPapGigiMulut(model);
		PapGigiMulut resultModel=papGigiMulutDao.save(model);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("papGigiMulut", resultModel);
			result.put("dataFound", true);
		}else{
			result.put("dataFound", false);			
		}
		return result;
	}

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

	@Override
	public Map<String,Object> findByNoCmAndTglInput(String noCm, Date tglInput) {
		Map<String,Object> result=new HashMap<String,Object>();
		PapGigiMulut resultModel=papGigiMulutDao.findPapGigiMulutByNoCmAndTglInput(noCm, tglInput);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("papGigiMulut", 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;
	}


}
