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.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.PelayananNakesDao;
import com.jasamedika.medifirst2000.dao.SkoringTindakanNakesDao;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.PelayananNakes;
import com.jasamedika.medifirst2000.entities.ProdukNakes;
import com.jasamedika.medifirst2000.entities.SkoringTindakanNakes;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.PelayananNakesService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import com.jasamedika.medifirst2000.vo.PelayananNakesVO;
import com.jasamedika.medifirst2000.vo.ProdukNakesVO;

/**
 * @author salmanoe
 * @since Oct 18, 2021
 */
@Service("pelayananNakesService")
public class PelayananNakesServiceImpl extends BaseVoServiceImpl implements PelayananNakesService {

	@Autowired
	private BaseConverterImpl<PelayananNakesVO, PelayananNakes> pelayananNakesConverter;

	@Autowired
	private BaseConverterImpl<ProdukNakesVO, ProdukNakes> produkNakesConverter;

	@Autowired
	private BaseConverterImpl<PegawaiVO, Pegawai> pegawaiConverter;

	@Autowired
	private PelayananNakesDao pelayananNakesDao;

	@Autowired
	private SkoringTindakanNakesDao skoringTindakanNakesDao;

	@Override
	public PelayananNakesVO add(PelayananNakesVO 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()))) {
			PelayananNakes pelayananNakes = pelayananNakesConverter.transferVOToModel(vo, new PelayananNakes());

			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);
			}

			PelayananNakes resultModel = pelayananNakesDao.save(pelayananNakes);

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

			return resultVO;
		} else {
			return null;
		}
	}

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

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

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

		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);
		}

		PelayananNakes resultModel = pelayananNakesDao.save(pelayananNakesBaru);

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

		return resultVO;
	}

	@Override
	public Boolean delete(String key) throws JpaSystemException {
		pelayananNakesDao.delete(key);

		return true;
	}

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

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

		return result;
	}

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

		Iterable<PelayananNakes> models = pelayananNakesDao.findAll();
		if (CommonUtil.isNotNullOrEmpty(models)) {
			for (PelayananNakes model : models) {
				PelayananNakesVO vo = new PelayananNakesVO();
				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;
	}

}
