package cn.reghao.oss.store.rpc;

import cn.reghao.oss.api.constant.ObjectScope;
import cn.reghao.oss.api.dto.*;
import cn.reghao.oss.api.dto.media.AudioInfo;
import cn.reghao.oss.api.dto.media.ConvertedImageInfo;
import cn.reghao.oss.api.dto.media.ImageInfo;
import cn.reghao.oss.api.dto.media.VideoInfo;
import cn.reghao.oss.api.util.AuthContext;
import cn.reghao.oss.store.db.repository.ObjectRepository;
import cn.reghao.oss.store.model.po.FileMeta;
import cn.reghao.oss.store.service.FileStoreService;
import cn.reghao.oss.store.service.ObjectNameService;
import cn.reghao.oss.api.iface.StoreService;
import cn.reghao.oss.store.service.SignService;
import cn.reghao.oss.store.service.StoreLocalService;
import cn.reghao.oss.store.task.MediaFileProcessor;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 获取存储节点信息
 * oss-console 调用
 *
 * @author reghao
 * @date 2023-08-01 14:54:23
 */
@DubboService
@Service
public class StoreServiceImpl implements StoreService {
    private final LocalDateTime epoch = LocalDateTime.parse("1970-01-01T08:00");

    private final ObjectNameService objectNameService;
    private final FileStoreService fileStoreService;
    private final ObjectRepository objectRepository;
    private final SignService signService;
    private final MediaFileProcessor mediaFileProcessor;

    public StoreServiceImpl(ObjectNameService objectNameService, FileStoreService fileStoreService,
                            ObjectRepository objectRepository, SignService signService,
                            MediaFileProcessor mediaFileProcessor) {
        this.objectNameService = objectNameService;
        this.fileStoreService = fileStoreService;
        this.objectRepository = objectRepository;
        this.signService = signService;
        this.mediaFileProcessor = mediaFileProcessor;
    }

    @Override
    public void createChannel(String channelPrefix) {
        objectNameService.createParentDirs(channelPrefix, ObjectScope.PUBLIC.getCode());
    }

    @Override
    public StoreInfo getStoreInfo() {
        List<StoreDiskDto> diskDtos = fileStoreService.getStoreDisks().stream()
                .map(StoreDiskDto::new)
                .collect(Collectors.toList());
        return new StoreInfo(diskDtos);
    }

    @Override
    public void setObjectScope(String objectId, int scope) {
        FileMeta fileMeta = objectRepository.getByObjectId(objectId);
        objectRepository.updateObjectScope(scope, fileMeta.getObjectName());
    }

    @Override
    public void deleteByObjectId(String objectId) {
        FileMeta fileMeta = objectRepository.getByObjectId(objectId);
        objectRepository.deleteObject(fileMeta);
    }

    @Override
    public void deleteByObjectUrl(String objectUrl) {
        String objectName = "";
        FileMeta fileMeta = objectRepository.getByObjectName(objectName);
        objectRepository.deleteObject(fileMeta);
    }

    @Override
    public ObjectInfo getObjectInfo(String objectId) {
        FileMeta fileMeta = objectRepository.getByObjectId(objectId);
        if (fileMeta == null) {
            return null;
        }

        String objectName = fileMeta.getObjectName();
        int fileType = fileMeta.getFileType();
        String filename = fileMeta.getFilename();
        long size = fileMeta.getSize();
        return new ObjectInfo(objectId, objectName, fileType, filename, size);
    }

    @Override
    public String getSignedUrl(int loginUser, String objectId) {
        int expire = 3600;
        ObjectMeta objectMeta = objectRepository.getObjectMetaById(objectId);
        String url = objectNameService.getObjectUrl(objectMeta.getObjectName());
        return signService.getSignedUrl(loginUser, url, expire);
    }

    @Override
    public VideoInfo getVideoInfo(String objectId) {
        try {
            VideoInfo videoInfo = mediaFileProcessor.getVideoInfo(objectId);
            return videoInfo;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public ImageInfo getImageInfo(String objectId) {
        try {
            ImageInfo imageInfo = mediaFileProcessor.getImageInfo(objectId);
            return imageInfo;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public ConvertedImageInfo getWebpInfo(String objectId) {
        try {
            return mediaFileProcessor.getWebpInfo(objectId);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public AudioInfo getAudioInfo(String objectId) {
        try {
            AudioInfo audioInfo = mediaFileProcessor.getAudioInfo(objectId);
            return audioInfo;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
