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.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jasamedika.medifirst2000.converter.DepartemenConverter;
import com.jasamedika.medifirst2000.dao.DepartemenDao;
import com.jasamedika.medifirst2000.entities.Departemen;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.DepartemenService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.DepartemenVO;


@Service("departemenService")
public class DepartemenServiceImpl extends BaseVoServiceImpl implements DepartemenService<DepartemenVO>{

	@Autowired
	private DepartemenDao departemenDao;	
	
	@Autowired
	private DepartemenConverter departemenConverter;
	
	
	@Override
	@Transactional(readOnly = false)
	public DepartemenVO add(DepartemenVO vo) throws JpaSystemException, ServiceVOException {
		Departemen departemen = departemenConverter.transferVOToModel(vo, new Departemen());
		Departemen resultModel = departemenDao.save(departemen);
		DepartemenVO resultVo = new DepartemenVO();
		resultVo = departemenConverter.transferModelToVO(resultModel, resultVo);
		return resultVo;
	}

	@Override
	@Transactional(readOnly = false)
	public DepartemenVO update(DepartemenVO vo) throws JpaSystemException, ServiceVOException {
		
		Departemen departemen = departemenDao.findOne(vo.getId());
		departemen = departemenConverter.transferVOToModel(vo, departemen);
		
		departemenDao.save(departemen);
		return vo;
	}

	@Override
	public Boolean delete(Integer key) throws JpaSystemException {
		if (CommonUtil.isNotNullOrEmpty(key))
		{
			
			Departemen departemen=departemenDao.findOne(key);
			departemenDao.delete(departemen);
			DepartemenVO dep = departemenConverter.transferModelToVO(departemen, new DepartemenVO());
			dep.setJenisPerawatan(null);
			return true;
		}
		else
		{
			return false;
		}
		
	}

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

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

	@Override
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public Map<String, Object> findAllWithPageAndLimitAndSortByAndDirectionParameter(Integer page, Integer limit,
			String sort, String dir) {
		List<DepartemenVO> departemenVOList = new ArrayList<DepartemenVO>();
		Pageable pageable = new PageRequest(page, limit, getSortBy(sort, dir));
		Page<Departemen> resultPage = departemenDao.findAll(pageable);
		List<Departemen> departemenList = resultPage.getContent();

		departemenConverter.transferListOfModelToListOfVO(departemenList, departemenVOList);

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

	
	@Override
	public List<Departemen> findAllDepartemen() {
		List<Departemen> models = (List<Departemen>)departemenDao.findAll();
		return models;
	}

	@Override
	public Map<String, Object> findAllDepartemeById5And6() {
		Map<String,Object> result=new HashMap<String,Object>();
		List<Map<String,Object>>  resultModel= departemenDao.findAllDepartemeById5And6();
		if(CommonUtil.isNotNullOrEmpty(resultModel)){			
			result.put("departemen", resultModel);
			result.put("dataFound", true);			
		}else{
			result.put("dataFound", false);
		}		
		return result;
	}
	

}
