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.PelayananFarmakologiDao;
import com.jasamedika.medifirst2000.dao.SkoringTindakanFarmakologiDao;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.PelayananFarmakologi;
import com.jasamedika.medifirst2000.entities.ProdukFarmakologi;
import com.jasamedika.medifirst2000.entities.SkoringTindakanFarmakologi;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.PelayananFarmakologiService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import com.jasamedika.medifirst2000.vo.PelayananFarmakologiVO;
import com.jasamedika.medifirst2000.vo.ProdukFarmakologiVO;

/**
 * @author salmanoe
 * @since Jun 20, 2022
 * 
 */
@Service("pelayananFarmakologiService")
public class PelayananFarmakologiServiceImpl extends BaseVoServiceImpl implements PelayananFarmakologiService {

	@Autowired
	private BaseConverterImpl<PelayananFarmakologiVO, PelayananFarmakologi> pelayananFarmakologiConverter;

	@Autowired
	private BaseConverterImpl<ProdukFarmakologiVO, ProdukFarmakologi> produkFarmakologiConverter;

	@Autowired
	private BaseConverterImpl<PegawaiVO, Pegawai> pegawaiConverter;

	@Autowired
	private PelayananFarmakologiDao pelayananFarmakologiDao;

	@Autowired
	private SkoringTindakanFarmakologiDao skoringTindakanFarmakologiDao;

	@Override
	public PelayananFarmakologiVO add(PelayananFarmakologiVO 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()))) {
			PelayananFarmakologi pelayananFarmakologi = pelayananFarmakologiConverter.transferVOToModel(vo,
					new PelayananFarmakologi());

			ProdukFarmakologi produk = produkFarmakologiConverter.transferVOToModel(vo.getProduk(),
					new ProdukFarmakologi());
			pelayananFarmakologi.setProduk(produk);

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

			SkoringTindakanFarmakologi skoringFarmakologi = skoringTindakanFarmakologiDao
					.findLast(vo.getProduk().getId(), df.format(vo.getTglPelayanan()));
			if (CommonUtil.isNotNullOrEmpty(skoringFarmakologi)) {
				pelayananFarmakologi.setSkor(skoringFarmakologi.getSkor());
			} else {
				pelayananFarmakologi.setSkor(1.00);
			}

			PelayananFarmakologi resultModel = pelayananFarmakologiDao.save(pelayananFarmakologi);

			PelayananFarmakologiVO resultVO = new PelayananFarmakologiVO();
			resultVO = pelayananFarmakologiConverter.transferModelToVO(resultModel, resultVO);

			return resultVO;
		} else {
			return null;
		}
	}

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

		PelayananFarmakologi pelayananFarmakologiLama = pelayananFarmakologiDao.findOne(vo.getNoRec());

		PelayananFarmakologi pelayananFarmakologiBaru = pelayananFarmakologiConverter.transferVOToModel(vo,
				pelayananFarmakologiLama);

		ProdukFarmakologi produk = produkFarmakologiConverter.transferVOToModel(vo.getProduk(),
				new ProdukFarmakologi());
		pelayananFarmakologiBaru.setProduk(produk);

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

		SkoringTindakanFarmakologi skoringFarmakologi = skoringTindakanFarmakologiDao.findLast(vo.getProduk().getId(),
				df.format(vo.getTglPelayanan()));
		if (CommonUtil.isNotNullOrEmpty(skoringFarmakologi)) {
			pelayananFarmakologiBaru.setSkor(skoringFarmakologi.getSkor());
		} else {
			pelayananFarmakologiBaru.setSkor(1.00);
		}

		PelayananFarmakologi resultModel = pelayananFarmakologiDao.save(pelayananFarmakologiBaru);

		PelayananFarmakologiVO resultVO = new PelayananFarmakologiVO();
		resultVO = pelayananFarmakologiConverter.transferModelToVO(resultModel, resultVO);

		return resultVO;
	}

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

		return true;
	}

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

		PelayananFarmakologi pelayananFarmakologi = pelayananFarmakologiDao.findOne(key);
		if (CommonUtil.isNotNullOrEmpty(pelayananFarmakologi)) {
			result = pelayananFarmakologiConverter.transferModelToVO(pelayananFarmakologi, result);
		}

		return result;
	}

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

		Iterable<PelayananFarmakologi> models = pelayananFarmakologiDao.findAll();
		if (CommonUtil.isNotNullOrEmpty(models)) {
			for (PelayananFarmakologi model : models) {
				PelayananFarmakologiVO vo = new PelayananFarmakologiVO();
				vo = pelayananFarmakologiConverter.transferModelToVO(model, vo);
				result.add(vo);
			}
		}

		return result;
	}

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

		return null;
	}

}
