package com.bares.service;

import java.util.List;

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 com.bares.domain.Comanda;
import com.bares.domain.*; // for static metamodels
import com.bares.repository.ComandaRepository;
import com.bares.service.dto.ComandaCriteria;

import com.bares.service.dto.ComandaDTO;
import com.bares.service.mapper.ComandaMapper;

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

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

    private final ComandaRepository comandaRepository;

    private final ComandaMapper comandaMapper;

    public ComandaQueryService(ComandaRepository comandaRepository, ComandaMapper comandaMapper) {
        this.comandaRepository = comandaRepository;
        this.comandaMapper = comandaMapper;
    }

    /**
     * Return a {@link List} of {@link ComandaDTO} 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<ComandaDTO> findByCriteria(ComandaCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<Comanda> specification = createSpecification(criteria);
        return comandaMapper.toDto(comandaRepository.findAll(specification));
    }

    /**
     * Return a {@link Page} of {@link ComandaDTO} 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<ComandaDTO> findByCriteria(ComandaCriteria criteria, Pageable page) {
        log.debug("find by criteria : {}, page: {}", criteria, page);
        final Specification<Comanda> specification = createSpecification(criteria);
        return comandaRepository.findAll(specification, page)
            .map(comandaMapper::toDto);
    }

    /**
     * Function to convert ComandaCriteria to a {@link Specification}
     */
    private Specification<Comanda> createSpecification(ComandaCriteria criteria) {
        Specification<Comanda> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), Comanda_.id));
            }
            if (criteria.getObservaciones() != null) {
                specification = specification.and(buildStringSpecification(criteria.getObservaciones(), Comanda_.observaciones));
            }
            if (criteria.getNombre() != null) {
                specification = specification.and(buildStringSpecification(criteria.getNombre(), Comanda_.nombre));
            }
            if (criteria.getPrecio() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getPrecio(), Comanda_.precio));
            }
            if (criteria.getServido() != null) {
                specification = specification.and(buildSpecification(criteria.getServido(), Comanda_.servido));
            }
            if (criteria.getDatePedido() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getDatePedido(), Comanda_.datePedido));
            }
            if (criteria.getDateServido() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getDateServido(), Comanda_.dateServido));
            }
            if (criteria.getMesaId() != null) {
                specification = specification.and(buildReferringEntitySpecification(criteria.getMesaId(), Comanda_.mesa, Mesa_.id));
            }
        }
        return specification;
    }

}
