package vn.azteam.tpf.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vn.azteam.tpf.domain.TBCodeDetails;
import vn.azteam.tpf.repository.TBCodeDetailsRepository;
import vn.azteam.tpf.repository.search.TBCodeDetailsSearchRepository;
import vn.azteam.tpf.service.TBCodeDetailsService;
import vn.azteam.tpf.service.dto.TBCodeDetailsDTO;
import vn.azteam.tpf.service.mapper.TBCodeDetailsMapper;
import vn.azteam.tpf.web.rest.errors.BadRequestAlertException;

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

/**
 * Service Implementation for managing TBCodeDetails.
 */

@Service
@Transactional
public class TBCodeDetailsImpl implements TBCodeDetailsService {
    private final Logger log = LoggerFactory.getLogger(TBCodeDetailsImpl.class);

    private final TBCodeDetailsRepository tbCodeDetailsRepository;

    private final TBCodeDetailsMapper tBCodeDetailsMapper;

    private final TBCodeDetailsSearchRepository tBCodeDetailsSearchRepository;

    public TBCodeDetailsImpl(TBCodeDetailsRepository tbCodeDetailsRepository, TBCodeDetailsMapper tBCodeDetailsMapper, TBCodeDetailsSearchRepository tBCodeDetailsSearchRepository) {
        this.tbCodeDetailsRepository = tbCodeDetailsRepository;
        this.tBCodeDetailsMapper = tBCodeDetailsMapper;
        this.tBCodeDetailsSearchRepository = tBCodeDetailsSearchRepository;
    }

    @Override
    public TBCodeDetailsDTO save(TBCodeDetailsDTO tBCodeDetailsDTO) {
        log.debug("Request to save TBCodeDetailsDTO : {}", tBCodeDetailsDTO);

        TBCodeDetails tBCodeDetails = tBCodeDetailsMapper.toEntity(tBCodeDetailsDTO);
        tBCodeDetails = tbCodeDetailsRepository.save(tBCodeDetails);
        TBCodeDetailsDTO result = tBCodeDetailsMapper.toDto(tBCodeDetails);
        tBCodeDetailsSearchRepository.save(tBCodeDetails);
        return result;
    }

    @Override
    public List<TBCodeDetailsDTO> saveAll(List<TBCodeDetailsDTO> tBCodeDetailsDTOs) {
        List<TBCodeDetails> tbCodes = new ArrayList<>();
        for (TBCodeDetailsDTO tBCodeDetailsDTO : tBCodeDetailsDTOs) {
            TBCodeDetails tbCodeDetails = tBCodeDetailsMapper.toEntity(tBCodeDetailsDTO);
            tbCodes.add(tbCodeDetails);
        }
        tbCodes = tbCodeDetailsRepository.saveAll(tbCodes);
        return tbCodes.stream().map(tBCodeDetailsMapper::toDto).collect(Collectors.toList());
    }

    @Transactional(readOnly = true)
    public void updateNumberScan(String code) throws BadRequestAlertException {
        Optional<TBCodeDetails> tbCodeDetailsOptional = tbCodeDetailsRepository.findByCode(code);
        if (tbCodeDetailsOptional.isPresent()) {
            TBCodeDetails tbCodeDetails = tbCodeDetailsOptional.get();
            if(tbCodeDetails.getTbCode() != null) {

            }
            Integer numberScan = tbCodeDetails.getNumberScan();
            tbCodeDetails.setNumberScan(numberScan.equals(0) ? 1 : numberScan + 1);
            tbCodeDetails.setLastUpdatedDate(Instant.now());
            tbCodeDetailsRepository.save(tbCodeDetails);
            log.debug("update number scan find by criteria code : {}", code);
        } else {
            throw new BadRequestAlertException("Not found", "tb code details to update with code:", code);
        }
    }

}
