package cn.reghao.oss.store.db.repository;

import cn.reghao.oss.api.constant.UploadChannel;
import cn.reghao.oss.api.dto.media.ImageUrlDto;
import cn.reghao.oss.store.db.mapper.ImageFileMapper;
import cn.reghao.oss.store.model.po.FileMeta;
import cn.reghao.oss.store.model.po.ImageFile;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2023-08-28 15:00:43
 */
@Repository
public class ImageRepository {
    private final ImageFileMapper imageFileMapper;
    private final ObjectRepository objectRepository;

    public ImageRepository(ImageFileMapper imageFileMapper, ObjectRepository objectRepository) {
        this.imageFileMapper = imageFileMapper;
        this.objectRepository = objectRepository;
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveImageFiles(List<ImageFile> imageFiles) {
        imageFileMapper.saveAll(imageFiles);
    }

    public void deleteByImageFileIds(List<String> imageFileIds) {
        imageFileIds.forEach(this::deleteImageFile);
    }

    public void deleteImageFile(String imageFileId) {
        List<String> objectIds = imageFileMapper.findByImageFileId(imageFileId).stream()
                .map(ImageFile::getObjectId)
                .collect(Collectors.toList());
        deleteImageFile0(imageFileId, objectIds);
    }

    public void deleteByObjectNames(List<String> imageObjectNames) {
        List<FileMeta> fileMetas = objectRepository.getByObjectNames(imageObjectNames);
        fileMetas.forEach(fileMeta -> {
            String objectId = fileMeta.getObjectId();
            deleteImageFile(objectId);
        });
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteImageFile0(String imageFileId, List<String> objectIds) {
        imageFileMapper.deleteByImageFileId(imageFileId);
        objectRepository.deleteByObjectIds(objectIds);
    }

    public List<ImageFile> findImageFiles(String imageFileId) {
        return imageFileMapper.findByImageFileId(imageFileId);
    }

    public List<ImageUrlDto> getImageUrls(int channelId, Set<String> imageFileIds) {
        List<ImageUrlDto> list = new ArrayList<>();
        for (String imageFileId : imageFileIds) {
            ImageUrlDto imageUrlDto = getImageUrl(channelId, imageFileId);
            if (imageUrlDto != null) {
                list.add(imageUrlDto);
            }
        }

        return list;
    }

    public ImageUrlDto getImageUrl(int channelId, String imageFileId) {
        List<ImageFile> list = imageFileMapper.findByImageFileId(imageFileId);
        if (list.isEmpty()) {
            return null;
        }

        ImageUrlDto imageUrlDto = new ImageUrlDto(imageFileId);
        if (channelId == UploadChannel.photo.getCode() || channelId == UploadChannel.disk.getCode()) {
            ImageFile original = list.get(0);
            imageUrlDto.setOriginalUrl(original.getUrl());
            ImageFile thumbnail = list.get(1);
            imageUrlDto.setThumbnailUrl(thumbnail.getUrl());
        } else {
            ImageFile original = list.get(0);
            imageUrlDto.setOriginalUrl(original.getUrl());
            imageUrlDto.setThumbnailUrl(original.getUrl());
        }

        return imageUrlDto;
    }
}
