package vn.azteam.tpf.service;

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

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 vn.azteam.tpf.Constants.Constants;
import vn.azteam.tpf.domain.TBSuppliesType;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBSuppliesTypeRepository;
import vn.azteam.tpf.repository.search.TBSuppliesTypeSearchRepository;
import vn.azteam.tpf.service.dto.*;
import vn.azteam.tpf.service.mapper.TBSuppliesTypeMapper;

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

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

    private final TBSuppliesTypeRepository tBSuppliesTypeRepository;

    private final TBSuppliesTypeMapper tBSuppliesTypeMapper;

    private final TBSuppliesTypeSearchRepository tBSuppliesTypeSearchRepository;

    public TBSuppliesTypeQueryService(TBSuppliesTypeRepository tBSuppliesTypeRepository, TBSuppliesTypeMapper tBSuppliesTypeMapper, TBSuppliesTypeSearchRepository tBSuppliesTypeSearchRepository) {
        this.tBSuppliesTypeRepository = tBSuppliesTypeRepository;
        this.tBSuppliesTypeMapper = tBSuppliesTypeMapper;
        this.tBSuppliesTypeSearchRepository = tBSuppliesTypeSearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBSuppliesTypeDTO} 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<TBSuppliesTypeDTO> findByCriteria(TBSuppliesTypeCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBSuppliesType> specification = createSpecification(criteria);
        return tBSuppliesTypeMapper.toDto(tBSuppliesTypeRepository.findAll(specification));
    }

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

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

    public List<TBSuppliesTypeDTO> getListTBSuppliesTypesCreate(String query) {
        List<TBSuppliesTypeDTO> result = new ArrayList<>();
        List<TBSuppliesType> data;
        if(query!=null){
            data = tBSuppliesTypeRepository.findAll();
        }else {
            data = tBSuppliesTypeRepository.findAll().stream()
                .filter(item -> item.getName().equals(Constants.ENUM_SUPPLIES_TYPE.GIONG.name()) == Boolean.FALSE)
                .collect(Collectors.toList());
        }
        result = tBSuppliesTypeMapper.toDto(data);
        return result;
    }
}
