package com.jasamedika.medifirst2000.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

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.MapPegawaiJabatanToUnitKerjaDao;
import com.jasamedika.medifirst2000.dao.PenilaianKinerjaDao;
import com.jasamedika.medifirst2000.dao.PenilaianKinerjaDetailDao;
import com.jasamedika.medifirst2000.entities.IndikatorKinerja;
import com.jasamedika.medifirst2000.entities.Jabatan;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.PenilaianKinerja;
import com.jasamedika.medifirst2000.entities.PenilaianKinerjaDetail;
import com.jasamedika.medifirst2000.entities.SatuanIndikator;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.PenilaianKinerjaService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.IndikatorKinerjaVO;
import com.jasamedika.medifirst2000.vo.JabatanVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import com.jasamedika.medifirst2000.vo.PenilaianKinerjaDetailVO;
import com.jasamedika.medifirst2000.vo.PenilaianKinerjaVO;
import com.jasamedika.medifirst2000.vo.SatuanIndikatorVO;

/**
 * @author salmanoe
 * @since Jul 6, 2022
 *
 */
@Service("penilaianKinerjaService")
public class PenilaianKinerjaServiceImpl extends BaseVoServiceImpl implements PenilaianKinerjaService {

	@Autowired
	private BaseConverterImpl<PenilaianKinerjaVO, PenilaianKinerja> penilaianConverter;

	@Autowired
	private BaseConverterImpl<PenilaianKinerjaDetailVO, PenilaianKinerjaDetail> penilaianDetailConverter;

	@Autowired
	private BaseConverterImpl<PegawaiVO, Pegawai> pegawaiConverter;

	@Autowired
	private BaseConverterImpl<JabatanVO, Jabatan> jabatanConverter;

	@Autowired
	private BaseConverterImpl<IndikatorKinerjaVO, IndikatorKinerja> indikatorConverter;

	@Autowired
	private BaseConverterImpl<SatuanIndikatorVO, SatuanIndikator> satuanConverter;

	@Autowired
	private PenilaianKinerjaDao penilaianKinerjaDao;

	@Autowired
	private PenilaianKinerjaDetailDao penilaianKinerjaDetailDao;

	@Autowired
	private MapPegawaiJabatanToUnitKerjaDao mappingJabatanDao;

	@Override
	public PenilaianKinerjaVO add(PenilaianKinerjaVO vo) throws JpaSystemException, ServiceVOException {
		Pegawai pegawai = pegawaiConverter.transferVOToModel(vo.getPegawai(), new Pegawai());
		Jabatan jabatan = jabatanConverter.transferVOToModel(vo.getJabatan(), new Jabatan());
		PenilaianKinerja penilaianKinerja = penilaianConverter.transferVOToModel(vo, new PenilaianKinerja());
		penilaianKinerja.setPegawai(pegawai);
		penilaianKinerja.setJabatan(jabatan);
		Optional<Double> nilaiJabatan = mappingJabatanDao.findByPegawaiAndJabatan(vo.getPegawai().getId(),
				vo.getJabatan().getId());
		if (CommonUtil.isNotNullOrEmpty(nilaiJabatan) && nilaiJabatan.isPresent()) {
			penilaianKinerja.setNilaiJabatan(nilaiJabatan.get());
		}
		penilaianKinerja.setStatusEnabled(Boolean.TRUE);
		penilaianKinerja.setKdProfile((short) 0);
		PenilaianKinerja resultModel = penilaianKinerjaDao.save(penilaianKinerja);
		if (CommonUtil.isNotNullOrEmpty(resultModel)) {
			for (PenilaianKinerjaDetailVO detailVO : vo.getDetail()) {
				IndikatorKinerja indikator = indikatorConverter.transferVOToModel(detailVO.getIndikatorKinerja(),
						new IndikatorKinerja());
				SatuanIndikator satuan = satuanConverter.transferVOToModel(detailVO.getSatuan(), new SatuanIndikator());
				PenilaianKinerjaDetail detail = penilaianDetailConverter.transferVOToModel(detailVO,
						new PenilaianKinerjaDetail());
				detail.setIndikatorKinerja(indikator);
				detail.setSatuan(satuan);
				detail.setPenilaianKinerja(resultModel);
				penilaianKinerjaDetailDao.save(detail);
			}
		}
		PenilaianKinerjaVO resultVO = new PenilaianKinerjaVO();
		resultVO = penilaianConverter.transferModelToVO(resultModel, resultVO);
		return resultVO;
	}

	@Override
	public PenilaianKinerjaVO update(PenilaianKinerjaVO vo) throws JpaSystemException, ServiceVOException {
		PenilaianKinerja penilaianKinerjaLama = penilaianKinerjaDao.findOne(vo.getNoRec());
		Pegawai pegawai = pegawaiConverter.transferVOToModel(vo.getPegawai(), new Pegawai());
		Jabatan jabatan = jabatanConverter.transferVOToModel(vo.getJabatan(), new Jabatan());
		PenilaianKinerja penilaianKinerjaBaru = penilaianConverter.transferVOToModel(vo, penilaianKinerjaLama);
		penilaianKinerjaBaru.setPegawai(pegawai);
		penilaianKinerjaBaru.setJabatan(jabatan);
		PenilaianKinerja resultModel = penilaianKinerjaDao.save(penilaianKinerjaBaru);
		PenilaianKinerjaVO resultVO = new PenilaianKinerjaVO();
		resultVO = penilaianConverter.transferModelToVO(resultModel, resultVO);
		return resultVO;
	}

	@Override
	public Boolean delete(String key) throws JpaSystemException {
		penilaianKinerjaDao.delete(key);
		return true;
	}

	@Override
	public PenilaianKinerjaVO findById(String key) throws JpaSystemException {
		PenilaianKinerjaVO result = new PenilaianKinerjaVO();
		PenilaianKinerja penilaianKinerja = penilaianKinerjaDao.findOne(key);
		if (CommonUtil.isNotNullOrEmpty(penilaianKinerja)) {
			result = penilaianConverter.transferModelToVO(penilaianKinerja, result);
		}
		return result;
	}

	@Override
	public List<PenilaianKinerjaVO> findAll() throws JpaSystemException {
		List<PenilaianKinerjaVO> result = new ArrayList<>();
		Iterable<PenilaianKinerja> models = penilaianKinerjaDao.findAll();
		if (CommonUtil.isNotNullOrEmpty(models)) {
			for (PenilaianKinerja model : models) {
				PenilaianKinerjaVO vo = new PenilaianKinerjaVO();
				vo = penilaianConverter.transferModelToVO(model, vo);
				result.add(vo);
			}
		}
		return result;
	}

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

}
