package com.eactive.eai.rms.data.entity.onl.flowcontrol.jms;

import java.util.Optional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.eactive.eai.data.entity.onl.flowcontrol.jms.FCQueueReceiver;
import com.eactive.eai.data.entity.onl.flowcontrol.jms.QFCQueueReceiver;
import com.eactive.eai.data.jpa.AbstractDataService;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;

@Service
@Transactional
public class FCQueueReceiverService extends AbstractDataService<FCQueueReceiver, String, FCQueueReceiverRepository> {

	@PersistenceContext
	private EntityManager entityManager;

	public Page<FCQueueReceiver> findPage(Pageable pageable, FCQueueReceiverSearch uiSearch) {
		BooleanExpression predicate = createPredicate(uiSearch);
		if (predicate == null)
			return repository.findAll(pageable);
		return repository.findAll(predicate, pageable);
	}

	public Iterable<FCQueueReceiver> findAll(FCQueueReceiverSearch uiSearch) {
		BooleanExpression predicate = createPredicate(uiSearch);
		Sort sort = Sort.by(Direction.ASC, "quercvername").and(Sort.by(Direction.ASC, "eaisevrinstncname"));
		if (predicate == null)
			return repository.findAll(sort);
		return repository.findAll(predicate, sort);
	}

	public Optional<FCQueueReceiver> findByQueueNameAndInstanceName(String queueName, String instanceName) {
		QFCQueueReceiver qObject = QFCQueueReceiver.fCQueueReceiver;
		BooleanExpression predicate = qObject.nonsyncztranquename
				.eq(queueName)
				.and(qObject.eaisevrinstncname.eq(instanceName));
		JPAQueryFactory queryFactory = new JPAQueryFactory(entityManager);
		FCQueueReceiver one = queryFactory.selectFrom(qObject).where(predicate).fetchOne();
		return one == null ? Optional.empty()
				: Optional.of(queryFactory.selectFrom(qObject).where(predicate).fetchOne());
	}

	private BooleanExpression createPredicate(FCQueueReceiverSearch uiSearch) {
		QFCQueueReceiver qObject = QFCQueueReceiver.fCQueueReceiver;
		BooleanExpression predicate = null;
		if (StringUtils.isNotBlank(uiSearch.getSearchInstance()))
			predicate = andExpression(predicate, qObject.eaisevrinstncname.contains(uiSearch.getSearchInstance()));

		if (StringUtils.isNotBlank(uiSearch.getSearchNomalQueRecvYn()))
			predicate = andExpression(predicate, qObject.bascquercveryn.eq(uiSearch.getSearchNomalQueRecvYn()));

		if (StringUtils.isNotBlank(uiSearch.getSearchQueRcverName()))
			predicate = andExpression(predicate, qObject.quercvername.contains(uiSearch.getSearchQueRcverName()));

		return predicate;
	}

	private BooleanExpression andExpression(BooleanExpression predicate, BooleanExpression right) {
		return predicate == null ? right : predicate.and(right);
	}
}
