package vn.azteam.tpf.service;

import java.util.List;
import java.util.stream.Collectors;

import io.github.jhipster.service.filter.LongFilter;
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 vn.azteam.tpf.domain.TBEquipmentType;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBEquipmentRepository;
import vn.azteam.tpf.repository.TBEquipmentTypeRepository;
import vn.azteam.tpf.repository.search.TBEquipmentTypeSearchRepository;
import vn.azteam.tpf.service.dto.TBEquipmentDTO;
import vn.azteam.tpf.service.dto.TBEquipmentTypeCriteria;
import vn.azteam.tpf.service.dto.TBEquipmentTypeDTO;
import vn.azteam.tpf.service.dto.UserDTO;
import vn.azteam.tpf.service.mapper.TBEquipmentMapper;
import vn.azteam.tpf.service.mapper.TBEquipmentTypeMapper;

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

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

    private final TBEquipmentTypeRepository tBEquipmentTypeRepository;

    private final TBEquipmentTypeMapper tBEquipmentTypeMapper;

    private final TBEquipmentTypeSearchRepository tBEquipmentTypeSearchRepository;

    private final UserService userService;

    private final TBEquipmentRepository tBEquipmentRepository;

    private final TBEquipmentMapper tBEquipmentMapper;

    public TBEquipmentTypeQueryService(TBEquipmentTypeRepository tBEquipmentTypeRepository,
                                       TBEquipmentTypeMapper tBEquipmentTypeMapper,
                                       TBEquipmentTypeSearchRepository tBEquipmentTypeSearchRepository,
                                       UserService userService, TBEquipmentRepository tBEquipmentRepository,
                                       TBEquipmentMapper tBEquipmentMapper) {
        this.tBEquipmentTypeRepository = tBEquipmentTypeRepository;
        this.tBEquipmentTypeMapper = tBEquipmentTypeMapper;
        this.tBEquipmentTypeSearchRepository = tBEquipmentTypeSearchRepository;
        this.userService = userService;
        this.tBEquipmentRepository = tBEquipmentRepository;
        this.tBEquipmentMapper = tBEquipmentMapper;
    }

    /**
     * Return a {@link List} of {@link TBEquipmentTypeDTO} 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<TBEquipmentTypeDTO> findByCriteria(TBEquipmentTypeCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBEquipmentType> specification = createSpecification(criteria);
        return tBEquipmentTypeMapper.toDto(tBEquipmentTypeRepository.findAll(specification));
    }

    /**
     * Return a {@link Page} of {@link TBEquipmentTypeDTO} 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<TBEquipmentTypeDTO> findByCriteria(TBEquipmentTypeCriteria criteria, Pageable page) {
        log.debug("find by criteria : {}, page: {}", criteria, page);
        final Specification<TBEquipmentType> specification = createSpecification(criteria);
        return tBEquipmentTypeRepository.findAll(specification, page)
            .map(tBEquipmentTypeMapper::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(TBEquipmentTypeCriteria criteria) {
        log.debug("count by criteria : {}", criteria);
        final Specification<TBEquipmentType> specification = createSpecification(criteria);
        return tBEquipmentTypeRepository.count(specification);
    }

    /**
     * Function to convert TBEquipmentTypeCriteria to a {@link Specification}
     */
    private Specification<TBEquipmentType> createSpecification(TBEquipmentTypeCriteria criteria) {
        Specification<TBEquipmentType> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), TBEquipmentType_.id));
            }
            if (criteria.getName() != null) {
                specification = specification.and(buildStringSpecification(criteria.getName(), TBEquipmentType_.name));
            }
            if (criteria.getDescription() != null) {
                specification = specification.and(buildStringSpecification(criteria.getDescription(), TBEquipmentType_.description));
            }
        }
        return specification;
    }

    @Transactional(readOnly = true)
    public Page<TBEquipmentTypeDTO> getBEquipmentTypesByCustomer(TBEquipmentTypeCriteria criteria, Pageable page) {
        UserDTO currentUser = userService.getCurrentUserDTO().get();
        List<TBEquipmentDTO> subType = tBEquipmentMapper.toDto(tBEquipmentRepository.findAll());
        List<Long> ids = subType.stream().map(TBEquipmentDTO::getTbEquipmentTypeId).distinct().collect(Collectors.toList());

        if (ids.size() > 0) {
            LongFilter idFilter = new LongFilter();
            idFilter.setIn(ids);
            criteria.setId(idFilter);
        }
        final Specification<TBEquipmentType> specification = createSpecification(criteria);
        return tBEquipmentTypeRepository.findAll(specification, page)
            .map(tBEquipmentTypeMapper::toDto);
    }
}
