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 org.springframework.transaction.annotation.Transactional;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.converter.SoapConverter;
import com.jasamedika.medifirst2000.dao.PasienDaftarDao;
import com.jasamedika.medifirst2000.dao.SoapDao;
import com.jasamedika.medifirst2000.entities.AntrianPasienDiPeriksa;
import com.jasamedika.medifirst2000.entities.DetailDiagnosaPasien;
import com.jasamedika.medifirst2000.entities.Pasien;
import com.jasamedika.medifirst2000.entities.PasienDaftar;
import com.jasamedika.medifirst2000.entities.Soap;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.DiagnosaPasienService;
import com.jasamedika.medifirst2000.service.DiagnosaTindakanPasienService;
import com.jasamedika.medifirst2000.service.SoapService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.JsonUtil;
import com.jasamedika.medifirst2000.vo.DetailDiagnosaPasienVO;
import com.jasamedika.medifirst2000.vo.DetailDiagnosaTindakanPasienVO;
import com.jasamedika.medifirst2000.vo.PasienDaftarVO;
import com.jasamedika.medifirst2000.vo.PasienVO;
import com.jasamedika.medifirst2000.vo.SoapVO;

/**
 * Implement class for PAPRehabilitasiService
 * 
 * @author Askur
 */
@Service("soapService")
public class SoapServiceImpl implements SoapService {
	@Autowired
	private SoapDao soapDao;
	@Autowired
	private PasienDaftarDao pasienDaftarDao;
	@Autowired
	private SoapConverter soapConverter;
	@Autowired
	private DiagnosaPasienService diagnosaPasienService;
	@Autowired
	private DiagnosaTindakanPasienService diagnosaTindakanPasienService;
	@Autowired
	private BaseConverterImpl<PasienDaftarVO, PasienDaftar> pasienDaftarConverter;
	@Autowired
	private BaseConverterImpl<PasienVO, Pasien> pasienConverter;
	
	
	@Override
	@Transactional(readOnly = false)
	public Map<String,Object> addSoap(SoapVO vo) {
		Map<String,Object> result=new HashMap<String,Object>();
		StringBuffer buffer=new StringBuffer();
		buffer.append("");
		Soap model=soapConverter.transferVOToModel(vo, new Soap());
		AntrianPasienDiPeriksa pd=new AntrianPasienDiPeriksa();
		pd.setNoRec(vo.getPasienDaftar().getNoRec());
		model.setPasienDaftar(pd);
		if(CommonUtil.isNotNullOrEmpty(vo.getDiagnosaTindakanPasien())){
			Map<String, Object> tindakan=diagnosaTindakanPasienService.saveDiagnosaTindakanPasien(vo.getDiagnosaTindakanPasien());
			if(tindakan!=null && !vo.getDiagnosaPasien().getDiagnosis().isEmpty()){
				buffer.append("Tindakan : ");
				for(DetailDiagnosaTindakanPasienVO detail:vo.getDiagnosaTindakanPasien().getDiagnosisTindakan()){
				buffer.append(detail.getDiagnosaTindakan().getNamaDiagnosaTindakan()+",");
				}
				buffer.substring(0, buffer.length()-1);
				buffer.append("; ");
			}
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getDiagnosaPasien())){
			Map<String, Object> diagnosa=diagnosaPasienService.saveDiagnosaPasien(vo.getDiagnosaPasien());
			if(diagnosa!=null && !vo.getDiagnosaPasien().getDiagnosis().isEmpty()){
				buffer.append("Diagnosa : ");
				for(DetailDiagnosaPasienVO detail:vo.getDiagnosaPasien().getDiagnosis()){
				buffer.append(detail.getDiagnosa().getNamaDiagnosa()+",");
				}
				buffer.substring(0, buffer.length()-1);
				buffer.append("; ");
			}
		}
		if(model.getA()!=null)
		buffer.append(model.getA());
		model.setA(buffer.toString());
		Soap resultModel=soapDao.save(model);
		Soap resultSoap=new Soap();
		resultSoap.setTglInput(resultModel.getTglInput());
		resultSoap.setNoRec(resultModel.getNoRec());
		resultSoap.setS(resultModel.getS());
		resultSoap.setO(resultModel.getO());
		resultSoap.setA(resultModel.getA());
		resultSoap.setP(resultModel.getP());
		
		//resultSoap.setPasienDaftar(resultModel.getPasienDaftar());
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("soap", resultSoap);
		}
		return result;
	}

	@Override
	public Map<String,Object> updateSoap(SoapVO vo) {
		return null;
	}

	@Override
	public Map<String,Object> findByNoCm(String noCm) {
		Map<String,Object> result=new HashMap<String,Object>();
		List<Soap> list=soapDao.finByNoCm(noCm);
		result.put("soapSet", list);
		return result;
	}

	@Override
	public Map<String,Object> findByNoCmAndTglInput(String noCm, Date tglInput) {
		Map<String,Object> result=new HashMap<String,Object>();
		List<Soap> list=soapDao.findByNoCmAndTglInput(noCm,tglInput);
		result.put("soapSet", list);
		return result;
	}
	
	@Override
	public Map<String,Object> findByNoRecAntrianAndTglInput(String noRec, Date tglInput) {
		Map<String,Object> result=new HashMap<String,Object>();
		List<Soap> list=soapDao.findByNoRecAntrianAndTglInput(noRec,tglInput);
		if(!list.isEmpty()){
			try {
				result.put("soapSet", JsonUtil.ToMaps(list));
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
			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 List findAll() throws JpaSystemException {
		return null;
	}

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

	@Override
	public Map findByNoRecAntrian(String noRec) {
		Map<String,Object> result=new HashMap<String,Object>();
		List<Soap> list=soapDao.findByNoRecAntrian(noRec);
		if(!list.isEmpty()){
			try {
				result.put("soapSet", JsonUtil.ToMaps(list));
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
			result.put("dataFound",true);
		}else{
			result.put("dataFound",false);			
		}
		return result;	
	}

}
