package com.ruoyi.common.utils;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.core.http.HttpClient;
import com.aliyun.core.http.HttpMethod;
import com.aliyun.core.http.ProxyOptions;
import com.aliyun.httpcomponent.httpclient.ApacheAsyncHttpClientBuilder;
import com.aliyun.sdk.service.dysmsapi20170525.models.*;
import com.aliyun.sdk.service.dysmsapi20170525.*;
import com.google.gson.Gson;
import darabonba.core.RequestConfiguration;
import darabonba.core.client.ClientOverrideConfiguration;
import darabonba.core.utils.CommonUtil;
import darabonba.core.TeaPair;

//import javax.net.ssl.KeyManager;
//import javax.net.ssl.X509TrustManager;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.io.*;

/**
 * @author Mr_gzy
 */
public class SendSms {
    public static void main(String[] args) throws Exception {

        // HttpClient 配置
        /*
        HttpClient httpClient = new ApacheAsyncHttpClientBuilder()
                //设置连接超时时间，默认为10秒
                .connectionTimeout(Duration.ofSeconds(10))
                //设置响应超时时间，默认为20秒
                .responseTimeout(Duration.ofSeconds(10))
                .maxConnections(128) // Set the connection pool size
                //设置连接池超时，默认为30秒
                .maxIdleTimeOut(Duration.ofSeconds(50))
                // 配置代理
                .proxy(new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress("<your-proxy-hostname>", 9001))
                        .setCredentials("<your-proxy-username>", "<your-proxy-password>"))
                // 如果是https连接，您需要配置证书，或者忽略证书（.ignoreSSL(true)）
                .x509TrustManagers(new X509TrustManager[]{})
                .keyManagers(new KeyManager[]{})
                .ignoreSSL(false)
                .build();*/

        // 配置凭据认证信息，包括ak、secret、token
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                //请确保设置了环境变量ALIBABA_CLOUD_ACCESS_KEY_ID和ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                .accessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
                .accessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
                //使用STS令牌
                //.securityToken(System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN"))
                .build());

        // 配置客户端
        AsyncClient client = AsyncClient.builder()
                // 区域ID
                .region("cn-qingdao")
                // 使用配置好的HttpClient，否则使用默认的HttpClient （Apache HttpClient）
                //.httpClient(httpClient)
                .credentialsProvider(provider)
                // Service-level 配置
                //.serviceConfiguration(Configuration.create())
                // 重写客户端级配置，可以设置端点、Http请求参数等
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                        //.setConnectTimeout(Duration.ofSeconds(30))
                )
                .build();

        //API请求的参数设置
        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .signName("阿里云通信")
                .templateCode("SMS_316275071")
                .phoneNumbers("13261758662")
                .templateParam("{\"code\":\"1234\"}")
                // 重写请求级配置，可以设置Http请求参数等。
                // .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
                .build();

        // 异步获取API请求的返回值
        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
        // 同步获取API请求的返回值
        SendSmsResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));
        // 异步处理返回值
        /*response.thenAccept(resp -> {
            System.out.println(new Gson().toJson(resp));
        }).exceptionally(throwable -> { // Handling exceptions
            System.out.println(throwable.getMessage());
            return null;
        });*/

        // Finally, close the client
        client.close();
    }
}
