package com.oguerreiro.resilient.service;

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

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.oguerreiro.resilient.domain.VariableCategory;
import com.oguerreiro.resilient.repository.VariableCategoryRepository;
import com.oguerreiro.resilient.service.dto.VariableCategoryDTO;
import com.oguerreiro.resilient.service.mapper.VariableCategoryMapper;

/**
 * Service Implementation for managing {@link com.oguerreiro.resilient.domain.VariableCategory}.
 */
@Service
@Transactional
public class VariableCategoryService extends AbstractResilientService<VariableCategoryDTO, VariableCategory, Long> {

  private final VariableCategoryRepository variableCategoryRepository;
  private final VariableCategoryMapper variableCategoryMapper;

  public VariableCategoryService(VariableCategoryRepository variableCategoryRepository,
      VariableCategoryMapper variableCategoryMapper) {
    super(VariableCategory.class, variableCategoryRepository, variableCategoryMapper);

    this.variableCategoryRepository = variableCategoryRepository;
    this.variableCategoryMapper = variableCategoryMapper;
  }

  /**
   * Get all the Categories, restricted by Scope. With option to filter hiddenForData.<br>
   * all==false, the resulting List WON'T have VariableCategories.hiddenForData == TRUE
   * all==true, the resulting List will have all without restriction
   *
   * @return the list of entities.
   */
  @Transactional(readOnly = true)
  public List<VariableCategoryDTO> findAllByScope(Long scopeId, Boolean all) {
    log.debug("Request to get all VariableCategories");

    //@formatter:off
    return variableCategoryRepository.findAllByScopeWithToOneRelationships(scopeId)
            .stream()
            .filter(vc -> all || vc.getHiddenForData() == null || !vc.getHiddenForData())           // Filters  VariableCategory.hiddenForData
            .map(variableCategoryMapper::toDto).collect(Collectors.toCollection(LinkedList::new));
    //@formatter:on
  }

  @Transactional(readOnly = true)
  public List<VariableCategoryDTO> findAllByScopeForFactor(Long scopeId) {
    log.debug("Request to get all findAllByScopeForFactor");

    //@formatter:off
    return variableCategoryRepository.findByVariableScopeIdAndHiddenForFactorIsFalseOrHiddenForFactorIsNullOrderByNameAsc(scopeId)
            .stream()
            .map(variableCategoryMapper::toDto).collect(Collectors.toCollection(LinkedList::new));
    //@formatter:on
  }
}
