package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBActivityExtendTypeService;
import vn.azteam.tpf.domain.TBActivityExtendType;
import vn.azteam.tpf.repository.TBActivityExtendTypeRepository;
import vn.azteam.tpf.repository.search.TBActivityExtendTypeSearchRepository;
import vn.azteam.tpf.service.UserService;
import vn.azteam.tpf.service.dto.TBActivityExtendTypeDTO;
import vn.azteam.tpf.service.mapper.TBActivityExtendTypeMapper;
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 TBActivityExtendType.
 */
@Service
@Transactional
public class TBActivityExtendTypeServiceImpl implements TBActivityExtendTypeService {

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

    private final TBActivityExtendTypeRepository tBActivityExtendTypeRepository;

    private final TBActivityExtendTypeMapper tBActivityExtendTypeMapper;

    private final TBActivityExtendTypeSearchRepository tBActivityExtendTypeSearchRepository;

    private final UserService userService;

    private final PageableUtil pageableUtil;

    public TBActivityExtendTypeServiceImpl(TBActivityExtendTypeRepository tBActivityExtendTypeRepository, TBActivityExtendTypeMapper tBActivityExtendTypeMapper, TBActivityExtendTypeSearchRepository tBActivityExtendTypeSearchRepository, UserService userService, PageableUtil pageableUtil) {
        this.tBActivityExtendTypeRepository = tBActivityExtendTypeRepository;
        this.tBActivityExtendTypeMapper = tBActivityExtendTypeMapper;
        this.tBActivityExtendTypeSearchRepository = tBActivityExtendTypeSearchRepository;
        this.userService = userService;
        this.pageableUtil = pageableUtil;
    }

    /**
     * Save a tBActivityExtendType.
     *
     * @param tBActivityExtendTypeDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBActivityExtendTypeDTO save(TBActivityExtendTypeDTO tBActivityExtendTypeDTO) {
        log.debug("Request to save TBActivityExtendType : {}", tBActivityExtendTypeDTO);

        TBActivityExtendType tBActivityExtendType = tBActivityExtendTypeMapper.toEntity(tBActivityExtendTypeDTO);
        tBActivityExtendType = tBActivityExtendTypeRepository.save(tBActivityExtendType);
        TBActivityExtendTypeDTO result = tBActivityExtendTypeMapper.toDto(tBActivityExtendType);
        tBActivityExtendTypeSearchRepository.save(tBActivityExtendType);
        return result;
    }

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


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

    /**
     * Delete the tBActivityExtendType by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBActivityExtendType : {}", id);
//        tBActivityExtendTypeRepository.deleteById(id);
//        tBActivityExtendTypeSearchRepository.deleteById(id);
        TBActivityExtendTypeDTO currentTBActivityExtendTypeDTO = findOne(id).get();
        currentTBActivityExtendTypeDTO.setDeletedDate(Instant.now());
        currentTBActivityExtendTypeDTO.setDeletedById(userService.getCurrentUserDTO().get().getId());
        tBActivityExtendTypeRepository.save(tBActivityExtendTypeMapper.toEntity(currentTBActivityExtendTypeDTO));
    }

    /**
     * Search for the tBActivityExtendType 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<TBActivityExtendTypeDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBActivityExtendTypes for query {}", query);
        List<TBActivityExtendTypeDTO> result = tBActivityExtendTypeSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBActivityExtendTypeMapper::toDto).getContent();
        result = result.stream()
            .filter(item -> item.getDeletedDate() == null)
            .collect(Collectors.toList());
        return pageableUtil.changeTBActivityExtendTypeDTOToPageFromList(result,pageable);
//        return tBActivityExtendTypeSearchRepository.search(queryStringQuery(query), pageable)
//            .map(tBActivityExtendTypeMapper::toDto);
    }

}
