package com.jasamedika.medifirst2000.service.impl;

import java.util.ArrayList;
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.NotifMessagingConverter;
import com.jasamedika.medifirst2000.dao.NotifMessagingDao;
import com.jasamedika.medifirst2000.entities.NotifMessaging;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.NotifMessagingService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.NotifMessagingVO;

@Service("NotifMessagingService")
public class NotifMessagingServiceImpl extends BaseVoServiceImpl implements NotifMessagingService<NotifMessagingVO> {
	
	
	@Autowired
	NotifMessagingConverter notifMessagingConverter;
	
	
	@Autowired
	NotifMessagingDao notifMessagingDao;

	@Override
	public List<NotifMessagingVO> findByObjekModulAplikasiId(Integer objekModulAplikasiId) {
		return notifMessagingConverter.transferListOfModelToListOfVO(notifMessagingDao.findByObjekModulAplikasiId(objekModulAplikasiId), null);
	}
	
	@Override
	public List<NotifMessagingVO> findByObjekModulAplikasiIdAndUrlBackEnd(Integer objekModulAplikasiId, String urlBackEnd){
		return notifMessagingConverter.transferListOfModelToListOfVO(notifMessagingDao.findByObjekModulAplikasiIdAndUrlBackEnd(objekModulAplikasiId, urlBackEnd), null);
	}
	
	@Override
	public List<NotifMessagingVO> findByObjekModulAplikasiIdAndUrlBackEndOrUrlBackEndIsNull(Integer objekModulAplikasiId, String urlBackEnd){
		return notifMessagingConverter.transferListOfModelToListOfVO(notifMessagingDao.findByObjekModulAplikasiIdAndUrlBackEndOrUrlBackEndIsNull(objekModulAplikasiId, urlBackEnd), null);
	}

	@Override
	@Transactional(readOnly = false)
	public NotifMessagingVO add(NotifMessagingVO vo) throws JpaSystemException, ServiceVOException {
		NotifMessaging notifMessaging = notifMessagingConverter.transferVOToModel(vo, new NotifMessaging());
		NotifMessaging resultModel = notifMessagingDao.save(notifMessaging);
		NotifMessagingVO resultVo = new NotifMessagingVO();
		resultVo = notifMessagingConverter.transferModelToVO(resultModel, resultVo);
		return resultVo;
	}

	@Override
	@Transactional(readOnly = false)
	public NotifMessagingVO update(NotifMessagingVO vo) throws JpaSystemException, ServiceVOException {

		NotifMessaging notifMessaging = notifMessagingDao.findOne(vo.getId());
		notifMessaging = notifMessagingConverter.transferVOToModel(vo, notifMessaging);

		notifMessagingDao.save(notifMessaging);
		return vo;
	}

	@Override
	@Transactional(readOnly = false)
	public Boolean delete(Integer key) throws JpaSystemException {
		if (CommonUtil.isNotNullOrEmpty(key)) {

			NotifMessaging notifMessaging = notifMessagingDao.findOne(key);
			notifMessagingDao.delete(notifMessaging);
			NotifMessagingVO notifMessagingVO = notifMessagingConverter.transferModelToVO(notifMessaging, new NotifMessagingVO());
			notifMessagingVO.setStatusEnabled(false);
			return true;
		} else {
			return false;
		}

	}

	@Override
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public NotifMessagingVO findById(Integer key) throws JpaSystemException {
		return notifMessagingConverter.transferModelToVO(notifMessagingDao.findOne(key), null);
	}

	@Override
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public List<NotifMessagingVO> findAll() throws JpaSystemException {
		return notifMessagingConverter.transferListOfModelToListOfVO(notifMessagingDao.findAll(), null);
	}

	@Override
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public Map<String, Object> findAllWithPageAndLimitAndSortByAndDirectionParameter(Integer page, Integer limit,
			String sort, String dir) {
		
		List<NotifMessagingVO> notifMessagingVOList = new ArrayList<NotifMessagingVO>();
		Pageable pageable = new PageRequest(page, limit, getSortBy(sort, dir));
		Page<NotifMessaging> resultPage = notifMessagingDao.findAll(pageable);
		List<NotifMessaging> notifMessagingList = resultPage.getContent();

		notifMessagingConverter.transferListOfModelToListOfVO(notifMessagingList, notifMessagingVOList);

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

}
