package com.jasamedika.medifirst2000.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.stereotype.Service;

import com.jasamedika.medifirst2000.converter.RencanaMitigasiRisikoConverter;
import com.jasamedika.medifirst2000.dao.RencanaMitigasiRisikoDao;
import com.jasamedika.medifirst2000.entities.KamusIndikator;
import com.jasamedika.medifirst2000.entities.TargetIndikator;
import com.jasamedika.medifirst2000.entities.RencanaMitigasiRisiko;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.RencanaMitigasiRisikoService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.RencanaMitigasiRisikoVO;

@Service("RencanaMitigasiRisikoService")
public class RencanaMitigasiRisikoServiceImpl extends BaseVoServiceImpl implements RencanaMitigasiRisikoService{

	@Autowired
	private RencanaMitigasiRisikoDao rencanaDao;
	
	@Autowired
	private RencanaMitigasiRisikoConverter rencanaConverter;
	
	@Override
	public RencanaMitigasiRisikoVO add(RencanaMitigasiRisikoVO vo) throws JpaSystemException, ServiceVOException {
		return null;
	}

	@Override
	public RencanaMitigasiRisikoVO update(RencanaMitigasiRisikoVO vo) throws JpaSystemException, ServiceVOException {
		return null;
	}

	@Override
	public Boolean delete(String key) throws JpaSystemException {
		return null;
	}

	@Override
	public RencanaMitigasiRisikoVO findById(String key) throws JpaSystemException {
		return null;
	}

	@Override
	public List<RencanaMitigasiRisikoVO> findAll() throws JpaSystemException {
		return null;
	}

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

	@Override
	public Map<String, Object> findAll(Integer page, Integer take, String sort, String dir,
			Specification<RencanaMitigasiRisiko> spec) {
		Direction direction=null;
		
		if(dir.equalsIgnoreCase("asc")){
			direction=Sort.Direction.ASC;
		}else{
			direction=Sort.Direction.DESC;
		}
		
		Pageable pageable = new PageRequest(page, take, direction, sort);
		Page<RencanaMitigasiRisiko> resultPage = rencanaDao.findAll(spec, pageable);
		List<RencanaMitigasiRisiko> resultData = resultPage.getContent();
		return constructMapReturn(resultData, resultPage.getTotalElements(), resultPage.getTotalPages());
	}

	@Override
	public Map<String, Object> save(List<RencanaMitigasiRisikoVO> vos) {
		Map<String,Object> result = new HashMap<String,Object>();
		for(RencanaMitigasiRisikoVO vo : vos){
		RencanaMitigasiRisiko model = rencanaConverter.transferVOToModel(vo, new RencanaMitigasiRisiko());
		
		rencanaDao.save(model);
		}
		
		return result;
	}

}
