package com.jasamedika.medifirst2000.service.impl;

import java.util.ArrayList;
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.BaseConverterImpl;
import com.jasamedika.medifirst2000.converter.TargetIndikatorConverter;
import com.jasamedika.medifirst2000.converter.SasaranStrategisConverter;
import com.jasamedika.medifirst2000.dao.TargetIndikatorDao;
import com.jasamedika.medifirst2000.entities.KamusIndikator;
import com.jasamedika.medifirst2000.entities.TargetIndikator;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.TargetIndikatorService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.TargetIndikatorVO;

@Service("TargetIndikatorService")
public class TargetIndikatorServiceImpl extends BaseVoServiceImpl implements TargetIndikatorService {
	
	@Autowired
	private TargetIndikatorDao targetIndikatorDao;
	
	@Autowired
	private TargetIndikatorConverter targetIndikatorConverter;

	@Override
	public TargetIndikatorVO add(TargetIndikatorVO vo) throws JpaSystemException, ServiceVOException {
		return null;
	}

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

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

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

	@Override
	public List<TargetIndikatorVO> 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<TargetIndikator> 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<TargetIndikator> resultPage = targetIndikatorDao.findAll(spec, pageable);
		List<TargetIndikator> resultData = resultPage.getContent();
		return constructMapReturn(resultData, resultPage.getTotalElements(), resultPage.getTotalPages());
	}

	@Override
	public Map<String, Object> save(List<TargetIndikatorVO> vos) {
		Map<String,Object> result = new HashMap<String,Object>();
		List<TargetIndikator>models = new ArrayList<TargetIndikator>();
		for(TargetIndikatorVO vo : vos){
		TargetIndikator model = targetIndikatorDao.findOne(vo.getId());
		model.setTarget(vo.getTarget());
		models.add(model);
		}
		targetIndikatorDao.save(models);
		
		return result;
	}
}
