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.dao.DetailDraftPeraturanDao;
import com.jasamedika.medifirst2000.entities.DetailDraftPeraturan;
import com.jasamedika.medifirst2000.entities.DetailKajianEvaluasi;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.DetailDraftPeraturanService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.DetailDraftPeraturanVO;
import com.jasamedika.medifirst2000.vo.DetailKajianEvaluasiVO;

@Service("DetailDraftPeraturanService")
public class DetailDraftPeraturanServiceImpl extends BaseVoServiceImpl implements DetailDraftPeraturanService{

	@Autowired
	private DetailDraftPeraturanDao detailDao;
	@Override
	public DetailDraftPeraturanVO add(DetailDraftPeraturanVO vo) throws JpaSystemException, ServiceVOException {
		return null;
	}

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

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

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

	@Override
	public List<DetailDraftPeraturanVO> 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> updateDraft(List<DetailDraftPeraturanVO> vos) {
		Map<String,Object> result = new HashMap<String,Object>();
		for(DetailDraftPeraturanVO vo:vos){
			DetailDraftPeraturan model = detailDao.findOne(vo.getId());
			if(CommonUtil.isNotNullOrEmpty(vo.getStatusDraft())){
				model.setStatusDraft(vo.getStatusDraft());
			}
			if(CommonUtil.isNotNullOrEmpty(vo.getKoreksi())){
				model.setKoreksi(vo.getKoreksi());
			}
			detailDao.save(model);
		}
		return result;
	}

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

}
