package com.tuoheng.admin.config;

import com.aliyun.teaopenapi.models.Config;
import com.aliyun.vod20170321.Client;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

/**
 * 阿里云点播服务配置类
 *
 * @author WangHaoran
 * @since 2022-03-11
 */
@Configuration
@Slf4j
public class AliyuncsVodConfig {

    /**
     * 阿里云endpoint
     */
    public final static String ENDPOINT = "vod.cn-shanghai.aliyuncs.com";

    /**
     * 账号
     */
    public static String accessKeyId;

    /**
     * 密码
     */
    public static String accessKeySecret;

    /**
     * 角色ARN
     */
    public static String roleArn;

    /**
     * Bucket名称
     */
    public static String bucketName;

    @Value("${aliyuncsVod.accessKeyId}")
    public void setAccessKeyId(String accessKeyId) {
        AliyuncsVodConfig.accessKeyId = accessKeyId;
    }

    @Value("${aliyuncsVod.accessKeySecret}")
    public void setAccessKeySecret(String accessKeySecret) {
        AliyuncsVodConfig.accessKeySecret = accessKeySecret;
    }

    @Value("${aliyuncsVod.roleArn}")
    public void setRoleArn(String roleArn) {
        AliyuncsVodConfig.roleArn = roleArn;
    }

    @Value("${aliyuncsVod.bucketName}")
    public void setBucketName(String bucketName) {
        AliyuncsVodConfig.bucketName = bucketName;
    }

    @Bean
    @Lazy
    public Client vodClient() {
        try {
            Config config = new Config()
                    .setAccessKeyId(accessKeyId)
                    .setAccessKeySecret(accessKeySecret)
                    .setEndpoint(ENDPOINT);
            return new Client(config);
        } catch (Exception e) {
            log.error("获取vodClient客户端失败", e);
        }
        return null;
    }
}
