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.HistoryLoginUserConverter;
import com.jasamedika.medifirst2000.dao.HistoryLoginUserDao;
import com.jasamedika.medifirst2000.entities.HistoryLoginUser;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.HistoryLoginUserService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.HistoryLoginUserVO;

@Service("historyLoginUserService")
public class HistoryLoginUserServiceImpl extends BaseVoServiceImpl implements HistoryLoginUserService<HistoryLoginUser> {

	@Autowired
	HistoryLoginUserDao historyLoginUserDao;
	
	@Autowired
	HistoryLoginUserConverter historyLoginUserConverter;
	
	@Override
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public Map<String, Object> findByHistoryLoginId(Integer historyLoginId){
		List<HistoryLoginUserVO> historyLoginUserVOList = new ArrayList<HistoryLoginUserVO>();
		List<HistoryLoginUser> historyLoginUserList = historyLoginUserDao.findByHistoryLoginId(historyLoginId);
		
		historyLoginUserConverter.transferListOfModelToListOfVO(historyLoginUserList, historyLoginUserVOList);
		return constructMapReturn(historyLoginUserVOList, 0, 0);
	}
	
	@Override
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public Map<String, Object> findByHistoryLoginId(Integer historyLoginId, Integer page, Integer limit,
			String sort, String dir){
		
		//HistoryLoginUser historyLoginUser = historyLoginUserDao.findOne(565);
		
		List<HistoryLoginUserVO> historyLoginUserVOList = new ArrayList<HistoryLoginUserVO>();

		Pageable pageable = new PageRequest(page, limit, getSortBy(sort, dir));
		Page<HistoryLoginUser> resultPage = historyLoginUserDao.findByHistoryLoginId(historyLoginId, pageable);
		List<HistoryLoginUser> historyLoginUserList = resultPage.getContent();

		historyLoginUserConverter.transferListOfModelToListOfVO(historyLoginUserList, historyLoginUserVOList);

		return constructMapReturn(historyLoginUserVOList, resultPage.getTotalElements(), resultPage.getTotalPages());
	}
	
	@Override
	@Transactional(readOnly = false)
	public HistoryLoginUserVO add(HistoryLoginUserVO vo) throws JpaSystemException, ServiceVOException {
		HistoryLoginUser historyLoginUser = historyLoginUserConverter.transferVOToModel(vo, new HistoryLoginUser());
		HistoryLoginUser resultModel = historyLoginUserDao.save(historyLoginUser);
		HistoryLoginUserVO resultVo = new HistoryLoginUserVO();
		resultVo = historyLoginUserConverter.transferModelToVO(resultModel, resultVo);
		return resultVo;
	}

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

		HistoryLoginUser historyLoginUser = historyLoginUserDao.findOne(vo.getId());
		historyLoginUser = historyLoginUserConverter.transferVOToModel(vo, historyLoginUser);

		historyLoginUserDao.save(historyLoginUser);
		return vo;
	}

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

			HistoryLoginUser historyLoginUser = historyLoginUserDao.findOne(key);
			historyLoginUserDao.delete(historyLoginUser);
			HistoryLoginUserVO historyLoginUserVO = historyLoginUserConverter.transferModelToVO(historyLoginUser, new HistoryLoginUserVO());
			historyLoginUserVO.setStatusEnabled(false);
			return true;
		} else {
			return false;
		}

	}

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

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

	@Override
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public Map<String, Object> findAllWithPageAndLimitAndSortByAndDirectionParameter(Integer page, Integer limit,
			String sort, String dir) {
		
		List<HistoryLoginUserVO> historyLoginUserVOList = new ArrayList<HistoryLoginUserVO>();
		Pageable pageable = new PageRequest(page, limit, getSortBy(sort, dir));
		Page<HistoryLoginUser> resultPage = historyLoginUserDao.findAll(pageable);
		List<HistoryLoginUser> historyLoginUserList = resultPage.getContent();

		historyLoginUserConverter.transferListOfModelToListOfVO(historyLoginUserList, historyLoginUserVOList);

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

}
