package vn.azteam.tpf.service.impl;

import org.springframework.data.domain.PageImpl;
import vn.azteam.tpf.service.TBActivityTypeService;
import vn.azteam.tpf.domain.TBActivityType;
import vn.azteam.tpf.repository.TBActivityTypeRepository;
import vn.azteam.tpf.repository.search.TBActivityTypeSearchRepository;
import vn.azteam.tpf.service.UserService;
import vn.azteam.tpf.service.dto.TBActivityTypeDTO;
import vn.azteam.tpf.service.mapper.TBActivityTypeMapper;
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 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 TBActivityType.
 */
@Service
@Transactional
public class TBActivityTypeServiceImpl implements TBActivityTypeService {

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

    private final TBActivityTypeRepository tBActivityTypeRepository;

    private final TBActivityTypeMapper tBActivityTypeMapper;

    private final TBActivityTypeSearchRepository tBActivityTypeSearchRepository;

    private final UserService userService;

    public TBActivityTypeServiceImpl(TBActivityTypeRepository tBActivityTypeRepository, TBActivityTypeMapper tBActivityTypeMapper, TBActivityTypeSearchRepository tBActivityTypeSearchRepository, UserService userService) {
        this.tBActivityTypeRepository = tBActivityTypeRepository;
        this.tBActivityTypeMapper = tBActivityTypeMapper;
        this.tBActivityTypeSearchRepository = tBActivityTypeSearchRepository;
        this.userService = userService;
    }

    /**
     * Save a tBActivityType.
     *
     * @param tBActivityTypeDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBActivityTypeDTO save(TBActivityTypeDTO tBActivityTypeDTO) {
        log.debug("Request to save TBActivityType : {}", tBActivityTypeDTO);

        TBActivityType tBActivityType = tBActivityTypeMapper.toEntity(tBActivityTypeDTO);
        tBActivityType = tBActivityTypeRepository.save(tBActivityType);
        TBActivityTypeDTO result = tBActivityTypeMapper.toDto(tBActivityType);
        tBActivityTypeSearchRepository.save(tBActivityType);
        return result;
    }

    /**
     * Get all the tBActivityTypes.
     *
     * @param pageable the pagination information
     * @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<TBActivityTypeDTO> findAll(Pageable pageable) {
        log.debug("Request to get all TBActivityTypes");
        List<TBActivityTypeDTO> result = tBActivityTypeRepository.findAll(pageable)
            .map(tBActivityTypeMapper::toDto)
            .getContent()
            .stream()
            .filter(item -> item.getDeletedDate() == null)
            .collect(Collectors.toList());
        return new PageImpl<>(result);
    }


    /**
     * Get one tBActivityType by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public Optional<TBActivityTypeDTO> findOne(Long id) {
        log.debug("Request to get TBActivityType : {}", id);
        return tBActivityTypeRepository.findById(id)
            .filter(tbActivityType -> tbActivityType.getDeletedDate() == null)
            .map(tBActivityTypeMapper::toDto);
    }

    /**
     * Delete the tBActivityType by id.
     *
     * @param id the id of the entity
     */
//    @Override
//    public void delete(Long id) {
//        log.debug("Request to delete TBActivityType : {}", id);
//        tBActivityTypeRepository.deleteById(id);
//        tBActivityTypeSearchRepository.deleteById(id);
//    }

    /**
     * Soft-Delete the tBActivityType by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to soft-delete TBActivityType : {}", id);
        TBActivityTypeDTO tbActivityTypeDTO = findOne(id).get();
        tbActivityTypeDTO.setDeletedDate(Instant.now());
        tbActivityTypeDTO.setDeletedById(userService.getCurrentUserDTO().get().getId());
        save(tbActivityTypeDTO);
    }

    /**
     * Search for the tBActivityType 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<TBActivityTypeDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBActivityTypes for query {}", query);
        return tBActivityTypeSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBActivityTypeMapper::toDto);
    }

    @Transactional(readOnly = true)
    public Optional<TBActivityTypeDTO> findOneByName(String name) {
        return tBActivityTypeRepository.getByName(name)
            .filter(tbActivityType -> tbActivityType.getDeletedDate() == null)
            .map(tBActivityTypeMapper::toDto);
    }
}
