package cn.reghao.tnb.file.app.rpc;

import cn.reghao.file.api.dto.VideoMeta;
import cn.reghao.file.api.dto.ObjectMeta;
import cn.reghao.file.api.iface.OssService;
import cn.reghao.oss.api.constant.ObjectAction;
import cn.reghao.oss.api.dto.ObjectInfo;
import cn.reghao.oss.api.dto.media.VideoInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

/**
 * @author reghao
 * @date 2024-07-03 14:40:25
 */
@Slf4j
@DubboService
@Service
public class StoreServiceWrapperRouter implements OssService {
    private final int ossType = 1;
    private final OssClientManager ossClientManager;

    public StoreServiceWrapperRouter(OssClientManager ossClientManager) {
        this.ossClientManager = ossClientManager;
    }

    @Override
    public void checkAndSetScope(String objectId, int scope) {
        try {
            ossClientManager.getOssClient().checkAndSetScope(objectId, scope);
        } catch (Exception e) {
            log.error("{}", e.getMessage());
        }
    }

    @Override
    public void deleteByObjectId(String objectId) {
        try {
            ossClientManager.getOssClient().deleteObject(objectId);
        } catch (Exception e) {
            log.error("{}", e.getMessage());
        }
    }

    @Override
    public void deleteByObjectUrl(String objectUrl) {
    }

    @Override
    public String getSignedUrl(String objectId, String objectUrl) {
        try {
            return ossClientManager.getOssClient().getSignedUrl(objectId, ObjectAction.access.getName(), objectUrl);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ObjectMeta getObjectMeta(String objectId) {
        ObjectInfo objectInfo = ossClientManager.getOssClient().getObjectInfo(objectId);
        if (objectInfo == null) {
            return null;
        }

        ObjectMeta objectMeta = new ObjectMeta();
        objectMeta.setObjectId(objectInfo.getObjectId());
        objectMeta.setObjectName(objectInfo.getObjectName());
        objectMeta.setFileType(objectInfo.getFileType());
        objectMeta.setFilename(objectInfo.getFilename());
        objectMeta.setSize(objectInfo.getSize());
        objectMeta.setUrl(objectInfo.getUrl());
        objectMeta.setScope(objectInfo.getScope());
        objectMeta.setSha256sum(objectInfo.getSha256sum());
        objectMeta.setUpdateTime(objectInfo.getUpdateTime());
        return objectMeta;
    }

    @Override
    public VideoMeta getVideoMeta(String videoFileId) {
        VideoInfo videoInfo = ossClientManager.getOssClient().getVideoInfo(videoFileId);
        if (videoInfo == null) {
            return null;
        }

        VideoMeta videoMeta = new VideoMeta();
        videoMeta.setObjectId(videoInfo.getObjectId());
        videoMeta.setVideoCodec(videoInfo.getVideoCodec());
        videoMeta.setVbitRate(videoInfo.getVbitRate());
        videoMeta.setAudioCodec(videoInfo.getAudioCodec());
        videoMeta.setAbitRate(videoInfo.getAbitRate());
        videoMeta.setFormatName(videoInfo.getFormatName());
        videoMeta.setUrlType(videoInfo.getUrlType());
        videoMeta.setUrl(videoInfo.getUrl());
        videoMeta.setQuality(videoInfo.getQuality());
        videoMeta.setWidth(videoInfo.getWidth());
        videoMeta.setHeight(videoInfo.getHeight());
        videoMeta.setDuration(videoInfo.getDuration());
        videoMeta.setSize(videoInfo.getSize());
        return videoMeta;
    }
}
