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

import cn.reghao.file.api.iface.OssService;
import cn.reghao.oss.api.constant.ObjectAction;
import cn.reghao.oss.api.constant.ObjectScope;
import cn.reghao.oss.api.dto.ObjectChannel;
import cn.reghao.oss.api.dto.ObjectInfo;
import cn.reghao.oss.api.dto.ServerInfo;
import cn.reghao.oss.api.dto.media.VideoInfo;
import cn.reghao.oss.api.dto.rest.UploadFileRet;
import cn.reghao.oss.api.dto.rest.UploadPrepare;
import cn.reghao.oss.api.dto.rest.UploadPrepareRet;
import cn.reghao.oss.api.dto.rest.UploadSample;
import cn.reghao.tnb.file.app.model.constant.OssType;
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 ServerInfo getUploadStore(int channelCode) {
        try {
            return ossClientManager.getOssClient().getServerInfo(channelCode);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public UploadPrepareRet prepareUpload(UploadPrepare uploadPrepare) {
        try {
            return ossClientManager.getOssClient().prepareUpload(uploadPrepare);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public UploadFileRet checkSample(UploadSample uploadSample) {
        try {
            return ossClientManager.getOssClient().checkSample(uploadSample);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ObjectChannel getChannel(int channelCode) {
        try {
            return ossClientManager.getOssClient().getChannel(channelCode);
            /*if (ossType == OssType.localOss.getCode()) {
                return ossClientManager.getOssClient().getChannel(channelCode);
            } else if (ossType == OssType.aliyunOss.getCode()) {
            }*/
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void setObjectScope(String objectId, int scope) {
        if (ossType == OssType.localOss.getCode()) {
            try {
                ossClientManager.getOssClient().setObjectScope(objectId, scope);
            } catch (Exception e) {
                log.error("{}", e.getMessage());
            }
        } else if (ossType == OssType.aliyunOss.getCode()) {
        }
    }

    @Override
    public void deleteByObjectId(String objectId) {
    }

    @Override
    public void deleteByObjectUrl(String objectUrl) {
    }

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

    @Override
    public ObjectInfo getObjectInfo(String objectId) {
        return ossClientManager.getOssClient().getObjectInfo(objectId);
    }

    @Override
    public VideoInfo getVideoInfo(String videoFileId) {
        return ossClientManager.getOssClient().getVideoInfo(videoFileId);
    }
}
