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

import cn.reghao.file.api.iface.OssService;
import cn.reghao.oss.sdk.model.constant.ObjectScope;
import cn.reghao.oss.sdk.model.dto.ObjectInfo;
import cn.reghao.oss.sdk.model.dto.ServerInfo;
import cn.reghao.oss.sdk.model.dto.media.AudioInfo;
import cn.reghao.oss.sdk.model.dto.media.ConvertedImageInfo;
import cn.reghao.oss.sdk.model.dto.media.ImageInfo;
import cn.reghao.oss.sdk.model.dto.media.VideoInfo;
import cn.reghao.oss.sdk.OssConsoleClient;
import cn.reghao.tnb.file.app.config.AppProperties;
import cn.reghao.tnb.file.app.config.OssConsoleClientFactory;
import cn.reghao.tnb.file.app.model.constant.OssType;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

/**
 * @author reghao
 * @date 2024-07-03 14:40:25
 */
@DubboService
@Service
public class StoreServiceWrapperRouter implements OssService {
    private final OssConsoleClientFactory ossConsoleClientFactory;
    private final int ossType;

    public StoreServiceWrapperRouter(OssConsoleClientFactory ossConsoleClientFactory, AppProperties appProperties) {
        this.ossConsoleClientFactory = ossConsoleClientFactory;
        this.ossType = appProperties.getOssType();
    }

    public ServerInfo getUploadStore(int channelId) throws Exception {
        OssConsoleClient ossConsoleClient = ossConsoleClientFactory.getOssConsoleClient();
        ServerInfo serverInfo = ossConsoleClient.getUploadStore(channelId);
        return serverInfo;
    }

    public Integer getChannelScope(int channelId) {
        /*try {
            if (ossType == OssType.localOss.getCode()) {
                OssConsoleClient ossConsoleClient = ossConsoleClientFactory.getOssConsoleClient();
                return ossConsoleClient.getChannelScope(channelId);
            } else if (ossType == OssType.aliyunOss.getCode()) {
            }
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        return ObjectScope.PRIVATE.getCode();
    }

    public void setObjectScope(int channelId, String objectId, int scope) throws Exception {
        if (ossType == OssType.localOss.getCode()) {
            OssConsoleClient ossConsoleClient = ossConsoleClientFactory.getOssConsoleClient();
            ossConsoleClient.setObjectScope(channelId, objectId, scope);
        } else if (ossType == OssType.aliyunOss.getCode()) {
        }
    }

    public void deleteByObjectId(int channelId, String objectId) throws Exception {
        if (ossType == OssType.localOss.getCode()) {
            OssConsoleClient ossConsoleClient = ossConsoleClientFactory.getOssConsoleClient();
            ossConsoleClient.deleteByObjectId(channelId, objectId);
        } else if (ossType == OssType.aliyunOss.getCode()) {
        }
    }

    public void deleteByObjectUrl(String objectUrl) throws Exception {
        if (ossType == OssType.localOss.getCode()) {
            OssConsoleClient ossConsoleClient = ossConsoleClientFactory.getOssConsoleClient();
            ossConsoleClient.deleteByObjectUrl(objectUrl);
        } else if (ossType == OssType.aliyunOss.getCode()) {
        }
    }

    public ObjectInfo getObjectInfo(int channelId, String objectId) throws Exception {
        if (ossType == OssType.localOss.getCode()) {
            OssConsoleClient ossConsoleClient = ossConsoleClientFactory.getOssConsoleClient();
            return ossConsoleClient.getObjectInfo(channelId, objectId);
        } else if (ossType == OssType.aliyunOss.getCode()) {
        }

        throw new Exception("OSSType not found");
    }

    public String getSignedUrl(int channelId, String objectId) throws Exception {
        if (ossType == OssType.localOss.getCode()) {
            OssConsoleClient ossConsoleClient = ossConsoleClientFactory.getOssConsoleClient();
            return ossConsoleClient.getSignedUrl(channelId, objectId);
        } else if (ossType == OssType.aliyunOss.getCode()) {
        }

        throw new Exception("OSSType not found");
    }

    public VideoInfo getVideoInfo(int channelId, String videoFileId) throws Exception {
        OssConsoleClient ossConsoleClient = ossConsoleClientFactory.getOssConsoleClient();
        return ossConsoleClient.getVideoInfo(channelId, videoFileId);
    }

    public ImageInfo getImageInfo(int channelId, String imageFileId) throws Exception {
        if (ossType == OssType.localOss.getCode()) {
            OssConsoleClient ossConsoleClient = ossConsoleClientFactory.getOssConsoleClient();
            return ossConsoleClient.getImageInfo(channelId, imageFileId);
        } else if (ossType == OssType.aliyunOss.getCode()) {
        }

        throw new Exception("OSSType not found");
    }

    public ConvertedImageInfo getWebpInfo(int channelId, String imageFileId) throws Exception {
        if (ossType == OssType.localOss.getCode()) {
            OssConsoleClient ossConsoleClient = ossConsoleClientFactory.getOssConsoleClient();
            return ossConsoleClient.getWebpInfo(channelId, imageFileId);
        } else if (ossType == OssType.aliyunOss.getCode()) {
        }
        throw new Exception("OSSType not found");
    }

    public AudioInfo getAudioInfo(int channelId, String audioFileId) throws Exception {
        if (ossType == OssType.localOss.getCode()) {
            OssConsoleClient ossConsoleClient = ossConsoleClientFactory.getOssConsoleClient();
            return ossConsoleClient.getAudioInfo(channelId, audioFileId);
        } else if (ossType == OssType.aliyunOss.getCode()) {
        }

        throw new Exception("OSSType not found");
    }
}
