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.orm.jpa.JpaSystemException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jasamedika.medifirst2000.converter.KelasConverter;
import com.jasamedika.medifirst2000.dao.KelasDao;
import com.jasamedika.medifirst2000.entities.Kelas;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.KelasService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.KelasVO;

@Service("kelasService")
public class KelasServiceImpl extends BaseVoServiceImpl implements KelasService<KelasVO> {

	@Autowired
	private KelasDao kelasDao;
	
	@Autowired
	private KelasConverter kelasConverter;
	
	@Override
	@Transactional(readOnly = false)
	public KelasVO add(KelasVO vo) throws JpaSystemException, ServiceVOException {
		Kelas kelas = kelasConverter.transferVOToModel(vo, new Kelas());
		Kelas resultModel = kelasDao.save(kelas);
		KelasVO resultVo=new KelasVO();
		resultVo=kelasConverter.transferModelToVO(resultModel, resultVo);
		return resultVo;
	}

	@Override
	@Transactional(readOnly = false)
	public KelasVO update(KelasVO vo) throws JpaSystemException, ServiceVOException {
		
		Kelas kelas = kelasDao.findOne(vo.getId());
		kelas=kelasConverter.transferVOToModel(vo, kelas);
		
		kelasDao.save(kelas);
		return vo;
	}

	@Override
	public Boolean delete(Integer key) throws JpaSystemException {
		if (CommonUtil.isNotNullOrEmpty(key))
		{
			Kelas kelas=kelasDao.findOne(key);
			kelasDao.delete(kelas);
			return true;
		}
		else
		{
			return false;
		}
		
	}

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

	@Override
	public List<KelasVO> findAll() throws JpaSystemException {
		return null;
	}

	@Override
	public Map<String, Object> findAllWithPageAndLimitAndSortByAndDirectionParameter(Integer page, Integer limit,
			String sort, String dir) {
		List<KelasVO> kelasVOList = new ArrayList<KelasVO>();
		Pageable pageable = new PageRequest(page, limit, getSortBy(sort, dir));
		Page<Kelas> resultPage = kelasDao.findAll(pageable);
		List<Kelas> ruanganList = resultPage.getContent();

		kelasConverter.transferListOfModelToListOfVO(ruanganList, kelasVOList);

		return constructMapReturn(kelasVOList, resultPage.getTotalElements(),
				resultPage.getTotalPages());
	}

	@Override
	public List<Kelas> findAllKelas() {
		List<Kelas> models=(List<Kelas>) kelasDao.findAll();
		return models;
	}

	@Override
	public Map<String, Object> findKelasByRuangan(Integer ruanganId) {
		Map<String,Object> result=new HashMap<String,Object>();
		List<Map<String, Object>> list=kelasDao.findKelasByRuangan(ruanganId);
		result.put("list", list);
		return result;
	}

}
