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 com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.converter.PapKeluhanTambahanConverter;
import com.jasamedika.medifirst2000.converter.PasienDaftarConverter;
import com.jasamedika.medifirst2000.dao.AntrianPasienDiPeriksaDao;
import com.jasamedika.medifirst2000.dao.PapKeluhanTambahanDao;
import com.jasamedika.medifirst2000.dao.PasienDaftarDao;
import com.jasamedika.medifirst2000.entities.AntrianPasienDiPeriksa;
import com.jasamedika.medifirst2000.entities.PapKeluhanTambahan;
import com.jasamedika.medifirst2000.entities.PapKeluhanUtama;
import com.jasamedika.medifirst2000.entities.PasienDaftar;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.PapKeluhanTambahanService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.PapKeluhanTambahanVO;
import com.jasamedika.medifirst2000.vo.PasienDaftarVO;

/**
 * Implement class for PAPKeluhanTambahanService
 * 
 * @author Askur
 */
@Service("PAPKeluhanTambahanService")
public class PapKeluhanTambahanServiceImpl implements PapKeluhanTambahanService {
	@Autowired
	private PapKeluhanTambahanDao papKeluhanTambahanDao;
	@Autowired
	private PapKeluhanTambahanConverter papKeluhanTambahanConverter;
	@Autowired
	private PasienDaftarDao pasienDaftarDao;
	@Autowired
	private AntrianPasienDiPeriksaDao antrianPasienDiPeriksaDao;
	
	@Autowired
	private BaseConverterImpl<PapKeluhanTambahanVO, PapKeluhanTambahan> pasienDaftarConverter;
	@Override
	public Map<String,Object> addKeluhanTambahan(PapKeluhanTambahanVO vo) throws JpaSystemException, ServiceVOException {
		Map<String,Object> result=new HashMap<String,Object>();
		PapKeluhanTambahan model=papKeluhanTambahanConverter.transferVOToModel(vo, new PapKeluhanTambahan());
		AntrianPasienDiPeriksa antrian=antrianPasienDiPeriksaDao.findOne(vo.getPasien().getNoRec());
		PasienDaftar pasienDaftar=antrian.getPasienDaftar();
		model.setPasienDaftar(pasienDaftar);
		PapKeluhanTambahan resultModel=papKeluhanTambahanDao.save(model);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			try {
				result.put("keluhanTambahan", resultModel.ToMap());
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	@Override
	public Map<String,Object> updateKeluhanTambahan(PapKeluhanTambahanVO vo) throws JpaSystemException, ServiceVOException {
		Map<String,Object> result=new HashMap<String,Object>();
		PapKeluhanTambahan model=papKeluhanTambahanConverter.transferVOToModel(vo, new PapKeluhanTambahan());
		PapKeluhanTambahan resultModel=papKeluhanTambahanDao.save(model);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("keluhanTambahan", resultModel);
			result.put("dataFound", true);
		}else{
			result.put("dataFound", false);
		}
		return result;
	}

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

	@Override
	public Map<String,Object> findByNoCmAndTglInput(String noCm, Date tglInput) {
		Map<String,Object> result=new HashMap<String,Object>();
		
		List<PapKeluhanTambahan> resultModel=papKeluhanTambahanDao.findPapKeluhanTambahanByNoCmAndTglInput(noCm, tglInput);
		List<Map<String, Object>> keluhanTembuhan;
		try {
			keluhanTembuhan = pasienDaftarConverter.ToMaps(resultModel);
			if(CommonUtil.isNotNullOrEmpty(resultModel)){			
				result.put("keluhanTambahan", keluhanTembuhan);
				result.put("dataFound", true);			
			}else{
				result.put("dataFound", false);
			}
		} catch (IllegalArgumentException | IllegalAccessException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	@Override
	public Map<String,Object> findByNoCmAndTglkeluhan( Date tglMeraskanKeluhan,String noCm) {
		Map<String,Object> result=new HashMap<String,Object>();
		
		List<PapKeluhanTambahan> resultModel=papKeluhanTambahanDao.findPapKeluhanTambahanByNoCmAndTglKeluhan(noCm, tglMeraskanKeluhan);
		List<Map<String, Object>> keluhanTembuhan;
		try {
			keluhanTembuhan = pasienDaftarConverter.ToMaps(resultModel);
			if(CommonUtil.isNotNullOrEmpty(resultModel)){			
				result.put("keluhanTambahan", keluhanTembuhan);
				result.put("dataFound", true);			
			}else{
				result.put("dataFound", false);
			}
		} catch (IllegalArgumentException | IllegalAccessException e) {
			e.printStackTrace();
		}
		
		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 List findAll() throws JpaSystemException {
		return null;
	}


}
