package com.jasamedika.medifirst2000.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

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.PapMataDao;
import com.jasamedika.medifirst2000.dao.PasienDao;
import com.jasamedika.medifirst2000.dao.RegistrasiPelayananPasienDao;
import com.jasamedika.medifirst2000.entities.AntrianPasienDiPeriksa;
import com.jasamedika.medifirst2000.entities.DataMata;
import com.jasamedika.medifirst2000.entities.KategoriKlinikMata;
import com.jasamedika.medifirst2000.entities.PapDataMata;
import com.jasamedika.medifirst2000.entities.PapKategoriMata;
import com.jasamedika.medifirst2000.entities.PapMata;
import com.jasamedika.medifirst2000.entities.Pasien;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.RegistrasiPelayananPasien;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.PapMataService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.AntrianPasienDiPeriksaVO;
import com.jasamedika.medifirst2000.vo.DataMataVO;
import com.jasamedika.medifirst2000.vo.KategoriKlinikMataVO;
import com.jasamedika.medifirst2000.vo.PapDataMataVO;
import com.jasamedika.medifirst2000.vo.PapKategoriMataVO;
import com.jasamedika.medifirst2000.vo.PapMataVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;

/**
 * Implement class for PAPMataService
 * 
 * @author Askur
 */
@Service("PAPMataService")
public class PapMataServiceImpl implements PapMataService {
	@Autowired
	private PapMataDao papMataDao;
	@Autowired
	private BaseConverterImpl<PapKategoriMataVO, PapKategoriMata> papKategoriMataConverter;
	@Autowired
	private BaseConverterImpl<PapMataVO, PapMata> papMataConverter;	
	@Autowired
	private BaseConverterImpl<KategoriKlinikMataVO, KategoriKlinikMata> kategoriKlinikMataConverter;
	@Autowired
	private BaseConverterImpl<PapDataMataVO, PapDataMata> papDataMataConverter;
	@Autowired
	private BaseConverterImpl<DataMataVO, DataMata> dataKacamataConverter;
	@Autowired
	private BaseConverterImpl<PegawaiVO, Pegawai> pegawaiConverter;
	@Autowired
	private BaseConverterImpl<AntrianPasienDiPeriksaVO, AntrianPasienDiPeriksa> pasienDaftarConverter;
	@Autowired
	private RegistrasiPelayananPasienDao registrasiPelayananPasienDao;
	@Autowired
	private PasienDao pasienDao;
	
	@Override
	public Map<String,Object> addMata(PapMataVO vo) throws JpaSystemException, ServiceVOException {
		Map<String,Object> result=new HashMap<String,Object>();
		Set<PapKategoriMata> setPapKategoriMata = new HashSet<PapKategoriMata>();
		Set<PapDataMata> setDataKacaMata = new HashSet<PapDataMata>();
		if(CommonUtil.isNotNullOrEmpty(vo))
		{
			PapMata papMata = papMataConverter.transferVOToModel(vo, new PapMata());
			for(PapKategoriMataVO vos : vo.getKategoriKlinikMataSet())
			{
				PapKategoriMata papKategori =  papKategoriMataConverter.transferVOToModel(vos, new PapKategoriMata());
				if(CommonUtil.isNotNullOrEmpty(vos.getKategoriKlinikMata()))
				{
					KategoriKlinikMata kategoriKlinikMata = kategoriKlinikMataConverter.transferVOToModel(vos.getKategoriKlinikMata(), new KategoriKlinikMata());
					papKategori.setKategoriKlinikMata(kategoriKlinikMata);
				}
				papKategori.setPapMata(papMata);
				setPapKategoriMata.add(papKategori);
			}
			for(PapDataMataVO vos : vo.getDataKacamataSet())
			{
				PapDataMata papDataMata = papDataMataConverter.transferVOToModel(vos, new PapDataMata());
				if(CommonUtil.isNotNullOrEmpty(vos.getDataMata()))
				{
					DataMata dataKacamata = dataKacamataConverter.transferVOToModel(vos.getDataMata(), new DataMata());
					papDataMata.setDataMata(dataKacamata);
				}
				papDataMata.setPapMata(papMata);
				setDataKacaMata.add(papDataMata);
			}
			if(CommonUtil.isNotNullOrEmpty(vo.getDokterKonsulen()))
			{
				papMata.setDokterKonsulen(pegawaiConverter.transferVOToModel(vo.getDokterKonsulen(), new Pegawai()));
			}
			if(CommonUtil.isNotNullOrEmpty(vo.getPasienDaftar()))
			{
				papMata.setPasien(pasienDaftarConverter.transferVOToModel(vo.getPasienDaftar(), new AntrianPasienDiPeriksa()));
			}
			papMata.getKategoriKlinikMataSet().clear();
			papMata.getDataKacamataSet().clear();
			papMata.setDataKacamataSet(setDataKacaMata);
			papMata.setKategoriKlinikMataSet(setPapKategoriMata);
			
			if(CommonUtil.isNotNullOrEmpty(papMata))
			{
				PapMata model = papMataDao.save(papMata);
				result.put("norec", model.getNoRec());
				PapDataMata data = model.getDataKacamataSet().iterator().next();				
			}
		}
		return result;
	}

	@Override
	public Map<String,Object> updateMata(PapMataVO vo) throws JpaSystemException, ServiceVOException {
		Map<String,Object> result=new HashMap<String,Object>();
		
		return result;
	}

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

	@Override
	public Map<String,Object> findByNoCmAndTglInput(String noCm, Date tglInput) {
		Map<String,Object> result=new HashMap<String,Object>();
		/*PapMata resultModel=papMataDao.findPapMataByNoCmAndTglInput(noCm, tglInput);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("papMata", 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;
	}

	@Override
	public List findByTglInput(String startDate, String endDate) {
		List<Map<String,Object>> result = new ArrayList<Map<String,Object>>();
		result = papMataDao.findPapMataByTglInput(startDate, endDate);
		for(Map<String,Object> data : result)
		{
			data.put("kategoriMata", papMataDao.findPapKategoriMata(data.get("noRec")+""));
			data.put("dataMata", papMataDao.findPapDataMata(data.get("noRec")+""));
			data.put("streak", papMataDao.findPapMataStreak(data.get("noRec")+""));
		}
		return result;
	}

	@Override
	public List findListData(String jenis) {
		List<Map<String,Object>> result = new ArrayList<Map<String,Object>>();
		result = papMataDao.findListDataMata(jenis);
		return result;
	}

	@Override
	public List findStreak() {
		List<Map<String,Object>> result = new ArrayList<Map<String,Object>>();
		result = papMataDao.findListStreak();
		return result;
	}

	@Override
	public List findKategoriMata() {
		List<Map<String,Object>> result = new ArrayList<Map<String,Object>>();
		result = papMataDao.findListKategoriMata();
		return result;
	}

	@Override
	public Map findPapMataByNoRec(String noRec) {
		Map<String,Object> result = new HashMap<String,Object>();
		result = papMataDao.findPapMataByNoRec(noRec);
		return result;
	}


}
