package <%=packageName%>.config;

import <%=packageName%>.service.standalone.upload.AliOssCloudClient;
import <%=packageName%>.service.standalone.upload.QcoscloudClient;
import <%=packageName%>.service.standalone.upload.QiniuOssCloudClient;
import com.aliyun.oss.OSSClient;
import com.qcloud.cos.COSClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OssClientConfiguration {
    private final ApplicationProperties properties;

    public OssClientConfiguration(ApplicationProperties properties) {
        this.properties = properties;
    }

    @Bean
    public OSSClient aliOssCloudClient() {
        if (properties.getUpload().getType().equals("aliOss")) {
            return AliOssCloudClient.getOSSClient(
                properties.getUpload().getAliOss().getEndpoint(),
                properties.getUpload().getAliOss().getAccessKey(),
                properties.getUpload().getAliOss().getSecretKey(),
                properties.getUpload().getAliOss().getBucketName(),
                properties.getUpload().getAliOss().getStaticDomain()
            );
        } else {
            return null;
        }
    }

    @Bean
    public COSClient qcosCloudClient() {
        if (properties.getUpload().getType().equals("qiniuOss")) {
            return QcoscloudClient.getCOSClient(
                properties.getUpload().getQcos().getBucketArea(),
                properties.getUpload().getQcos().getAppId(),
                properties.getUpload().getQcos().getSecretId(),
                properties.getUpload().getQcos().getAppKey(),
                properties.getUpload().getQcos().getBucketName()
            );
        } else {
            return null;
        }
    }

    @Bean
    public void qiniuCloudClient() {
        if (properties.getUpload().getType().equals("qcos")) {
            QiniuOssCloudClient.init(
                properties.getUpload().getQiniuOss().getBuckArea(),
                properties.getUpload().getQiniuOss().getBucket(),
                properties.getUpload().getQiniuOss().getAccessKey(),
                properties.getUpload().getQiniuOss().getSecretKey()
            );
        }
    }
}
