package com.jasamedika.medifirst2000.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
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.constants.Master;
import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.LogbookKinerjaDao;
import com.jasamedika.medifirst2000.dao.PelayananPasienNakesDao;
import com.jasamedika.medifirst2000.dao.SkoringTindakanNakesDao;
import com.jasamedika.medifirst2000.entities.AntrianPasienDiPeriksa;
import com.jasamedika.medifirst2000.entities.LogbookKinerja;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.PelayananPasienNakes;
import com.jasamedika.medifirst2000.entities.PelayananPasienPerawat;
import com.jasamedika.medifirst2000.entities.ProdukNakes;
import com.jasamedika.medifirst2000.entities.SkoringTindakanNakes;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.PelayananPasienNakesService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.AntrianPasienDiPeriksaVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import com.jasamedika.medifirst2000.vo.PelayananPasienNakesVO;
import com.jasamedika.medifirst2000.vo.ProdukNakesVO;

@Service("pelayananPasienNakesService")
public class PelayananPasienNakesServiceImpl extends BaseVoServiceImpl implements PelayananPasienNakesService {

	@Autowired
	private BaseConverterImpl<PelayananPasienNakesVO, PelayananPasienNakes> pelayananNakesConverter;

	@Autowired
	private BaseConverterImpl<AntrianPasienDiPeriksaVO, AntrianPasienDiPeriksa> antrianPasienConverter;

	@Autowired
	private BaseConverterImpl<ProdukNakesVO, ProdukNakes> produkNakesConverter;

	@Autowired
	private BaseConverterImpl<PegawaiVO, Pegawai> pegawaiConverter;

	@Autowired
	private PelayananPasienNakesDao pelayananNakesDao;

	@Autowired
	private SkoringTindakanNakesDao skoringTindakanNakesDao;

	@Autowired
	private LogbookKinerjaDao logbookKinerjaDao;

	@Override
	public PelayananPasienNakesVO add(PelayananPasienNakesVO vo) throws JpaSystemException, ServiceVOException {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		DateFormat mf = new SimpleDateFormat("yyyy-MM");

		if (mf.format(vo.getTglPelayanan()).equals(mf.format(new Date()))) {
			PelayananPasienNakes pelayananNakes = pelayananNakesConverter.transferVOToModel(vo,
					new PelayananPasienNakes());

			AntrianPasienDiPeriksa antrianPasien = antrianPasienConverter
					.transferVOToModel(vo.getAntrianPasienDiperiksa(), new AntrianPasienDiPeriksa());
			pelayananNakes.setAntrianPasienDiperiksa(antrianPasien);

			ProdukNakes produk = produkNakesConverter.transferVOToModel(vo.getProduk(), new ProdukNakes());
			pelayananNakes.setProduk(produk);

			Pegawai pegawai = pegawaiConverter.transferVOToModel(vo.getPegawai(), new Pegawai());
			pelayananNakes.setPegawai(pegawai);

			SkoringTindakanNakes skoringNakes = skoringTindakanNakesDao.findLast(vo.getProduk().getId(),
					df.format(vo.getTglPelayanan()));
			if (CommonUtil.isNotNullOrEmpty(skoringNakes)) {
				pelayananNakes.setSkor(skoringNakes.getSkor());
			} else {
				pelayananNakes.setSkor(1.00);
			}

			PelayananPasienNakes resultModel = pelayananNakesDao.save(pelayananNakes);

			PelayananPasienNakesVO resultVO = new PelayananPasienNakesVO();
			resultVO = pelayananNakesConverter.transferModelToVO(resultModel, resultVO);

			return resultVO;
		} else {
			return null;
		}
	}

	@Override
	public PelayananPasienNakesVO update(PelayananPasienNakesVO vo) throws JpaSystemException, ServiceVOException {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

		PelayananPasienNakes pelayananNakesLama = pelayananNakesDao.findOne(vo.getNoRec());

		PelayananPasienNakes pelayananNakesBaru = pelayananNakesConverter.transferVOToModel(vo, pelayananNakesLama);

		AntrianPasienDiPeriksa antrianPasien = antrianPasienConverter.transferVOToModel(vo.getAntrianPasienDiperiksa(),
				new AntrianPasienDiPeriksa());
		pelayananNakesBaru.setAntrianPasienDiperiksa(antrianPasien);

		ProdukNakes produk = produkNakesConverter.transferVOToModel(vo.getProduk(), new ProdukNakes());
		pelayananNakesBaru.setProduk(produk);

		Pegawai pegawai = pegawaiConverter.transferVOToModel(vo.getPegawai(), new Pegawai());
		pelayananNakesBaru.setPegawai(pegawai);

		SkoringTindakanNakes skoringNakes = skoringTindakanNakesDao.findLast(vo.getProduk().getId(),
				df.format(vo.getTglPelayanan()));
		if (CommonUtil.isNotNullOrEmpty(skoringNakes)) {
			pelayananNakesBaru.setSkor(skoringNakes.getSkor());
		} else {
			pelayananNakesBaru.setSkor(1.00);
		}

		PelayananPasienNakes resultModel = pelayananNakesDao.save(pelayananNakesBaru);

		PelayananPasienNakesVO resultVO = new PelayananPasienNakesVO();
		resultVO = pelayananNakesConverter.transferModelToVO(resultModel, resultVO);

		return resultVO;
	}

	@Override
	public Boolean delete(String key) throws JpaSystemException {
		{
			/**
			 * Reset verifikasi logbook skor kinerja
			 */
			DateFormat df = new SimpleDateFormat("yyyy-MM");
			PelayananPasienNakes pelayananNakes = pelayananNakesDao.findOne(key);
			List<LogbookKinerja> listLogbook = logbookKinerjaDao.findLogbookKinerja(pelayananNakes.getPegawai().getId(),
					df.format(pelayananNakes.getTglPelayanan()), Master.IndikatorKinerja.PELAYANAN_NAKES);
			for (LogbookKinerja logbook : listLogbook) {
				logbook.setCapaian(0.0);
			}
			logbookKinerjaDao.save(listLogbook);
		}

		pelayananNakesDao.delete(key);

		return true;
	}

	@Override
	public PelayananPasienNakesVO findById(String key) throws JpaSystemException {
		PelayananPasienNakesVO result = new PelayananPasienNakesVO();

		PelayananPasienNakes pelayananNakes = pelayananNakesDao.findOne(key);
		if (CommonUtil.isNotNullOrEmpty(pelayananNakes)) {
			result = pelayananNakesConverter.transferModelToVO(pelayananNakes, result);
		}

		return result;
	}

	@Override
	public List<PelayananPasienNakesVO> findAll() throws JpaSystemException {
		List<PelayananPasienNakesVO> result = new ArrayList<PelayananPasienNakesVO>();

		Iterable<PelayananPasienNakes> models = pelayananNakesDao.findAll();
		if (CommonUtil.isNotNullOrEmpty(models)) {
			for (PelayananPasienNakes model : models) {
				PelayananPasienNakesVO vo = new PelayananPasienNakesVO();
				vo = pelayananNakesConverter.transferModelToVO(model, vo);
				result.add(vo);
			}
		}

		return result;
	}

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

		return null;
	}

	@Override
	public List<Map<String, Object>> findByAntrianPasienDiperiksa(String noRec, Integer idPegawai)
			throws JpaSystemException {
		List<Map<String, Object>> result = pelayananNakesDao.findByAntrianPasienDiperiksa(noRec, idPegawai);

		return result;
	}

}
