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.PerspektifConverter;
import com.jasamedika.medifirst2000.dao.PerspektifDao;
import com.jasamedika.medifirst2000.entities.Perspektif;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.PerspektifService;
import com.jasamedika.medifirst2000.vo.PerspektifVO;

@Service("PerspektifService")
public class PerspektifServiceImpl extends BaseVoServiceImpl implements PerspektifService{

	@Autowired
	private PerspektifDao perspektifDao;
	
	@Autowired
	private PerspektifConverter perspektifConverter;
	
	@Override
	public PerspektifVO add(PerspektifVO vo) throws JpaSystemException, ServiceVOException {
		return null;
	}

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

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

	@Override
	public PerspektifVO findById(Integer key) throws JpaSystemException {
		return null;
	}

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

	@Override
	public Map<String, Object> save(PerspektifVO vo) {
		Map<String,Object> result = new HashMap<String,Object>();
		Perspektif model = perspektifConverter.transferVOToModel(vo, new Perspektif());
		
		perspektifDao.save(model);
		
		return result;
	}

}
