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.DetailTOWSConverter;
import com.jasamedika.medifirst2000.dao.DetailTOWSDao;
import com.jasamedika.medifirst2000.entities.DetailTOWS;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.DetailTOWSService;
import com.jasamedika.medifirst2000.vo.DetailTOWSVO;

@Service("DetailTOWSService")
public class DetailTOWSServiceImpl extends BaseVoServiceImpl implements DetailTOWSService{

	@Autowired
	private DetailTOWSDao detailDao;
	
	@Autowired
	private DetailTOWSConverter converter;
	
	@Override
	public DetailTOWSVO add(DetailTOWSVO vo) throws JpaSystemException, ServiceVOException {
		return null;
	}

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

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

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

	@Override
	public List<DetailTOWSVO> 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> findDetail(Integer page, Integer take, String sort, String dir,
			Specification<DetailTOWS> 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<DetailTOWS> resultPage = detailDao.findAll(spec, pageable);
		List<DetailTOWS> resultData = resultPage.getContent();
		return constructMapReturn(resultData, resultPage.getTotalElements(), resultPage.getTotalPages());
	}

	@Override
	public Map<String, Object> save(DetailTOWSVO vo) {
		Map<String,Object> result = new HashMap<String,Object>();
		DetailTOWS model = converter.transferVOToModel(vo, new DetailTOWS());
		
		detailDao.save(model);
		
		return result;
	}

}
