package cn.reghao.oss.store.rpc.media;

import cn.reghao.oss.api.constant.UploadChannel;
import cn.reghao.oss.store.db.repository.ImageRepository;
import cn.reghao.oss.store.rpc.SignService;
import cn.reghao.oss.api.dto.media.ImageUrlDto;
import cn.reghao.oss.api.iface.media.ImageFileService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @author reghao
 * @date 2023-08-28 15:00:43
 */
@DubboService
@Service
public class ImageFileServiceImpl implements ImageFileService {
    private final ImageRepository imageRepository;
    private final SignService signService;

    public ImageFileServiceImpl(ImageRepository imageRepository, SignService signService) {
        this.imageRepository = imageRepository;
        this.signService = signService;
    }

    @Override
    public void deleteByImageFileIds(List<String> imageFileIds) {
        imageRepository.deleteByImageFileIds(imageFileIds);
    }

    @Override
    public void deleteByObjectNames(List<String> objectNames) {
        imageRepository.deleteByObjectNames(objectNames);
    }

    @Override
    public ImageUrlDto getImageUrl(int channelId, String imageFileId) {
        ImageUrlDto imageUrlDto = imageRepository.getImageUrl(channelId, imageFileId);
        String originalUrl = imageUrlDto.getOriginalUrl();
        String thumbnailUrl = imageUrlDto.getThumbnailUrl();
        return imageUrlDto;
    }

    @Override
    public List<ImageUrlDto> getImageUrls(Set<String> imageFileIds) {
        int channelId = UploadChannel.photo.getCode();
        List<ImageUrlDto> list = imageRepository.getImageUrls(channelId, imageFileIds);
        return list;
    }

    @Override
    public String getSignedUrl(String url) {
        long loginUser = 1;
        int channelId = 1;
        String signedUrl = signService.getSignedUrl(loginUser, url, channelId, 600);
        return signedUrl;
    }
}
