package com.brframework.commonoss.core;

import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;

/**
 * @author xu
 * @date 2019/8/21 10:44
 */
public class AliYunOSSClientBuilder extends OSSClientBuilder {

    private String defaultBucketName;

    private String defaultCDNDomain;

    public AliYunOSSClientBuilder setDefaultBucketName(String defaultBucketName){
        this.defaultBucketName = defaultBucketName;
        return this;
    }

    public AliYunOSSClientBuilder setDefaultCDNDomain(String defaultCDNDomain){
        this.defaultCDNDomain = defaultCDNDomain;
        return this;
    }


    @Override
    public OSS build(String endpoint, String accessKeyId, String secretAccessKey) {
        return new AliYunOSSClient(endpoint, getDefaultCredentialProvider(accessKeyId, secretAccessKey),
                getClientConfiguration(), defaultBucketName, defaultCDNDomain);
    }

    @Override
    public OSS build(String endpoint, String accessKeyId, String secretAccessKey, String securityToken) {
        return new AliYunOSSClient(endpoint, getDefaultCredentialProvider(accessKeyId, secretAccessKey, securityToken),
                getClientConfiguration(), defaultBucketName, defaultCDNDomain);
    }

    @Override
    public OSS build(String endpoint, String accessKeyId, String secretAccessKey, ClientBuilderConfiguration config) {
        return new AliYunOSSClient(endpoint, getDefaultCredentialProvider(accessKeyId, secretAccessKey),
                getClientConfiguration(config), defaultBucketName, defaultCDNDomain);
    }

    @Override
    public OSS build(String endpoint, String accessKeyId, String secretAccessKey, String securityToken,
                     ClientBuilderConfiguration config) {
        return new AliYunOSSClient(endpoint, getDefaultCredentialProvider(accessKeyId, secretAccessKey, securityToken),
                getClientConfiguration(config), defaultBucketName, defaultCDNDomain);
    }

    @Override
    public OSS build(String endpoint, CredentialsProvider credsProvider) {
        return new AliYunOSSClient(endpoint, credsProvider, getClientConfiguration(), defaultBucketName, defaultCDNDomain);
    }

    @Override
    public OSS build(String endpoint, CredentialsProvider credsProvider, ClientBuilderConfiguration config) {
        return new AliYunOSSClient(endpoint, credsProvider, getClientConfiguration(config), defaultBucketName, defaultCDNDomain);
    }

    private static ClientBuilderConfiguration getClientConfiguration() {
        return new ClientBuilderConfiguration();
    }

    private static ClientBuilderConfiguration getClientConfiguration(ClientBuilderConfiguration config) {
        if (config == null) {
            config = new ClientBuilderConfiguration();
        }
        return config;
    }

    private static DefaultCredentialProvider getDefaultCredentialProvider(String accessKeyId, String secretAccessKey) {
        return new DefaultCredentialProvider(accessKeyId, secretAccessKey);
    }

    private static DefaultCredentialProvider getDefaultCredentialProvider(String accessKeyId, String secretAccessKey,
                                                                          String securityToken) {
        return new DefaultCredentialProvider(accessKeyId, secretAccessKey, securityToken);
    }

}
