package com.jasamedika.medifirst2000.service.impl;

import java.util.ArrayList;
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.TargetLayananConverter;
import com.jasamedika.medifirst2000.dao.TargetLayananDao;
import com.jasamedika.medifirst2000.entities.TargetLayanan;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.TargetLayananService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.TargetLayananVO;

@Service("targetLayananService")
public class TargetLayananServiceImpl extends BaseVoServiceImpl implements TargetLayananService {

	@Autowired
	private TargetLayananConverter targetLayananConverter;

	@Autowired
	private TargetLayananDao targetLayananDao;

	@Override
	public TargetLayananVO add(TargetLayananVO vo) throws JpaSystemException, ServiceVOException {
		TargetLayanan targetLayanan = targetLayananConverter.transferVOToModel(vo, new TargetLayanan());
		TargetLayanan resultModel = targetLayananDao.save(targetLayanan);

		TargetLayananVO resultVo = new TargetLayananVO();
		resultVo = targetLayananConverter.transferModelToVO(resultModel, resultVo);
		return resultVo;
	}

	@Override
	public TargetLayananVO update(TargetLayananVO vo) throws JpaSystemException, ServiceVOException {
		TargetLayanan targetLayananLama = targetLayananDao.findOne(vo.getNoRec());
		TargetLayanan targetLayananBaru = targetLayananConverter.transferVOToModel(vo, targetLayananLama);
		TargetLayanan resultModel = targetLayananDao.save(targetLayananBaru);

		TargetLayananVO resultVo = new TargetLayananVO();
		resultVo = targetLayananConverter.transferModelToVO(resultModel, resultVo);
		return resultVo;
	}

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

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

		TargetLayanan targetLayanan = targetLayananDao.findOne(key);
		if (CommonUtil.isNotNullOrEmpty(targetLayanan)) {
			result = targetLayananConverter.transferModelToVO(targetLayanan, result);
		}

		return result;
	}

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

		Iterable<TargetLayanan> models = targetLayananDao.findAll();
		if (CommonUtil.isNotNullOrEmpty(models)) {
			for (TargetLayanan model : models) {
				TargetLayananVO vo = new TargetLayananVO();
				vo = targetLayananConverter.transferModelToVO(model, vo);
				result.add(vo);
			}
		}

		return result;
	}

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

}
