package com.jasamedika.medifirst2000.service.impl;

import java.util.HashMap;
import java.util.LinkedList;
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.DraftPeraturanConverter;
import com.jasamedika.medifirst2000.dao.DetailDraftPeraturanDao;
import com.jasamedika.medifirst2000.dao.DraftPeraturanDao;
import com.jasamedika.medifirst2000.entities.DetailDraftPeraturan;
import com.jasamedika.medifirst2000.entities.DraftPeraturan;
import com.jasamedika.medifirst2000.entities.KajianEvaluasi;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.DraftPeraturanService;
import com.jasamedika.medifirst2000.vo.DraftPeraturanVO;
import com.jasamedika.medifirst2000.vo.KajianEvaluasiVO;

@Service("DraftPeraturanService")
public class DraftPeraturanServiceImpl extends BaseVoServiceImpl implements DraftPeraturanService{

	@Autowired
	private DraftPeraturanDao draftDao;
	
	@Autowired
	private DetailDraftPeraturanDao detailDao;
	
	@Autowired
	private DraftPeraturanConverter converter;
	
	@Override
	public DraftPeraturanVO add(DraftPeraturanVO vo) throws JpaSystemException, ServiceVOException {
		return null;
	}

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

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

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

	@Override
	public List<DraftPeraturanVO> 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<DraftPeraturan> 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<DraftPeraturan> resultPage = draftDao.findAll(spec, pageable);
		List<DraftPeraturan> modelList = resultPage.getContent();
		
		for(DraftPeraturan model : modelList){
			for(DetailDraftPeraturan detail : model.getDetailDraftSet()){
				detail.setDraftPeraturan(null);
			}
		}
		
		return constructMapReturn(modelList, resultPage.getTotalElements(), resultPage.getTotalPages());
	}

	@Override
	public Map<String, Object> saveDraft(DraftPeraturanVO vo) {
		Map<String,Object> result = new HashMap<String, Object>();
		DraftPeraturan model = converter.transferVOToModel(vo, new DraftPeraturan());
		
		List<DetailDraftPeraturan> listDetail = new LinkedList<DetailDraftPeraturan>();
		for(DetailDraftPeraturan detail : model.getDetailDraftSet())
		{
			listDetail.add(detail);
		}
		model.setDetailDraftSet(null);
		draftDao.save(model);
		
		for (DetailDraftPeraturan detail : listDetail) {
			detail.setDraftPeraturan(model);
			detailDao.save(detail);
		}
		
		draftDao.save(model);
		
		
		return result;
	}

	@Override
	public Map<String, Object> generateNoPeraturan(Integer bulan, Integer tahun) {
		Map<String,Object> result = new HashMap<String,Object>();
		
		int count = draftDao.CountbyMonthYear(bulan, tahun);
		
		String noUsulan =tahun+"/"+String.format("%02d", bulan)+"/DP/"+String.format("%02d",count+1)+"/00"; // result = "year/month/EK/noCount"
		
		result.put("noUsulan", noUsulan);
		return result;
	}

	@Override
	public Map<String, Object> deleteDraft(List<DraftPeraturanVO> vos) {
		Map<String,Object> result = new HashMap<String,Object>();
		for(DraftPeraturanVO vo:vos){
			DraftPeraturan model = draftDao.findOne(vo.getId());
			draftDao.delete(model);
		}
		return result;
	}

}
