package vn.azteam.tpf.service;

import java.util.ArrayList;
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.TBEnvironmentalParameter;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBEnvironmentalParameterRepository;
import vn.azteam.tpf.repository.search.TBEnvironmentalParameterSearchRepository;
import vn.azteam.tpf.service.dto.*;
import vn.azteam.tpf.service.mapper.TBEnvironmentalParameterMapper;

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

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

    private final TBEnvironmentalParameterRepository tBEnvironmentalParameterRepository;

    private final TBEnvironmentalParameterMapper tBEnvironmentalParameterMapper;

    private final TBEnvironmentalParameterSearchRepository tBEnvironmentalParameterSearchRepository;

    private final UserService userService;

    public TBEnvironmentalParameterQueryService(TBEnvironmentalParameterRepository tBEnvironmentalParameterRepository, TBEnvironmentalParameterMapper tBEnvironmentalParameterMapper, TBEnvironmentalParameterSearchRepository tBEnvironmentalParameterSearchRepository, UserService userService) {
        this.tBEnvironmentalParameterRepository = tBEnvironmentalParameterRepository;
        this.tBEnvironmentalParameterMapper = tBEnvironmentalParameterMapper;
        this.tBEnvironmentalParameterSearchRepository = tBEnvironmentalParameterSearchRepository;
        this.userService = userService;
    }

    /**
     * Return a {@link List} of {@link TBEnvironmentalParameterDTO} 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<TBEnvironmentalParameterDTO> findByCriteria(TBEnvironmentalParameterCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBEnvironmentalParameter> specification = createSpecification(criteria);
        return tBEnvironmentalParameterMapper.toDto(tBEnvironmentalParameterRepository.findAll(specification));
    }

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

    /**
     * Function to convert TBEnvironmentalParameterCriteria to a {@link Specification}
     */
    private Specification<TBEnvironmentalParameter> createSpecification(TBEnvironmentalParameterCriteria criteria) {
        Specification<TBEnvironmentalParameter> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), TBEnvironmentalParameter_.id));
            }
            if (criteria.getKey() != null) {
                specification = specification.and(buildStringSpecification(criteria.getKey(), TBEnvironmentalParameter_.key));
            }
            if (criteria.getName() != null) {
                specification = specification.and(buildStringSpecification(criteria.getName(), TBEnvironmentalParameter_.name));
            }
            if (criteria.getUnit() != null) {
                specification = specification.and(buildStringSpecification(criteria.getUnit(), TBEnvironmentalParameter_.unit));
            }
            if (criteria.getDescription() != null) {
                specification = specification.and(buildStringSpecification(criteria.getDescription(), TBEnvironmentalParameter_.description));
            }
            if (criteria.getCreatedDate() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getCreatedDate(), TBEnvironmentalParameter_.createdDate));
            }
            if (criteria.getModifiedDate() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getModifiedDate(), TBEnvironmentalParameter_.modifiedDate));
            }
            if (criteria.getDeletedDate() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getDeletedDate(), TBEnvironmentalParameter_.deletedDate));
            }
            if (criteria.getDeletedById() != null) {
                specification = specification.and(buildSpecification(criteria.getDeletedById(),
                    root -> root.join(TBEnvironmentalParameter_.deletedBy, JoinType.LEFT).get(TBDetailUser_.id)));
            }
            if (criteria.getCreatedById() != null) {
                specification = specification.and(buildSpecification(criteria.getCreatedById(),
                    root -> root.join(TBEnvironmentalParameter_.createdBy, JoinType.LEFT).get(TBDetailUser_.id)));
            }
            if (criteria.getModifiedById() != null) {
                specification = specification.and(buildSpecification(criteria.getModifiedById(),
                    root -> root.join(TBEnvironmentalParameter_.modifiedBy, JoinType.LEFT).get(TBDetailUser_.id)));
            }
            if (criteria.getTbCustomerId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbCustomerId(),
                    root -> root.join(TBEnvironmentalParameter_.tbCustomer, JoinType.LEFT).get(TBCustomer_.id)));
            }
        }
        return specification;
    }

    public List<ActivityObjectUpdateDetailDTO> getListEnvironmentalParameter(Page<TBEnvironmentalParameterDTO> page) {
        List<ActivityObjectUpdateDetailDTO> result = new ArrayList<ActivityObjectUpdateDetailDTO>();
        UserDTO currentUser = userService.getCurrentUserDTO().get();
        for (TBEnvironmentalParameterDTO item : page){
            if((currentUser.getCustomerId() != null && currentUser.getCustomerId() == item.getTbCustomerId())
                || item.getTbCustomerId() == null) {
                ActivityObjectUpdateDetailDTO item_ = new ActivityObjectUpdateDetailDTO();
                item_.setName(item.getName());
                item_.setIndex(null);
                result.add(item_);
            }
        }
        return result;
    }
}
