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.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jasamedika.medifirst2000.converter.JawabanKonsultasiConverter;
import com.jasamedika.medifirst2000.converter.LembarKonsultasiConverter;
import com.jasamedika.medifirst2000.dao.AntrianPasienDiPeriksaDao;
import com.jasamedika.medifirst2000.dao.JawabanKonsultasiDao;
import com.jasamedika.medifirst2000.dao.LembarKonsultasiDao;
import com.jasamedika.medifirst2000.dao.PasienDaftarDao;
import com.jasamedika.medifirst2000.entities.DiagnosaTindakanPasien;
import com.jasamedika.medifirst2000.entities.JawabanKonsultasi;
import com.jasamedika.medifirst2000.entities.LembarKonsultasi;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.LembarKonsultasiService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.LembarKonsultasiVO;
import com.jasamedika.medifirst2000.vo.RuanganVO;


@Service("lembarKonsultasiService")
public class LembarKonsultasiServiceImpl implements LembarKonsultasiService {
	@Autowired
	private AntrianPasienDiPeriksaDao antrianPasienDiPeriksaDao;
	@Autowired
	private LembarKonsultasiDao lembarKonsultasiDao;
	@Autowired
	private JawabanKonsultasiDao jawabanKonsultasiDao;
	@Autowired
	private PasienDaftarDao pasienDaftarDao;
	@Autowired
	private LembarKonsultasiConverter lembarKonsultasiConverter;
	@Autowired
	private JawabanKonsultasiConverter jawabanKonsultasiConverter;

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

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

	@Override
	//@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public Map<String, Object> findByNocm(String noCm) {
		Map<String,Object> result=new HashMap<String,Object>();
		LembarKonsultasi resultModel  = lembarKonsultasiDao.findLembarKonsultasiByNocm(noCm);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("noRec", resultModel.getNoRec());
			result.put("hasilPemeriksaan", resultModel.getHasilPemeriksaan());
			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
	@Transactional(readOnly=false)
	public Map<String, Object> saveLembarKonsultasi(LembarKonsultasiVO vo) {
		Map<String,Object> result=new HashMap<String,Object>();
		LembarKonsultasi lembarKonsultasi = lembarKonsultasiConverter.transferVOToModel(vo, new LembarKonsultasi());
		JawabanKonsultasi jawabanKonsultasi = jawabanKonsultasiConverter.transferVOToModel(vo.getJawabanKonsultasi(), new JawabanKonsultasi());
		lembarKonsultasi.setPasienDaftar(pasienDaftarDao.finByNoIdAndTglRegistrasi(Short.parseShort("0"), vo.getPasien().getId(), vo.getTanggal()));
		//jawabanKonsultasi.setLembarKonsultasi(lembarKonsultasi);
		lembarKonsultasi.setJawabanKonsultasi(jawabanKonsultasi);
		LembarKonsultasi resultModel  = lembarKonsultasiDao.save(lembarKonsultasi);
		LembarKonsultasi resultLK=new LembarKonsultasi();
		resultLK.setNoRec(resultModel.getNoRec());
		resultLK.setPasien(resultModel.getPasien());
		resultLK.setSaran(resultModel.getSaran());
		resultLK.setJawabanKonsultasi(resultModel.getJawabanKonsultasi());
		resultLK.setHasilPemeriksaan(resultModel.getHasilPemeriksaan());
		resultLK.setSaran(resultModel.getSaran());
		resultLK.setTanggal(resultModel.getTanggal());
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("lembarKonsultasi", resultLK);
		}
		return result;
	}

	@Override
	public Map<String, Object> updateLembarKonsultasi(LembarKonsultasiVO vo) {
		Map<String,Object> result=new HashMap<String,Object>();
		LembarKonsultasi  model = lembarKonsultasiDao.findOne(vo.getNoRec());
		model  = lembarKonsultasiConverter.transferVOToModel(vo, model);
		LembarKonsultasi resultModel =	lembarKonsultasiDao.save(model);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("id", resultModel.getNoRec());
			result.put("LembarKonsultasi", resultModel.getPasien());
		}
		return result;
	}

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