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

import cn.reghao.oss.store.db.mapper.AudioFileMapper;
import cn.reghao.oss.store.db.mapper.ImageFileMapper;
import cn.reghao.oss.store.db.mapper.VideoFileMapper;
import cn.reghao.oss.store.db.repository.ObjectRepository;
import cn.reghao.oss.store.model.po.AudioFile;
import cn.reghao.oss.store.model.po.ImageFile;
import cn.reghao.oss.store.model.po.VideoFile;
import cn.reghao.oss.api.constant.ObjectType;
import cn.reghao.oss.api.iface.media.MediaScopeService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2023-10-12 15:06:21
 */
@DubboService
@Service
public class MediaScopeServiceImpl implements MediaScopeService {
    private final ImageFileMapper imageFileMapper;
    private final AudioFileMapper audioFileMapper;
    private final VideoFileMapper videoFileMapper;
    private final ObjectRepository objectRepository;

    public MediaScopeServiceImpl(ImageFileMapper imageFileMapper, AudioFileMapper audioFileMapper,
                                 VideoFileMapper videoFileMapper, ObjectRepository objectRepository) {
        this.imageFileMapper = imageFileMapper;
        this.audioFileMapper = audioFileMapper;
        this.videoFileMapper = videoFileMapper;
        this.objectRepository = objectRepository;
    }

    @Override
    public void setVideoScope(String videoFileId, int scope) {
        List<String> objectIds = videoFileMapper.findByVideoFileId(videoFileId).stream()
                .map(VideoFile::getObjectId)
                .collect(Collectors.toList());
        if (!objectIds.isEmpty()) {
            List<String> objectNames = objectRepository.getObjectNames(objectIds);
            objectRepository.updateObjectScope1(scope, objectNames);
        }
    }

    @Override
    public void setAudioScope(String audioFileId, int scope) {
        List<String> objectIds = audioFileMapper.findByAudioFileId(audioFileId).stream()
                .map(AudioFile::getObjectId)
                .collect(Collectors.toList());
        if (!objectIds.isEmpty()) {
            List<String> objectNames = objectRepository.getObjectNames(objectIds);
            objectRepository.updateObjectScope1(scope, objectNames);
        }
    }

    @Override
    public void setImagesScope(Set<String> imageFileIds, int scope) {
        List<String> objectIds = imageFileMapper.findByImageFileIds(imageFileIds).stream()
                .map(ImageFile::getObjectId)
                .collect(Collectors.toList());
        if (!objectIds.isEmpty()) {
            List<String> objectNames = objectRepository.getObjectNames(objectIds);
            objectRepository.updateObjectScope1(scope, objectNames);
        }
    }

    @Override
    public void setObjectScope(int scope, String objectId, int contentType) {
        List<String> objectIds;
        if (contentType == ObjectType.Image.getCode()) {
            objectIds = imageFileMapper.findByImageFileId(objectId).stream()
                    .map(ImageFile::getObjectId)
                    .collect(Collectors.toList());
        } else  if (contentType == ObjectType.Video.getCode()) {
            objectIds = videoFileMapper.findByVideoFileId(objectId).stream()
                    .map(VideoFile::getObjectId)
                    .collect(Collectors.toList());
        } else if (contentType == ObjectType.Audio.getCode()) {
            objectIds = audioFileMapper.findByAudioFileId(objectId).stream()
                    .map(AudioFile::getObjectId)
                    .collect(Collectors.toList());
        } else if (contentType == ObjectType.Text.getCode()) {
            objectIds = Collections.emptyList();
        } else {
            objectIds = Collections.emptyList();
        }

        if (!objectIds.isEmpty()) {
            List<String> objectNames = objectRepository.getObjectNames(objectIds);
            objectRepository.updateObjectScope1(scope, objectNames);
        }
    }

    @Override
    public void setObjectsScope(int scope, List<String> objectsId, int contentType) {
        List<String> objectIds = new ArrayList<>();
        if (contentType == ObjectType.Image.getCode()) {
            objectIds = imageFileMapper.findByImageFileIds(new HashSet<>(objectsId)).stream()
                    .map(ImageFile::getObjectId)
                    .collect(Collectors.toList());
        }

        if (!objectIds.isEmpty()) {
            List<String> objectNames = objectRepository.getObjectNames(objectIds);
            objectRepository.updateObjectScope1(scope, objectNames);
        }
    }
}
