package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBActivityService;
import vn.azteam.tpf.service.TBActivityTypeService;
import vn.azteam.tpf.Constants.Constants;
import vn.azteam.tpf.domain.TBActivity;
import vn.azteam.tpf.repository.TBActivityRepository;
import vn.azteam.tpf.repository.search.TBActivitySearchRepository;
import vn.azteam.tpf.service.TBCropQueryService;
import vn.azteam.tpf.service.dto.TBActivityDTO;
import vn.azteam.tpf.service.dto.TBActivityTypeDTO;
import vn.azteam.tpf.service.mapper.TBActivityMapper;
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.Optional;

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

/**
 * Service Implementation for managing TBActivity.
 */
@Service
@Transactional
public class TBActivityServiceImpl implements TBActivityService {

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

    private final TBActivityRepository tBActivityRepository;

    private final TBActivityMapper tBActivityMapper;

    private final TBActivitySearchRepository tBActivitySearchRepository;

    private final TBActivityTypeService tbActivityTypeService;

    private final TBCropQueryService tBCropQueryService;

    public TBActivityServiceImpl(TBActivityRepository tBActivityRepository,
                                 TBActivityMapper tBActivityMapper,
                                 TBActivitySearchRepository tBActivitySearchRepository,
                                 TBActivityTypeService tbActivityTypeService, TBCropQueryService tBCropQueryService) {
        this.tBActivityRepository = tBActivityRepository;
        this.tBActivityMapper = tBActivityMapper;
        this.tBActivitySearchRepository = tBActivitySearchRepository;
        this.tbActivityTypeService = tbActivityTypeService;
        this.tBCropQueryService = tBCropQueryService;
    }

    /**
     * Save a tBActivity.
     *
     * @param tBActivityDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBActivityDTO save(TBActivityDTO tBActivityDTO) {
        log.debug("Request to save TBActivity : {}", tBActivityDTO);
        if (tBActivityDTO.getCropId() != null) {
            int ageDay = tBCropQueryService.getAgeDayByCropID(tBActivityDTO.getCropId(),tBActivityDTO.getExecuteDate());
            tBActivityDTO.setAgeDay(ageDay);
        }
        TBActivity tBActivity = tBActivityMapper.toEntity(tBActivityDTO);
        tBActivity = tBActivityRepository.save(tBActivity);
        TBActivityDTO result = tBActivityMapper.toDto(tBActivity);
        tBActivitySearchRepository.save(tBActivity);
        return result;
    }

    /**
     * Get all the tBActivities.
     *
     * @param pageable the pagination information
     * @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<TBActivityDTO> findAll(Pageable pageable) {
        log.debug("Request to get all TBActivities");
        return tBActivityRepository.findAll(pageable)
            .map(tBActivityMapper::toDto);
    }


    /**
     * Get one tBActivity by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public Optional<TBActivityDTO> findOne(Long id) {
        log.debug("Request to get TBActivity : {}", id);
        return tBActivityRepository.findById(id)
            .map(tBActivityMapper::toDto);
    }

    /**
     * Delete the tBActivity by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBActivity : {}", id);
        tBActivityRepository.deleteById(id);
        tBActivitySearchRepository.deleteById(id);
    }

    /**
     * Search for the tBActivity 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<TBActivityDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBActivities for query {}", query);
        return tBActivitySearchRepository.search(queryStringQuery(query), pageable)
            .map(tBActivityMapper::toDto);
    }

    public TBActivityDTO getHarvestActivityInstance() {
        Optional<TBActivityTypeDTO> activityTypeDTO = tbActivityTypeService
            .findOneByName(Constants.ACTIVE_TYPE_HARVEST);
        TBActivityDTO tbActivityDTO = new TBActivityDTO();
        if (activityTypeDTO.isPresent()) {
            tbActivityDTO.setActivityTypeId(activityTypeDTO.get().getId());
        }
        tbActivityDTO.setExecuteDate(Instant.now());
        tbActivityDTO.setCreatedDate(Instant.now());
        return tbActivityDTO;
    }
}
