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 java.util.UUID;

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.AntrianPasienRegistrasiConverter;
import com.jasamedika.medifirst2000.dao.AntrianPasienRegistrasiDao;
import com.jasamedika.medifirst2000.dao.KelompokPasienDao;
import com.jasamedika.medifirst2000.dao.PasienInstruksiDao;
import com.jasamedika.medifirst2000.entities.Agama;
import com.jasamedika.medifirst2000.entities.AntrianPasienDiPeriksa;
import com.jasamedika.medifirst2000.entities.AntrianPasienRegistrasi;
import com.jasamedika.medifirst2000.entities.AsalRujukan;
import com.jasamedika.medifirst2000.entities.DesaKelurahan;
import com.jasamedika.medifirst2000.entities.GolonganDarah;
import com.jasamedika.medifirst2000.entities.JenisKelamin;
import com.jasamedika.medifirst2000.entities.Pasien;
import com.jasamedika.medifirst2000.entities.PasienDaftar;
import com.jasamedika.medifirst2000.entities.PasienInstruksiPerjanjianT;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.Pekerjaan;
import com.jasamedika.medifirst2000.entities.Pendidikan;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.entities.StatusPerkawinan;
import com.jasamedika.medifirst2000.entities.StrukRetur;
import com.jasamedika.medifirst2000.entities.TitlePasien;
import com.jasamedika.medifirst2000.enums.TipePasienEnum;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.AntrianPasienRegistrasiService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.AntrianPasienRegistrasiVO;
import com.jasamedika.medifirst2000.vo.PapInstruksiPerjanjianVO;


/**
 * Implement class for PasienService
 * 
 * @author Adik
 * @param <T>
 */


@Service("antrianPasienRegistrasiService")
public class AntrianPasienRegistrasiServiceImpl extends BaseVoServiceImpl implements AntrianPasienRegistrasiService {

	@Autowired
	private AntrianPasienRegistrasiDao antrianPasienRegistrasiDao;
	
	@Autowired
	private AntrianPasienRegistrasiConverter antrianPasienRegistrasiConverter;
	
	@Autowired
	private KelompokPasienDao kelompokPasienDao;
	
	@Autowired
	private PasienInstruksiDao pasienInstruksiDao;
	
	
	@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 {
		List<AntrianPasienRegistrasiVO> vos=new ArrayList<AntrianPasienRegistrasiVO>();
		List<AntrianPasienRegistrasi> models=(List<AntrianPasienRegistrasi>) antrianPasienRegistrasiDao.findAll();
		antrianPasienRegistrasiConverter.transferListOfModelToListOfVO(models, vos);
		return vos;
	}

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

	@Override
	public Integer getFormatNumberByMonthYear() {
		Date dateNow=new Date();
		
		Integer countByMonthAndYear=antrianPasienRegistrasiDao.getCountByMonthAndYear(dateNow);
		if(countByMonthAndYear==0){
			countByMonthAndYear=countByMonthAndYear+1;
		}else{
			countByMonthAndYear=countByMonthAndYear+1;
		}
        return countByMonthAndYear;
    }

	@Override
	public Map<String,Object> updateAntrianPasienRegistrasiFromPembatalanPasien(StrukRetur strukRetur, String noRec) {
		AntrianPasienRegistrasi antrianPasienRegistrasi=antrianPasienRegistrasiDao.findByNoRec(noRec);
		antrianPasienRegistrasi.setStrukRetur(strukRetur);
		antrianPasienRegistrasiDao.save(antrianPasienRegistrasi);
		Map<String,Object> result=new HashMap<String,Object>();
		result.put("antrianPasienRegistrasi", antrianPasienRegistrasi);
		return result;
	}
	
	@Override
	public Map<String, Object> saveAntrianPasienRegFromPerjanjian(PasienDaftar pasienDaftar,PapInstruksiPerjanjianVO papInstruksiPerjanjianVO,Ruangan ruangan) {
		AntrianPasienRegistrasi model = new AntrianPasienRegistrasi();
		model.setTglReservasi(papInstruksiPerjanjianVO.getTglInput());
		model.setTipePasien(TipePasienEnum.LAMA.getVal());
		model.setType("REGULER");
		if(CommonUtil.isNotNullOrEmpty(pasienDaftar.getPasien())){
			model.setPendidikan(pasienDaftar.getPasien().getPendidikan());
			model.setJenisKelamin(pasienDaftar.getPasien().getJenisKelamin());
			model.setPasien(pasienDaftar.getPasien());
		}
		
		UUID noReservasi = UUID.randomUUID();
		model.setNoReservasi("38"+noReservasi.toString().substring(1, 8));
		AntrianPasienRegistrasi antrianPasienRegistrasi = antrianPasienRegistrasiDao.save(model);
		model.setNoAntrian(antrianPasienRegistrasiDao.getCountByMonthAndYear(model.getTglReservasi()));
		model.setRuangan(ruangan);
		model.setTglReservasi(papInstruksiPerjanjianVO.getTglPerjanjian());
		antrianPasienRegistrasi = antrianPasienRegistrasiDao.save(model);
		Map<String, Object> result = new HashMap<String, Object>();
		if (CommonUtil.isNotNullOrEmpty(antrianPasienRegistrasi)) {
			result.put("noRec", antrianPasienRegistrasi.getNoRec());
			result.put("noReservasi", antrianPasienRegistrasi.getNoReservasi());
			if (CommonUtil.isNotNullOrEmpty(antrianPasienRegistrasi.getPasienId())) {
				result.put("pasien", antrianPasienRegistrasi.getPasien());
			}
		}
		return result;
	}

	@Override
	@Transactional
	public AntrianPasienRegistrasi findOne(String noRec) {
		return antrianPasienRegistrasiDao.findByNoRec(noRec);
	}

	@Override
	public Integer countAntrian(Integer pasienId) {	
		Integer jlhKunjungan = 0;
		PasienInstruksiPerjanjianT pasienInstruksiPerjanjian = pasienInstruksiDao.findPapInstruksiPerjanjianByNoCm(pasienId);
		if(CommonUtil.isNotNullOrEmpty(pasienInstruksiPerjanjian))
		{
			jlhKunjungan =  pasienInstruksiPerjanjian.getJumlahKunjungan();
		}
		return jlhKunjungan;
	}


}
