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 vn.azteam.tpf.domain.TBAddress;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBAddressRepository;
import vn.azteam.tpf.repository.search.TBAddressSearchRepository;
import vn.azteam.tpf.service.dto.TBAddressCriteria;
import vn.azteam.tpf.service.dto.TBAddressDTO;
import vn.azteam.tpf.service.mapper.TBAddressMapper;

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

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

    private final TBAddressRepository tBAddressRepository;

    private final TBAddressMapper tBAddressMapper;

    private final TBAddressSearchRepository tBAddressSearchRepository;

    public TBAddressQueryService(TBAddressRepository tBAddressRepository, TBAddressMapper tBAddressMapper, TBAddressSearchRepository tBAddressSearchRepository) {
        this.tBAddressRepository = tBAddressRepository;
        this.tBAddressMapper = tBAddressMapper;
        this.tBAddressSearchRepository = tBAddressSearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBAddressDTO} 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<TBAddressDTO> findByCriteria(TBAddressCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBAddress> specification = createSpecification(criteria);
        return tBAddressMapper.toDto(tBAddressRepository.findAll(specification));
    }

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

    /**
     * Function to convert TBAddressCriteria to a {@link Specification}
     */
    private Specification<TBAddress> createSpecification(TBAddressCriteria criteria) {
        Specification<TBAddress> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), TBAddress_.id));
            }
            if (criteria.getAddress() != null) {
                specification = specification.and(buildStringSpecification(criteria.getAddress(), TBAddress_.address));
            }
            if (criteria.getTbCountryId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbCountryId(),
                    root -> root.join(TBAddress_.tbCountry, JoinType.LEFT).get(TBCountry_.id)));
            }
            if (criteria.getTbCityId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbCityId(),
                    root -> root.join(TBAddress_.tbCity, JoinType.LEFT).get(TBCity_.id)));
            }
            if (criteria.getTbDistrictId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbDistrictId(),
                    root -> root.join(TBAddress_.tbDistrict, JoinType.LEFT).get(TBDistrict_.id)));
            }
            if (criteria.getTbWardId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbWardId(),
                    root -> root.join(TBAddress_.tbWard, JoinType.LEFT).get(TBWard_.id)));
            }
        }
        return specification;
    }
}
