package vn.azteam.tpf.service;

import java.util.List;

import javax.persistence.criteria.JoinType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import io.github.jhipster.service.QueryService;
import io.github.jhipster.service.filter.LongFilter;
import vn.azteam.tpf.domain.TBCropType;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBCropTypeRepository;
import vn.azteam.tpf.repository.search.TBCropTypeSearchRepository;
import vn.azteam.tpf.service.dto.TBCropTypeCriteria;
import vn.azteam.tpf.service.dto.TBCropTypeDTO;
import vn.azteam.tpf.service.dto.UserDTO;
import vn.azteam.tpf.service.mapper.TBCropTypeMapper;

/**
 * Service for executing complex queries for TBCropType entities in the database.
 * The main input is a {@link TBCropTypeCriteria} which gets converted to {@link Specification},
 * in a way that all the filters must apply.
 * It returns a {@link List} of {@link TBCropTypeDTO} or a {@link Page} of {@link TBCropTypeDTO} which fulfills the criteria.
 */
@Service
@Transactional(readOnly = true)
public class TBCropTypeQueryService extends QueryService<TBCropType> {

    private final Logger log = LoggerFactory.getLogger(TBCropTypeQueryService.class);

    private final TBCropTypeRepository tBCropTypeRepository;

    private final TBCropTypeMapper tBCropTypeMapper;

    private final TBCropTypeSearchRepository tBCropTypeSearchRepository;

    private final UserService userService;

    private final TBCustomerHasCropTypeQueryService tBCustomerHasCropTypeQueryService;

    public TBCropTypeQueryService(TBCropTypeRepository tBCropTypeRepository, TBCropTypeMapper tBCropTypeMapper, TBCropTypeSearchRepository tBCropTypeSearchRepository,
                                  UserService userService,
                                  TBCustomerHasCropTypeQueryService tBCustomerHasCropTypeQueryService) {
        this.tBCropTypeRepository = tBCropTypeRepository;
        this.tBCropTypeMapper = tBCropTypeMapper;
        this.tBCropTypeSearchRepository = tBCropTypeSearchRepository;
        this.userService = userService;
        this.tBCustomerHasCropTypeQueryService = tBCustomerHasCropTypeQueryService;
    }

    /**
     * Return a {@link List} of {@link TBCropTypeDTO} which matches the criteria from the database
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the matching entities.
     */
    @Transactional(readOnly = true)
    public List<TBCropTypeDTO> findByCriteria(TBCropTypeCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBCropType> specification = createSpecification(criteria);
        return tBCropTypeMapper.toDto(tBCropTypeRepository.findAll(specification));
    }

    /**
     * Return a {@link Page} of {@link TBCropTypeDTO} which matches the criteria from the database
     * @param criteria The object which holds all the filters, which the entities should match.
     * @param page The page, which should be returned.
     * @return the matching entities.
     */
    @Transactional(readOnly = true)
    public Page<TBCropTypeDTO> findByCriteria(TBCropTypeCriteria criteria, Pageable page) {
        log.debug("find by criteria : {}, page: {}", criteria, page);
        final Specification<TBCropType> specification = createSpecification(criteria);
        return tBCropTypeRepository.findAll(specification, page)
            .map(tBCropTypeMapper::toDto);
    }

    /**
     * Return the number of matching entities in the database
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the number of matching entities.
     */
    @Transactional(readOnly = true)
    public long countByCriteria(TBCropTypeCriteria criteria) {
        log.debug("count by criteria : {}", criteria);
        final Specification<TBCropType> specification = createSpecification(criteria);
        return tBCropTypeRepository.count(specification);
    }

    /**
     * Function to convert TBCropTypeCriteria to a {@link Specification}
     */
    private Specification<TBCropType> createSpecification(TBCropTypeCriteria criteria) {
        Specification<TBCropType> specification = Specification.where(null);
        if (criteria != null) {
            UserDTO currentUser = userService.getCurrentUserDTO().get();

            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), TBCropType_.id));
            }
            if (criteria.getName() != null) {
                specification = specification.and(buildStringSpecification(criteria.getName(), TBCropType_.name));
            }

            if (currentUser.getCustomerId() != null) {
                List<Long> cropTypeIds = tBCustomerHasCropTypeQueryService.getCropTypeIdsByCustomerId(currentUser.getCustomerId());
                if (cropTypeIds.size() > 0) {
                    LongFilter cropTypeIdFilter = new LongFilter();
                    cropTypeIdFilter.setIn(cropTypeIds);
                    specification = specification.and(buildSpecification(cropTypeIdFilter, TBCropType_.id));
                } else {
                    specification = specification.and((root, query, cb) -> cb.disjunction());
                }
            }

        }
        return specification;
    }
}
