package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBObjectParameterService;
import vn.azteam.tpf.domain.TBObjectParameter;
import vn.azteam.tpf.repository.TBObjectParameterRepository;
import vn.azteam.tpf.repository.search.TBObjectParameterSearchRepository;
import vn.azteam.tpf.service.UserService;
import vn.azteam.tpf.service.dto.TBObjectParameterDTO;
import vn.azteam.tpf.service.mapper.TBObjectParameterMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vn.azteam.tpf.service.util.PageableUtil;

import java.time.Instant;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing TBObjectParameter.
 */
@Service
@Transactional
public class TBObjectParameterServiceImpl implements TBObjectParameterService {

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

    private final TBObjectParameterRepository tBObjectParameterRepository;

    private final TBObjectParameterMapper tBObjectParameterMapper;

    private final TBObjectParameterSearchRepository tBObjectParameterSearchRepository;

    private final UserService userService;

    private final PageableUtil pageableUtil;

    public TBObjectParameterServiceImpl(TBObjectParameterRepository tBObjectParameterRepository, TBObjectParameterMapper tBObjectParameterMapper, TBObjectParameterSearchRepository tBObjectParameterSearchRepository, UserService userService, PageableUtil pageableUtil) {
        this.tBObjectParameterRepository = tBObjectParameterRepository;
        this.tBObjectParameterMapper = tBObjectParameterMapper;
        this.tBObjectParameterSearchRepository = tBObjectParameterSearchRepository;
        this.userService = userService;
        this.pageableUtil = pageableUtil;
    }

    /**
     * Save a tBObjectParameter.
     *
     * @param tBObjectParameterDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBObjectParameterDTO save(TBObjectParameterDTO tBObjectParameterDTO) {
        log.debug("Request to save TBObjectParameter : {}", tBObjectParameterDTO);

        TBObjectParameter tBObjectParameter = tBObjectParameterMapper.toEntity(tBObjectParameterDTO);
        tBObjectParameter = tBObjectParameterRepository.save(tBObjectParameter);
        TBObjectParameterDTO result = tBObjectParameterMapper.toDto(tBObjectParameter);
        tBObjectParameterSearchRepository.save(tBObjectParameter);
        return result;
    }

    /**
     * Get all the tBObjectParameters.
     *
     * @param pageable the pagination information
     * @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<TBObjectParameterDTO> findAll(Pageable pageable) {
        log.debug("Request to get all TBObjectParameters");
        List<TBObjectParameterDTO> result = tBObjectParameterRepository.findAll(Pageable.unpaged())
            .map(tBObjectParameterMapper::toDto).getContent();
        result = result.stream()
            .filter(item -> item.getDeletedDate() == null)
            .collect(Collectors.toList());
        return pageableUtil.changeTBObjectParameterDTOToPageFromList(result,pageable);
    }


    /**
     * Get one tBObjectParameter by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public Optional<TBObjectParameterDTO> findOne(Long id) {
        log.debug("Request to get TBObjectParameter : {}", id);
        return tBObjectParameterRepository.findById(id)
            .filter(item -> item.getDeletedDate() == null)
            .map(tBObjectParameterMapper::toDto);
    }

    /**
     * Delete the tBObjectParameter by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBObjectParameter : {}", id);
//        tBObjectParameterRepository.deleteById(id);
//        tBObjectParameterSearchRepository.deleteById(id);
        TBObjectParameterDTO currentTBObjectParameterDTO = findOne(id).get();
        currentTBObjectParameterDTO.setDeletedDate(Instant.now());
        currentTBObjectParameterDTO.setDeletedById(userService.getCurrentUserDTO().get().getId());
        tBObjectParameterRepository.save(tBObjectParameterMapper.toEntity(currentTBObjectParameterDTO));
    }

    /**
     * Search for the tBObjectParameter corresponding to the query.
     *
     * @param query the query of the search
     * @param pageable the pagination information
     * @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<TBObjectParameterDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBObjectParameters for query {}", query);
        List<TBObjectParameterDTO> result = tBObjectParameterSearchRepository.search(queryStringQuery(query), Pageable.unpaged())
            .map(tBObjectParameterMapper::toDto).getContent();
        result = result.stream()
            .filter(item -> item.getDeletedDate() == null)
            .collect(Collectors.toList());
        return pageableUtil.changeTBObjectParameterDTOToPageFromList(result,pageable);
//        return tBObjectParameterSearchRepository.search(queryStringQuery(query), pageable)
//            .map(tBObjectParameterMapper::toDto);
    }

    @Override
    @Transactional(readOnly = true)
    public Optional<TBObjectParameter> findObjectParameterByName(String name) {
        log.debug("Request to search for a page of TBObjectParameters for name {}", name);
        return tBObjectParameterRepository.findTopTbObjectParameterByName(name).filter(item -> item.getDeletedDate() == null);
    }
}
