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 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.LoggingUserDao;
import com.jasamedika.medifirst2000.dao.RemunerasiOperasionalDao;
import com.jasamedika.medifirst2000.entities.LoggingUser;
import com.jasamedika.medifirst2000.entities.LoginUser;
import com.jasamedika.medifirst2000.entities.RemunerasiOperasional;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.RemunerasiOperasionalService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.RemunerasiOperasionalVO;

@Service("remunerasiOperasionalService")
public class RemunerasiOperasionalServiceImpl extends BaseVoServiceImpl implements RemunerasiOperasionalService {

	@Autowired
	private BaseConverterImpl<RemunerasiOperasionalVO, RemunerasiOperasional> remunerasiOperasionalConverter;

	@Autowired
	private RemunerasiOperasionalDao remunerasiOperasionalDao;

	@Autowired
	private LoggingUserDao loggingUserDao;

	public void addLoggingUser(Integer idLoginUser, String idReferensi, String keterangan) {
		LoggingUser loggingUser = new LoggingUser();
		LoginUser loginUser = new LoginUser();

		loggingUser.setKdProfile((short) 0);
		loggingUser.setStatusEnabled(true);
		loggingUser.setTanggal(new Date());
		loginUser.setId(idLoginUser);
		loggingUser.setLoginUser(loginUser);
		loggingUser.setJenisLog("Anggaran Remunerasi Fee for Service");
		loggingUser.setReferensiId(idReferensi);
		loggingUser.setReferensi("sdm_remunerasioperasional_t");
		loggingUser.setKeterangan(keterangan);

		loggingUserDao.save(loggingUser);
	}

	public void addAllLoggingUser(List<Map<String, Object>> listLog) {
		List<LoggingUser> listLoggingUser = new ArrayList<>();
		Date now = new Date();

		for (Map<String, Object> mapLog : listLog) {
			LoggingUser loggingUser = new LoggingUser();
			LoginUser loginUser = new LoginUser();

			loggingUser.setKdProfile((short) 0);
			loggingUser.setStatusEnabled(true);
			loggingUser.setTanggal(now);
			loginUser.setId(Integer.valueOf(mapLog.get("idLoginUser").toString()));
			loggingUser.setLoginUser(loginUser);
			loggingUser.setJenisLog("Anggaran Remunerasi Fee for Service");
			loggingUser.setReferensiId(mapLog.get("idReferensi").toString());
			loggingUser.setReferensi("sdm_remunerasioperasional_t");
			loggingUser.setKeterangan(mapLog.get("keterangan").toString());
			listLoggingUser.add(loggingUser);
		}

		loggingUserDao.save(listLoggingUser);
	}

	@Override
	public RemunerasiOperasionalVO add(RemunerasiOperasionalVO vo) throws JpaSystemException, ServiceVOException {
		RemunerasiOperasional remunerasiOperasional = remunerasiOperasionalConverter.transferVOToModel(vo,
				new RemunerasiOperasional());

		remunerasiOperasional.setTanggalPembaharuanData(new Date());
		RemunerasiOperasional resultModel = remunerasiOperasionalDao.save(remunerasiOperasional);

		addLoggingUser(vo.getLoginUserId(), resultModel.getNoRec(), "Buat data");

		RemunerasiOperasionalVO resultVO = new RemunerasiOperasionalVO();
		resultVO = remunerasiOperasionalConverter.transferModelToVO(resultModel, resultVO);

		return resultVO;
	}

	@Override
	public RemunerasiOperasionalVO update(RemunerasiOperasionalVO vo) throws JpaSystemException, ServiceVOException {
		RemunerasiOperasional remunerasiOperasionalLama = remunerasiOperasionalDao.findOne(vo.getNoRec());

		RemunerasiOperasional remunerasiOperasionalBaru = remunerasiOperasionalConverter.transferVOToModel(vo,
				remunerasiOperasionalLama);

		remunerasiOperasionalBaru.setTanggalPembaharuanData(new Date());
		RemunerasiOperasional resultModel = remunerasiOperasionalDao.save(remunerasiOperasionalBaru);

		addLoggingUser(vo.getLoginUserId(), resultModel.getNoRec(), "Edit data");

		RemunerasiOperasionalVO resultVO = new RemunerasiOperasionalVO();
		resultVO = remunerasiOperasionalConverter.transferModelToVO(resultModel, resultVO);

		return resultVO;
	}

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

		return true;
	}

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

		RemunerasiOperasional remunerasiOperasional = remunerasiOperasionalDao.findOne(key);
		if (CommonUtil.isNotNullOrEmpty(remunerasiOperasional)) {
			result = remunerasiOperasionalConverter.transferModelToVO(remunerasiOperasional, result);
		}

		return result;
	}

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

		Iterable<RemunerasiOperasional> models = remunerasiOperasionalDao.findAll();
		if (CommonUtil.isNotNullOrEmpty(models)) {
			for (RemunerasiOperasional model : models) {
				RemunerasiOperasionalVO vo = new RemunerasiOperasionalVO();
				vo = remunerasiOperasionalConverter.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 Map<String, Object> findAnggaranTahunIni() throws JpaSystemException {
		Map<String, Object> result = new HashMap<>();

		List<Map<String, Object>> data = remunerasiOperasionalDao.findTahunIni();
		if (CommonUtil.isNotNullOrEmpty(data)) {
			result = data.get(0);
		}

		return result;
	}

}
